From 038b800222bc8a5bccce35dfdd60d860042b561a Mon Sep 17 00:00:00 2001 From: abhimanyu-fm Date: Mon, 13 May 2024 21:41:46 -0700 Subject: [PATCH] Fixed failing test cases for getEvents --- .../platform/dynamodb/model/Event.java | 15 +++ .../graphql/builder/EventBuilder.java | 15 ++- .../rest/RazorPaymentCallbackController.java | 2 +- .../graphql/builder/EventBuilderTest.java | 103 +++++++++++++++--- 4 files changed, 118 insertions(+), 17 deletions(-) diff --git a/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java b/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java index dbfd9d89..fc5622cf 100644 --- a/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java +++ b/platform/src/main/java/com/flickmatch/platform/dynamodb/model/Event.java @@ -8,6 +8,7 @@ import java.util.Date; import java.util.List; +import java.util.Objects; @DynamoDBTable(tableName="Event") @Builder @@ -248,5 +249,19 @@ public void setDate(String date) { this.date = date; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + EventId eventId = (EventId) o; + return Objects.equals(cityId, eventId.cityId) && + Objects.equals(date, eventId.date); + } + + @Override + public int hashCode() { + return Objects.hash(cityId, date); + } + } } diff --git a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java index cae8dd7a..65c286be 100644 --- a/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java +++ b/platform/src/main/java/com/flickmatch/platform/graphql/builder/EventBuilder.java @@ -25,6 +25,8 @@ import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import static java.lang.String.format; + @Service @Log4j2 public class EventBuilder { @@ -90,14 +92,23 @@ public Event joinEventRazorPayment(final RazorPaymentRequest paymentRequest) { public List getEvents(String cityId, String localTimeZone) { Date currentTime = new Date(System.currentTimeMillis()); List eventList = new ArrayList<>(); + List prevDayList = getPastEvents(cityId, 2, localTimeZone); + eventList.addAll(prevDayList); + final int GET_EVENT_DAYS = 7; + log.info(format("Fetching events for cityId %s in the next %d days", cityId, GET_EVENT_DAYS)); + for (int i = 0; i < GET_EVENT_DAYS; i++) { Date currentDate = Date.from(currentTime.toInstant().plus(i, ChronoUnit.DAYS)); String formattedDate = DateUtil.extractDateFromISOFormatDate(currentDate, localTimeZone); - Optional eventData = eventRepository.findById(new Event.EventId(cityId, formattedDate)); + Event.EventId eventId = Event.EventId.builder().cityId(cityId).date(formattedDate).build(); + Optional eventData = eventRepository.findById(eventId); + if (eventData.isPresent()) { List dailyEventList = eventData.get().getEventDetailsList().stream() + // TODO: Add condition here to check the the event is starting no later than 7 days + .filter(eventDetails -> eventDetails.getStartTime().after(currentTime)) .map(eventDetails -> mapEventToGQLType(eventDetails, formattedDate, localTimeZone, cityId)) .toList(); eventList.addAll(dailyEventList); @@ -111,6 +122,7 @@ public List getPastEvents(String cit Date currentTime = new Date(System.currentTimeMillis()); // Calculate the date before inDays Date dateBeforeInDays = Date.from(currentTime.toInstant().minus(inDays, ChronoUnit.DAYS)); + log.info(format("Fetching events for cityId %s in the last %d days.", cityId, inDays)); eventRepository.findAll().forEach(event -> { if (event.getCityId().equals(cityId)) { List pastEventsInCity = @@ -124,6 +136,7 @@ public List getPastEvents(String cit pastEventList.addAll(pastEventsInCity); } }); + log.info(format("Found a total of %d events", pastEventList.size())); return pastEventList; } diff --git a/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java b/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java index a9864a06..bcf9928c 100644 --- a/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java +++ b/platform/src/main/java/com/flickmatch/platform/rest/RazorPaymentCallbackController.java @@ -74,7 +74,7 @@ public ResponseEntity processRazorCallback(@RequestParam("razorpay_order_id") } catch (Exception e) { - log.error("Error processing callback: {}", e.getStackTrace()); + log.error("Error processing callback: ", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing callback"); } diff --git a/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java b/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java index 6a0d1695..479cec5c 100644 --- a/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java +++ b/platform/src/test/java/com/flickmatch/platform/graphql/builder/EventBuilderTest.java @@ -1,22 +1,32 @@ package com.flickmatch.platform.graphql.builder; +import static com.flickmatch.platform.graphql.util.DateUtil.extractDateFromISOFormatDate; +import static java.time.Instant.now; +import static java.time.temporal.ChronoUnit.DAYS; +import static java.time.temporal.ChronoUnit.HOURS; +import static java.util.Date.from; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.flickmatch.platform.dynamodb.model.Event; import com.flickmatch.platform.dynamodb.repository.EventRepository; import com.flickmatch.platform.graphql.input.JoinEventInput; import com.flickmatch.platform.graphql.input.PlayerInput; +import com.flickmatch.platform.graphql.util.DateUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.*; public class EventBuilderTest { @@ -92,34 +102,42 @@ private Optional createEventMockData() { .build(); List eventDetailsList = new ArrayList<>(); + eventDetailsList.add(getEventDetails(new Date(), new Date())); - Event.PlayerDetails playerDetails = Event.PlayerDetails.builder() + Optional event = Optional.ofNullable(Event.builder() + .eventId(eventId) + .eventDetailsList(eventDetailsList) + .build()); + return event; + } + + private List getListOfPlayerDetails() { + List playerDetailsList = new ArrayList<>(); + playerDetailsList.add(getPlayerDetails()); + + return playerDetailsList; + } + + private Event.PlayerDetails getPlayerDetails() { + return Event.PlayerDetails.builder() .name("name") .waNumber("waNumber") .build(); + } - List playerDetailsList = new ArrayList<>(); - playerDetailsList.add(playerDetails); - - Event.EventDetails eventDetails = Event.EventDetails.builder() + private Event.EventDetails getEventDetails(Date startTime, Date endTime) { + return Event.EventDetails.builder() .index(1) - .startTime(new Date()) - .endTime(new Date()) + .startTime(startTime) + .endTime(endTime) .charges(10.0) .reservedPlayersCount(0) .waitListPlayersCount(1) .sportName("Football") .venueName("venueName") .venueLocationLink("venueLocationLink") - .playerDetailsList(playerDetailsList) + .playerDetailsList(getListOfPlayerDetails()) .build(); - eventDetailsList.add(eventDetails); - - Optional event = Optional.ofNullable(Event.builder() - .eventId(eventId) - .eventDetailsList(eventDetailsList) - .build()); - return event; } @Test @@ -158,4 +176,59 @@ public void testGetCurrencyForCity() { assertEquals("USD", currencyForSanFrancisco); } + + @Test + public void testGetEvents() { + // Mock input data + String cityId = "2"; + String localTimeZone = "GMT+05:30"; + + List pastEvents = new ArrayList<>(); + + Instant currentTime = Instant.now(); + + String todayDate = extractDateFromISOFormatDate(from(currentTime), localTimeZone); + String yesterdayDate = extractDateFromISOFormatDate(from(currentTime.minus(1, DAYS)), localTimeZone); + Event eventToday = getMockEvent(cityId, todayDate, getEventDetailsList(currentTime)); + Event eventYesterday = getMockEvent(cityId, yesterdayDate, getEventDetailsList(currentTime)); + pastEvents.add(eventYesterday); + + Event.EventId eventIdForToday = Event.EventId.builder().cityId(cityId).date(todayDate).build(); + + // Mock the event repository to return the list of events + when(eventRepository.findById(eq(eventIdForToday))).thenReturn(Optional.of(eventToday)); + when(eventRepository.findAll()).thenReturn(pastEvents); + + + // Call the method under test + List result = eventBuilder.getEvents(cityId, localTimeZone); + + // Verify the result + assertEquals(2, result.size()); + } + + private List getEventDetailsList(Instant currentTime) { + + Event.EventDetails eventInFuture1 = getEventDetails(from(currentTime.plus(1, HOURS)), + from(currentTime.plus(2, HOURS))); + + Event.EventDetails eventInPast1 = getEventDetails(from(currentTime.minus(4, HOURS)), + from(currentTime.minus(3, HOURS))); + + Event.EventDetails eventInPast2 = getEventDetails(from(currentTime.minus(4, DAYS)), + from(currentTime.minus(3, DAYS))); + + List listOfEventDetailsForADay = new ArrayList<>(); + listOfEventDetailsForADay.add(eventInFuture1); + listOfEventDetailsForADay.add(eventInPast1); + + return listOfEventDetailsForADay; + } + + private Event getMockEvent(String cityId, String date, List eventDetailsList) { + return Event.builder() + .eventId(Event.EventId.builder().cityId(cityId).date(date).build()) + .eventDetailsList(eventDetailsList) + .build(); + } }