Skip to content

Commit

Permalink
Convert play-ws groovy to java (#12377)
Browse files Browse the repository at this point in the history
Co-authored-by: Lauri Tulmin <[email protected]>
  • Loading branch information
heyams and laurit authored Oct 30, 2024
1 parent 00de5e3 commit 0b9dffa
Show file tree
Hide file tree
Showing 29 changed files with 1,169 additions and 410 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v1_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayJavaStreamedWsClientBaseTest;

class PlayJavaStreamedWsClientTest extends PlayJavaStreamedWsClientBaseTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v1_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayJavaWsClientBaseTest;

class PlayJavaWsClientTest extends PlayJavaWsClientBaseTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v1_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayScalaStreamedWsClientBaseTest;

class PlayScalaStreamedWsClientTest extends PlayScalaStreamedWsClientBaseTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v1_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayScalaWsClientBaseTest;

class PlayScalaWsClientTest extends PlayScalaWsClientBaseTest {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayJavaStreamedWsClientBaseTest;

class PlayJavaStreamedWsClientTest extends PlayJavaStreamedWsClientBaseTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayJavaWsClientBaseTest;

class PlayJavaWsClientTest extends PlayJavaWsClientBaseTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayScalaStreamedWsClientBaseTest;

class PlayScalaStreamedWsClientTest extends PlayScalaStreamedWsClientBaseTest {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_0;

import io.opentelemetry.javaagent.instrumentation.playws.PlayScalaWsClientBaseTest;

class PlayScalaWsClientTest extends PlayScalaWsClientBaseTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,33 @@ dependencies {
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
testInstrumentation(project(":instrumentation:akka:akka-http-10.0:javaagent"))
testInstrumentation(project(":instrumentation:akka:akka-actor-2.3:javaagent"))
}

val testLatestDeps = findProperty("testLatestDeps") as Boolean

latestDepTestLibrary("com.typesafe.play:play-ahc-ws-standalone_2.13:+")
testing {
suites {
val latestDepTest by registering(JvmTestSuite::class) {
dependencies {
implementation("com.typesafe.play:play-ahc-ws-standalone_2.13:+")
}
}
}
}

if (findProperty("testLatestDeps") as Boolean) {
configurations {
// play-ws artifact name is different for regular and latest tests
testImplementation {
exclude("com.typesafe.play", "play-ahc-ws-standalone_$scalaVersion")
tasks {
if (testLatestDeps) {
// disable regular test running and compiling tasks when latest dep test task is run
named("test") {
enabled = false
}
}

named("latestDepTest") {
enabled = testLatestDeps
}

check {
dependsOn(testing.suites)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_1;

import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import play.libs.ws.StandaloneWSClient;
import play.libs.ws.StandaloneWSRequest;
import play.libs.ws.StandaloneWSResponse;
import play.libs.ws.ahc.StandaloneAhcWSClient;

class PlayJavaStreamedWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {

private static StandaloneWSClient wsClient;
private static StandaloneWSClient wsClientWithReadTimeout;

@BeforeAll
static void setup() {
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
wsClientWithReadTimeout =
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
}

@AfterAll
static void cleanup() throws IOException {
wsClient.close();
wsClientWithReadTimeout.close();
}

@Override
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
StandaloneWSRequest request = getClient(uri).url(uri.toString()).setFollowRedirects(true);
headers.forEach(request::addHeader);
request.setMethod(method);
return request;
}

@Override
public int sendRequest(
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
throws ExecutionException, InterruptedException {
return internalSendRequest(request).toCompletableFuture().get().getStatus();
}

@Override
public void sendRequestWithCallback(
StandaloneWSRequest request,
String method,
URI uri,
Map<String, String> headers,
HttpClientResult requestResult) {
internalSendRequest(request)
.whenComplete(
(response, throwable) -> {
if (throwable != null) {
requestResult.complete(throwable.getCause());
} else {
requestResult.complete(response.getStatus());
}
});
}

private static CompletionStage<StandaloneWSResponse> internalSendRequest(
StandaloneWSRequest request) {
CompletionStage<? extends StandaloneWSResponse> stream = request.stream();
// The status can be ready before the body so explicitly call wait for body to be ready
return stream
.thenCompose(
response -> response.getBodyAsSource().runFold("", (acc, out) -> "", materializer))
.thenCombine(stream, (body, response) -> response);
}

private static StandaloneWSClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return wsClientWithReadTimeout;
}
return wsClient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.playws.v2_1;

import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import play.libs.ws.StandaloneWSClient;
import play.libs.ws.StandaloneWSRequest;
import play.libs.ws.ahc.StandaloneAhcWSClient;

class PlayJavaWsClientTest extends PlayWsClientBaseTest<StandaloneWSRequest> {

private static StandaloneWSClient wsClient;
private static StandaloneWSClient wsClientWithReadTimeout;

@BeforeAll
static void setup() {
wsClient = new StandaloneAhcWSClient(asyncHttpClient, materializer);
wsClientWithReadTimeout =
new StandaloneAhcWSClient(asyncHttpClientWithReadTimeout, materializer);
}

@AfterAll
static void cleanup() throws IOException {
wsClient.close();
wsClientWithReadTimeout.close();
}

@Override
public StandaloneWSRequest buildRequest(String method, URI uri, Map<String, String> headers) {
StandaloneWSRequest request = getClient(uri).url(uri.toString()).setFollowRedirects(true);
headers.forEach(request::addHeader);
return request.setMethod(method);
}

@Override
public int sendRequest(
StandaloneWSRequest request, String method, URI uri, Map<String, String> headers)
throws ExecutionException, InterruptedException {
return request.execute().toCompletableFuture().get().getStatus();
}

@Override
public void sendRequestWithCallback(
StandaloneWSRequest request,
String method,
URI uri,
Map<String, String> headers,
HttpClientResult requestResult) {
request
.execute()
.whenComplete(
(response, throwable) -> {
if (throwable != null) {
requestResult.complete(throwable);
} else {
requestResult.complete(response.getStatus());
}
});
}

private static StandaloneWSClient getClient(URI uri) {
if (uri.toString().contains("/read-timeout")) {
return wsClientWithReadTimeout;
}
return wsClient;
}
}
Loading

0 comments on commit 0b9dffa

Please sign in to comment.