Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
urio999 committed Jan 29, 2025
1 parent 55399d0 commit b8e88f2
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 42 deletions.
61 changes: 60 additions & 1 deletion core/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import greencity.dto.event.AddEventDtoRequest;
import greencity.dto.event.AddressDto;
import greencity.dto.event.EventAuthorDto;
import greencity.dto.event.EventDateInformationDto;
import greencity.dto.event.EventDateLocationDto;
import greencity.dto.event.EventDto;
import greencity.dto.event.EventInformationDto;
import greencity.dto.event.EventResponseDto;
import greencity.dto.event.UpdateEventDateLocationDto;
import greencity.dto.event.UpdateEventRequestDto;
import greencity.dto.favoriteplace.FavoritePlaceDto;
Expand All @@ -39,6 +42,7 @@
import greencity.dto.location.LocationDto;
import greencity.dto.location.MapBoundsDto;
import greencity.dto.place.PlaceByBoundsDto;
import greencity.dto.tag.TagDto;
import greencity.dto.todolistitem.CustomToDoListItemResponseDto;
import greencity.dto.todolistitem.ToDoListItemPostDto;
import greencity.dto.todolistitem.ToDoListItemRequestDto;
Expand All @@ -59,15 +63,19 @@
import greencity.enums.ArticleType;
import greencity.enums.CommentStatus;
import greencity.enums.EventStatus;
import greencity.enums.EventType;
import greencity.enums.Role;
import greencity.enums.ToDoListItemStatus;
import greencity.enums.TagType;
import greencity.enums.UserStatus;
import java.security.Principal;
import java.sql.Date;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -588,4 +596,55 @@ public static List<PlaceByBoundsDto> getPlaceByBoundsDto() {
.location(new LocationDto())
.build());
}
}

public static EventResponseDto getEventResponseDto() {
return EventResponseDto.builder()
.id(1L)
.eventInformation(EventInformationDto.builder()
.title("Test Event")
.description("New Test Event")
.tags(List.of(TagUaEnDto.builder()
.id(2L)
.nameUa("Соціальний")
.nameEn("Social")
.build()))
.build())
.organizer(EventAuthorDto.builder().id(1L).name("Test").email("[email protected]").build())
.creationDate(LocalDate.of(2025, 1, 10))
.isOpen(true)
.dates(List.of(
EventDateInformationDto.builder()
.startDate(ZonedDateTime.of(2025, 12, 26, 12, 30, 0, 0, ZoneOffset.UTC))
.finishDate(ZonedDateTime.of(2025, 12, 26, 21, 59, 0, 0, ZoneOffset.UTC))
.onlineLink("www.testlink.com")
.coordinates(AddressDto.builder()
.latitude(50.44628775288652)
.longitude(30.49364829378446)
.streetEn("Halytska Square")
.streetUa("Галицька площа")
.houseNumber("1")
.cityEn("Kyiv")
.cityUa("Київ")
.regionEn("Kyiv")
.regionUa("місто Київ")
.countryEn("Ukraine")
.countryUa("Україна")
.formattedAddressEn("Halytska Sq, 1, Kyiv, Ukraine, 02000")
.formattedAddressUa("Галицька пл., 1, Київ, Україна, 02000")
.build())
.build()))
.titleImage("https://test.png")
.additionalImages(List.of("https://test1.png", "https://test2.png"))
.type(EventType.OFFLINE)
.isRelevant(true)
.likes(3)
.dislikes(1)
.countComments(1)
.eventRate(20.0)
.currentUserGrade(50)
.isSubscribed(false)
.isFavorite(false)
.isOrganizedByFriend(false)
.build();
}
}
31 changes: 31 additions & 0 deletions core/src/test/java/greencity/controller/EventControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import greencity.dto.PageableAdvancedDto;
import greencity.dto.event.AddEventDtoRequest;
import greencity.dto.event.EventDto;
import greencity.dto.event.EventResponseDto;
import greencity.dto.event.UpdateEventRequestDto;
import greencity.dto.filter.FilterEventDto;
import greencity.dto.user.UserVO;
Expand Down Expand Up @@ -756,4 +757,34 @@ void declineRequestTest() {
.andExpect(status().isOk());
verify(eventService).declineRequest(eventId, principal.getName(), userId);
}

