Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7817 fix api response status 500 instead of 400 when coordinates not specified #7850

Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need constants INVALID_LONGITUDE and INVALID_LATITUDE?
You don't use them anywhere.

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: ";
Expand Down
29 changes: 28 additions & 1 deletion service/src/main/java/greencity/service/EventServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public class EventServiceImpl implements EventService {
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);
Expand Down Expand Up @@ -601,6 +601,33 @@ private void addNewImages(Event toUpdate, UpdateEventDto updateEventDto, Multipa
}
}

private void validateEventRequest(AddEventDtoRequest addEventDtoRequest) {
checkingEqualityDateTimeInEventDateLocationDto(addEventDtoRequest.getDatesLocations());
ChernenkoVitaliy marked this conversation as resolved.
Show resolved Hide resolved

if (!validateCoordinates(addEventDtoRequest.getDatesLocations())) {
throw new BadRequestException(ErrorMessage.INVALID_COORDINATES);
}
addAddressToLocation(addEventDtoRequest.getDatesLocations());
}

public boolean validateCoordinates(List<EventDateLocationDto> eventDateLocationDtos) {
for (EventDateLocationDto eventDateLocationDto : eventDateLocationDtos) {
AddressDto coordinates = eventDateLocationDto.getCoordinates();

if (coordinates == null || coordinates.getLatitude() == null || coordinates.getLongitude() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make sens use Objects.isNull() ckeck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

return false;
}

double latitude = coordinates.getLatitude();
double longitude = coordinates.getLongitude();

if (latitude < -90.0 || latitude > 90.0 || longitude < -180.0 || longitude > 180.0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this check to separate method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

return false;
}
}
return true;
}

private void addAddressToLocation(List<EventDateLocationDto> eventDateLocationDtos) {
eventDateLocationDtos.stream()
.filter(eventDateLocationDto -> Objects.nonNull(eventDateLocationDto.getCoordinates()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
41 changes: 12 additions & 29 deletions service/src/test/java/greencity/service/EventServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ void save() {
}.getType())).thenReturn(tags);
when(googleApiService.getResultFromGeoCodeByCoordinates(any()))
.thenReturn(ModelUtils.getAddressLatLngResponse());
AddressDto build = AddressDto.builder()
.latitude(ModelUtils.getAddressDto().getLatitude())
.longitude(ModelUtils.getAddressDto().getLongitude())
.build();
when(modelMapper.map(ModelUtils.getAddressLatLngResponse(), AddressDto.class)).thenReturn(build);
Copy link
Contributor

@KizerovDmitriy KizerovDmitriy Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this AddressDto builder to ModelsUtils class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

when(eventRepo.findFavoritesAmongEventIds(eventIds, user.getId())).thenReturn(List.of(event));
when(eventRepo.findSubscribedAmongEventIds(eventIds, user.getId())).thenReturn(List.of());

Expand Down Expand Up @@ -184,38 +189,16 @@ void save() {
@Test
void saveEventWithoutAddress() {
User user = ModelUtils.getUser();
EventDto eventDtoWithoutCoordinatesDto = ModelUtils.getEventDtoWithoutAddress();
List<Long> eventIds = List.of(eventDtoWithoutCoordinatesDto.getId());
AddEventDtoRequest addEventDtoWithoutCoordinates = ModelUtils.addEventDtoWithoutAddressRequest;
Event eventWithoutCoordinates = ModelUtils.getEventWithoutAddress();
List<Tag> 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<TagVO> tagVOList = Collections.singletonList(ModelUtils.getTagVO());
when(tagService.findTagsWithAllTranslationsByNamesAndType(addEventDtoWithoutCoordinates.getTags(),
TagType.EVENT)).thenReturn(tagVOList);
when(modelMapper.map(tagVOList, new TypeToken<List<Tag>>() {
}.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
Expand Down
Loading