use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project aws-java-serverless by hermanlintvelt.
the class GetExpensesHandlerTest method testGetExpenses.
@Test
@DisplayName("Test that request to function returns list of expenses")
void testGetExpenses() {
APIGatewayProxyResponseEvent result = testHandler.handleRequest(new APIGatewayProxyRequestEvent(), testContext);
assertThat(result.getStatusCode()).isEqualTo(200);
try {
List<Expense> expenses = OBJECT_MAPPER.readValue(result.getBody(), List.class);
assertThat(expenses).asList().isNotEmpty();
assertThat(expenses).asList().size().isGreaterThanOrEqualTo(3);
} catch (JsonProcessingException e) {
fail("did not expect json error");
}
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project aws-lambda-powertools-java by awslabs.
the class BasePersistenceStoreTest method deleteRecord_cacheEnabled_shouldDeleteRecordFromCache.
@Test
public void deleteRecord_cacheEnabled_shouldDeleteRecordFromCache() {
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
persistenceStore.configure(IdempotencyConfig.builder().withUseLocalCache(true).build(), null, cache);
cache.put("testFunction#47261bd5b456f400f8d191cfb3a7482f", new DataRecord("testFunction#47261bd5b456f400f8d191cfb3a7482f", DataRecord.Status.COMPLETED, 123, null, null));
persistenceStore.deleteRecord(JsonConfig.get().getObjectMapper().valueToTree(event), new ArithmeticException());
assertThat(status).isEqualTo(3);
assertThat(cache).isEmpty();
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project aws-lambda-powertools-java by awslabs.
the class BasePersistenceStoreTest method saveInProgress_withLocalCache_NotExpired_ShouldThrowException.
@Test
public void saveInProgress_withLocalCache_NotExpired_ShouldThrowException() {
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
persistenceStore.configure(IdempotencyConfig.builder().withUseLocalCache(true).withEventKeyJMESPath("powertools_json(body).id").build(), null, cache);
Instant now = Instant.now();
cache.put("testFunction#2fef178cc82be5ce3da6c5e0466a6182", new DataRecord("testFunction#2fef178cc82be5ce3da6c5e0466a6182", DataRecord.Status.INPROGRESS, now.plus(3600, ChronoUnit.SECONDS).getEpochSecond(), null, null));
assertThatThrownBy(() -> persistenceStore.saveInProgress(JsonConfig.get().getObjectMapper().valueToTree(event), now)).isInstanceOf(IdempotencyItemAlreadyExistsException.class);
assertThat(status).isEqualTo(-1);
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project aws-lambda-powertools-java by awslabs.
the class BasePersistenceStoreTest method getRecord_cacheEnabledExpired_shouldReturnRecordFromPersistence.
@Test
public void getRecord_cacheEnabledExpired_shouldReturnRecordFromPersistence() throws IdempotencyItemNotFoundException, IdempotencyValidationException {
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
LRUCache<String, DataRecord> cache = new LRUCache<>(2);
persistenceStore.configure(IdempotencyConfig.builder().withUseLocalCache(true).build(), "myfunc", cache);
Instant now = Instant.now();
DataRecord dr = new DataRecord("testFunction.myfunc#47261bd5b456f400f8d191cfb3a7482f", DataRecord.Status.COMPLETED, now.minus(3, ChronoUnit.SECONDS).getEpochSecond(), "result of the function", null);
cache.put("testFunction.myfunc#47261bd5b456f400f8d191cfb3a7482f", dr);
DataRecord record = persistenceStore.getRecord(JsonConfig.get().getObjectMapper().valueToTree(event), now);
assertThat(record.getIdempotencyKey()).isEqualTo("testFunction.myfunc#47261bd5b456f400f8d191cfb3a7482f");
assertThat(record.getStatus()).isEqualTo(DataRecord.Status.INPROGRESS);
assertThat(record.getResponseData()).isEqualTo("Response");
assertThat(status).isEqualTo(0);
assertThat(cache).isEmpty();
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project aws-lambda-powertools-java by awslabs.
the class BasePersistenceStoreTest method getRecord_invalidPayload_shouldThrowValidationException.
@Test
public void getRecord_invalidPayload_shouldThrowValidationException() {
APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
persistenceStore.configure(IdempotencyConfig.builder().withEventKeyJMESPath("powertools_json(body).id").withPayloadValidationJMESPath("powertools_json(body).message").build(), "myfunc");
// "Lambda rocks" ==> 70c24d88041893f7fbab4105b76fd9e1
this.validationHash = "different hash";
assertThatThrownBy(() -> persistenceStore.getRecord(JsonConfig.get().getObjectMapper().valueToTree(event), Instant.now())).isInstanceOf(IdempotencyValidationException.class);
}
Aggregations