|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.spring.smoketest; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import io.opentelemetry.api.trace.SpanKind; |
| 11 | +import io.opentelemetry.sdk.testing.assertj.TraceAssert; |
| 12 | +import io.opentelemetry.semconv.HttpAttributes; |
| 13 | +import io.opentelemetry.semconv.UrlAttributes; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 18 | +import org.springframework.web.client.RestClient; |
| 19 | + |
| 20 | +@SpringBootTest( |
| 21 | + classes = { |
| 22 | + OtelSpringStarterSmokeTestApplication.class, |
| 23 | + AbstractOtelSpringStarterSmokeTest.TestConfiguration.class, |
| 24 | + SpringSmokeOtelConfiguration.class |
| 25 | + }, |
| 26 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 27 | +class OtelSpringStarterSmokeTest extends AbstractSpringStarterSmokeTest { |
| 28 | + |
| 29 | + @Autowired RestClient.Builder restClientBuilder; |
| 30 | + @LocalServerPort private int port; |
| 31 | + |
| 32 | + @Test |
| 33 | + void restClient() { |
| 34 | + testing.clearAllExportedData(); |
| 35 | + |
| 36 | + RestClient client = restClientBuilder.baseUrl("http://localhost:" + port).build(); |
| 37 | + assertThat( |
| 38 | + client |
| 39 | + .get() |
| 40 | + .uri(OtelSpringStarterSmokeTestController.PING) |
| 41 | + .retrieve() |
| 42 | + .body(String.class)) |
| 43 | + .isEqualTo("pong"); |
| 44 | + |
| 45 | + if (System.getProperty("org.graalvm.nativeimage.imagecode") != null) { |
| 46 | + // ignore the trace for creating the db table |
| 47 | + testing.waitAndAssertTraces(trace -> {}, OtelSpringStarterSmokeTest::assertClient); |
| 48 | + } else { |
| 49 | + testing.waitAndAssertTraces(OtelSpringStarterSmokeTest::assertClient); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static void assertClient(TraceAssert traceAssert) { |
| 54 | + traceAssert.hasSpansSatisfyingExactly( |
| 55 | + nestedClientSpan -> |
| 56 | + nestedClientSpan |
| 57 | + .hasKind(SpanKind.CLIENT) |
| 58 | + .hasAttributesSatisfying( |
| 59 | + a -> assertThat(a.get(UrlAttributes.URL_FULL)).endsWith("/ping")), |
| 60 | + nestedServerSpan -> |
| 61 | + nestedServerSpan |
| 62 | + .hasKind(SpanKind.SERVER) |
| 63 | + .hasAttribute(HttpAttributes.HTTP_ROUTE, "/ping")); |
| 64 | + } |
| 65 | +} |
0 commit comments