Skip to content

Commit

Permalink
Bugfix #7827 - Fixed incorrect status code and error message when rat…
Browse files Browse the repository at this point in the history
…e own event. (#7855)

* Added restriction to rate own event in method rateEvent() in EventServiceImpl;
Replaced restTemplate to userRepo for finding userByEmail in rateEvent() method in EventServiceImpl;
Added error message constant 'Organizer has no rights to rate the own event' into ErrorMessages class;
Created missed unit tests for rateEvent() method for EventServiceImpl class;
Added method getEventNotStartedYet() for creating not started event yet into ModelUtils class;

* Fixed code formatting according to formatter-maven-plugin cause build faided by CI/CD;

* Refactored unit tests for rateEvent() method in EventServiceImplTests according to sonar issues;

* Additional refactoring according to sonar issue documentation;

* Removed underscores from test method names in EventServiceImplTest;

* Replaced wildcard import to separated;

* Added word 'Test' to names for each test in EventServiceImplTest;
  • Loading branch information
ChernenkoVitaliy authored Dec 3, 2024
1 parent 6d2b3c4 commit f54b81b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public class ErrorMessage {
public static final String YOU_CANNOT_SUBSCRIBE_TO_CLOSE_EVENT =
"The event is close. You can't createSubscription to it";
public static final String HAVE_ALREADY_RATED = "You have already rated this event";
public static final String USER_HAS_NO_RIGHTS_TO_RATE_EVENT = "Organizer have no rights to rate the own event";
public static final String EVENT_IS_NOT_FINISHED = "Event is not finished yet";
public static final String EVENT_NOT_FOUND_BY_ID = "Event doesn't exist by this id: ";
public static final String EVENT_ID_IN_PATH_PARAM_AND_ENTITY_NOT_EQUAL =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,12 @@ public EventDto update(UpdateEventRequestDto eventDtoRequest, String email, Mult
public void rateEvent(Long eventId, String email, int grade) {
Event event = eventRepo.findById(eventId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.EVENT_NOT_FOUND));
User currentUser = modelMapper.map(restClient.findByEmail(email), User.class);
User currentUser = userRepo.findByEmail(email)
.orElseThrow(() -> new NotFoundException(ErrorMessage.USER_NOT_FOUND_BY_EMAIL + email));

if (event.getOrganizer().getId().equals(currentUser.getId())) {
throw new UserHasNoPermissionToAccessException(ErrorMessage.USER_HAS_NO_RIGHTS_TO_RATE_EVENT);
}
if (findLastEventDateTime(event).isAfter(ZonedDateTime.now())) {
throw new BadRequestException(ErrorMessage.EVENT_IS_NOT_FINISHED);
}
Expand Down
21 changes: 21 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,27 @@ public static Event getEventWithFinishedDate() {
return event;
}

public static Event getEventNotStartedYet() {
LocalDate start = LocalDate.now().plusDays(1);
LocalDate end = LocalDate.now().plusDays(2);
Event event = new Event();
event.setDescription("Some event description");
event.setId(1L);
event.setOrganizer(getUser());
event.setTitle("Some event title");
List<EventDateLocation> dates = new ArrayList<>();
dates.add(new EventDateLocation(1L, event,
ZonedDateTime.of(start.getYear(), start.getMonthValue(), start.getDayOfMonth(), 1, 1, 1, 1,
ZoneId.systemDefault()),
ZonedDateTime.of(end.getYear(), end.getMonthValue(), end.getDayOfMonth(), 1, 1, 1, 1,
ZoneId.systemDefault()),
getAddress(), null));
event.setDates(dates);
event.setTags(List.of(getEventTag()));
event.setTitleImage(AppConstant.DEFAULT_HABIT_IMAGE);
return event;
}

public static Event getEventWithoutAddress() {
Event event = new Event();
event.setDescription("Description");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import greencity.entity.event.Event;
import greencity.entity.event.EventDateLocation;
import greencity.entity.event.EventImages;
import greencity.entity.event.EventGrade;
import greencity.enums.NotificationType;
import greencity.enums.Role;
import greencity.enums.TagType;
Expand Down Expand Up @@ -77,12 +78,14 @@
import static greencity.ModelUtils.getUserVO;
import static greencity.ModelUtils.getUsersHashSet;
import static greencity.ModelUtils.testUserVo;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyList;
Expand Down Expand Up @@ -814,14 +817,94 @@ void rateEvent() {
User user = ModelUtils.getAttenderUser();
event.setAttenders(Set.of(user));
when(eventRepo.findById(any())).thenReturn(Optional.of(event));
when(modelMapper.map(restClient.findByEmail(user.getEmail()), User.class)).thenReturn(user);
when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user));
doNothing().when(userService).updateEventOrganizerRating(event.getOrganizer().getId(), 2.0);
List<Event> events = List.of(event, ModelUtils.getExpectedEvent(), ModelUtils.getEventWithGrades());
when(eventRepo.getAllByOrganizer(event.getOrganizer())).thenReturn(events);
eventService.rateEvent(event.getId(), user.getEmail(), 2);
verify(eventRepo).save(event);
}

@Test
void rateEventEventNotExistsNotFoundExceptionThrownTest() {
long notExistsEventId = 999L;
String userEmail = ModelUtils.testEmail;
when(eventRepo.findById(notExistsEventId)).thenReturn(Optional.empty());

assertThatThrownBy(() -> eventService.rateEvent(notExistsEventId, userEmail, 2))
.isInstanceOf(NotFoundException.class).hasMessage(ErrorMessage.EVENT_NOT_FOUND);

verify(eventRepo, times(0)).save(any());
verify(userService, times(0)).updateEventOrganizerRating(anyLong(), anyDouble());
}

@Test
void rateEventUserRatesOwnEventUserHasNoPermissionToAccessExceptionThrownTest() {
Event event = ModelUtils.getEventWithFinishedDate();
User user = event.getOrganizer();
Long eventId = event.getId();
String userEmail = user.getEmail();
when(eventRepo.findById(any())).thenReturn(Optional.of(event));
when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user));

assertThatThrownBy(() -> eventService.rateEvent(eventId, userEmail, 2))
.isInstanceOf(UserHasNoPermissionToAccessException.class)
.hasMessage(ErrorMessage.USER_HAS_NO_RIGHTS_TO_RATE_EVENT);

verify(eventRepo, times(0)).save(event);
verify(userService, times(0)).updateEventOrganizerRating(anyLong(), anyDouble());
}

