Skip to content

Commit

Permalink
refactor to distinguish extensions from base interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Nov 8, 2023
1 parent 655b4b6 commit 985b074
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 68 deletions.
34 changes: 20 additions & 14 deletions src/test/java/itest/UploadRecordingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public static void createRecording() throws Exception {
CREATE_RECORDING_URL =
String.format("/api/v1/targets/%s/recordings", getSelfReferenceConnectUrlEncoded());
HttpResponse<Buffer> resp =
webClient.post(CREATE_RECORDING_URL, true, form, RECORDING_DURATION_SECONDS);
webClient
.extensions()
.post(CREATE_RECORDING_URL, true, form, RECORDING_DURATION_SECONDS);
MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(201));
Thread.sleep(
Long.valueOf(
Expand All @@ -78,12 +80,14 @@ public static void createRecording() throws Exception {
public static void deleteRecording() throws Exception {
try {
HttpResponse<Buffer> resp =
webClient.delete(
String.format(
"/api/v1/targets/%s/recordings/%s",
getSelfReferenceConnectUrlEncoded(), RECORDING_NAME),
true,
REQUEST_TIMEOUT_SECONDS);
webClient
.extensions()
.delete(
String.format(
"/api/v1/targets/%s/recordings/%s",
getSelfReferenceConnectUrlEncoded(), RECORDING_NAME),
true,
REQUEST_TIMEOUT_SECONDS);
MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(204));
} catch (InterruptedException | ExecutionException | TimeoutException e) {
logger.error(
Expand All @@ -97,13 +101,15 @@ public static void deleteRecording() throws Exception {
public void shouldLoadRecordingToDatasource() throws Exception {

HttpResponse<Buffer> resp =
webClient.post(
String.format(
"/api/v1/targets/%s/recordings/%s/upload",
getSelfReferenceConnectUrlEncoded(), RECORDING_NAME),
true,
null,
0);
webClient
.extensions()
.post(
String.format(
"/api/v1/targets/%s/recordings/%s/upload",
getSelfReferenceConnectUrlEncoded(), RECORDING_NAME),
true,
null,
0);

MatcherAssert.assertThat(resp.statusCode(), Matchers.equalTo(200));

Expand Down
122 changes: 68 additions & 54 deletions src/test/java/itest/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,66 +82,80 @@ public static FileSystem getFileSystem() {
return VERTX.fileSystem();
}

public interface RedirectExtensions {
HttpResponse<Buffer> post(String url, boolean authentication, MultiMap form, int timeout)
throws InterruptedException, ExecutionException, TimeoutException;

HttpResponse<Buffer> delete(String url, boolean authentication, int timeout)
throws InterruptedException, ExecutionException, TimeoutException;
}

public static class TestWebClient extends WebClientBase {

public TestWebClient(HttpClient client, WebClientOptions options) {
super(client, options);
}

public HttpResponse<Buffer> post(
String url, boolean authentication, MultiMap form, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req = this.request(HttpMethod.POST, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
if (form != null) {
req.sendForm(
form,
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
} else {
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
}
if (future.get().statusCode() == 308) {
return post(future.get().getHeader("Location"), true, form, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}
public RedirectExtensions extensions() {
return new RedirectExtensions() {
public HttpResponse<Buffer> post(
String url, boolean authentication, MultiMap form, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req = TestWebClient.this.request(HttpMethod.POST, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
if (form != null) {
req.sendForm(
form,
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
} else {
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
}
if (future.get().statusCode() == 308) {
return post(future.get().getHeader("Location"), true, form, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}

public HttpResponse<Buffer> delete(String url, boolean authentication, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req = this.request(HttpMethod.DELETE, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
if (future.get().statusCode() == 308) {
return delete(future.get().getHeader("Location"), true, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
public HttpResponse<Buffer> delete(String url, boolean authentication, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req =
TestWebClient.this.request(HttpMethod.DELETE, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
if (future.get().statusCode() == 308) {
return delete(future.get().getHeader("Location"), true, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}
};
}
}
}

0 comments on commit 985b074

Please sign in to comment.