Migrate google-http-client tests to Java.
DELETE
instrumentation/google-http-client-1.19/javaagent/src/test/groovy/AbstractGoogleHttpClientTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import com.google.api.client.http.GenericUrl
-import com.google.api.client.http.HttpRequest
-import com.google.api.client.http.HttpResponse
-import com.google.api.client.http.javanet.NetHttpTransport
-import io.opentelemetry.instrumentation.test.AgentTestTrait
-import io.opentelemetry.instrumentation.test.base.HttpClientTest
-import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
-import spock.lang.Shared
-
-import static io.opentelemetry.api.trace.SpanKind.CLIENT
-import static io.opentelemetry.api.trace.StatusCode.ERROR
-import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP
-
-abstract class AbstractGoogleHttpClientTest extends HttpClientTest<HttpRequest> implements AgentTestTrait {
-
- @Shared
- def requestFactory = new NetHttpTransport().createRequestFactory()
-
- @Override
- boolean testCallback() {
- // executeAsync does not actually allow asynchronous execution since it returns a standard
- // Future which cannot have callbacks attached. We instrument execute and executeAsync
- // differently so test both but do not need to run our normal asynchronous tests, which check
- // context propagation, as there is no possible context propagation.
- return false
- }
-
- @Override
- boolean testReadTimeout() {
- true
- }
-
- @Override
- HttpRequest buildRequest(String method, URI uri, Map<String, String> headers) {
- def genericUrl = new GenericUrl(uri)
-
- def request = requestFactory.buildRequest(method, genericUrl, null)
- request.connectTimeout = CONNECT_TIMEOUT_MS
- if (uri.toString().contains("/read-timeout")) {
- request.readTimeout = READ_TIMEOUT_MS
- }
-
- // GenericData::putAll method converts all known http headers to List<String>
- // and lowercase all other headers
- def ci = request.getHeaders().getClassInfo()
- request.getHeaders().putAll(headers.collectEntries { name, value
- -> [(name): (ci.getFieldInfo(name) != null ? [value] : value.toLowerCase())]
- })
-
- request.setThrowExceptionOnExecuteError(false)
- return request
- }
-
- @Override
- int sendRequest(HttpRequest request, String method, URI uri, Map<String, String> headers) {
- return sendRequest(request).getStatusCode()
- }
-
- abstract HttpResponse sendRequest(HttpRequest request)
-
- @Override
- boolean testCircularRedirects() {
- // Circular redirects don't throw an exception with Google Http Client
- return false
- }
-
- def "error traces when exception is not thrown"() {
- given:
- def uri = resolveAddress("/error")
-
- when:
- def responseCode = doRequest(method, uri)
-
- then:
- responseCode == 500
- assertTraces(1) {
- trace(0, 2) {
- span(0) {
- kind CLIENT
- status ERROR
- attributes {
- "$SemanticAttributes.NET_TRANSPORT" IP_TCP
- "$SemanticAttributes.NET_PEER_NAME" "localhost"
- "$SemanticAttributes.NET_PEER_PORT" Long
- "$SemanticAttributes.HTTP_URL" "${uri}"
- "$SemanticAttributes.HTTP_METHOD" method
- "$SemanticAttributes.HTTP_STATUS_CODE" 500
- "$SemanticAttributes.HTTP_FLAVOR" "1.1"
- }
- }
- serverSpan(it, 1, span(0))
- }
- }
-
- where:
- method = "GET"
- }
-}

DELETE
instrumentation/google-http-client-1.19/javaagent/src/test/groovy/GoogleHttpClientAsyncTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import com.google.api.client.http.HttpRequest
-import com.google.api.client.http.HttpResponse
-
-class GoogleHttpClientAsyncTest extends AbstractGoogleHttpClientTest {
- @Override
- HttpResponse sendRequest(HttpRequest request) {
- return request.executeAsync().get()
- }
-}

DELETE
instrumentation/google-http-client-1.19/javaagent/src/test/groovy/GoogleHttpClientTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import com.google.api.client.http.HttpRequest
-import com.google.api.client.http.HttpResponse
-
-class GoogleHttpClientTest extends AbstractGoogleHttpClientTest {
- @Override
- HttpResponse sendRequest(HttpRequest request) {
- return request.execute()
- }
-}

