Skip to content

Commit b4c8390

Browse files
authored
Merge pull request #162 from saucelabs/refactor-deleteTunnel
Resolve #161.
2 parents f6f1e2e + 2766a65 commit b4c8390

4 files changed

Lines changed: 72 additions & 8 deletions

File tree

src/main/java/com/saucelabs/saucerest/ErrorExplainers.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ static String LogNotFound() {
6161
" * A error occurred where the job was created on Sauce Labs but no test were executed."
6262
);
6363
}
64+
65+
static String TunnelNotFound() {
66+
return String.join(System.lineSeparator(),
67+
" * Tunnel id could not be found. Possible reasons:",
68+
" * The tunnel id requested does not exist in this data center. Ensure the data center endpoint is correct.",
69+
" * A tunnel with this id never existed."
70+
);
71+
}
6472
}

src/main/java/com/saucelabs/saucerest/SauceException.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public NotAuthorized() {
3232
}
3333
}
3434

35+
public static class NotFound extends SauceException {
36+
37+
public NotFound(String message) {
38+
super(message);
39+
}
40+
41+
public NotFound() {
42+
}
43+
}
44+
3545
public static class TooManyRequests extends SauceException {
3646
}
3747

src/main/java/com/saucelabs/saucerest/SauceREST.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,8 @@ private BufferedInputStream downloadFileData(String jobId, URL restEndpoint) thr
11731173
return new BufferedInputStream(stream);
11741174
}
11751175

1176-
private HttpURLConnection setConnection(String jobId, URL restEndpoint) throws IOException {
1177-
HttpURLConnection connection = openConnection("GET", restEndpoint);
1176+
private HttpURLConnection setConnection(String jobId, URL restEndpoint, String method) throws IOException {
1177+
HttpURLConnection connection = openConnection(method, restEndpoint);
11781178

11791179
int responseCode = connection.getResponseCode();
11801180
logger.log(Level.FINEST, "{0} - {1} for: {2}", new Object[] { responseCode, restEndpoint, jobId });
@@ -1189,6 +1189,9 @@ private HttpURLConnection setConnection(String jobId, URL restEndpoint) throws I
11891189
errorDetails = ErrorExplainers.HARMissing();
11901190
} else if (path.endsWith("log") || path.endsWith("json")) {
11911191
errorDetails = ErrorExplainers.LogNotFound();
1192+
} else if (path.contains("tunnels")) {
1193+
errorDetails = ErrorExplainers.TunnelNotFound();
1194+
throw new SauceException.NotFound(String.join(System.lineSeparator(), errorDetails));
11921195
}
11931196

11941197
String error = ErrorExplainers.resourceMissing();
@@ -1228,6 +1231,14 @@ private HttpURLConnection setConnection(String jobId, URL restEndpoint) throws I
12281231
return connection;
12291232
}
12301233

1234+
private HttpURLConnection setConnection(String jobId, URL restEndpoint) throws IOException {
1235+
return setConnection(jobId, restEndpoint, "GET");
1236+
}
1237+
1238+
private HttpURLConnection setConnection(URL restEndpoint, String method) throws IOException {
1239+
return setConnection("", restEndpoint, method);
1240+
}
1241+
12311242
private boolean saveAsset(String jobId, String assetName, String location, String fileName) {
12321243
return handleErrorAtDownloadGracefully(() -> saveAssetOrThrowException(jobId, assetName, location, fileName));
12331244
}
@@ -1613,19 +1624,26 @@ protected String encodeAuthentication() {
16131624
* Invokes the Sauce REST API to delete a tunnel.
16141625
*
16151626
* @param tunnelId Identifier of the tunnel to delete
1627+
* @return BufferedInputStream with response from server
16161628
*/
1617-
public void deleteTunnel(String tunnelId) {
1618-
1629+
public BufferedInputStream deleteTunnel(String tunnelId) throws IOException {
16191630
HttpURLConnection connection = null;
16201631
try {
16211632
URL restEndpoint = buildURL(username + "/tunnels/" + tunnelId);
1622-
connection = openConnection("DELETE", restEndpoint);
1623-
connection.getOutputStream().write("".getBytes());
1633+
connection = setConnection(restEndpoint, "DELETE");
16241634
} catch (IOException e) {
1625-
logger.log(Level.WARNING, "Error stopping Sauce Job", e);
1635+
Throwable throwable = e.getCause();
1636+
1637+
if (throwable instanceof SauceException.NotAuthorized) {
1638+
throw (SauceException.NotAuthorized) throwable;
1639+
} else if (throwable instanceof SauceException.NotFound) {
1640+
throw (SauceException.NotFound) throwable;
1641+
}
1642+
logger.log(Level.WARNING, "Error deleting tunnel", e);
16261643
}
16271644

1628-
closeInputStream(connection);
1645+
InputStream stream = connection.getInputStream();
1646+
return new BufferedInputStream(stream);
16291647
}
16301648

16311649
/**

src/test/java/com/saucelabs/saucerest/SauceRESTTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,34 @@ void testGetTunnelInformation() {
355355
assertEquals(this.urlConnection.getRealURL().getPath(), "/rest/v1/" + this.sauceREST.getUsername() + "/tunnels/1234-1234-1231-123-123");
356356
}
357357

358+
@Test
359+
void testDeleteTunnel() throws Exception {
360+
urlConnection.setResponseCode(401);
361+
urlConnection.setInputStream(new ByteArrayInputStream("Not authorized".getBytes(StandardCharsets.UTF_8)));
362+
assertThrows(SauceException.NotAuthorized.class, () -> sauceREST.deleteTunnel("1234"));
363+
364+
urlConnection.setResponseCode(404);
365+
urlConnection.setInputStream(new ByteArrayInputStream("Not found".getBytes(StandardCharsets.UTF_8)));
366+
assertThrows(SauceException.NotFound.class, () -> sauceREST.deleteTunnel("1234"));
367+
368+
urlConnection.setResponseCode(200);
369+
urlConnection.setInputStream(new ByteArrayInputStream("{\"result\": true, \"id\": \"1234\", \"jobs_running\": 0}".getBytes(StandardCharsets.UTF_8)));
370+
371+
String results;
372+
try (BufferedInputStream stream = sauceREST.deleteTunnel("1234")) {
373+
assertEquals("/rest/v1/" + sauceREST.getUsername() + "/tunnels/1234",
374+
urlConnection.getRealURL().getPath());
375+
376+
results = IOUtils.toString(stream, StandardCharsets.UTF_8);
377+
}
378+
JSONObject jsonObject = new JSONObject(results);
379+
380+
assertTrue(jsonObject.getBoolean("result"));
381+
assertEquals("1234", jsonObject.getString("id"));
382+
assertEquals(0, jsonObject.getInt("jobs_running"));
383+
assertFalse(jsonObject.isEmpty());
384+
}
385+
358386
@Test
359387
void testGetActivity() {
360388
urlConnection.setResponseCode(200);

0 commit comments

Comments
 (0)