@Test
void rateEventUserRatesNotFinishedEventYetBadRequestExceptionThrownTest() {
Event event = ModelUtils.getEventNotStartedYet();
User user = ModelUtils.getTestUser();
Long eventId = event.getId();
String userEmail = user.getEmail();
when(eventRepo.findById(any())).thenReturn(Optional.of(event));
when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user));

assertThatThrownBy(() -> eventService.rateEvent(eventId, userEmail, 2))
.isInstanceOf(BadRequestException.class).hasMessage(ErrorMessage.EVENT_IS_NOT_FINISHED);

verify(eventRepo, times(0)).save(event);
verify(userService, times(0)).updateEventOrganizerRating(anyLong(), anyDouble());
}

@Test
void rateEventUserNotEventSubscriberBadRequestExceptionThrownTest() {
Event event = ModelUtils.getEventWithFinishedDate();
User user = ModelUtils.getTestUser();
Long eventId = event.getId();
String userEmail = user.getEmail();
when(eventRepo.findById(any())).thenReturn(Optional.of(event));
when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user));

assertThatThrownBy(() -> eventService.rateEvent(eventId, userEmail, 2))
.isInstanceOf(BadRequestException.class).hasMessage(ErrorMessage.YOU_ARE_NOT_EVENT_SUBSCRIBER);

verify(eventRepo, times(0)).save(event);
verify(userService, times(0)).updateEventOrganizerRating(anyLong(), anyDouble());
}

@Test
void rateEventUserAlreadyRatedEventBadRequestExceptionThrownTest() {
Event event = ModelUtils.getEventWithFinishedDate();
User userWhoRatesEvent = ModelUtils.getTestUser();
Long eventId = event.getId();
String userEmail = userWhoRatesEvent.getEmail();
event.setAttenders(Set.of(userWhoRatesEvent));
event.setEventGrades(List.of(EventGrade.builder().grade(2).event(event).user(userWhoRatesEvent).build()));
when(eventRepo.findById(any())).thenReturn(Optional.of(event));
when(userRepo.findByEmail(userWhoRatesEvent.getEmail())).thenReturn(Optional.of(userWhoRatesEvent));

assertThatThrownBy(() -> eventService.rateEvent(eventId, userEmail, 2))
.isInstanceOf(BadRequestException.class).hasMessage(ErrorMessage.HAVE_ALREADY_RATED);

verify(eventRepo, times(0)).save(event);
verify(userService, times(0)).updateEventOrganizerRating(anyLong(), anyDouble());
}

@Test
void getAllEventAttenders() {
Event event = ModelUtils.getEvent();
Expand Down

0 comments on commit f54b81b

Please sign in to comment.