Skip to content

Commit

Permalink
DPO-2506 ScheduleTrains -> LiveTrains
Browse files Browse the repository at this point in the history
  • Loading branch information
teijosol committed Mar 18, 2024
1 parent f237f79 commit 69222f4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
@Transactional
public interface TrainRepository extends CustomGeneralRepository<Train, TrainId> {

String BASE_TRAIN_SELECT = "select distinct train from Train train " +
" inner join fetch train.timeTableRows timeTableRow " +
" left join fetch timeTableRow.causes c " +
" left join fetch c.categoryCode categoryCode " +
" left join fetch c.detailedCategoryCode detailedCategoryCode " +
" left join fetch c.thirdCategoryCode thirdCategoryCode " +
" left join fetch timeTableRow.trainReadies tr ";
String BASE_TRAIN_SELECT = """
select distinct train from Train train
inner join fetch train.timeTableRows timeTableRow
left join fetch timeTableRow.causes c
left join fetch c.categoryCode categoryCode
left join fetch c.detailedCategoryCode detailedCategoryCode
left join fetch c.thirdCategoryCode thirdCategoryCode
left join fetch timeTableRow.trainReadies tr """;

String BASE_TRAIN_ORDER = " order by train.id.departureDate, train.id.trainNumber, timeTableRow.scheduledTime, timeTableRow.type";
String IS_NOT_DELETED = "(train.deleted is null or train.deleted = false)";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Collections;
import java.util.List;

import fi.livi.rata.avoindata.common.domain.jsonview.TrainJsonView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -48,17 +49,17 @@ public class ScheduleController extends ADataController {
private TrainRepository trainRepository;

@Operation(summary = "Return trains that run from {arrival_station} to {departure_station}", ignoreJsonView = true)
@JsonView(ScheduleTrains.class)
@JsonView(TrainJsonView.LiveTrains.class)
@RequestMapping(path = "station/{departure_station}/{arrival_station}", method = RequestMethod.GET)
@Transactional(timeout = 30, readOnly = true)
public List<Train> getTrainsFromDepartureToArrivalStation(
@Parameter(description = "departure_station") @PathVariable String departure_station,
@Parameter(description = "arrival_station") @PathVariable String arrival_station,
@Parameter(description = "departure_date") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate departure_date,
@Parameter(description = "include_nonstopping") @RequestParam(required = false, defaultValue = "false") Boolean include_nonstopping,
@Parameter(description = "startDate") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime startDate,
@Parameter(description = "endDate") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime endDate,
@Parameter(description = "limit") @RequestParam(required = false) Integer limit, HttpServletResponse response) {
@Parameter(description = "departure_date") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) final LocalDate departure_date,
@Parameter(description = "include_nonstopping") @RequestParam(required = false, defaultValue = "false") final Boolean include_nonstopping,
@Parameter(description = "startDate") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) final ZonedDateTime startDate,
@Parameter(description = "endDate") @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) final ZonedDateTime endDate,
@Parameter(description = "limit") @RequestParam(required = false) Integer limit, final HttpServletResponse response) {
if (limit == null) {
limit = MAX_ROUTE_SEARCH_RESULT_SIZE;
}
Expand All @@ -73,7 +74,7 @@ public List<Train> getTrainsFromDepartureToArrivalStation(
}
}

List<Train> list = findTrains(departure_station, arrival_station, departure_date, include_nonstopping, startDate, endDate);
final List<Train> list = findTrains(departure_station, arrival_station, departure_date, include_nonstopping, startDate, endDate);

CacheConfig.SCHEDULE_STATION_CACHECONTROL.setCacheParameter(response, list, -1);

