Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent conversion of non-plan OTP responses to POJO #74

Merged
merged 2 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.net.URI;
import java.net.http.HttpResponse;

import static org.opentripplanner.middleware.otp.OtpDispatcher.OTP_PLAN_ENDPOINT;

/**
* An OTP dispatcher response represents the status code and body return from a call to an OTP end point e.g. plan
*/
Expand Down Expand Up @@ -66,10 +68,16 @@ public void setResponse(Response response) {

@Override
public String toString() {
// Only include the plan response if requestUri.path ends with OTP_PLAN_ENDPOINT.
// Without this check, we are sending valid responses from non-plan OTP endpoints to Bugsnag as errors.
String planResponse = requestUri.getPath().endsWith(OTP_PLAN_ENDPOINT)
? ", response=" + getResponse()
: "";

return "OtpDispatcherResponse{" +
"statusCode=" + statusCode +
", responseBody='" + responseBody + '\'' +
", response=" + getResponse() +
planResponse +
'}';
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/opentripplanner/middleware/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

public class TestUtils {
private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
public static final String TEST_RESOURCE_PATH = "src/test/resources/org/opentripplanner/middleware/";

/**
* Prevents the mock OTP server being initialized more than once
Expand All @@ -51,7 +52,7 @@ public static boolean getBooleanEnvVar(String var) {

public static <T> T getResourceFileContentsAsJSON (String resourcePathName, Class<T> clazz) throws IOException {
return FileUtils.getFileContentsAsJSON(
String.format("src/test/resources/org/opentripplanner/middleware/%s", resourcePathName),
TEST_RESOURCE_PATH + resourcePathName,
clazz
);
}
Expand Down Expand Up @@ -148,10 +149,9 @@ static void mockOtpServer() {
* Mock an OTP server plan response by provide a static response from file.
*/
private static String mockOtpPlanResponse(Request request, Response response) throws IOException {
final String filePath = "src/test/resources/org/opentripplanner/middleware/";
OtpDispatcherResponse otpDispatcherResponse = new OtpDispatcherResponse();
otpDispatcherResponse.statusCode = HttpStatus.OK_200;
otpDispatcherResponse.responseBody = FileUtils.getFileContents(filePath + "planResponse.json");
otpDispatcherResponse.responseBody = FileUtils.getFileContents(TEST_RESOURCE_PATH + "planResponse.json");

response.type(APPLICATION_JSON);
response.status(otpDispatcherResponse.statusCode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.opentripplanner.middleware.otp;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.opentripplanner.middleware.utils.FileUtils;

import java.io.IOException;

import static org.opentripplanner.middleware.TestUtils.TEST_RESOURCE_PATH;
import static org.opentripplanner.middleware.otp.OtpDispatcher.OTP_PLAN_ENDPOINT;

public class OtpDispatcherResponseTest {
@Test
public void toStringShouldExludeResponseFieldIfNotCallingPlan() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo Exlude: Exclude.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks.

String mockParkRideResponse = FileUtils.getFileContents(
TEST_RESOURCE_PATH + "otp/parkRideResponse.json"
);


// The call below creates a request URI that is "http://test.com",
// which is not the OTP /plan endpoint, but we still assert that too.
OtpDispatcherResponse dispatcherResponse = new OtpDispatcherResponse(mockParkRideResponse);
Assertions.assertFalse(dispatcherResponse.requestUri.getPath().endsWith(OTP_PLAN_ENDPOINT));

// Expected string does not include the response={} field.
String expected = "OtpDispatcherResponse{statusCode=200, responseBody='" + mockParkRideResponse + "'}";

Assertions.assertEquals(expected, dispatcherResponse.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static com.mongodb.client.model.Filters.eq;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.opentripplanner.middleware.TestUtils.TEST_RESOURCE_PATH;
import static org.opentripplanner.middleware.TestUtils.getBooleanEnvVar;
import static org.opentripplanner.middleware.persistence.PersistenceUtil.createMonitoredTrip;
import static org.opentripplanner.middleware.persistence.PersistenceUtil.createUser;
Expand Down Expand Up @@ -64,7 +65,7 @@ public class CheckMonitoredTripTest extends OtpMiddlewareTest {
public static void setup() throws IOException {
user = createUser("user@example.com");
mockResponse = FileUtils.getFileContents(
"src/test/resources/org/opentripplanner/middleware/persistence/planResponse.json"
TEST_RESOURCE_PATH + "persistence/planResponse.json"
);
otpDispatcherResponse = new OtpDispatcherResponse(mockResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"P+R","x":-81.36548775,"y":28.4528734},{"name":"P+R","x":-81.274944,"y":28.911900600000003},{"name":"P+R","x":-81.7390652,"y":28.540740550000002},{"name":"P+R","x":-81.13372815,"y":28.561692100000002}]