|
| 1 | +package io.quarkus.amazon.lambda.deployment.testing; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.hamcrest.CoreMatchers.containsString; |
| 6 | + |
| 7 | +import java.util.logging.Level; |
| 8 | +import java.util.logging.LogRecord; |
| 9 | + |
| 10 | +import jakarta.annotation.Priority; |
| 11 | +import jakarta.decorator.Decorator; |
| 12 | +import jakarta.decorator.Delegate; |
| 13 | +import jakarta.enterprise.inject.Any; |
| 14 | +import jakarta.inject.Inject; |
| 15 | + |
| 16 | +import org.jboss.logging.Logger; |
| 17 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 18 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 21 | + |
| 22 | +import com.amazonaws.services.lambda.runtime.Context; |
| 23 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 24 | + |
| 25 | +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; |
| 26 | +import io.quarkus.test.QuarkusUnitTest; |
| 27 | + |
| 28 | +class LambdaWithDecoratorTest { |
| 29 | + |
| 30 | + @RegisterExtension |
| 31 | + static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap |
| 32 | + .create(JavaArchive.class) |
| 33 | + .addClasses(LambdaWithDecorator.class, RequestHandlerDecorator.class, InputPerson.class)) |
| 34 | + .setLogRecordPredicate(record -> record.getLevel().intValue() == Level.INFO.intValue() |
| 35 | + && record.getMessage().contains("handling request with id")) |
| 36 | + .assertLogRecords(records -> assertThat(records) |
| 37 | + .extracting(LogRecord::getMessage) |
| 38 | + .isNotEmpty()); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testLambdaWithDecorator() throws Exception { |
| 42 | + // you test your lambdas by invoking on http://localhost:8081 |
| 43 | + // this works in dev mode too |
| 44 | + |
| 45 | + InputPerson in = new InputPerson("Stu"); |
| 46 | + given() |
| 47 | + .contentType("application/json") |
| 48 | + .accept("application/json") |
| 49 | + .body(in) |
| 50 | + .when() |
| 51 | + .post() |
| 52 | + .then() |
| 53 | + .statusCode(200) |
| 54 | + .body(containsString("Hey Stu")); |
| 55 | + } |
| 56 | + |
| 57 | + public static class LambdaWithDecorator implements RequestHandler<InputPerson, String> { |
| 58 | + |
| 59 | + @Override |
| 60 | + public String handleRequest(InputPerson input, Context context) { |
| 61 | + return "Hey " + input.getName(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Priority(10) |
| 66 | + @Decorator |
| 67 | + public static class RequestHandlerDecorator<I, O> implements RequestHandler<I, O> { |
| 68 | + |
| 69 | + @Inject |
| 70 | + Logger logger; |
| 71 | + |
| 72 | + @Inject |
| 73 | + @Any |
| 74 | + @Delegate |
| 75 | + RequestHandler<I, O> delegate; |
| 76 | + |
| 77 | + @Override |
| 78 | + public O handleRequest(I i, Context context) { |
| 79 | + logger.info("handling request with id " + context.getAwsRequestId()); |
| 80 | + return delegate.handleRequest(i, context); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments