Search in sources :

Example 16 with APIGatewayProxyRequestEvent

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");
    }
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Expense(com.example.expenses.model.Expense) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 17 with APIGatewayProxyRequestEvent

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();
}
Also used : LRUCache(software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Test(org.junit.jupiter.api.Test)

Example 18 with APIGatewayProxyRequestEvent

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);
}
Also used : LRUCache(software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 19 with APIGatewayProxyRequestEvent

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();
}
Also used : LRUCache(software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test)

Example 20 with APIGatewayProxyRequestEvent

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);
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Test(org.junit.jupiter.api.Test)

Aggregations

APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)239 Test (org.junit.jupiter.api.Test)217 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)182 HashMap (java.util.HashMap)70 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)33 ErrorObject (com.nimbusds.oauth2.sdk.ErrorObject)29 NotifyRequest (uk.gov.di.authentication.shared.entity.NotifyRequest)17 URI (java.net.URI)15 Map (java.util.Map)15 UserProfile (uk.gov.di.authentication.shared.entity.UserProfile)14 Context (com.amazonaws.services.lambda.runtime.Context)13 NotifyRequest (uk.gov.di.accountmanagement.entity.NotifyRequest)13 Subject (com.nimbusds.oauth2.sdk.id.Subject)12 Instant (java.time.Instant)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 ClientID (com.nimbusds.oauth2.sdk.id.ClientID)10 AuthenticationRequest (com.nimbusds.openid.connect.sdk.AuthenticationRequest)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 JWSObject (com.nimbusds.jose.JWSObject)8