@Test
@SneakyThrows
void getEventV2Test() {
Long eventId = 1L;
EventResponseDto eventResponseDto = ModelUtils.getEventResponseDto();

when(eventService.getEventV2(eventId, principal)).thenReturn(eventResponseDto);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();
String expectedJson = objectMapper.writeValueAsString(eventResponseDto);

mockMvc.perform(get(EVENTS_CONTROLLER_LINK + "/v2/{eventId}", eventId)
.principal(principal)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(expectedJson));

verify(eventService, times(1)).getEventV2(eventId, principal);
}

@Test
@SneakyThrows
void getEventV2FailedTest() {
mockMvc.perform(get(EVENTS_CONTROLLER_LINK + "/v2/{eventId}", "not_number").principal(principal))
.andExpect(status().isBadRequest());

verify(eventService, times(0)).getEventV2(1L, principal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ public class EventResponseDto {

private Double eventRate;

private Double currentUserGrade;
private Integer currentUserGrade;
}
21 changes: 4 additions & 17 deletions service/src/main/java/greencity/mapping/events/EventDtoMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import greencity.entity.event.Address;
import greencity.entity.event.Event;
import greencity.entity.event.EventDateLocation;
import greencity.entity.event.EventGrade;
import greencity.entity.event.EventImages;
import java.time.ZonedDateTime;
import greencity.service.CommentService;
import greencity.utils.EventUtils;
import org.modelmapper.AbstractConverter;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -51,7 +50,7 @@ public EventDto convert(Event event) {
eventDto.setTitleImage(event.getTitleImage());
eventDto.setOpen(event.isOpen());
eventDto.setType(event.getType());
eventDto.setIsRelevant(isRelevant(event.getDates()));
eventDto.setIsRelevant(EventUtils.isRelevant(event.getDates()));
eventDto.setLikes(event.getUsersLikedEvents().size());
eventDto.setCountComments(commentService.countCommentsForEvent(event.getId()));
User organizer = event.getOrganizer();
Expand Down Expand Up @@ -80,7 +79,7 @@ public EventDto convert(Event event) {
eventDto.setAdditionalImages(event.getAdditionalImages().stream()
.map(EventImages::getLink).collect(Collectors.toList()));
}
eventDto.setEventRate(calculateEventRate(event.getEventGrades()));
eventDto.setEventRate(EventUtils.calculateEventRate(event.getEventGrades()));
return eventDto;
}

Expand Down Expand Up @@ -111,16 +110,4 @@ private EventDateLocationDto convertEventDateLocation(EventDateLocation eventDat
}
return eventDateLocationDto;
}

private double calculateEventRate(List<EventGrade> eventGrades) {
return eventGrades.stream()
.mapToInt(EventGrade::getGrade)
.average()
.orElse(0.0);
}

private boolean isRelevant(List<EventDateLocation> dates) {
return dates.getLast().getFinishDate().isAfter(ZonedDateTime.now())
|| dates.getLast().getFinishDate().isEqual(ZonedDateTime.now());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,30 @@
import greencity.dto.tag.TagUaEnDto;
import greencity.entity.User;
import greencity.entity.event.Event;
import greencity.entity.event.EventDateLocation;
import greencity.entity.event.EventGrade;
import greencity.entity.event.EventImages;
import greencity.entity.localization.TagTranslation;
import greencity.service.CommentService;
import lombok.NonNull;
import greencity.utils.EventUtils;
import org.modelmapper.AbstractConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* Mapper class for converting {@link Event} into {@link EventResponseDto}.
*/
@Component
public class EventMapper extends AbstractConverter<Event, EventResponseDto> {
public class EventResponseDtoMapper extends AbstractConverter<Event, EventResponseDto> {
private static final String LANGUAGE_UA = "ua";
private static final String LANGUAGE_EN = "en";
private static final int MAX_ADDITIONAL_IMAGES = 4;

private final CommentService commentService;

@Autowired
public EventMapper(@Lazy CommentService commentService) {
public EventResponseDtoMapper(@Lazy CommentService commentService) {
this.commentService = commentService;
}

Expand All @@ -53,12 +50,12 @@ protected EventResponseDto convert(Event event) {
.map(tag -> TagUaEnDto.builder()
.id(tag.getId())
.nameUa(tag.getTagTranslations().stream()
.filter(tt -> LANGUAGE_EN.equals(tt.getLanguage().getCode()))
.filter(tt -> LANGUAGE_UA.equals(tt.getLanguage().getCode()))
.findFirst()
.map(TagTranslation::getName)
.orElse(null))
.nameEn(tag.getTagTranslations().stream()
.filter(tt -> LANGUAGE_UA.equals(tt.getLanguage().getCode()))
.filter(tt -> LANGUAGE_EN.equals(tt.getLanguage().getCode()))
.findFirst()
.map(TagTranslation::getName)
.orElse(null))
Expand Down Expand Up @@ -105,29 +102,16 @@ protected EventResponseDto convert(Event event) {
.isOpen(event.isOpen())
.dates(dateInformation)
.titleImage(event.getTitleImage())
.isRelevant(isRelevantCheck(event.getDates()))
.isRelevant(EventUtils.isRelevant(event.getDates()))
.likes(event.getUsersLikedEvents().size())
.dislikes(event.getUsersDislikedEvents().size())
.countComments(commentService.countCommentsForEvent(event.getId()))
.type(event.getType())
.eventRate(calculateEventRate(event.getEventGrades()))
.currentUserGrade(organizer.getRating())
.eventRate(EventUtils.calculateEventRate(event.getEventGrades()))
.additionalImages(event.getAdditionalImages().stream()
.map(EventImages::getLink)
.limit(MAX_ADDITIONAL_IMAGES)
.toList())
.build();
}

private boolean isRelevantCheck(@NonNull List<EventDateLocation> dates) {
return dates.getLast().getFinishDate().isAfter(ZonedDateTime.now())
|| dates.getLast().getFinishDate().isEqual(ZonedDateTime.now());
}

private double calculateEventRate(List<EventGrade> eventGrades) {
return eventGrades.stream()
.mapToInt(EventGrade::getGrade)
.average()
.orElse(0.0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,7 @@ private EventResponseDto buildEventResponseDto(Event event, Long userId) {
.orElse(null);
setFollowers(List.of(), userId);
setSubscribes(List.of(), userId);
eventResponseDto.setCurrentUserGrade(currentUserGrade);

return eventResponseDto;
}
Expand Down
23 changes: 23 additions & 0 deletions service/src/main/java/greencity/utils/EventUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package greencity.utils;

import greencity.entity.event.EventDateLocation;
import greencity.entity.event.EventGrade;
import java.time.ZonedDateTime;
import java.util.List;

public class EventUtils {
public static boolean isRelevant(List<EventDateLocation> dates) {
if (dates == null || dates.isEmpty()) {
return false;
}
return dates.getLast().getFinishDate().isAfter(ZonedDateTime.now())
|| dates.getLast().getFinishDate().isEqual(ZonedDateTime.now());
}

public static double calculateEventRate(List<EventGrade> eventGrades) {
return eventGrades.stream()
.mapToInt(EventGrade::getGrade)
.average()
.orElse(0.0);
}
}
40 changes: 40 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@
import greencity.dto.event.AddressDto;
import greencity.dto.event.EventAttenderDto;
import greencity.dto.event.EventAuthorDto;
import greencity.dto.event.EventDateInformationDto;
import greencity.dto.event.EventDateLocationDto;
import greencity.dto.event.EventDto;
import greencity.dto.event.EventInformationDto;
import greencity.dto.event.EventResponseDto;
import greencity.dto.event.EventVO;
import greencity.dto.event.UpdateAddressDto;
import greencity.dto.event.UpdateEventDateLocationDto;
Expand Down Expand Up @@ -3431,4 +3434,41 @@ public static CategoryDto getCategoryDto() {
"Category Ua",
1L);
}

public static EventResponseDto getEventResponseDto() {
return EventResponseDto.builder()
.id(1L)
.eventInformation(EventInformationDto.builder()
.title("Title")
.description("New Test Event")
.tags(List.of(TagUaEnDto.builder()
.id(2L)
.nameUa("Соціальний")
.nameEn("Social")
.build()))
.build())
.organizer(EventAuthorDto.builder().id(1L).name("Test").build())
.creationDate(LocalDate.of(2025, 1, 10))
.isOpen(true)
.dates(List.of(
EventDateInformationDto.builder()
.startDate(ZonedDateTime.of(2025, 12, 26, 12, 30, 0, 0, ZoneOffset.UTC))
.finishDate(ZonedDateTime.of(2025, 12, 26, 21, 59, 0, 0, ZoneOffset.UTC))
.onlineLink("www.link.com")
.coordinates(getAddressDtoCorrect()).build()))
.type(EventType.OFFLINE)
.isRelevant(true)
.countComments(1)
.isSubscribed(false)
.isFavorite(false)
.isOrganizedByFriend(false)
.build();
}

public static EventDateLocation createEventDateLocation(ZonedDateTime start, ZonedDateTime finish) {
return EventDateLocation.builder()
.startDate(start)
.finishDate(finish)
.build();
}
}
Loading

0 comments on commit b8e88f2

Please sign in to comment.