Expand All @@ -92,10 +93,10 @@ public List<Train> getTrainsFromDepartureToArrivalStation(

private List<Train> findTrains(final String departure_station, final String arrival_station, final LocalDate departure_date,
final Boolean include_nonstopping, final ZonedDateTime from, final ZonedDateTime to) {
ZonedDateTime actualTrainStart;
ZonedDateTime actualTrainEnd;
LocalDate departureDateStart;
LocalDate departureDateEnd;
final ZonedDateTime actualTrainStart;
final ZonedDateTime actualTrainEnd;
final LocalDate departureDateStart;
final LocalDate departureDateEnd;
if (departure_date != null) {
actualTrainStart = departure_date.minusDays(1).atStartOfDay(ZoneId.of("Europe/Helsinki"));
actualTrainEnd = departure_date.plusDays(2).atStartOfDay(ZoneId.of("Europe/Helsinki"));
Expand All @@ -122,7 +123,7 @@ private List<Train> findTrains(final String departure_station, final String arri
departureDateEnd = actualTrainEnd.plusDays(1).toLocalDate();
}

List<Train> list = trainRepository.findByStationsAndScheduledDate(departure_station, TimeTableRow.TimeTableRowType.DEPARTURE,
final List<Train> list = trainRepository.findByStationsAndScheduledDate(departure_station, TimeTableRow.TimeTableRowType.DEPARTURE,
arrival_station, TimeTableRow.TimeTableRowType.ARRIVAL, actualTrainStart, actualTrainEnd, departureDateStart,
departureDateEnd, !include_nonstopping);
return list;
Expand All @@ -131,8 +132,8 @@ private List<Train> findTrains(final String departure_station, final String arri
private void sortByDepartureStationScheduledTime(final String departure_station, final List<Train> trains) {
Collections.sort(trains, (firstTrain, secondTrain) -> {
final Predicate<TimeTableRow> stationShortCodePredicate = s -> s.station.stationShortCode.equals(departure_station);
TimeTableRow departureTimeTableRowFirst = Iterables.find(firstTrain.timeTableRows, stationShortCodePredicate);
TimeTableRow departureTimeTableRowSecond = Iterables.find(secondTrain.timeTableRows, stationShortCodePredicate);
final TimeTableRow departureTimeTableRowFirst = Iterables.find(firstTrain.timeTableRows, stationShortCodePredicate);
final TimeTableRow departureTimeTableRowSecond = Iterables.find(secondTrain.timeTableRows, stationShortCodePredicate);

return departureTimeTableRowFirst.scheduledTime.compareTo(departureTimeTableRowSecond.scheduledTime);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ public class LiveTrainControllerTest extends MockMvcBaseTest {
public void baseAttributesShouldBeCorrect() throws Exception {
trainFactory.createBaseTrain();

final ResultActions r1 = getJson("/live-trains/51");

r1.andExpect(jsonPath("$[0].trainNumber").value("51"));
r1.andExpect(jsonPath("$[0].departureDate").value(LocalDate.now().toString()));
r1.andExpect(jsonPath("$[0].operatorUICCode").value("1"));
r1.andExpect(jsonPath("$[0].operatorShortCode").value("test"));
r1.andExpect(jsonPath("$[0].commuterLineID").value("Z"));
r1.andExpect(jsonPath("$[0].runningCurrently").value("true"));
r1.andExpect(jsonPath("$[0].cancelled").value("false"));
r1.andExpect(jsonPath("$[0].version").value("1"));
getJson("/live-trains/51")
.andExpect(jsonPath("$[0].trainNumber").value("51"))
.andExpect(jsonPath("$[0].departureDate").value(LocalDate.now().toString()))
.andExpect(jsonPath("$[0].operatorUICCode").value("1"))
.andExpect(jsonPath("$[0].operatorShortCode").value("test"))
.andExpect(jsonPath("$[0].commuterLineID").value("Z"))
.andExpect(jsonPath("$[0].runningCurrently").value("true"))
.andExpect(jsonPath("$[0].cancelled").value("false"))
.andExpect(jsonPath("$[0].version").value("1"))
.andExpect(jsonPath("$[0].timeTableRows").isNotEmpty())
.andExpect(jsonPath("$[0].timeTableRows[0].actualTime").isNotEmpty())
;
}

@Test
Expand Down Expand Up @@ -150,7 +152,7 @@ public void correctDepartureDateShouldBeSelected() throws Exception {
public void timetableTypeShouldWork() throws Exception {
trainFactory.createBaseTrain(new TrainId(51L, LocalDate.now()));

Train train2 = trainFactory.createBaseTrain(new TrainId(52L, LocalDate.now()));
final Train train2 = trainFactory.createBaseTrain(new TrainId(52L, LocalDate.now()));
train2.timetableType = Train.TimetableType.ADHOC;
trainRepository.save(train2);

Expand Down Expand Up @@ -181,9 +183,9 @@ public void versionLessThanShouldShow() throws Exception {
}


private void clearActualTimes(Train... trains) {
for (Train train : trains) {
for (TimeTableRow timeTableRow : train.timeTableRows) {
private void clearActualTimes(final Train... trains) {
for (final Train train : trains) {
for (final TimeTableRow timeTableRow : train.timeTableRows) {
timeTableRow.actualTime = null;
}
}
Expand Down Expand Up @@ -283,14 +285,14 @@ public void stationSearchShouldWork() throws Exception {
@Transactional
@Disabled
public void trainCategoryFilteringShouldWork() throws Exception {
TrainCategory trainCategory1 = trainCategoryFactory.create(1L, "test category");
TrainCategory trainCategory2 = trainCategoryFactory.create(2L, "test cat");
TrainType trainType = trainTypeFactory.create(trainCategory1);
final TrainCategory trainCategory1 = trainCategoryFactory.create(1L, "test category");
final TrainCategory trainCategory2 = trainCategoryFactory.create(2L, "test cat");
final TrainType trainType = trainTypeFactory.create(trainCategory1);

ReflectionTestUtils.setField(bes, "executor", MoreExecutors.newDirectExecutorService());

Train train1 = trainFactory.createBaseTrain(new TrainId(1L, LocalDate.now()));
Train train2 = trainFactory.createBaseTrain(new TrainId(2L, LocalDate.now()));
final Train train1 = trainFactory.createBaseTrain(new TrainId(1L, LocalDate.now()));
final Train train2 = trainFactory.createBaseTrain(new TrainId(2L, LocalDate.now()));

clearActualTimes(train1, train2);

Expand Down Expand Up @@ -353,7 +355,7 @@ public void deletedTrainShouldNotBeReturnedTroughLiveTrain() throws Exception {
public void deletedTrainShouldNotBeReturnedTroughLiveTrain2() throws Exception {
ReflectionTestUtils.setField(bes, "executor", MoreExecutors.newDirectExecutorService());

LocalDate dateNow = LocalDate.now();
final LocalDate dateNow = LocalDate.now();
final Train train1 = trainFactory.createBaseTrain(new TrainId(1L, dateNow));
final Train train2 = trainFactory.createBaseTrain(new TrainId(2L, dateNow));
final Train train3 = trainFactory.createBaseTrain(new TrainId(3L, dateNow));
Expand All @@ -362,7 +364,7 @@ public void deletedTrainShouldNotBeReturnedTroughLiveTrain2() throws Exception {

clearActualTimes(train1, train2, train3, train4, train5);

ZonedDateTime zonedDateTimeNow = ZonedDateTime.now();
final ZonedDateTime zonedDateTimeNow = ZonedDateTime.now();
train1.timeTableRows.get(0).scheduledTime = zonedDateTimeNow.plusMinutes(1);
train2.timeTableRows.get(0).scheduledTime = zonedDateTimeNow.plusMinutes(2);
train3.timeTableRows.get(0).scheduledTime = zonedDateTimeNow.plusMinutes(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

public class ScheduleControllerTest extends MockMvcBaseTest {
@Autowired
private TrainFactory trainFactory;
Expand Down Expand Up @@ -39,4 +44,23 @@ public void routeSearchShouldWork() throws Exception {
//Nothing matches
assertException("/live-trains/station/TKU/JOE","TRAIN_NOT_FOUND");
}

@Test
@Transactional
public void attributesPresent() throws Exception {
trainFactory.createBaseTrain();

getJson("/live-trains/station/HKI/OL")
.andExpect(jsonPath("$[0].trainNumber").value("51"))
.andExpect(jsonPath("$[0].departureDate").value(LocalDate.now().toString()))
.andExpect(jsonPath("$[0].operatorUICCode").value("1"))
.andExpect(jsonPath("$[0].operatorShortCode").value("test"))
.andExpect(jsonPath("$[0].commuterLineID").value("Z"))
.andExpect(jsonPath("$[0].runningCurrently").value("true"))
.andExpect(jsonPath("$[0].cancelled").value("false"))
.andExpect(jsonPath("$[0].version").value("1"))
.andExpect(jsonPath("$[0].timeTableRows").isNotEmpty())
.andExpect(jsonPath("$[0].timeTableRows[0].actualTime").isNotEmpty())
;
}
}

0 comments on commit 69222f4

Please sign in to comment.