Skip to content

Commit

Permalink
Fixed failing test cases for getEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyu-fm committed May 14, 2024
1 parent abbfcbc commit 038b800
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Date;
import java.util.List;
import java.util.Objects;

@DynamoDBTable(tableName="Event")
@Builder
Expand Down Expand Up @@ -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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import static java.lang.String.format;

@Service
@Log4j2
public class EventBuilder {
Expand Down Expand Up @@ -90,14 +92,23 @@ public Event joinEventRazorPayment(final RazorPaymentRequest paymentRequest) {
public List<com.flickmatch.platform.graphql.type.Event> getEvents(String cityId, String localTimeZone) {
Date currentTime = new Date(System.currentTimeMillis());
List<com.flickmatch.platform.graphql.type.Event> eventList = new ArrayList<>();
List<com.flickmatch.platform.graphql.type.Event> 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<Event> eventData = eventRepository.findById(new Event.EventId(cityId, formattedDate));
Event.EventId eventId = Event.EventId.builder().cityId(cityId).date(formattedDate).build();
Optional<Event> eventData = eventRepository.findById(eventId);

if (eventData.isPresent()) {
List<com.flickmatch.platform.graphql.type.Event> 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);
Expand All @@ -111,6 +122,7 @@ public List<com.flickmatch.platform.graphql.type.Event> 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<com.flickmatch.platform.graphql.type.Event> pastEventsInCity =
Expand All @@ -124,6 +136,7 @@ public List<com.flickmatch.platform.graphql.type.Event> getPastEvents(String cit
pastEventList.addAll(pastEventsInCity);
}
});
log.info(format("Found a total of %d events", pastEventList.size()));
return pastEventList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -92,34 +102,42 @@ private Optional<Event> createEventMockData() {
.build();

List<Event.EventDetails> eventDetailsList = new ArrayList<>();
eventDetailsList.add(getEventDetails(new Date(), new Date()));

Event.PlayerDetails playerDetails = Event.PlayerDetails.builder()
Optional<Event> event = Optional.ofNullable(Event.builder()
.eventId(eventId)
.eventDetailsList(eventDetailsList)
.build());
return event;
}

private List<Event.PlayerDetails> getListOfPlayerDetails() {
List<Event.PlayerDetails> playerDetailsList = new ArrayList<>();
playerDetailsList.add(getPlayerDetails());

return playerDetailsList;
}

private Event.PlayerDetails getPlayerDetails() {
return Event.PlayerDetails.builder()
.name("name")
.waNumber("waNumber")
.build();
}

List<Event.PlayerDetails> 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> event = Optional.ofNullable(Event.builder()
.eventId(eventId)
.eventDetailsList(eventDetailsList)
.build());
return event;
}

@Test
Expand Down Expand Up @@ -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<Event> 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<com.flickmatch.platform.graphql.type.Event> result = eventBuilder.getEvents(cityId, localTimeZone);

// Verify the result
assertEquals(2, result.size());
}

private List<Event.EventDetails> 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<Event.EventDetails> listOfEventDetailsForADay = new ArrayList<>();
listOfEventDetailsForADay.add(eventInFuture1);
listOfEventDetailsForADay.add(eventInPast1);

return listOfEventDetailsForADay;
}

private Event getMockEvent(String cityId, String date, List<Event.EventDetails> eventDetailsList) {
return Event.builder()
.eventId(Event.EventId.builder().cityId(cityId).date(date).build())
.eventDetailsList(eventDetailsList)
.build();
}
}

0 comments on commit 038b800

Please sign in to comment.