-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40464 from hamburml/feature/AmazonLambdaHandlerCo…
…llections Fix correct parsing of collections in AmazonLambdaRecorder
- Loading branch information
Showing
14 changed files
with
263 additions
and
51 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
.../io/quarkus/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.amazon.lambda.deployment.testing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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; | ||
|
||
public abstract class AbstractInputCollectionOutputCollection implements RequestHandler<List<InputPerson>, List<OutputPerson>> { | ||
|
||
@Override | ||
public List<OutputPerson> handleRequest(List<InputPerson> inputPeronList, Context context) { | ||
List<OutputPerson> personList = new ArrayList<>(); | ||
inputPeronList.forEach(person -> personList.add(new OutputPerson(person.getName()))); | ||
return personList; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...s/amazon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.quarkus.amazon.lambda.deployment.testing; | ||
|
||
public class AbstractInputCollectionOutputCollectionLambdaImpl extends AbstractInputCollectionOutputCollection { | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...azon/lambda/deployment/testing/AbstractInputCollectionOutputCollectionLambdaImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.amazon.lambda.deployment.testing; | ||
|
||
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 java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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.amazon.lambda.deployment.testing.model.InputPerson; | ||
import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
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<InputPerson> 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")))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...loyment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/GreetingLambdaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.amazon.lambda.deployment.testing; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
|
||
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.amazon.lambda.deployment.testing.model.InputPerson; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
class GreetingLambdaTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap | ||
.create(JavaArchive.class) | ||
.addClasses(GreetingLambda.class, InputPerson.class)); | ||
|
||
@Test | ||
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() | ||
.contentType("application/json") | ||
.accept("application/json") | ||
.body(in) | ||
.when() | ||
.post() | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Hey Stu")); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
...va/io/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.amazon.lambda.deployment.testing; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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; | ||
|
||
public class InputCollectionOutputCollectionLambda implements RequestHandler<List<InputPerson>, List<OutputPerson>> { | ||
|
||
@Override | ||
public List<OutputPerson> handleRequest(List<InputPerson> people, Context context) { | ||
|
||
List<OutputPerson> outputPeople = new ArrayList<>(); | ||
people.stream().parallel().forEach((person) -> { | ||
outputPeople.add(new OutputPerson(person.getName())); | ||
}); | ||
|
||
return outputPeople; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...o/quarkus/amazon/lambda/deployment/testing/InputCollectionOutputCollectionLambdaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.amazon.lambda.deployment.testing; | ||
|
||
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 java.util.ArrayList; | ||
import java.util.List; | ||
|
||
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.amazon.lambda.deployment.testing.model.InputPerson; | ||
import io.quarkus.amazon.lambda.deployment.testing.model.OutputPerson; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
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<InputPerson> 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")))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
...deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/LambdaHandlerET.java
This file was deleted.
Oops, something went wrong.
15 changes: 0 additions & 15 deletions
15
...n-lambda/deployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/Person.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...ployment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/InputPerson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...loyment/src/test/java/io/quarkus/amazon/lambda/deployment/testing/model/OutputPerson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...untime/src/main/java/io/quarkus/amazon/lambda/runtime/handlers/CollectionInputReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.quarkus.amazon.lambda.runtime.handlers; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
import java.util.Collection; | ||
|
||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
|
||
import io.quarkus.amazon.lambda.runtime.LambdaInputReader; | ||
|
||
public class CollectionInputReader<T> implements LambdaInputReader<Collection<T>> { | ||
final ObjectReader reader; | ||
|
||
public CollectionInputReader(ObjectMapper mapper, Method handler) { | ||
Type genericParameterType = handler.getGenericParameterTypes()[0]; | ||
JavaType constructParameterType = mapper.getTypeFactory().constructType(genericParameterType); | ||
this.reader = mapper.readerFor(constructParameterType); | ||
} | ||
|
||
@Override | ||
public Collection<T> readValue(InputStream is) throws IOException { | ||
return this.reader.readValue(is); | ||
} | ||
} |