diff --git a/AvoinDataCommon/src/main/java/fi/livi/rata/avoindata/common/dao/train/TrainRepository.java b/AvoinDataCommon/src/main/java/fi/livi/rata/avoindata/common/dao/train/TrainRepository.java index be0053e3..727a0e21 100644 --- a/AvoinDataCommon/src/main/java/fi/livi/rata/avoindata/common/dao/train/TrainRepository.java +++ b/AvoinDataCommon/src/main/java/fi/livi/rata/avoindata/common/dao/train/TrainRepository.java @@ -21,13 +21,14 @@ @Transactional public interface TrainRepository extends CustomGeneralRepository { - 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)"; diff --git a/AvoinDataServer/src/main/java/fi/livi/rata/avoindata/server/controller/api/ScheduleController.java b/AvoinDataServer/src/main/java/fi/livi/rata/avoindata/server/controller/api/ScheduleController.java index 20998bcd..dec476d9 100644 --- a/AvoinDataServer/src/main/java/fi/livi/rata/avoindata/server/controller/api/ScheduleController.java +++ b/AvoinDataServer/src/main/java/fi/livi/rata/avoindata/server/controller/api/ScheduleController.java @@ -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; @@ -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 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; } @@ -73,7 +74,7 @@ public List getTrainsFromDepartureToArrivalStation( } } - List list = findTrains(departure_station, arrival_station, departure_date, include_nonstopping, startDate, endDate); + final List list = findTrains(departure_station, arrival_station, departure_date, include_nonstopping, startDate, endDate); CacheConfig.SCHEDULE_STATION_CACHECONTROL.setCacheParameter(response, list, -1); @@ -92,10 +93,10 @@ public List getTrainsFromDepartureToArrivalStation( private List 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")); @@ -122,7 +123,7 @@ private List findTrains(final String departure_station, final String arri departureDateEnd = actualTrainEnd.plusDays(1).toLocalDate(); } - List list = trainRepository.findByStationsAndScheduledDate(departure_station, TimeTableRow.TimeTableRowType.DEPARTURE, + final List list = trainRepository.findByStationsAndScheduledDate(departure_station, TimeTableRow.TimeTableRowType.DEPARTURE, arrival_station, TimeTableRow.TimeTableRowType.ARRIVAL, actualTrainStart, actualTrainEnd, departureDateStart, departureDateEnd, !include_nonstopping); return list; @@ -131,8 +132,8 @@ private List findTrains(final String departure_station, final String arri private void sortByDepartureStationScheduledTime(final String departure_station, final List trains) { Collections.sort(trains, (firstTrain, secondTrain) -> { final Predicate 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); }); diff --git a/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/LiveTrainControllerTest.java b/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/LiveTrainControllerTest.java index 6f56cd9d..11a2d3c8 100644 --- a/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/LiveTrainControllerTest.java +++ b/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/LiveTrainControllerTest.java @@ -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 @@ -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); @@ -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; } } @@ -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); @@ -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)); @@ -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); diff --git a/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/ScheduleControllerTest.java b/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/ScheduleControllerTest.java index fc3e8301..a7c315b4 100644 --- a/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/ScheduleControllerTest.java +++ b/AvoinDataServer/src/test/java/fi/livi/rata/avoindata/server/controller/api/ScheduleControllerTest.java @@ -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; @@ -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()) + ; + } } \ No newline at end of file