Skip to content

Commit

Permalink
add test for abstractHandler
Browse files Browse the repository at this point in the history
add test for collection input and collection output
rename tests for clarification what they are for
  • Loading branch information
hamburml committed Jun 12, 2024
1 parent 06d24d4 commit b640031
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -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<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;
}
}
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 {

}
Original file line number Diff line number Diff line change
@@ -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<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"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person, String> {
public class GreetingLambda implements RequestHandler<InputPerson, String> {

@Override
public String handleRequest(Person input, Context context) {
public String handleRequest(InputPerson input, Context context) {
return "Hey " + input.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> 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"));
}

}
Original file line number Diff line number Diff line change
@@ -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<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;
}
}
Original file line number Diff line number Diff line change
@@ -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<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"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")),
Expand All @@ -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);
}
});

Expand All @@ -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());
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

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;
}
}
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;
}
}

0 comments on commit b640031

Please sign in to comment.