Skip to content

Commit

Permalink
Bugfix #7847 - Fixed incorrect behavior for saving the Event without …
Browse files Browse the repository at this point in the history
…a place but with a link. (#7909)

* Fixed validation for online event in validateCoordinates method in EventServiceImpl class;
Added test for EventServiceImpl for saving online event;
Added method mapAllToList into EventDateLocationDtoMapper class;
Added necessary test in EventDateLocationDtoMapperTest class;
Added getEventDateLocationDtoWithLinkAndCoordinates method into ModelUtils class in service module;

* Formatted code with formatter-maven-plugin;

* Replased equals call to == for enums in the validateCoordinates method in EventServiceImpl class;
  • Loading branch information
ChernenkoVitaliy authored Dec 11, 2024
1 parent 90a68f4 commit 06c0754
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.modelmapper.AbstractConverter;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;

/**
* Class that used by {@link ModelMapper} to map {@link Event} into
Expand Down Expand Up @@ -39,4 +41,16 @@ public EventDateLocation convert(EventDateLocationDto eventDateLocationDto) {
}
return eventDateLocation;
}

/**
* Method that build {@link List} of {@link EventDateLocation} from {@link List}
* of {@link EventDateLocationDto}.
*
* @param eventDateLocationDtoList {@link List} of {@link EventDateLocationDto}
*
* @return {@link List} of {@link EventDateLocation}
*/
public List<EventDateLocation> mapAllToList(List<EventDateLocationDto> eventDateLocationDtoList) {
return eventDateLocationDtoList.stream().map(this::convert).collect(Collectors.toList());
}
}
6 changes: 6 additions & 0 deletions service/src/main/java/greencity/service/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import greencity.exception.exceptions.BadRequestException;
import greencity.exception.exceptions.NotFoundException;
import greencity.exception.exceptions.UserHasNoPermissionToAccessException;
import greencity.mapping.events.EventDateLocationDtoMapper;
import greencity.rating.RatingCalculation;
import greencity.repository.EventRepo;
import greencity.repository.RatingPointsRepo;
Expand Down Expand Up @@ -116,6 +117,7 @@ public class EventServiceImpl implements EventService {
private static final String DEFAULT_TITLE_IMAGE_PATH = AppConstant.DEFAULT_EVENT_IMAGES;
private final EventRepo eventRepo;
private final ModelMapper modelMapper;
private final EventDateLocationDtoMapper eventDateLocationDtoMapper;
private final RestClient restClient;
private final FileService fileService;
private final TagsService tagService;
Expand Down Expand Up @@ -627,7 +629,11 @@ private boolean isValidCoordinate(double latitude, double longitude) {
public boolean validateCoordinates(List<EventDateLocationDto> eventDateLocationDtos) {
for (EventDateLocationDto eventDateLocationDto : eventDateLocationDtos) {
AddressDto coordinates = eventDateLocationDto.getCoordinates();
EventType eventType = getEventType(eventDateLocationDtoMapper.mapAllToList(eventDateLocationDtos));

if (EventType.ONLINE == eventType) {
return true;
}
if (Objects.isNull(coordinates) || Objects.isNull(coordinates.getLatitude())
|| Objects.isNull(coordinates.getLongitude())) {
return false;
Expand Down
16 changes: 14 additions & 2 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1887,11 +1887,11 @@ public static Event getEventWithoutAddress() {
dates.add(new EventDateLocation(1L, event,
ZonedDateTime.of(2000, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
ZonedDateTime.of(2000, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
getAddress(), null));
null, "http://somelink.com"));
dates.add(new EventDateLocation(2L, event,
ZonedDateTime.of(2002, 1, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
ZonedDateTime.of(2002, 2, 1, 1, 1, 1, 1, ZoneId.systemDefault()),
null, "url/"));
null, "http://somelink.com"));
event.setDates(dates);
event.setTags(List.of(getEventTag()));
return event;
Expand Down Expand Up @@ -3233,4 +3233,16 @@ public static List<EventDateLocationDto> getEventDateLocationDtoWithInvalidDurat
return List.of(invalidDto1, invalidDto2);
}

public static EventDateLocationDto getEventDateLocationDtoWithLinkAndCoordinates() {
return EventDateLocationDto.builder()
.id(1L)
.startDate(ZonedDateTime.now())
.finishDate(ZonedDateTime.now().plusDays(2L))
.onlineLink("https://someevents.com")
.coordinates(AddressDto.builder()
.latitude(50.1234)
.latitude(30.1234)
.build())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package greencity.mapping.events;

import greencity.ModelUtils;
import greencity.dto.event.EventDateLocationDto;
import greencity.entity.event.EventDateLocation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static greencity.ModelUtils.*;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

Expand All @@ -21,12 +21,12 @@ class EventDateLocationDtoMapperTest {

@Test
void convert() {
EventDateLocation expected = getEventDateLocation();
EventDateLocation expected = ModelUtils.getEventDateLocation();
EventDateLocationDto dto = EventDateLocationDto.builder()
.id(expected.getId())
.startDate(expected.getStartDate())
.finishDate(expected.getFinishDate())
.coordinates(getAddressDto())
.coordinates(ModelUtils.getAddressDto())
.build();
EventDateLocation actual = mapper.convert(dto);
assertEquals(expected.getId(), actual.getId());
Expand All @@ -36,7 +36,7 @@ void convert() {

@Test
void convertWithoutAddress() {
EventDateLocation expected = getEventDateLocation();
EventDateLocation expected = ModelUtils.getEventDateLocation();
expected.setAddress(null);
EventDateLocationDto dto = EventDateLocationDto.builder()
.id(expected.getId())
Expand All @@ -48,4 +48,19 @@ void convertWithoutAddress() {
assertEquals(expected.getStartDate(), actual.getStartDate());
assertNull(actual.getAddress());
}

@Test
void mapAllToListWithValidEventDateLocationDtoTest() {
EventDateLocationDto eventDateLocationDto = ModelUtils.getEventDateLocationDtoWithLinkAndCoordinates();

List<EventDateLocationDto> eventDateLocationDtoList = List.of(eventDateLocationDto);

EventDateLocation dto = EventDateLocation.builder().id(eventDateLocationDto.getId())
.startDate(eventDateLocationDto.getStartDate()).finishDate(eventDateLocationDto.getFinishDate())
.address(addressDtoMapper.convert(eventDateLocationDto.getCoordinates()))
.onlineLink(eventDateLocationDto.getOnlineLink()).build();

List<EventDateLocation> expectedList = List.of(dto);
assertEquals(expectedList, mapper.mapAllToList(eventDateLocationDtoList));
}
}
37 changes: 37 additions & 0 deletions service/src/test/java/greencity/service/EventServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import greencity.exception.exceptions.BadRequestException;
import greencity.exception.exceptions.NotFoundException;
import greencity.exception.exceptions.UserHasNoPermissionToAccessException;
import greencity.mapping.events.EventDateLocationDtoMapper;
import greencity.rating.RatingCalculation;
import greencity.repository.AchievementCategoryRepo;
import greencity.repository.EventRepo;
Expand Down Expand Up @@ -102,6 +103,9 @@ class EventServiceImplTest {
@Mock
ModelMapper modelMapper;

@Mock
EventDateLocationDtoMapper eventDateLocationDtoMapper;

@Mock
EventRepo eventRepo;

Expand Down Expand Up @@ -164,6 +168,8 @@ void save() {
when(modelMapper.map(ModelUtils.getAddressLatLngResponse(), AddressDto.class)).thenReturn(build);
when(eventRepo.findFavoritesAmongEventIds(eventIds, user.getId())).thenReturn(List.of(event));
when(eventRepo.findSubscribedAmongEventIds(eventIds, user.getId())).thenReturn(List.of());
when(eventDateLocationDtoMapper.mapAllToList(addEventDtoRequest.getDatesLocations()))
.thenReturn(event.getDates());

EventDto resultEventDto = eventService.save(addEventDtoRequest, user.getEmail(), null);
assertEquals(eventDto, resultEventDto);
Expand Down Expand Up @@ -201,6 +207,37 @@ void saveEventWithoutAddress() {
verify(eventRepo, times(0)).save(eventWithoutCoordinates);
}

@Test
void saveOnlineEventSuccessTest() {
User user = ModelUtils.getUser();
AddEventDtoRequest addEventDtoRequest = ModelUtils.addEventDtoWithoutAddressRequest;
Event event = ModelUtils.getEventWithoutAddress();
List<Tag> tags = ModelUtils.getEventTags();
AddressDto addressDto = ModelUtils.getLongitudeAndLatitude();
EventDto eventDto = ModelUtils.getEventDtoWithoutAddress();
MultipartFile[] multipartFiles = ModelUtils.getMultipartFiles();

when(eventDateLocationDtoMapper.mapAllToList(addEventDtoRequest.getDatesLocations()))
.thenReturn(event.getDates());
when(googleApiService.getResultFromGeoCodeByCoordinates(any()))
.thenReturn(ModelUtils.getAddressLatLngResponse());
when(modelMapper.map(ModelUtils.getAddressLatLngResponse(), AddressDto.class)).thenReturn(addressDto);
when(modelMapper.map(addEventDtoRequest, Event.class)).thenReturn(event);
when(restClient.findByEmail(anyString())).thenReturn(testUserVo);
when(modelMapper.map(testUserVo, User.class)).thenReturn(user);
List<TagVO> tagVOList = Collections.singletonList(ModelUtils.getTagVO());
when(tagService.findTagsByNamesAndType(anyList(), eq(TagType.ECO_NEWS))).thenReturn(tagVOList);
when(modelMapper.map(tagVOList, new TypeToken<List<Tag>>() {
}.getType())).thenReturn(tags);
when(eventRepo.save(event)).thenReturn(event);
when(modelMapper.map(event, EventDto.class)).thenReturn(eventDto);
when(fileService.upload(multipartFiles[0])).thenReturn("/url1");
when(fileService.upload(multipartFiles[1])).thenReturn("/url2");

assertEquals(eventDto,
eventService.save(addEventDtoRequest, ModelUtils.getUser().getEmail(), multipartFiles));
}

@Test
void update() {
EventDto eventDto = ModelUtils.getEventDto();
Expand Down

0 comments on commit 06c0754

Please sign in to comment.