ADD
instrumentation/google-http-client-1.19/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/AbstractGoogleHttpClientTest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.googlehttpclient;
+
+import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
+
+import com.google.api.client.http.GenericUrl;
+import com.google.api.client.http.HttpRequest;
+import com.google.api.client.http.HttpRequestFactory;
+import com.google.api.client.http.HttpResponse;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.util.ClassInfo;
+import io.opentelemetry.api.trace.SpanKind;
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
+import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
+import io.opentelemetry.sdk.trace.data.StatusData;
+import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.URI;
+import java.util.Collections;
+import java.util.Locale;
+import java.util.Map;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+public abstract class AbstractGoogleHttpClientTest extends AbstractHttpClientTest<HttpRequest> {
+
+ @RegisterExtension
+ static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
+
+ private HttpRequestFactory requestFactory;
+
+ @BeforeAll
+ void setUp() {
+ requestFactory = new NetHttpTransport().createRequestFactory();
+ }
+
+ @Override
+ protected final HttpRequest buildRequest(String method, URI uri, Map<String, String> headers) {
+ GenericUrl genericUrl = new GenericUrl(uri);
+
+ HttpRequest request;
+ try {
+ request = requestFactory.buildRequest(method, genericUrl, null);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ request.setConnectTimeout((int) connectTimeout().toMillis());
+ if (uri.toString().contains("/read-timeout")) {
+ request.setReadTimeout((int) readTimeout().toMillis());
+ }
+
+ // GenericData::putAll method converts all known http headers to List<String>
+ // and lowercase all other headers
+ ClassInfo ci = request.getHeaders().getClassInfo();
+ headers.forEach(
+ (name, value) ->
+ request
+ .getHeaders()
+ .put(name, ci.getFieldInfo(name) != null ? value : value.toLowerCase(Locale.ROOT)));
+
+ request.setThrowExceptionOnExecuteError(false);
+ return request;
+ }
+
+ @Override
+ protected final int sendRequest(
+ HttpRequest request, String method, URI uri, Map<String, String> headers) throws Exception {
+ return sendRequest(request).getStatusCode();
+ }
+
+ protected abstract HttpResponse sendRequest(HttpRequest request) throws Exception;
+
+ @Test
+ void errorTracesWhenExceptionIsNotThrown() throws Exception {
+ URI uri = resolveAddress("/error");
+
+ HttpRequest request = buildRequest("GET", uri, Collections.emptyMap());
+ int responseCode = sendRequest(request, "GET", uri, Collections.emptyMap());
+
+ assertThat(responseCode).isEqualTo(500);
+ testing.waitAndAssertTraces(
+ trace ->
+ trace.hasSpansSatisfyingExactly(
+ span ->
+ span.hasKind(SpanKind.CLIENT)
+ .hasStatus(StatusData.error())
+ .hasAttributesSatisfying(
+ attrs ->
+ assertThat(attrs)
+ .hasSize(7)
+ .containsEntry(
+ SemanticAttributes.NET_TRANSPORT,
+ SemanticAttributes.NetTransportValues.IP_TCP)
+ .containsEntry(SemanticAttributes.NET_PEER_NAME, "localhost")
+ .hasEntrySatisfying(
+ SemanticAttributes.NET_PEER_PORT,
+ port -> assertThat(port).isPositive())
+ .containsEntry(SemanticAttributes.HTTP_URL, uri.toString())
+ .containsEntry(SemanticAttributes.HTTP_METHOD, "GET")
+ .containsEntry(SemanticAttributes.HTTP_STATUS_CODE, 500)
+ .containsEntry(
+ SemanticAttributes.HTTP_FLAVOR,
+ SemanticAttributes.HttpFlavorValues.HTTP_1_1)),
+ span -> span.hasKind(SpanKind.SERVER).hasParent(trace.getSpan(0))));
+ }
+
+ @Override
+ protected void configure(HttpClientTestOptions options) {
+ // executeAsync does not actually allow asynchronous execution since it returns a standard
+ // Future which cannot have callbacks attached. We instrument execute and executeAsync
+ // differently so test both but do not need to run our normal asynchronous tests, which check
+ // context propagation, as there is no possible context propagation.
+ options.disableTestCallback();
+
+ options.enableTestReadTimeout();
+
+ // Circular redirects don't throw an exception with Google Http Client
+ options.disableTestCircularRedirects();
+ }
+}

ADD
instrumentation/google-http-client-1.19/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/GoogleHttpClientAsyncTest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.googlehttpclient;
+
+import com.google.api.client.http.HttpRequest;
+import com.google.api.client.http.HttpResponse;
+
+class GoogleHttpClientAsyncTest extends AbstractGoogleHttpClientTest {
+
+ @Override
+ protected HttpResponse sendRequest(HttpRequest request) throws Exception {
+ return request.executeAsync().get();
+ }
+}

ADD
instrumentation/google-http-client-1.19/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/googlehttpclient/GoogleHttpClientTest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.googlehttpclient;
+
+import com.google.api.client.http.HttpRequest;
+import com.google.api.client.http.HttpResponse;
+
+class GoogleHttpClientTest extends AbstractGoogleHttpClientTest {
+
+ @Override
+ protected HttpResponse sendRequest(HttpRequest request) throws Exception {
+ return request.execute();
+ }
+}

Migrate google-http-client tests to Java. (#5399)