From b6400317cad966e72f06d2a11718a5971e0af3f7 Mon Sep 17 00:00:00 2001 From: Michael Hamburger Date: Mon, 3 Jun 2024 20:37:24 +0200 Subject: [PATCH] add test for abstractHandler add test for collection input and collection output rename tests for clarification what they are for --- ...stractInputCollectionOutputCollection.java | 19 ++++++++ ...tCollectionOutputCollectionLambdaImpl.java | 5 +++ ...lectionOutputCollectionLambdaImplTest.java | 44 ++++++++++++++++++ .../deployment/testing/GreetingLambda.java | 5 ++- ...ambdaTest.java => GreetingLambdaTest.java} | 29 ++++++------ ...InputCollectionOutputCollectionLambda.java | 23 ++++++++++ ...tCollectionOutputCollectionLambdaTest.java | 45 +++++++++++++++++++ ...aDevServicesContinuousTestingTestCase.java | 8 ++-- .../deployment/testing/LambdaHandlerET.java | 30 ------------- .../lambda/deployment/testing/Person.java | 22 --------- .../deployment/testing/PersonListLambda.java | 15 ------- .../deployment/testing/model/InputPerson.java | 22 +++++++++ .../testing/model/OutputPerson.java | 20 +++++++++ 13 files changed, 201 insertions(+), 86 deletions(-) create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollection.java create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImpl.java create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImplTest.java rename extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/{PersonListLambdaTest.java => GreetingLambdaTest.java} (51%) create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambda.java create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambdaTest.java delete mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaHandlerET.java delete mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/Person.java delete mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambda.java create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/InputPerson.java create mode 100644 extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/OutputPerson.java diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollection.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollection.java new file mode 100644 index 0000000000000..07bd0e1d560cb --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollection.java @@ -0,0 +1,19 @@ +package io.quarkus.amazon.lambda.deployment.testing; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; +import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; + +import java.util.ArrayList; +import java.util.List; + +public abstract class AbstractInputCollectionOutputCollection implements RequestHandler, List> { + + @Override + public List handleRequest(List inputPeronList, Context context) { + List personList = new ArrayList<>(); + inputPeronList.forEach(person -> personList.add(new OutputPerson(person.getName()))); + return personList; + } +} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImpl.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImpl.java new file mode 100644 index 0000000000000..6b8aff7356e9f --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImpl.java @@ -0,0 +1,5 @@ +package io.quarkus.amazon.lambda.deployment.testing; + +public class AbstractInputCollectionOutputCollectionLambdaImpl extends AbstractInputCollectionOutputCollection { + +} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImplTest.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImplTest.java new file mode 100644 index 0000000000000..4a51cbee3521b --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImplTest.java @@ -0,0 +1,44 @@ +package io.quarkus.amazon.lambda.deployment.testing; + +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; +import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; +import io.quarkus.test.QuarkusUnitTest; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.util.ArrayList; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.Matchers.hasEntry; + +public class AbstractInputCollectionOutputCollectionLambdaImplTest { + + @RegisterExtension + static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap + .create(JavaArchive.class) + .addClasses(AbstractInputCollectionOutputCollectionLambdaImpl.class, AbstractInputCollectionOutputCollection.class, InputPerson.class, OutputPerson.class)); + + @Test + void abstractRequestHandler_InputCollectionInputPerson_OutputCollectionOutputPerson() { + + List personList = new ArrayList<>(); + personList.add(new InputPerson("Chris")); + personList.add(new InputPerson("Fred")); + + given() + .body(personList) + .when() + .post() + .then() + .statusCode(200) + .body("", hasItem(hasEntry("outputname", "Chris"))) // OutputPerson serializes name with key outputname + .body("", hasItem(hasEntry("outputname", "Fred"))) + .body("", not(hasItem(hasEntry("name", "Chris")))) // make sure that there is no key name + .body("", not(hasItem(hasEntry("name", "Fred")))); + } +} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambda.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambda.java index f62a7ef7eef95..ccaf7b3d7af87 100644 --- a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambda.java +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambda.java @@ -2,11 +2,12 @@ import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; -public class GreetingLambda implements RequestHandler { +public class GreetingLambda implements RequestHandler { @Override - public String handleRequest(Person input, Context context) { + public String handleRequest(InputPerson input, Context context) { return "Hey " + input.getName(); } } diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambdaTest.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambdaTest.java similarity index 51% rename from extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambdaTest.java rename to extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambdaTest.java index 0e37d6a34ad03..2800f758d6feb 100644 --- a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambdaTest.java +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambdaTest.java @@ -3,36 +3,37 @@ import static io.restassured.RestAssured.given; import static org.hamcrest.CoreMatchers.containsString; -import java.util.ArrayList; -import java.util.List; - +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; +import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; +import io.quarkus.test.QuarkusUnitTest; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; -import io.quarkus.test.QuarkusUnitTest; +import org.junit.jupiter.api.extension.RegisterExtension; -public class PersonListLambdaTest { +class GreetingLambdaTest { @RegisterExtension static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap .create(JavaArchive.class) - .addClasses(PersonListLambda.class, Person.class)); + .addClasses(GreetingLambda.class, InputPerson.class)); @Test - void testFruitsLambda() throws Exception { - - List personList = new ArrayList<>(); - personList.add(new Person("Chris")); - personList.add(new Person("Fred")); + public void requestHandler_InputPerson_OutputString() throws Exception { + // you test your lambdas by invoking on http://localhost:8081 + // this works in dev mode too + InputPerson in = new InputPerson("Stu"); given() - .body(personList) + .contentType("application/json") + .accept("application/json") + .body(in) .when() .post() .then() .statusCode(200) - .body(containsString("Chris Fred")); + .body(containsString("Hey Stu")); } + } diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambda.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambda.java new file mode 100644 index 0000000000000..cc9a317722ec3 --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambda.java @@ -0,0 +1,23 @@ +package io.quarkus.amazon.lambda.deployment.testing; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; +import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; + +import java.util.ArrayList; +import java.util.List; + +public class InputCollectionOutputCollectionLambda implements RequestHandler, List> { + + @Override + public List handleRequest(List people, Context context) { + + List outputPeople = new ArrayList<>(); + people.stream().parallel().forEach((person) -> { + outputPeople.add(new OutputPerson(person.getName())); + }); + + return outputPeople; + } +} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambdaTest.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambdaTest.java new file mode 100644 index 0000000000000..c15b0cba5902f --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambdaTest.java @@ -0,0 +1,45 @@ +package io.quarkus.amazon.lambda.deployment.testing; + +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; +import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; +import io.quarkus.test.QuarkusUnitTest; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.util.ArrayList; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasItems; + +public class InputCollectionOutputCollectionLambdaTest { + + @RegisterExtension + static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap + .create(JavaArchive.class) + .addClasses(InputCollectionOutputCollectionLambda.class, InputPerson.class, OutputPerson.class)); + + @Test + void requestHandler_InputCollectionInputPerson_OutputCollectionOutputPerson() { + + List personList = new ArrayList<>(); + personList.add(new InputPerson("Chris")); + personList.add(new InputPerson("Fred")); + + given() + .body(personList) + .when() + .post() + .then() + .statusCode(200) + .body("", hasItem(hasEntry("outputname", "Chris"))) // OutputPerson serializes name with key outputname + .body("", hasItem(hasEntry("outputname", "Fred"))) + .body("", not(hasItem(hasEntry("name", "Chris")))) // make sure that there is no key name + .body("", not(hasItem(hasEntry("name", "Fred")))); + } +} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaDevServicesContinuousTestingTestCase.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaDevServicesContinuousTestingTestCase.java index cef9a2bda6761..8b480b98d7be3 100644 --- a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaDevServicesContinuousTestingTestCase.java +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaDevServicesContinuousTestingTestCase.java @@ -2,6 +2,8 @@ import java.util.function.Supplier; +import io.quarkus.amazon.lambda.deployment.testing.model.InputPerson; +import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.JavaArchive; @@ -21,7 +23,7 @@ public class LambdaDevServicesContinuousTestingTestCase { @Override public JavaArchive get() { return ShrinkWrap.create(JavaArchive.class) - .addClasses(GreetingLambda.class, Person.class) + .addClasses(GreetingLambda.class, InputPerson.class, OutputPerson.class) .addAsResource( new StringAsset(ContinuousTestingTestUtils.appProperties( "quarkus.log.category.\"io.quarkus.amazon.lambda.runtime\".level=DEBUG")), @@ -30,7 +32,7 @@ public JavaArchive get() { }).setTestArchiveProducer(new Supplier<>() { @Override public JavaArchive get() { - return ShrinkWrap.create(JavaArchive.class).addClass(LambdaHandlerET.class); + return ShrinkWrap.create(JavaArchive.class).addClass(GreetingLambdaTest.class); } }); @@ -45,7 +47,7 @@ public void testLambda() throws Exception { result = utils.waitForNextCompletion(); Assertions.assertEquals(0, result.getTotalTestsPassed()); Assertions.assertEquals(1, result.getTotalTestsFailed()); - test.modifyTestSourceFile(LambdaHandlerET.class, s -> s.replace("Hey", "Yo")); + test.modifyTestSourceFile(GreetingLambdaTest.class, s -> s.replace("Hey", "Yo")); result = utils.waitForNextCompletion(); Assertions.assertEquals(1, result.getTotalTestsPassed()); Assertions.assertEquals(0, result.getTotalTestsFailed()); diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaHandlerET.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaHandlerET.java deleted file mode 100644 index 4e3737d92b2eb..0000000000000 --- a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaHandlerET.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.quarkus.amazon.lambda.deployment.testing; - -import static io.restassured.RestAssured.given; -import static org.hamcrest.CoreMatchers.containsString; - -import org.junit.jupiter.api.Test; - -import io.quarkus.test.junit.QuarkusTest; - -@QuarkusTest -class LambdaHandlerET { - - @Test - public void testSimpleLambdaSuccess() throws Exception { - // you test your lambdas by invoking on http://localhost:8081 - // this works in dev mode too - - Person in = new Person("Stu"); - given() - .contentType("application/json") - .accept("application/json") - .body(in) - .when() - .post() - .then() - .statusCode(200) - .body(containsString("Hey Stu")); - } - -} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/Person.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/Person.java deleted file mode 100644 index b419cbce90c8a..0000000000000 --- a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/Person.java +++ /dev/null @@ -1,22 +0,0 @@ -package io.quarkus.amazon.lambda.deployment.testing; - -public class Person { - - public Person() { - } - - public Person(String name) { - this.name = name; - } - - private String name; - - public String getName() { - return name; - } - - public Person setName(String name) { - this.name = name; - return this; - } -} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambda.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambda.java deleted file mode 100644 index e873d3c600d69..0000000000000 --- a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/PersonListLambda.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.quarkus.amazon.lambda.deployment.testing; - -import java.util.List; -import java.util.stream.Collectors; - -import com.amazonaws.services.lambda.runtime.Context; -import com.amazonaws.services.lambda.runtime.RequestHandler; - -public class PersonListLambda implements RequestHandler, String> { - - @Override - public String handleRequest(List people, Context context) { - return people.stream().map(Person::getName).collect(Collectors.joining(" ")); - } -} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/InputPerson.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/InputPerson.java new file mode 100644 index 0000000000000..b74c9482d8c3c --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/InputPerson.java @@ -0,0 +1,22 @@ +package io.quarkus.amazon.lambda.deployment.testing.model; + +public class InputPerson { + + public InputPerson() { + } + + public InputPerson(String name) { + this.name = name; + } + + private String name; + + public String getName() { + return name; + } + + public InputPerson setName(String name) { + this.name = name; + return this; + } +} diff --git a/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/OutputPerson.java b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/OutputPerson.java new file mode 100644 index 0000000000000..f448321ffdd5f --- /dev/null +++ b/extensions/amazon-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/OutputPerson.java @@ -0,0 +1,20 @@ +package io.quarkus.amazon.lambda.deployment.testing.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class OutputPerson { + + public OutputPerson() { + } + + public OutputPerson(String name) { + this.name = name; + } + + @JsonProperty("outputname") + private String name; + + public String getName() { + return name; + } +}