Skip to content

Commit

Permalink
GH-337: [feat] Test searchEvents functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
wade3hamati committed Feb 22, 2025
1 parent 7e37989 commit 2779a3e
Show file tree
Hide file tree
Showing 2 changed files with 541 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import app.sportahub.eventservice.dto.request.EventRequest;
import app.sportahub.eventservice.dto.response.EventResponse;
import app.sportahub.eventservice.dto.response.LocationResponse;
import app.sportahub.eventservice.dto.response.ParticipantResponse;
import app.sportahub.eventservice.enums.EventSortingField;
import app.sportahub.eventservice.enums.SkillLevelEnum;
import app.sportahub.eventservice.enums.SortDirection;
import app.sportahub.eventservice.model.event.participant.ParticipantAttendStatus;
import app.sportahub.eventservice.service.event.EventService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -17,6 +19,8 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.time.LocalDate;
Expand All @@ -29,6 +33,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class EventControllerTest {
Expand Down Expand Up @@ -98,102 +104,186 @@ public void setUp() {

@Test
public void testGetEventById() {
Mockito.when(eventService.getEventById(anyString())).thenReturn(eventResponse);
when(eventService.getEventById(anyString())).thenReturn(eventResponse);

EventResponse response = eventController.getEventById("testId");

assertEquals(eventResponse, response);
Mockito.verify(eventService).getEventById("testId");
verify(eventService).getEventById("testId");
}

@Test
public void testGetAllEvents() {
List<EventResponse> eventList = Arrays.asList(eventResponse);
Mockito.when(eventService.getAllEvents()).thenReturn(eventList);
when(eventService.getAllEvents()).thenReturn(eventList);

List<EventResponse> response = eventController.getAllEvents();

assertEquals(eventList, response);
Mockito.verify(eventService).getAllEvents();
verify(eventService).getAllEvents();
}

@Test
public void testCreateEvent() {
Mockito.when(eventService.createEvent(any(EventRequest.class))).thenReturn(eventResponse);
when(eventService.createEvent(any(EventRequest.class))).thenReturn(eventResponse);

EventResponse response = eventController.createEvent(eventRequest);

assertEquals(eventResponse, response);
Mockito.verify(eventService).createEvent(eventRequest);
verify(eventService).createEvent(eventRequest);
}

@Test
public void testUpdateEvent() {
Mockito.when(eventService.updateEvent(anyString(), any(EventRequest.class))).thenReturn(eventResponse);
when(eventService.updateEvent(anyString(), any(EventRequest.class))).thenReturn(eventResponse);

EventResponse response = eventController.updateEvent("testId", eventRequest);

assertEquals(eventResponse, response);
Mockito.verify(eventService).updateEvent("testId", eventRequest);
verify(eventService).updateEvent("testId", eventRequest);
}

@Test
public void testPatchEvent() {
Mockito.when(eventService.patchEvent(anyString(), any(EventRequest.class))).thenReturn(eventResponse);
when(eventService.patchEvent(anyString(), any(EventRequest.class))).thenReturn(eventResponse);

EventResponse response = eventController.patchEvent("testId", eventRequest);

assertEquals(eventResponse, response);
Mockito.verify(eventService).patchEvent("testId", eventRequest);
verify(eventService).patchEvent("testId", eventRequest);
}

@Test
public void testDeleteEvent() {
eventController.deleteEvent("testId");

Mockito.verify(eventService).deleteEvent("testId");
verify(eventService).deleteEvent("testId");
}

@Test
public void testJoinEvent() {
Mockito.when(eventService.joinEvent(anyString(), anyString())).thenReturn(participantResponse);
when(eventService.joinEvent(anyString(), anyString())).thenReturn(participantResponse);

ParticipantResponse response = eventController.joinEvent("testId", "userId");

assertEquals(participantResponse, response);
Mockito.verify(eventService).joinEvent("testId", "userId");
verify(eventService).joinEvent("testId", "userId");
}

@Test
public void testGetEventsByParticipantId() {
Page<EventResponse> eventPage = new PageImpl<>(Arrays.asList(eventResponse));
Mockito.when(eventService.getEventsByParticipantId(anyString(), any(int.class), any(int.class), any(SortDirection.class), any(EventSortingField.class)))
when(eventService.getEventsByParticipantId(anyString(), any(int.class), any(int.class), any(SortDirection.class), any(EventSortingField.class)))
.thenReturn(eventPage);

Page<EventResponse> response = eventController.getEventByUserId("userId", 0, 10, SortDirection.DESC, EventSortingField.DATE);

assertEquals(eventPage, response);
Mockito.verify(eventService).getEventsByParticipantId("userId", 0, 10, SortDirection.DESC, EventSortingField.DATE);
verify(eventService).getEventsByParticipantId("userId", 0, 10, SortDirection.DESC, EventSortingField.DATE);
}

@Test
public void testGetEventsCreatedByUserId() {
Page<EventResponse> eventPage = new PageImpl<>(Arrays.asList(eventResponse));
Mockito.when(eventService.getEventsCreatedByUserId(anyString(), any(int.class), any(int.class), any(SortDirection.class), any(EventSortingField.class)))
when(eventService.getEventsCreatedByUserId(anyString(), any(int.class), any(int.class), any(SortDirection.class), any(EventSortingField.class)))
.thenReturn(eventPage);

Page<EventResponse> response = eventController.getEventsCreatedByUserId("userId", 0, 10, SortDirection.DESC, EventSortingField.DATE);

assertEquals(eventPage, response);
Mockito.verify(eventService).getEventsCreatedByUserId("userId", 0, 10, SortDirection.DESC, EventSortingField.DATE);
verify(eventService).getEventsCreatedByUserId("userId", 0, 10, SortDirection.DESC, EventSortingField.DATE);
}

@Test void testLeaveEvent() {
Mockito.when(eventService.leaveEvent(anyString(), anyString())).thenReturn(participantResponse);
when(eventService.leaveEvent(anyString(), anyString())).thenReturn(participantResponse);

ParticipantResponse response = eventController.leaveEvent("testId", "userId");

assertEquals(participantResponse, response);
Mockito.verify(eventService).leaveEvent("testId", "userId");
verify(eventService).leaveEvent("testId", "userId");
}

@Test
void testSearchEvents() {
// Arrange
Pageable pageable = PageRequest.of(0, 10);
EventResponse eventResponse = new EventResponse(
"event123",
null,
"Soccer Match",
"Friendly",
"Soccer",
new LocationResponse("Central Park", "", "", "New York", "NY", "USA", "10001", "", "", "", ""),
LocalDate.of(2023, 10, 15),
LocalTime.of(14, 0),
LocalTime.of(16, 0),
"120",
20,
Collections.emptyList(), // participants
"user123",
Collections.emptyList(), // teams
"18:00", // cutOffTime
"A friendly soccer match in Central Park",
false,
Collections.emptyList(), // whitelistedUsers
EnumSet.of(SkillLevelEnum.INTERMEDIATE)
);
Page<EventResponse> mockPage = new PageImpl<>(List.of(eventResponse), pageable, 1);

// Mock the service method
when(eventService.searchEvents(
anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(),
anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), any(), any(), any()
)).thenReturn(mockPage);

// Act
Page<EventResponse> result = eventController.searchEvents(
"Soccer Match", // eventName
"Friendly", // eventType
"Soccer", // sportType
"Central Park", // locationName
"New York", // city
"NY", // province
"USA", // country
"10001", // postalCode
"2023-10-15", // date
"14:00", // startTime
"16:00", // endTime
"120", // duration
"20", // maxParticipants
"user123", // createdBy
false, // isPrivate
List.of(SkillLevelEnum.INTERMEDIATE), // requiredSkillLevel
0, // page
10 // size
);

// Assert
assertEquals(1, result.getTotalElements());
assertEquals("event123", result.getContent().getFirst().id());
assertEquals("Soccer Match", result.getContent().getFirst().eventName());
assertEquals("Friendly", result.getContent().getFirst().eventType());
assertEquals("Soccer", result.getContent().getFirst().sportType());
assertEquals("Central Park", result.getContent().getFirst().locationResponse().name());
assertEquals("New York", result.getContent().getFirst().locationResponse().city());
assertEquals("NY", result.getContent().getFirst().locationResponse().province());
assertEquals("USA", result.getContent().getFirst().locationResponse().country());
assertEquals("10001", result.getContent().getFirst().locationResponse().postalCode());
assertEquals(LocalDate.of(2023, 10, 15), result.getContent().getFirst().date());
assertEquals(LocalTime.of(14, 0), result.getContent().getFirst().startTime());
assertEquals(LocalTime.of(16, 0), result.getContent().getFirst().endTime());
assertEquals("120", result.getContent().getFirst().duration());
assertEquals(20, result.getContent().getFirst().maxParticipants());
assertEquals("user123", result.getContent().getFirst().createdBy());
assertEquals("18:00", result.getContent().getFirst().cutOffTime());
assertEquals("A friendly soccer match in Central Park", result.getContent().getFirst().description());
Assertions.assertFalse(result.getContent().getFirst().isPrivate());
assertEquals(EnumSet.of(SkillLevelEnum.INTERMEDIATE), result.getContent().getFirst().requiredSkillLevel());

// Verify interactions
verify(eventService).searchEvents(
"Soccer Match", "Friendly", "Soccer", "Central Park", "New York", "NY", "USA", "10001",
"2023-10-15", "14:00", "16:00", "120", "20", "user123", false, List.of(SkillLevelEnum.INTERMEDIATE), pageable
);
}
}
Loading

0 comments on commit 2779a3e

Please sign in to comment.