From feeddac1097f12319f90ed7a36f187f8c4e26344 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 15 May 2024 14:48:03 -0400 Subject: [PATCH 1/4] refactor(TrackedTripControllerTest): Get headers once per test execution. --- .../api/TrackedTripControllerTest.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java index 1f1d17d9c..0cbe33347 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java @@ -30,8 +30,8 @@ import org.opentripplanner.middleware.utils.HttpResponseValues; import org.opentripplanner.middleware.utils.JsonUtils; -import java.io.IOException; import java.util.Date; +import java.util.HashMap; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -56,9 +56,10 @@ public class TrackedTripControllerTest extends OtpMiddlewareTestEnvironment { private static final String TRACK_TRIP_PATH = ROUTE_PATH + "track"; private static final String END_TRACKING_TRIP_PATH = ROUTE_PATH + "endtracking"; private static final String FORCIBLY_END_TRACKING_TRIP_PATH = ROUTE_PATH + "forciblyendtracking"; + private static HashMap headers; @BeforeAll - public static void setUp() throws IOException { + public static void setUp() throws Exception { assumeTrue(IS_END_TO_END); setAuthDisabled(false); OtpTestUtils.mockOtpServer(); @@ -75,6 +76,7 @@ public static void setUp() throws IOException { monitoredTrip = new MonitoredTrip(); monitoredTrip.userId = soloOtpUser.id; Persistence.monitoredTrips.create(monitoredTrip); + headers = getMockHeaders(soloOtpUser); } @AfterAll @@ -102,7 +104,7 @@ void canCompleteJourneyLifeCycle() throws Exception { var response = makeRequest( START_TRACKING_TRIP_PATH, JsonUtils.toJson(createStartTrackingPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -119,7 +121,7 @@ void canCompleteJourneyLifeCycle() throws Exception { response = makeRequest( UPDATE_TRACKING_TRIP_PATH, JsonUtils.toJson(createUpdateTrackingPayload(startTrackingResponse.journeyId)), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -138,7 +140,7 @@ void canCompleteJourneyLifeCycle() throws Exception { response = makeRequest( END_TRACKING_TRIP_PATH, JsonUtils.toJson(createEndTrackingPayload(startTrackingResponse.journeyId)), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); var endTrackingResponse = JsonUtils.getPOJOFromJSON(response.responseBody, EndTrackingResponse.class); @@ -155,7 +157,7 @@ void canNotRestartAnOngoingJourney() throws Exception { var response = makeRequest( START_TRACKING_TRIP_PATH, JsonUtils.toJson(createStartTrackingPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -166,7 +168,7 @@ void canNotRestartAnOngoingJourney() throws Exception { response = makeRequest( START_TRACKING_TRIP_PATH, JsonUtils.toJson(createStartTrackingPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -182,7 +184,7 @@ void canStartThenUpdateOngoingJourney() throws Exception { var response = makeRequest( TRACK_TRIP_PATH, JsonUtils.toJson(createTrackPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -195,7 +197,7 @@ void canStartThenUpdateOngoingJourney() throws Exception { response = makeRequest( TRACK_TRIP_PATH, JsonUtils.toJson(createTrackPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -213,7 +215,7 @@ void canForciblyEndJourney() throws Exception { var response = makeRequest( START_TRACKING_TRIP_PATH, JsonUtils.toJson(createStartTrackingPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -224,7 +226,7 @@ void canForciblyEndJourney() throws Exception { response = makeRequest( FORCIBLY_END_TRACKING_TRIP_PATH, JsonUtils.toJson(createForceEndTrackingPayload(monitoredTrip.id)), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); var endTrackingResponse = JsonUtils.getPOJOFromJSON(response.responseBody, EndTrackingResponse.class); @@ -239,7 +241,7 @@ void canNotUseUnassociatedTrip() throws Exception { HttpResponseValues response = makeRequest( START_TRACKING_TRIP_PATH, JsonUtils.toJson(createStartTrackingPayload("unassociated-trip-id")), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -255,7 +257,7 @@ void canNotUpdateUnknownJourney() throws Exception { HttpResponseValues response = makeRequest( UPDATE_TRACKING_TRIP_PATH, JsonUtils.toJson(createUpdateTrackingPayload("unknown-journey-id")), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -271,7 +273,7 @@ void canNotUpdateCompletedJourney() throws Exception { var response = makeRequest( START_TRACKING_TRIP_PATH, JsonUtils.toJson(createStartTrackingPayload()), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); @@ -284,7 +286,7 @@ void canNotUpdateCompletedJourney() throws Exception { response = makeRequest( END_TRACKING_TRIP_PATH, JsonUtils.toJson(createEndTrackingPayload(startTrackingResponse.journeyId)), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); assertEquals(HttpStatus.OK_200, response.status); @@ -292,7 +294,7 @@ void canNotUpdateCompletedJourney() throws Exception { response = makeRequest( UPDATE_TRACKING_TRIP_PATH, JsonUtils.toJson(createUpdateTrackingPayload(startTrackingResponse.journeyId)), - getMockHeaders(soloOtpUser), + headers, HttpMethod.POST ); From 8a986d4cd6104b9975fb89b9145d0f9103ff916a Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 15 May 2024 15:34:06 -0400 Subject: [PATCH 2/4] fix(ManageTripTracking): Fix startTrip param, add hardeneing tests to TrackedTripController --- .../triptracker/ManageTripTracking.java | 2 +- .../triptracker/TravelerLocator.java | 15 +- .../api/TrackedTripControllerTest.java | 152 +++++++++++++++++- 3 files changed, 149 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java b/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java index 10543c3fb..3e94b0220 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/ManageTripTracking.java @@ -70,7 +70,7 @@ private static TrackingResponse doUpdateTracking(Request request, TripTrackingDa // Provide response. return new TrackingResponse( TRIP_TRACKING_UPDATE_FREQUENCY_SECONDS, - TravelerLocator.getInstruction(tripStatus, travelerPosition, true), + TravelerLocator.getInstruction(tripStatus, travelerPosition, create), trackedJourney.id, tripStatus.name() ); diff --git a/src/main/java/org/opentripplanner/middleware/triptracker/TravelerLocator.java b/src/main/java/org/opentripplanner/middleware/triptracker/TravelerLocator.java index 8f7ad20ea..2f0f79d78 100644 --- a/src/main/java/org/opentripplanner/middleware/triptracker/TravelerLocator.java +++ b/src/main/java/org/opentripplanner/middleware/triptracker/TravelerLocator.java @@ -87,25 +87,12 @@ private static TripInstruction getBackOnTrack(TravelerPosition travelerPosition, */ @Nullable public static TripInstruction alignTravelerToTrip(TravelerPosition travelerPosition, boolean isStartOfTrip) { - - if (isStartOfTrip) { - // If the traveler has just started the trip and is within a set distance of the first step. - Step firstStep = travelerPosition.expectedLeg.steps.get(0); - if (firstStep == null) { - return null; - } - double distance = getDistance(travelerPosition.currentPosition, new Coordinates(firstStep)); - return (distance <= TRIP_INSTRUCTION_UPCOMING_RADIUS) - ? new TripInstruction(distance, firstStep) - : null; - } - if (isApproachingDestination(travelerPosition)) { return new TripInstruction(getDistanceToDestination(travelerPosition), travelerPosition.expectedLeg.to.name); } Step nextStep = snapToStep(travelerPosition); - if (nextStep != null && !isPositionPastStep(travelerPosition, nextStep)) { + if (nextStep != null && (!isPositionPastStep(travelerPosition, nextStep) || isStartOfTrip)) { return new TripInstruction( getDistance(travelerPosition.currentPosition, new Coordinates(nextStep)), nextStep diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java index 0cbe33347..0aa3c5b81 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java @@ -7,15 +7,23 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.opentripplanner.middleware.auth.Auth0Users; import org.opentripplanner.middleware.models.MonitoredTrip; import org.opentripplanner.middleware.models.OtpUser; import org.opentripplanner.middleware.models.TrackedJourney; +import org.opentripplanner.middleware.otp.response.Itinerary; +import org.opentripplanner.middleware.otp.response.Leg; +import org.opentripplanner.middleware.otp.response.Step; import org.opentripplanner.middleware.persistence.Persistence; import org.opentripplanner.middleware.testutils.ApiTestUtils; +import org.opentripplanner.middleware.testutils.CommonTestUtils; import org.opentripplanner.middleware.testutils.OtpMiddlewareTestEnvironment; import org.opentripplanner.middleware.testutils.OtpTestUtils; import org.opentripplanner.middleware.testutils.PersistenceTestUtils; +import org.opentripplanner.middleware.tripmonitor.JourneyState; import org.opentripplanner.middleware.triptracker.ManageTripTracking; import org.opentripplanner.middleware.triptracker.TrackingLocation; import org.opentripplanner.middleware.triptracker.TripStatus; @@ -26,13 +34,16 @@ import org.opentripplanner.middleware.triptracker.payload.UpdatedTrackingPayload; import org.opentripplanner.middleware.triptracker.response.EndTrackingResponse; import org.opentripplanner.middleware.triptracker.response.TrackingResponse; +import org.opentripplanner.middleware.utils.Coordinates; import org.opentripplanner.middleware.utils.DateTimeUtils; import org.opentripplanner.middleware.utils.HttpResponseValues; import org.opentripplanner.middleware.utils.JsonUtils; +import java.time.Instant; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -43,12 +54,15 @@ import static org.opentripplanner.middleware.testutils.ApiTestUtils.TEMP_AUTH0_USER_PASSWORD; import static org.opentripplanner.middleware.testutils.ApiTestUtils.getMockHeaders; import static org.opentripplanner.middleware.testutils.ApiTestUtils.makeRequest; +import static org.opentripplanner.middleware.triptracker.TripInstruction.NO_INSTRUCTION; +import static org.opentripplanner.middleware.utils.GeometryUtils.createPoint; public class TrackedTripControllerTest extends OtpMiddlewareTestEnvironment { private static OtpUser soloOtpUser; private static MonitoredTrip monitoredTrip; private static TrackedJourney trackedJourney; + private static Itinerary itinerary; private static final String ROUTE_PATH = "api/secure/monitoredtrip/"; private static final String START_TRACKING_TRIP_PATH = ROUTE_PATH + "starttracking"; @@ -63,6 +77,12 @@ public static void setUp() throws Exception { assumeTrue(IS_END_TO_END); setAuthDisabled(false); OtpTestUtils.mockOtpServer(); + + itinerary = JsonUtils.getPOJOFromJSON( + CommonTestUtils.getTestResourceAsString("controllers/api/adair-avenue-to-monroe-drive.json"), + Itinerary.class + ); + soloOtpUser = PersistenceTestUtils.createUser(ApiTestUtils.generateEmailAddress("test-solootpuser")); try { // Should use Auth0User.createNewAuth0User but this generates a random password preventing the mock headers @@ -70,11 +90,15 @@ public static void setUp() throws Exception { var auth0User = Auth0Users.createAuth0UserForEmail(soloOtpUser.email, TEMP_AUTH0_USER_PASSWORD); soloOtpUser.auth0UserId = auth0User.getId(); Persistence.otpUsers.replace(soloOtpUser.id, soloOtpUser); + headers = getMockHeaders(soloOtpUser); } catch (Auth0Exception e) { throw new RuntimeException(e); } monitoredTrip = new MonitoredTrip(); monitoredTrip.userId = soloOtpUser.id; + monitoredTrip.itinerary = itinerary; + monitoredTrip.journeyState = new JourneyState(); + monitoredTrip.journeyState.matchingItinerary = itinerary; Persistence.monitoredTrips.create(monitoredTrip); headers = getMockHeaders(soloOtpUser); } @@ -177,13 +201,15 @@ void canNotRestartAnOngoingJourney() throws Exception { assertEquals(HttpStatus.FORBIDDEN_403, response.status); } - @Test - void canStartThenUpdateOngoingJourney() throws Exception { + @ParameterizedTest + @MethodSource("createStartThenUpdateCases") + void canStartThenUpdateOngoingJourney(Coordinates coords, String instruction, String message) throws Exception { assumeTrue(IS_END_TO_END); + // First request for starting a journey. var response = makeRequest( TRACK_TRIP_PATH, - JsonUtils.toJson(createTrackPayload()), + JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), coords.lat, coords.lon)))), headers, HttpMethod.POST ); @@ -191,19 +217,131 @@ void canStartThenUpdateOngoingJourney() throws Exception { assertEquals(HttpStatus.OK_200, response.status); var trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); assertNotEquals(0, trackResponse.frequencySeconds); + assertEquals(instruction, trackResponse.instruction, message); assertNotNull(trackResponse.journeyId); trackedJourney = Persistence.trackedJourneys.getById(trackResponse.journeyId); + // Second request to update a journey response = makeRequest( TRACK_TRIP_PATH, - JsonUtils.toJson(createTrackPayload()), + JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), coords.lat, coords.lon)))), headers, HttpMethod.POST ); assertEquals(HttpStatus.OK_200, response.status); trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); + assertEquals(instruction, trackResponse.instruction, message); + assertNotNull(trackResponse.journeyId); + assertEquals(trackedJourney.id, trackResponse.journeyId); + } + + private static Stream createStartThenUpdateCases() { + final int NORTH_WEST_BEARING = 315; + final int NORTH_EAST_BEARING = 45; + final int WEST_BEARING = 270; + + Leg firstLeg = itinerary.legs.get(0); + Coordinates firstStepCoords = new Coordinates(firstLeg.steps.get(0)); + Coordinates thirdStepCoords = new Coordinates(firstLeg.steps.get(2)); + + return Stream.of( + Arguments.of( + createPoint(firstStepCoords, 1, NORTH_EAST_BEARING), + "IMMEDIATE: Head WEST on Adair Avenue Northeast", + "Coords near first step should produce relevant instruction" + ), + Arguments.of( + createPoint(firstStepCoords, 4, NORTH_EAST_BEARING), + "UPCOMING: Head WEST on Adair Avenue Northeast", + "Coords near first step should produce relevant instruction" + ), + Arguments.of( + createPoint(firstStepCoords, 30, NORTH_EAST_BEARING), + "Head to Adair Avenue Northeast", + "Deviated coords near first step should produce instruction to head to first step #1" + ), + Arguments.of( + createPoint(firstStepCoords, 15, NORTH_WEST_BEARING), + "Head to Adair Avenue Northeast", + "Deviated coords near first step should produce instruction to head to first step #2" + ), + Arguments.of( + createPoint(firstStepCoords, 20, WEST_BEARING), + NO_INSTRUCTION, + "Coords along a step should produce no instruction" + ), + Arguments.of( + thirdStepCoords, + "IMMEDIATE: LEFT on Ponce de Leon Place Northeast", + "Coords near a not-first step should produce relevant instruction" + ), + Arguments.of( + createPoint(thirdStepCoords, 30, NORTH_WEST_BEARING), + "Head to Ponce de Leon Place Northeast", + "Deviated coords near a not-first step should produce instruction to head to step" + ) + ); + } + + //@Test + void canStartThenUpdateOngoingJourney1() throws Exception { + assumeTrue(IS_END_TO_END); + + final String FIRST_INSTRUCTION_IF_DEVIATED = "Head to Adair Avenue Northeast"; + final int NORTH_WEST_BEARING = 315; + + Leg firstLeg = itinerary.legs.get(0); + Step thirdStep = firstLeg.steps.get(2); + Coordinates thirdStepCoords = new Coordinates(thirdStep); + Coordinates deviatedCoordsNearThirdStep = createPoint(thirdStepCoords, 30, NORTH_WEST_BEARING); + + // First request deviated should keep same instruction. + var response = makeRequest( + TRACK_TRIP_PATH, + // JsonUtils.toJson(createTrackPayload()), + JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), deviatedCoordsNearThirdStep.lat, deviatedCoordsNearThirdStep.lon)))), + headers, + HttpMethod.POST + ); + + assertEquals(HttpStatus.OK_200, response.status); + var trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); assertNotEquals(0, trackResponse.frequencySeconds); + // When starting tracking, a deviated location should result in an instruction to head to the first step. + assertEquals(FIRST_INSTRUCTION_IF_DEVIATED, trackResponse.instruction); + assertNotNull(trackResponse.journeyId); + trackedJourney = Persistence.trackedJourneys.getById(trackResponse.journeyId); + + // Test 1 + response = makeRequest( + TRACK_TRIP_PATH, + JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), deviatedCoordsNearThirdStep.lat, deviatedCoordsNearThirdStep.lon)))), + getMockHeaders(soloOtpUser), + HttpMethod.POST + ); + + assertEquals(HttpStatus.OK_200, response.status); + trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); + // When updating tracking, a deviated location near a step that is not the first step + // should result in an instruction to head to that closest step (and not the first step). + assertNotEquals("Head to Ponce de Leon Place Northeast", trackResponse.instruction); + assertNotNull(trackResponse.journeyId); + assertEquals(trackedJourney.id, trackResponse.journeyId); + + // Test 2 + response = makeRequest( + TRACK_TRIP_PATH, + JsonUtils.toJson(createTrackPayload()), + headers, + HttpMethod.POST + ); + + assertEquals(HttpStatus.OK_200, response.status); + trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); + // When updating tracking, a deviated location near a step that is not the first step + // should result in an instruction to head to that closest step (and not the first step). + assertNotEquals(FIRST_INSTRUCTION_IF_DEVIATED, trackResponse.instruction); assertNotNull(trackResponse.journeyId); assertEquals(trackedJourney.id, trackResponse.journeyId); } @@ -335,9 +473,13 @@ private UpdatedTrackingPayload createUpdateTrackingPayload(String journeyId) { } private TrackPayload createTrackPayload() { + return createTrackPayload(createTrackingLocations()); + } + + private TrackPayload createTrackPayload(List locations) { var payload = new TrackPayload(); payload.tripId = monitoredTrip.id; - payload.locations = createTrackingLocations(); + payload.locations = locations; return payload; } From 841eacba2acac06bbb04592415872c242e281df6 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 15 May 2024 15:53:08 -0400 Subject: [PATCH 3/4] refactor(TrackedTripControllerTest): Remove, refactor unused and duplicate code. --- .../api/TrackedTripControllerTest.java | 84 +++---------------- 1 file changed, 12 insertions(+), 72 deletions(-) diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java index 0aa3c5b81..8bd247ffa 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java @@ -16,7 +16,6 @@ import org.opentripplanner.middleware.models.TrackedJourney; import org.opentripplanner.middleware.otp.response.Itinerary; import org.opentripplanner.middleware.otp.response.Leg; -import org.opentripplanner.middleware.otp.response.Step; import org.opentripplanner.middleware.persistence.Persistence; import org.opentripplanner.middleware.testutils.ApiTestUtils; import org.opentripplanner.middleware.testutils.CommonTestUtils; @@ -100,7 +99,6 @@ public static void setUp() throws Exception { monitoredTrip.journeyState = new JourneyState(); monitoredTrip.journeyState.matchingItinerary = itinerary; Persistence.monitoredTrips.create(monitoredTrip); - headers = getMockHeaders(soloOtpUser); } @AfterAll @@ -178,9 +176,11 @@ void canCompleteJourneyLifeCycle() throws Exception { void canNotRestartAnOngoingJourney() throws Exception { assumeTrue(IS_END_TO_END); + String jsonPayload = JsonUtils.toJson(createStartTrackingPayload()); + var response = makeRequest( START_TRACKING_TRIP_PATH, - JsonUtils.toJson(createStartTrackingPayload()), + jsonPayload, headers, HttpMethod.POST ); @@ -191,7 +191,7 @@ void canNotRestartAnOngoingJourney() throws Exception { response = makeRequest( START_TRACKING_TRIP_PATH, - JsonUtils.toJson(createStartTrackingPayload()), + jsonPayload, headers, HttpMethod.POST ); @@ -206,10 +206,12 @@ void canNotRestartAnOngoingJourney() throws Exception { void canStartThenUpdateOngoingJourney(Coordinates coords, String instruction, String message) throws Exception { assumeTrue(IS_END_TO_END); + String jsonPayload = JsonUtils.toJson(createTrackPayload(coords)); + // First request for starting a journey. var response = makeRequest( TRACK_TRIP_PATH, - JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), coords.lat, coords.lon)))), + jsonPayload, headers, HttpMethod.POST ); @@ -224,7 +226,7 @@ void canStartThenUpdateOngoingJourney(Coordinates coords, String instruction, St // Second request to update a journey response = makeRequest( TRACK_TRIP_PATH, - JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), coords.lat, coords.lon)))), + jsonPayload, headers, HttpMethod.POST ); @@ -284,68 +286,6 @@ private static Stream createStartThenUpdateCases() { ); } - //@Test - void canStartThenUpdateOngoingJourney1() throws Exception { - assumeTrue(IS_END_TO_END); - - final String FIRST_INSTRUCTION_IF_DEVIATED = "Head to Adair Avenue Northeast"; - final int NORTH_WEST_BEARING = 315; - - Leg firstLeg = itinerary.legs.get(0); - Step thirdStep = firstLeg.steps.get(2); - Coordinates thirdStepCoords = new Coordinates(thirdStep); - Coordinates deviatedCoordsNearThirdStep = createPoint(thirdStepCoords, 30, NORTH_WEST_BEARING); - - // First request deviated should keep same instruction. - var response = makeRequest( - TRACK_TRIP_PATH, - // JsonUtils.toJson(createTrackPayload()), - JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), deviatedCoordsNearThirdStep.lat, deviatedCoordsNearThirdStep.lon)))), - headers, - HttpMethod.POST - ); - - assertEquals(HttpStatus.OK_200, response.status); - var trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); - assertNotEquals(0, trackResponse.frequencySeconds); - // When starting tracking, a deviated location should result in an instruction to head to the first step. - assertEquals(FIRST_INSTRUCTION_IF_DEVIATED, trackResponse.instruction); - assertNotNull(trackResponse.journeyId); - trackedJourney = Persistence.trackedJourneys.getById(trackResponse.journeyId); - - // Test 1 - response = makeRequest( - TRACK_TRIP_PATH, - JsonUtils.toJson(createTrackPayload(List.of(new TrackingLocation(Instant.now(), deviatedCoordsNearThirdStep.lat, deviatedCoordsNearThirdStep.lon)))), - getMockHeaders(soloOtpUser), - HttpMethod.POST - ); - - assertEquals(HttpStatus.OK_200, response.status); - trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); - // When updating tracking, a deviated location near a step that is not the first step - // should result in an instruction to head to that closest step (and not the first step). - assertNotEquals("Head to Ponce de Leon Place Northeast", trackResponse.instruction); - assertNotNull(trackResponse.journeyId); - assertEquals(trackedJourney.id, trackResponse.journeyId); - - // Test 2 - response = makeRequest( - TRACK_TRIP_PATH, - JsonUtils.toJson(createTrackPayload()), - headers, - HttpMethod.POST - ); - - assertEquals(HttpStatus.OK_200, response.status); - trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); - // When updating tracking, a deviated location near a step that is not the first step - // should result in an instruction to head to that closest step (and not the first step). - assertNotEquals(FIRST_INSTRUCTION_IF_DEVIATED, trackResponse.instruction); - assertNotNull(trackResponse.journeyId); - assertEquals(trackedJourney.id, trackResponse.journeyId); - } - @Test void canForciblyEndJourney() throws Exception { assumeTrue(IS_END_TO_END); @@ -472,10 +412,6 @@ private UpdatedTrackingPayload createUpdateTrackingPayload(String journeyId) { return payload; } - private TrackPayload createTrackPayload() { - return createTrackPayload(createTrackingLocations()); - } - private TrackPayload createTrackPayload(List locations) { var payload = new TrackPayload(); payload.tripId = monitoredTrip.id; @@ -483,6 +419,10 @@ private TrackPayload createTrackPayload(List locations) { return payload; } + private TrackPayload createTrackPayload(Coordinates coords) { + return createTrackPayload(List.of(new TrackingLocation(Instant.now(), coords.lat, coords.lon))); + } + private EndTrackingPayload createEndTrackingPayload(String journeyId) { var payload = new EndTrackingPayload(); payload.journeyId = journeyId; From f6f7c9c5c1ebe16b1b5e82b1c68db8d62504bfb0 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Wed, 15 May 2024 16:05:24 -0400 Subject: [PATCH 4/4] test(TrackedTripControllerTest): Add back test for nonzero frequency field. --- .../middleware/controllers/api/TrackedTripControllerTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java index 8bd247ffa..acd1be155 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/TrackedTripControllerTest.java @@ -233,6 +233,7 @@ void canStartThenUpdateOngoingJourney(Coordinates coords, String instruction, St assertEquals(HttpStatus.OK_200, response.status); trackResponse = JsonUtils.getPOJOFromJSON(response.responseBody, TrackingResponse.class); + assertNotEquals(0, trackResponse.frequencySeconds); assertEquals(instruction, trackResponse.instruction, message); assertNotNull(trackResponse.journeyId); assertEquals(trackedJourney.id, trackResponse.journeyId);