Skip to content

Commit 3f64d55

Browse files
author
Pascal Romanens
committed
#1298 implemented suggested changes
1 parent ecf056e commit 3f64d55

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

docs/utilities/validation.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ We support JSON schema version 4, 6, 7 and 201909 (from [jmespath-jackson librar
156156

157157
`@Validation` annotation is used to validate either inbound events or functions' response.
158158

159-
It will fail fast if an event or response doesn't conform with given JSON Schema. For most type of events a `ValidationException` will be thrown. But for
160-
specific `APIGatewayProxyRequestEvent` and `APIGatewayV2HTTPEvent`, instead, the `@Validation` annotation will build and return a custom
161-
400 (BAD_REQUEST) response. Its body will contain the validation error(s) message(s).
159+
It will fail fast if an event or response doesn't conform with given JSON Schema. For most type of events a `ValidationException` will be thrown.
160+
For API gateway events associated with REST APIs and HTTP APIs - `APIGatewayProxyRequestEvent` and `APIGatewayV2HTTPEvent` - the `@Validation`
161+
annotation will build and return a custom 400 / "Bad Request" response, with a body containing the validation errors. This saves you from having
162+
to catch the validation exception and map it back to a meaningful user error yourself.
162163

163164
While it is easier to specify a json schema file in the classpath (using the notation `"classpath:/path/to/schema.json"`), you can also provide a JSON String containing the schema.
164165

powertools-e2e-tests/pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,6 @@
172172
<artifactId>powertools-serialization</artifactId>
173173
<scope>test</scope>
174174
</dependency>
175-
176-
<dependency>
177-
<groupId>com.amazonaws</groupId>
178-
<artifactId>aws-lambda-java-tests</artifactId>
179-
<version>1.1.1</version>
180-
<scope>test</scope>
181-
</dependency>
182175
</dependencies>
183176

184177
<build>

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/ValidationE2ET.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919
import static software.amazon.lambda.powertools.testutils.lambda.LambdaInvoker.invokeFunction;
2020

2121
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.nio.charset.StandardCharsets;
2224
import java.util.Map;
2325
import java.util.concurrent.TimeUnit;
2426

27+
import org.apache.commons.io.IOUtils;
2528
import org.junit.jupiter.api.AfterAll;
2629
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Test;
2731
import org.junit.jupiter.api.Timeout;
28-
import org.junit.jupiter.params.ParameterizedTest;
2932

30-
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
31-
import com.amazonaws.services.lambda.runtime.tests.annotations.Event;
3233
import com.fasterxml.jackson.databind.JsonNode;
3334
import com.fasterxml.jackson.databind.ObjectMapper;
3435

@@ -58,42 +59,49 @@ public static void tearDown() {
5859
}
5960
}
6061

61-
@ParameterizedTest
62-
@Event(value = "/validation/valid_api_gw_in_out_event.json", type = APIGatewayProxyRequestEvent.class)
63-
void test_validInboundApiGWEvent(APIGatewayProxyRequestEvent validEvent) throws IOException {
62+
@Test
63+
void test_validInboundApiGWEvent() throws IOException {
64+
InputStream inputStream = this.getClass().getResourceAsStream("/validation/valid_api_gw_in_out_event.json");
65+
String validEvent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
66+
6467
// WHEN
65-
InvocationResult invocationResult = invokeFunction(functionName, objectMapper.writeValueAsString(validEvent));
68+
InvocationResult invocationResult = invokeFunction(functionName, validEvent);
6669

6770
// THEN
6871
// invocation should pass validation and return 200
6972
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
7073
assertThat(validJsonNode.get("statusCode").asInt()).isEqualTo(200);
7174
assertThat(validJsonNode.get("body").asText()).isEqualTo("{\"price\": 150}");
7275
}
73-
74-
@ParameterizedTest
75-
@Event(value = "/validation/invalid_api_gw_in_event.json", type = APIGatewayProxyRequestEvent.class)
76-
void test_invalidInboundApiGWEvent(APIGatewayProxyRequestEvent validEvent) throws IOException {
76+
77+
@Test
78+
void test_invalidInboundApiGWEvent() throws IOException {
79+
InputStream inputStream = this.getClass().getResourceAsStream("/validation/invalid_api_gw_in_event.json");
80+
String invalidEvent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
81+
7782
// WHEN
78-
InvocationResult invocationResult = invokeFunction(functionName, objectMapper.writeValueAsString(validEvent));
83+
InvocationResult invocationResult = invokeFunction(functionName, invalidEvent);
7984

8085
// THEN
8186
// invocation should fail inbound validation and return 400
8287
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
8388
assertThat(validJsonNode.get("statusCode").asInt()).isEqualTo(400);
8489
assertThat(validJsonNode.get("body").asText()).contains("$.price: is missing but it is required");
8590
}
86-
87-
@ParameterizedTest
88-
@Event(value = "/validation/invalid_api_gw_out_event.json", type = APIGatewayProxyRequestEvent.class)
89-
void test_invalidOutboundApiGWEvent(APIGatewayProxyRequestEvent validEvent) throws IOException {
91+
92+
@Test
93+
void test_invalidOutboundApiGWEvent() throws IOException {
94+
InputStream inputStream = this.getClass().getResourceAsStream("/validation/invalid_api_gw_out_event.json");
95+
String invalidEvent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
96+
9097
// WHEN
91-
InvocationResult invocationResult = invokeFunction(functionName, objectMapper.writeValueAsString(validEvent));
98+
InvocationResult invocationResult = invokeFunction(functionName, invalidEvent);
9299

93100
// THEN
94101
// invocation should fail outbound validation and return 400
95102
JsonNode validJsonNode = objectMapper.readTree(invocationResult.getResult());
96103
assertThat(validJsonNode.get("statusCode").asInt()).isEqualTo(400);
97-
assertThat(validJsonNode.get("body").asText()).contains("$.price: must have an exclusive maximum value of 1000");
104+
assertThat(validJsonNode.get("body").asText())
105+
.contains("$.price: must have an exclusive maximum value of 1000");
98106
}
99107
}

0 commit comments

Comments
 (0)