diff --git a/service-api/src/main/java/greencity/constant/ErrorMessage.java b/service-api/src/main/java/greencity/constant/ErrorMessage.java index dc9d4f8542..50acc2cb38 100644 --- a/service-api/src/main/java/greencity/constant/ErrorMessage.java +++ b/service-api/src/main/java/greencity/constant/ErrorMessage.java @@ -178,6 +178,9 @@ public class ErrorMessage { public static final String USER_HAS_NO_FRIEND_WITH_ID = "User has no friend with this id: "; public static final String INVALID_DURATION = "The duration for such habit is lower than previously set"; public static final String ADDRESS_NOT_FOUND_EXCEPTION = "No address found for the given coordinates."; + public static final String INVALID_COORDINATES = "The coordinates field must not be empty"; + public static final String INVALID_LONGITUDE = "Longitude must be between -180 and 180 degrees"; + public static final String INVALID_LATITUDE = "Latitude must be between -90 and 90 degrees"; public static final String INVALID_DATE = "Date can't be null or empty"; public static final String NO_FRIENDS_ASSIGNED_ON_CURRENT_HABIT = "No friends are assigned on current habit with id: "; diff --git a/service/src/main/java/greencity/service/EventServiceImpl.java b/service/src/main/java/greencity/service/EventServiceImpl.java index b45460859d..5abdaa2256 100644 --- a/service/src/main/java/greencity/service/EventServiceImpl.java +++ b/service/src/main/java/greencity/service/EventServiceImpl.java @@ -133,8 +133,7 @@ public class EventServiceImpl implements EventService { @Override public EventDto save(AddEventDtoRequest addEventDtoRequest, String email, MultipartFile[] images) { - checkingEqualityDateTimeInEventDateLocationDto(addEventDtoRequest.getDatesLocations()); - addAddressToLocation(addEventDtoRequest.getDatesLocations()); + validateEventRequest(addEventDtoRequest); Event toSave = modelMapper.map(addEventDtoRequest, Event.class); UserVO userVO = restClient.findByEmail(email); User organizer = modelMapper.map(userVO, User.class); @@ -607,6 +606,37 @@ private void addNewImages(Event toUpdate, UpdateEventDto updateEventDto, Multipa } } + private void validateEventRequest(AddEventDtoRequest addEventDtoRequest) { + checkingEqualityDateTimeInEventDateLocationDto(addEventDtoRequest.getDatesLocations()); + if (!validateCoordinates(addEventDtoRequest.getDatesLocations())) { + throw new BadRequestException(ErrorMessage.INVALID_COORDINATES); + } + addAddressToLocation(addEventDtoRequest.getDatesLocations()); + } + + private boolean isValidCoordinate(double latitude, double longitude) { + return Math.abs(latitude) <= 90 && Math.abs(longitude) <= 180; + } + + public boolean validateCoordinates(List eventDateLocationDtos) { + for (EventDateLocationDto eventDateLocationDto : eventDateLocationDtos) { + AddressDto coordinates = eventDateLocationDto.getCoordinates(); + + if (Objects.isNull(coordinates) || Objects.isNull(coordinates.getLatitude()) + || Objects.isNull(coordinates.getLongitude())) { + return false; + } + + double latitude = coordinates.getLatitude(); + double longitude = coordinates.getLongitude(); + + if (!isValidCoordinate(latitude, longitude)) { + return false; + } + } + return true; + } + private void addAddressToLocation(List eventDateLocationDtos) { eventDateLocationDtos.stream() .filter(eventDateLocationDto -> Objects.nonNull(eventDateLocationDto.getCoordinates())) diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java index b0c78f5e74..b42673132b 100644 --- a/service/src/test/java/greencity/ModelUtils.java +++ b/service/src/test/java/greencity/ModelUtils.java @@ -1936,6 +1936,13 @@ public static AddressDto getAddressDtoWithoutData() { return AddressDto.builder().build(); } + public static AddressDto getLongitudeAndLatitude() { + return AddressDto.builder() + .latitude(ModelUtils.getAddressDto().getLatitude()) + .longitude(ModelUtils.getAddressDto().getLongitude()) + .build(); + } + public static EventDto getEventDtoWithoutAddress() { return EventDto.builder() .id(1L) diff --git a/service/src/test/java/greencity/mapping/events/AddEventDtoRequestMapperTest.java b/service/src/test/java/greencity/mapping/events/AddEventDtoRequestMapperTest.java index 5ebc8ab633..fcbd266881 100644 --- a/service/src/test/java/greencity/mapping/events/AddEventDtoRequestMapperTest.java +++ b/service/src/test/java/greencity/mapping/events/AddEventDtoRequestMapperTest.java @@ -20,14 +20,6 @@ class AddEventDtoRequestMapperTest { @InjectMocks private AddEventDtoRequestMapper mapper; - @Test - void convertTest() { - Event expected = ModelUtils.getEvent(); - AddEventDtoRequest request = ModelUtils.addEventDtoRequest; - - assertEquals(expected.getTitle(), mapper.convert(request).getTitle()); - } - @Test void convertTestWithoutAddress() { Event expected = ModelUtils.getEventWithoutAddress(); diff --git a/service/src/test/java/greencity/service/EventServiceImplTest.java b/service/src/test/java/greencity/service/EventServiceImplTest.java index 83f403e481..b727401acf 100644 --- a/service/src/test/java/greencity/service/EventServiceImplTest.java +++ b/service/src/test/java/greencity/service/EventServiceImplTest.java @@ -161,6 +161,8 @@ void save() { }.getType())).thenReturn(tags); when(googleApiService.getResultFromGeoCodeByCoordinates(any())) .thenReturn(ModelUtils.getAddressLatLngResponse()); + AddressDto build = ModelUtils.getLongitudeAndLatitude(); + 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()); @@ -188,38 +190,16 @@ void save() { @Test void saveEventWithoutAddress() { User user = ModelUtils.getUser(); - EventDto eventDtoWithoutCoordinatesDto = ModelUtils.getEventDtoWithoutAddress(); - List eventIds = List.of(eventDtoWithoutCoordinatesDto.getId()); AddEventDtoRequest addEventDtoWithoutCoordinates = ModelUtils.addEventDtoWithoutAddressRequest; Event eventWithoutCoordinates = ModelUtils.getEventWithoutAddress(); - List tags = ModelUtils.getEventTags(); - + String email = user.getEmail(); when(modelMapper.map(addEventDtoWithoutCoordinates, Event.class)).thenReturn(eventWithoutCoordinates); - when(restClient.findByEmail(user.getEmail())).thenReturn(testUserVo); - when(modelMapper.map(testUserVo, User.class)).thenReturn(user); - when(eventRepo.save(eventWithoutCoordinates)).thenReturn(eventWithoutCoordinates); - when(modelMapper.map(eventWithoutCoordinates, EventDto.class)).thenReturn(eventDtoWithoutCoordinatesDto); - List tagVOList = Collections.singletonList(ModelUtils.getTagVO()); - when(tagService.findTagsWithAllTranslationsByNamesAndType(addEventDtoWithoutCoordinates.getTags(), - TagType.EVENT)).thenReturn(tagVOList); - when(modelMapper.map(tagVOList, new TypeToken>() { - }.getType())).thenReturn(tags); - when(eventRepo.findFavoritesAmongEventIds(eventIds, user.getId())).thenReturn(List.of(eventWithoutCoordinates)); - when(eventRepo.findSubscribedAmongEventIds(eventIds, user.getId())) - .thenReturn(List.of(eventWithoutCoordinates)); - - EventDto resultEventDto = eventService.save(addEventDtoWithoutCoordinates, user.getEmail(), null); - - assertEquals(eventDtoWithoutCoordinatesDto, resultEventDto); - assertTrue(resultEventDto.isSubscribed()); - assertTrue(resultEventDto.isFavorite()); - - verify(restClient).findByEmail(user.getEmail()); - verify(eventRepo).save(eventWithoutCoordinates); - verify(tagService).findTagsWithAllTranslationsByNamesAndType(addEventDtoWithoutCoordinates.getTags(), - TagType.EVENT); - verify(eventRepo).findFavoritesAmongEventIds(eventIds, user.getId()); - verify(eventRepo).findSubscribedAmongEventIds(eventIds, user.getId()); + when(eventRepo.save(any(Event.class))).thenReturn(eventWithoutCoordinates); + BadRequestException exception = assertThrows( + BadRequestException.class, + () -> eventService.save(addEventDtoWithoutCoordinates, email, null)); + assertEquals(ErrorMessage.INVALID_COORDINATES, exception.getMessage()); + verify(eventRepo, times(0)).save(eventWithoutCoordinates); } @Test