|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.instrumenter.http; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.mockito.ArgumentMatchers.anyMap; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import io.opentelemetry.api.trace.StatusCode; |
| 13 | +import io.opentelemetry.instrumentation.api.tracer.HttpStatusConverter; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Map; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.ValueSource; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class HttpClientSpanStatusExtractorTest { |
| 25 | + @Mock private HttpClientAttributesExtractor<Map<String, String>, Map<String, String>> extractor; |
| 26 | + |
| 27 | + @ParameterizedTest |
| 28 | + @ValueSource(ints = {1, 100, 101, 200, 201, 300, 301, 400, 401, 500, 501, 600, 601}) |
| 29 | + void hasStatus(int statusCode) { |
| 30 | + when(extractor.statusCode(anyMap(), anyMap())).thenReturn(statusCode); |
| 31 | + assertThat( |
| 32 | + HttpSpanStatusExtractor.create(extractor) |
| 33 | + .extract(Collections.emptyMap(), Collections.emptyMap(), null)) |
| 34 | + .isEqualTo(HttpStatusConverter.CLIENT.statusFromHttpStatus(statusCode)); |
| 35 | + } |
| 36 | + |
| 37 | + @ParameterizedTest |
| 38 | + @ValueSource(ints = {1, 100, 101, 200, 201, 300, 301, 400, 401, 500, 501, 600, 601}) |
| 39 | + void hasStatusAndException(int statusCode) { |
| 40 | + when(extractor.statusCode(anyMap(), anyMap())).thenReturn(statusCode); |
| 41 | + |
| 42 | + // Presence of exception has no effect. |
| 43 | + assertThat( |
| 44 | + HttpSpanStatusExtractor.create(extractor) |
| 45 | + .extract( |
| 46 | + Collections.emptyMap(), Collections.emptyMap(), new IllegalStateException())) |
| 47 | + .isEqualTo(StatusCode.ERROR); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void hasNoStatus_fallsBackToDefault_unset() { |
| 52 | + when(extractor.statusCode(anyMap(), anyMap())).thenReturn(null); |
| 53 | + |
| 54 | + assertThat( |
| 55 | + HttpSpanStatusExtractor.create(extractor) |
| 56 | + .extract(Collections.emptyMap(), Collections.emptyMap(), null)) |
| 57 | + .isEqualTo(StatusCode.UNSET); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void hasNoStatus_fallsBackToDefault_error() { |
| 62 | + when(extractor.statusCode(anyMap(), anyMap())).thenReturn(null); |
| 63 | + |
| 64 | + assertThat( |
| 65 | + HttpSpanStatusExtractor.create(extractor) |
| 66 | + .extract( |
| 67 | + Collections.emptyMap(), Collections.emptyMap(), new IllegalStateException())) |
| 68 | + .isEqualTo(StatusCode.ERROR); |
| 69 | + } |
| 70 | +} |
0 commit comments