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 d542f1c commit 9efaf10
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 50 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,19 +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, 1, localTimeZone);
List<com.flickmatch.platform.graphql.type.Event> prevDayList = getPastEvents(cityId, 2, localTimeZone);
eventList.addAll(prevDayList);

final int GET_EVENT_DAYS = 7;
eventList.addAll(prevDayList);
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);
System.out.println(formattedDate);
Optional<Event> eventData = eventRepository.findById(new Event.EventId(cityId, formattedDate));
System.out.println(eventData);
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 @@ -117,6 +123,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 @@ -130,6 +137,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,23 +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 +101,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 @@ -164,47 +181,53 @@ public void testGetEvents() {
// Mock input data
String cityId = "2";
String localTimeZone = "GMT+05:30";
List<Event> events = createMockEvents(cityId);
// Print details of each event
// for (Event event : events) {
// System.out.println("Event Details:");
// System.out.println("City ID: " + event.getCityId());
// System.out.println("Date: " + event.getDate());
// // Add more details as needed
// }

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.findAll()).thenReturn(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("2", "GMT+5:30");
List<com.flickmatch.platform.graphql.type.Event> result = eventBuilder.getEvents(cityId, localTimeZone);

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

private List<Event> createMockEvents(String cityId) {
List<Event> events = new ArrayList<>();
private List<Event.EventDetails> getEventDetailsList(Instant currentTime) {

// Get today's date
LocalDate today = LocalDate.now();
Event.EventDetails eventInFuture1 = getEventDetails(from(currentTime.plus(1, HOURS)),
from(currentTime.plus(2, HOURS)));

// Create an event for today
Event eventToday = new Event();
eventToday.setCityId(cityId);
eventToday.setDate(today.toString());
eventToday.setEventDetailsList(new ArrayList<>());
events.add(eventToday);
Event.EventDetails eventInPast1 = getEventDetails(from(currentTime.minus(4, HOURS)),
from(currentTime.minus(3, HOURS)));

// Get yesterday's date
LocalDate yesterday = today.minusDays(1);
Event.EventDetails eventInPast2 = getEventDetails(from(currentTime.minus(4, DAYS)),
from(currentTime.minus(3, DAYS)));

// Create an event for yesterday
Event eventYesterday = new Event();
eventYesterday.setCityId(cityId);
eventYesterday.setDate(yesterday.toString());
eventYesterday.setEventDetailsList(new ArrayList<>());
events.add(eventYesterday);
List<Event.EventDetails> listOfEventDetailsForADay = new ArrayList<>();
listOfEventDetailsForADay.add(eventInFuture1);
listOfEventDetailsForADay.add(eventInPast1);

return events;
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 9efaf10

Please sign in to comment.