|
| 1 | +package com.newrelic.opentracing.aws; |
| 2 | + |
| 3 | +import com.amazonaws.services.lambda.runtime.Context; |
| 4 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 5 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; |
| 6 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.Arrays; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | + |
| 15 | +public class ReflectionTest { |
| 16 | + private static final int SYNTHETIC_MODIFIER = 0x1000; |
| 17 | + |
| 18 | + private Object handler; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void setup() { |
| 22 | + handler = new TestHandler(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testInputReflection() { |
| 27 | + // Ignoring synthetics, we expect handleRequest to take the declared type as its first arg. |
| 28 | + // This is necessary for correct payload deserialization. |
| 29 | + final Method handleRequest = Arrays.stream(handler.getClass().getMethods()) |
| 30 | + .filter(m -> ((m.getModifiers() & SYNTHETIC_MODIFIER) == 0) |
| 31 | + && m.getName().equals("handleRequest")) |
| 32 | + .findFirst() |
| 33 | + .orElseThrow(AssertionError::new); |
| 34 | + |
| 35 | + assertEquals(APIGatewayProxyRequestEvent.class, handleRequest.getParameterTypes()[0]); |
| 36 | + } |
| 37 | + |
| 38 | + public static class TestHandler implements TracingRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 39 | + @Override |
| 40 | + public APIGatewayProxyResponseEvent doHandleRequest(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) { |
| 41 | + return new APIGatewayProxyResponseEvent(); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments