Skip to content

Commit

Permalink
Surface SC-Request-Id on SmartcarException (#81)
Browse files Browse the repository at this point in the history
* Surface SC-Request-Id on SmartcarException.

* Generate README and Javadoc.

* Update integration test selenium setup to match new Connect flow DOM structure.
  • Loading branch information
sankethkatta authored Mar 24, 2020
1 parent 5e50ee5 commit 1566a82
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 23 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ 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
```xml
<dependency>
<groupId>com.smartcar.sdk</groupId>
<artifactId>java-sdk</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
```

### 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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/com/smartcar/sdk/BatchResponseMissingException.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ <h3>Method Summary</h3>
<!-- -->
</a>
<h3>Methods inherited from class&nbsp;com.smartcar.sdk.<a href="../../../com/smartcar/sdk/SmartcarException.html" title="class in com.smartcar.sdk">SmartcarException</a></h3>
<code><a href="../../../com/smartcar/sdk/SmartcarException.html#Factory-okhttp3.Response-">Factory</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getCode--">getCode</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getError--">getError</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getMessage--">getMessage</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getStatusCode--">getStatusCode</a></code></li>
<code><a href="../../../com/smartcar/sdk/SmartcarException.html#Factory-okhttp3.Response-">Factory</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getCode--">getCode</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getError--">getError</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getMessage--">getMessage</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getRequestId--">getRequestId</a>, <a href="../../../com/smartcar/sdk/SmartcarException.html#getStatusCode--">getStatusCode</a></code></li>
</ul>
<ul class="blockList">
<li class="blockList"><a name="methods.inherited.from.class.java.lang.Throwable">
Expand Down
32 changes: 27 additions & 5 deletions docs/com/smartcar/sdk/SmartcarException.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
catch(err) {
}
//-->
var methods = {"i0":9,"i1":10,"i2":10,"i3":10,"i4":10};
var methods = {"i0":9,"i1":10,"i2":10,"i3":10,"i4":10,"i5":10};
var tabs = {65535:["t0","All Methods"],1:["t1","Static Methods"],2:["t2","Instance Methods"],8:["t4","Concrete Methods"]};
var altColor = "altColor";
var rowColor = "rowColor";
Expand Down Expand Up @@ -151,10 +151,11 @@ <h3>Constructor Summary</h3>
<th class="colOne" scope="col">Constructor and Description</th>
</tr>
<tr class="altColor">
<td class="colOne"><code><span class="memberNameLink"><a href="../../../com/smartcar/sdk/SmartcarException.html#SmartcarException-int-java.lang.String-java.lang.String-java.lang.String-">SmartcarException</a></span>(int&nbsp;statusCode,
<td class="colOne"><code><span class="memberNameLink"><a href="../../../com/smartcar/sdk/SmartcarException.html#SmartcarException-int-java.lang.String-java.lang.String-java.lang.String-java.lang.String-">SmartcarException</a></span>(int&nbsp;statusCode,
java.lang.String&nbsp;error,
java.lang.String&nbsp;message,
java.lang.String&nbsp;code)</code>&nbsp;</td>
java.lang.String&nbsp;code,
java.lang.String&nbsp;requestId)</code>&nbsp;</td>
</tr>
<tr class="rowColor">
<td class="colOne"><code><span class="memberNameLink"><a href="../../../com/smartcar/sdk/SmartcarException.html#SmartcarException-java.lang.String-">SmartcarException</a></span>(java.lang.String&nbsp;message)</code>
Expand Down Expand Up @@ -199,6 +200,12 @@ <h3>Method Summary</h3>
</td>
</tr>
<tr id="i4" class="altColor">
<td class="colFirst"><code>java.lang.String</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../com/smartcar/sdk/SmartcarException.html#getRequestId--">getRequestId</a></span>()</code>
<div class="block">Return the Smartcar request id from the response headers</div>
</td>
</tr>
<tr id="i5" class="rowColor">
<td class="colFirst"><code>int</code></td>
<td class="colLast"><code><span class="memberNameLink"><a href="../../../com/smartcar/sdk/SmartcarException.html#getStatusCode--">getStatusCode</a></span>()</code>
<div class="block">Returns the HTTP status code.</div>
Expand Down Expand Up @@ -233,7 +240,7 @@ <h3>Methods inherited from class&nbsp;java.lang.Object</h3>
<!-- -->
</a>
<h3>Constructor Detail</h3>
<a name="SmartcarException-int-java.lang.String-java.lang.String-java.lang.String-">
<a name="SmartcarException-int-java.lang.String-java.lang.String-java.lang.String-java.lang.String-">
<!-- -->
</a>
<ul class="blockList">
Expand All @@ -242,7 +249,8 @@ <h4>SmartcarException</h4>
<pre>public&nbsp;SmartcarException(int&nbsp;statusCode,
java.lang.String&nbsp;error,
java.lang.String&nbsp;message,
java.lang.String&nbsp;code)</pre>
java.lang.String&nbsp;code,
java.lang.String&nbsp;requestId)</pre>
</li>
</ul>
<a name="SmartcarException-java.lang.String-">
Expand Down Expand Up @@ -325,6 +333,20 @@ <h4>getStatusCode</h4>
</dl>
</li>
</ul>
<a name="getRequestId--">
<!-- -->
</a>
<ul class="blockList">
<li class="blockList">
<h4>getRequestId</h4>
<pre>public&nbsp;java.lang.String&nbsp;getRequestId()</pre>
<div class="block">Return the Smartcar request id from the response headers</div>
<dl>
<dt><span class="returnLabel">Returns:</span></dt>
<dd>the request id</dd>
</dl>
</li>
</ul>
<a name="Factory-okhttp3.Response-">
<!-- -->
</a>
Expand Down
6 changes: 5 additions & 1 deletion docs/index-all.html
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ <h2 class="title">G</h2>
<dd>
<div class="block">Return the Smartcar request id from the response headers</div>
</dd>
<dt><span class="memberNameLink"><a href="com/smartcar/sdk/SmartcarException.html#getRequestId--">getRequestId()</a></span> - Method in exception com.smartcar.sdk.<a href="com/smartcar/sdk/SmartcarException.html" title="class in com.smartcar.sdk">SmartcarException</a></dt>
<dd>
<div class="block">Return the Smartcar request id from the response headers</div>
</dd>
<dt><span class="memberNameLink"><a href="com/smartcar/sdk/data/VehicleCharge.html#getState--">getState()</a></span> - Method in class com.smartcar.sdk.data.<a href="com/smartcar/sdk/data/VehicleCharge.html" title="class in com.smartcar.sdk.data">VehicleCharge</a></dt>
<dd>
<div class="block">Returns the charge state</div>
Expand Down Expand Up @@ -849,7 +853,7 @@ <h2 class="title">S</h2>
<dd>
<div class="block">Thrown when the Smartcar API library encounters a problem.</div>
</dd>
<dt><span class="memberNameLink"><a href="com/smartcar/sdk/SmartcarException.html#SmartcarException-int-java.lang.String-java.lang.String-java.lang.String-">SmartcarException(int, String, String, String)</a></span> - Constructor for exception com.smartcar.sdk.<a href="com/smartcar/sdk/SmartcarException.html" title="class in com.smartcar.sdk">SmartcarException</a></dt>
<dt><span class="memberNameLink"><a href="com/smartcar/sdk/SmartcarException.html#SmartcarException-int-java.lang.String-java.lang.String-java.lang.String-java.lang.String-">SmartcarException(int, String, String, String, String)</a></span> - Constructor for exception com.smartcar.sdk.<a href="com/smartcar/sdk/SmartcarException.html" title="class in com.smartcar.sdk">SmartcarException</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="com/smartcar/sdk/SmartcarException.html#SmartcarException-java.lang.String-">SmartcarException(String)</a></span> - Constructor for exception com.smartcar.sdk.<a href="com/smartcar/sdk/SmartcarException.html" title="class in com.smartcar.sdk">SmartcarException</a></dt>
<dd>
Expand Down
6 changes: 5 additions & 1 deletion docs/serialized-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ <h4>error</h4>
<h4>message</h4>
<pre>java.lang.String message</pre>
</li>
<li class="blockListLast">
<li class="blockList">
<h4>code</h4>
<pre>java.lang.String code</pre>
</li>
<li class="blockListLast">
<h4>requestId</h4>
<pre>java.lang.String requestId</pre>
</li>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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

Expand Down
17 changes: 12 additions & 5 deletions src/integration/java/com/smartcar/sdk/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebElement> elements = this.driver.findElements(By.className("input-button-custom"));
for (int i=0; i<elements.size();i++){
elements.get(i).click();
List<WebElement> 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();

Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/smartcar/sdk/SmartcarException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/smartcar/sdk/data/BatchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private <T extends ApiData> SmartcarResponse<T> get(String path, Class<T> 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");
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/smartcar/sdk/SmartcarExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/smartcar/sdk/VehicleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BatchResponse> 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"});
Expand All @@ -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);
}
}
}

0 comments on commit 1566a82

Please sign in to comment.