|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.awssdk.v1_11; |
| 7 | + |
| 8 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 9 | + |
| 10 | +import com.amazonaws.SDKGlobalConfiguration; |
| 11 | +import com.amazonaws.auth.AWSStaticCredentialsProvider; |
| 12 | +import com.amazonaws.auth.AnonymousAWSCredentials; |
| 13 | +import com.amazonaws.client.builder.AwsClientBuilder; |
| 14 | +import com.amazonaws.handlers.RequestHandler2; |
| 15 | +import com.amazonaws.services.s3.AmazonS3; |
| 16 | +import com.amazonaws.services.s3.AmazonS3ClientBuilder; |
| 17 | +import com.amazonaws.services.s3.model.Bucket; |
| 18 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 19 | +import io.opentelemetry.testing.internal.armeria.common.HttpResponse; |
| 20 | +import io.opentelemetry.testing.internal.armeria.common.HttpStatus; |
| 21 | +import io.opentelemetry.testing.internal.armeria.common.MediaType; |
| 22 | +import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension; |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.util.List; |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +public abstract class AbstractAws1ClientTest { |
| 30 | + protected abstract InstrumentationExtension testing(); |
| 31 | + |
| 32 | + private static final MockWebServerExtension server = new MockWebServerExtension(); |
| 33 | + private static AwsClientBuilder.EndpointConfiguration endpoint; |
| 34 | + private static final AWSStaticCredentialsProvider credentialsProvider = |
| 35 | + new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()); |
| 36 | + |
| 37 | + @BeforeAll |
| 38 | + public static void setUp() { |
| 39 | + System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY, "my-access-key"); |
| 40 | + System.setProperty(SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY, "my-secret-key"); |
| 41 | + server.start(); |
| 42 | + endpoint = new AwsClientBuilder.EndpointConfiguration("${server.httpUri()}", "us-west-2"); |
| 43 | + server.beforeTestExecution(null); |
| 44 | + } |
| 45 | + |
| 46 | + @AfterAll |
| 47 | + public static void cleanUp() { |
| 48 | + System.clearProperty(SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY); |
| 49 | + System.clearProperty(SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY); |
| 50 | + server.stop(); |
| 51 | + } |
| 52 | + |
| 53 | + // abstract AwsClientBuilder<?> configureClient(AwsClientBuilder client); |
| 54 | + |
| 55 | + @Test |
| 56 | + @SuppressWarnings("unchecked") |
| 57 | + public void test() throws NoSuchFieldException, IllegalAccessException { |
| 58 | + String body = ""; |
| 59 | + AmazonS3ClientBuilder clientBuilder = |
| 60 | + AmazonS3ClientBuilder.standard().withPathStyleAccessEnabled(true); |
| 61 | + AmazonS3 client = |
| 62 | + (AmazonS3) |
| 63 | + configureClient(clientBuilder) |
| 64 | + .withEndpointConfiguration(endpoint) |
| 65 | + .withCredentials(credentialsProvider) |
| 66 | + .build(); |
| 67 | + |
| 68 | + server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, body)); |
| 69 | + Bucket response = client.createBucket("testbucket"); |
| 70 | + |
| 71 | + assertThat(response).isNotNull(); |
| 72 | + |
| 73 | + Field requestHandler2sField = client.getClass().getDeclaredField("requestHandler2s"); |
| 74 | + requestHandler2sField.setAccessible(true); |
| 75 | + List<RequestHandler2> requestHandler2s = |
| 76 | + (List<RequestHandler2>) requestHandler2sField.get(client); |
| 77 | + |
| 78 | + assertThat(requestHandler2s).isNotNull(); |
| 79 | + assertThat( |
| 80 | + requestHandler2s.stream() |
| 81 | + .filter(h -> "TracingRequestHandler".equals(h.getClass().getSimpleName()))) |
| 82 | + .isNotNull(); |
| 83 | + } |
| 84 | +} |
0 commit comments