Skip to content

Commit fe94d7c

Browse files
committed
- add test
1 parent 4fd60f2 commit fe94d7c

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.awslambdaevents.v2_2;
7+
8+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.mockito.Mockito.when;
11+
12+
import com.amazonaws.services.lambda.runtime.Context;
13+
import com.amazonaws.services.lambda.runtime.RequestHandler;
14+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
15+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
16+
import io.opentelemetry.api.trace.SpanKind;
17+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
18+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
19+
import io.opentelemetry.semconv.HttpAttributes;
20+
import io.opentelemetry.semconv.UrlAttributes;
21+
import io.opentelemetry.semconv.UserAgentAttributes;
22+
import io.opentelemetry.semconv.incubating.FaasIncubatingAttributes;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.junit.jupiter.api.extension.RegisterExtension;
30+
import org.mockito.Mock;
31+
import org.mockito.junit.jupiter.MockitoExtension;
32+
import org.mockito.junit.jupiter.MockitoSettings;
33+
import org.mockito.quality.Strictness;
34+
35+
@ExtendWith(MockitoExtension.class)
36+
@MockitoSettings(strictness = Strictness.LENIENT)
37+
public class AwsLambdaApiGatewayHandlerTest {
38+
39+
@RegisterExtension
40+
public static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
41+
42+
@Mock private Context context;
43+
44+
@BeforeEach
45+
void setUp() {
46+
when(context.getFunctionName()).thenReturn("test_function");
47+
when(context.getAwsRequestId()).thenReturn("1-22-2024");
48+
}
49+
50+
@AfterEach
51+
void tearDown() {
52+
assertThat(testing.forceFlushCalled()).isTrue();
53+
}
54+
55+
@Test
56+
void tracedWithHttpPropagation() {
57+
Map<String, String> headers = new HashMap<>();
58+
headers.put("traceparent", "00-ee13e7026227ebf4c74278ae29691d7a-0000000000000456-01");
59+
headers.put("User-Agent", "Clever Client");
60+
headers.put("host", "localhost:2024");
61+
headers.put("X-FORWARDED-PROTO", "http");
62+
63+
APIGatewayProxyRequestEvent input =
64+
new APIGatewayProxyRequestEvent()
65+
.withHttpMethod("PUT")
66+
.withResource("/hello/{param}")
67+
.withPath("/hello/world")
68+
.withBody("hello")
69+
.withHeaders(headers);
70+
71+
APIGatewayProxyResponseEvent result =
72+
new TestRequestHandlerApiGateway().handleRequest(input, context);
73+
assertThat(result.getBody()).isEqualTo("hello world");
74+
assertThat(result.getStatusCode()).isEqualTo(201);
75+
76+
testing.waitAndAssertTraces(
77+
trace ->
78+
trace.hasSpansSatisfyingExactly(
79+
span ->
80+
span.hasName("PUT /hello/{param}")
81+
.hasKind(SpanKind.SERVER)
82+
.hasTraceId("ee13e7026227ebf4c74278ae29691d7a")
83+
.hasParentSpanId("0000000000000456")
84+
.hasAttributesSatisfyingExactly(
85+
equalTo(FaasIncubatingAttributes.FAAS_INVOCATION_ID, "1-22-2024"),
86+
equalTo(FaasIncubatingAttributes.FAAS_TRIGGER, "http"),
87+
equalTo(HttpAttributes.HTTP_REQUEST_METHOD, "PUT"),
88+
equalTo(UserAgentAttributes.USER_AGENT_ORIGINAL, "Clever Client"),
89+
equalTo(UrlAttributes.URL_FULL, "http://localhost:2024/hello/world"))));
90+
}
91+
92+
public static class TestRequestHandlerApiGateway
93+
implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
94+
95+
@Override
96+
public APIGatewayProxyResponseEvent handleRequest(
97+
APIGatewayProxyRequestEvent input, Context context) {
98+
return new APIGatewayProxyResponseEvent().withStatusCode(201).withBody("hello world");
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)