From 1566a82fc5d7c06bd050e459ba84add1205d9ad5 Mon Sep 17 00:00:00 2001 From: Sanketh Katta Date: Tue, 24 Mar 2020 12:59:32 -0700 Subject: [PATCH] Surface SC-Request-Id on SmartcarException (#81) * Surface SC-Request-Id on SmartcarException. * Generate README and Javadoc. * Update integration test selenium setup to match new Connect flow DOM structure. --- README.md | 12 +++---- .../sdk/BatchResponseMissingException.html | 2 +- docs/com/smartcar/sdk/SmartcarException.html | 32 ++++++++++++++++--- docs/index-all.html | 6 +++- docs/serialized-form.html | 6 +++- gradle.properties | 2 +- .../com/smartcar/sdk/IntegrationTest.java | 17 +++++++--- .../com/smartcar/sdk/SmartcarException.java | 17 ++++++++-- .../com/smartcar/sdk/data/BatchResponse.java | 2 +- .../smartcar/sdk/SmartcarExceptionTest.java | 5 +++ .../java/com/smartcar/sdk/VehicleTest.java | 3 ++ 11 files changed, 81 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index a83594e2..01aa1993 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ download links are also provided below. ### Gradle ```groovy -compile "com.smartcar.sdk:java-sdk:2.4.1" +compile "com.smartcar.sdk:java-sdk:2.4.2" ``` ### Maven @@ -19,14 +19,14 @@ compile "com.smartcar.sdk:java-sdk:2.4.1" com.smartcar.sdk java-sdk - 2.4.1 + 2.4.2 ``` ### Jar Direct Download -* [java-sdk-2.4.1.jar](https://bintray.com/smartcar/library/download_file?file_path=com%2Fsmartcar%2Fsdk%2Fjava-sdk%2F2.4.1%2Fjava-sdk-2.4.1.jar) -* [java-sdk-2.4.1-sources.jar](https://bintray.com/smartcar/library/download_file?file_path=com%2Fsmartcar%2Fsdk%2Fjava-sdk%2F2.4.1%2Fjava-sdk-2.4.1-sources.jar) -* [java-sdk-2.4.1-docs.jar](https://bintray.com/smartcar/library/download_file?file_path=com%2Fsmartcar%2Fsdk%2Fjava-sdk%2F2.4.1%2Fjava-sdk-2.4.1-docs.jar) +* [java-sdk-2.4.2.jar](https://bintray.com/smartcar/library/download_file?file_path=com%2Fsmartcar%2Fsdk%2Fjava-sdk%2F2.4.2%2Fjava-sdk-2.4.2.jar) +* [java-sdk-2.4.2-sources.jar](https://bintray.com/smartcar/library/download_file?file_path=com%2Fsmartcar%2Fsdk%2Fjava-sdk%2F2.4.2%2Fjava-sdk-2.4.2-sources.jar) +* [java-sdk-2.4.2-docs.jar](https://bintray.com/smartcar/library/download_file?file_path=com%2Fsmartcar%2Fsdk%2Fjava-sdk%2F2.4.2%2Fjava-sdk-2.4.2-docs.jar) ## Usage @@ -129,5 +129,5 @@ start making requests to vehicles. [ci-url]: https://travis-ci.com/smartcar/java-sdk [coverage-image]: https://codecov.io/gh/smartcar/java-sdk/branch/master/graph/badge.svg?token=nZAITx7w3X [coverage-url]: https://codecov.io/gh/smartcar/java-sdk -[javadoc-image]: https://img.shields.io/badge/javadoc-2.4.1-brightgreen.svg +[javadoc-image]: https://img.shields.io/badge/javadoc-2.4.2-brightgreen.svg [javadoc-url]: https://smartcar.github.io/java-sdk diff --git a/docs/com/smartcar/sdk/BatchResponseMissingException.html b/docs/com/smartcar/sdk/BatchResponseMissingException.html index fc275c01..dc6247db 100644 --- a/docs/com/smartcar/sdk/BatchResponseMissingException.html +++ b/docs/com/smartcar/sdk/BatchResponseMissingException.html @@ -164,7 +164,7 @@

Method Summary

Methods inherited from class com.smartcar.sdk.SmartcarException

-Factory, getCode, getError, getMessage, getStatusCode +Factory, getCode, getError, getMessage, getRequestId, getStatusCode + + + + diff --git a/docs/index-all.html b/docs/index-all.html index 3ab8fd1a..309a19a1 100644 --- a/docs/index-all.html +++ b/docs/index-all.html @@ -503,6 +503,10 @@

G

Return the Smartcar request id from the response headers
+
getRequestId() - Method in exception com.smartcar.sdk.SmartcarException
+
+
Return the Smartcar request id from the response headers
+
getState() - Method in class com.smartcar.sdk.data.VehicleCharge
Returns the charge state
@@ -849,7 +853,7 @@

S

Thrown when the Smartcar API library encounters a problem.
-
SmartcarException(int, String, String, String) - Constructor for exception com.smartcar.sdk.SmartcarException
+
SmartcarException(int, String, String, String, String) - Constructor for exception com.smartcar.sdk.SmartcarException
 
SmartcarException(String) - Constructor for exception com.smartcar.sdk.SmartcarException
diff --git a/docs/serialized-form.html b/docs/serialized-form.html index 5b0f4389..0dd8bfbc 100644 --- a/docs/serialized-form.html +++ b/docs/serialized-form.html @@ -101,10 +101,14 @@

error

message

java.lang.String message
-
  • +
  • code

    java.lang.String code
  • +
  • +

    requestId

    +
    java.lang.String requestId
    +
  • diff --git a/gradle.properties b/gradle.properties index 4f400aa8..65ac2e6e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ libGroup=com.smartcar.sdk libName=java-sdk -libVersion=2.4.1 +libVersion=2.4.2 libDescription=The Smartcar Java SDK libUrl=https://github.com/smartcar/java-sdk diff --git a/src/integration/java/com/smartcar/sdk/IntegrationTest.java b/src/integration/java/com/smartcar/sdk/IntegrationTest.java index f3f974b0..162eb088 100644 --- a/src/integration/java/com/smartcar/sdk/IntegrationTest.java +++ b/src/integration/java/com/smartcar/sdk/IntegrationTest.java @@ -104,13 +104,20 @@ private String getAuthCode(String connectAuthUrl, String oemUsername, String oem this.driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); this.wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".page-content"))); - // Select Chevy Volt by unclicking all the boxes and then clicking the Volt + // Select Chevy Volt by unclicking all the boxes except the Volt // This is needed because the Volt is the only one that has all the endpoints needed. - List elements = this.driver.findElements(By.className("input-button-custom")); - for (int i=0; i elements = this.driver.findElements(By.className("input-button-label")); + for (WebElement el : elements) { + WebElement vehicleText = el.findElement(By.className("input-button-label-text")); + WebElement checkbox = el.findElement(By.className("input-button-custom")); + if (vehicleText == null) { + throw new Exception("input-button-label-text not found"); + } else if (checkbox == null) { + throw new Exception("input-button-custom not found"); + } else if (!vehicleText.getText().contains("Volt")) { + checkbox.click(); + } } - this.driver.findElement(By.cssSelector("span[data-model='Volt']")).click(); this.driver.findElement(By.id("approval-button")).click(); diff --git a/src/main/java/com/smartcar/sdk/SmartcarException.java b/src/main/java/com/smartcar/sdk/SmartcarException.java index fd9b6b62..996d1312 100644 --- a/src/main/java/com/smartcar/sdk/SmartcarException.java +++ b/src/main/java/com/smartcar/sdk/SmartcarException.java @@ -17,13 +17,15 @@ public class SmartcarException extends java.lang.Exception { private String error; private String message; private String code; + private String requestId; - public SmartcarException(int statusCode, String error, String message, String code) { + public SmartcarException(int statusCode, String error, String message, String code, String requestId) { super(message); this.message = message; this.code = code; this.error = error; this.statusCode = statusCode; + this.requestId = requestId; } /** @@ -72,6 +74,15 @@ public int getStatusCode() { return this.statusCode; } + /** + * Return the Smartcar request id from the response headers + * + * @return the request id + */ + public String getRequestId() { + return this.requestId; + } + public static SmartcarException Factory(final Response response) throws IOException { JsonObject body = gson.fromJson(response.body().string(), JsonObject.class); @@ -94,6 +105,8 @@ public static SmartcarException Factory(final Response response) throws IOExcept error = body.get("error").getAsString(); } - return new SmartcarException(statusCode, error, message, code); + String requestId = response.header("sc-request-id", ""); + + return new SmartcarException(statusCode, error, message, code, requestId); } } diff --git a/src/main/java/com/smartcar/sdk/data/BatchResponse.java b/src/main/java/com/smartcar/sdk/data/BatchResponse.java index 1a32c5f4..d36b55cd 100644 --- a/src/main/java/com/smartcar/sdk/data/BatchResponse.java +++ b/src/main/java/com/smartcar/sdk/data/BatchResponse.java @@ -59,7 +59,7 @@ private SmartcarResponse get(String path, Class dataTy code = body.get("code").getAsString(); } - throw new SmartcarException(statusCode, error, message, code); + throw new SmartcarException(statusCode, error, message, code, this.requestId); } JsonElement header = res.get("headers"); diff --git a/src/test/java/com/smartcar/sdk/SmartcarExceptionTest.java b/src/test/java/com/smartcar/sdk/SmartcarExceptionTest.java index 6070badd..353ec95d 100644 --- a/src/test/java/com/smartcar/sdk/SmartcarExceptionTest.java +++ b/src/test/java/com/smartcar/sdk/SmartcarExceptionTest.java @@ -11,6 +11,8 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.testng.PowerMockTestCase; + +import static org.mockito.Matchers.eq; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.when; @@ -51,6 +53,7 @@ public void testNewExceptionWithRequestAndResponse() throws Exception { String expectedError = "expected_error"; String expectedMessage = "expected message"; String expectedCode = "VS_000"; + String expectedRequestId = "011660dc-8322-4064-a972-53826c8dff9c"; int expectedStatusCode = 200; String response = Json.createObjectBuilder() @@ -63,12 +66,14 @@ public void testNewExceptionWithRequestAndResponse() throws Exception { Response mockResponse = mock(Response.class); ResponseBody mockResponseBody = mock(ResponseBody.class); + when(mockResponse.header(eq("sc-request-id"), eq(""))).thenReturn(expectedRequestId); when(mockResponse.body()).thenReturn(mockResponseBody); when(mockResponseBody.string()).thenReturn(response); when(mockResponse.code()).thenReturn(expectedStatusCode); SmartcarException ex = SmartcarException.Factory(mockResponse); + Assert.assertEquals(ex.getRequestId(), expectedRequestId); Assert.assertEquals(ex.getCode(), expectedCode); Assert.assertEquals(ex.getMessage(), expectedMessage); Assert.assertEquals(ex.getError(), expectedError); diff --git a/src/test/java/com/smartcar/sdk/VehicleTest.java b/src/test/java/com/smartcar/sdk/VehicleTest.java index da81a5a0..4b0797c7 100644 --- a/src/test/java/com/smartcar/sdk/VehicleTest.java +++ b/src/test/java/com/smartcar/sdk/VehicleTest.java @@ -364,10 +364,12 @@ public void testBatchHTTPError() throws Exception { .build() ) .build(); + String expectedRequestId = "27767d51-3c32-47c0-8521-6f2be21bfb5b"; RequestBody body = RequestBody.create(Vehicle.JSON, json.toString()); JsonElement error = loadJsonResource("BatchResponseError"); BatchResponse expectedBatch = new BatchResponse(error.getAsJsonArray()); SmartcarResponse res = new SmartcarResponse(expectedBatch); + res.setRequestId(expectedRequestId); PowerMockito.doReturn(res).when(this.subject, "call", eq("batch"), eq("POST"), refEq(body), refEq(BatchResponse.class)); BatchResponse batch = this.subject.batch(new String[] {"/odometer"}); @@ -379,6 +381,7 @@ public void testBatchHTTPError() throws Exception { Assert.assertEquals(e.getMessage(), "Vehicle state cannot be determined."); Assert.assertEquals(e.getError(), "vehicle_state_error"); Assert.assertEquals(e.getCode(), "VS_000"); + Assert.assertEquals(e.getRequestId(), expectedRequestId); } } }