Search in sources :

Example 1 with LRUCache

use of software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache 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 2 with LRUCache

use of software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache 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 3 with LRUCache

use of software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache 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 4 with LRUCache

use of software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache in project aws-lambda-powertools-java by awslabs.

the class BasePersistenceStoreTest method saveSuccess_shouldUpdateRecord.

// </editor-fold>
// =================================================================
// =================================================================
// <editor-fold desc="saveSuccess">
@Test
public void saveSuccess_shouldUpdateRecord() throws JsonProcessingException {
    APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
    LRUCache<String, DataRecord> cache = new LRUCache<>(2);
    persistenceStore.configure(IdempotencyConfig.builder().build(), null, cache);
    Product product = new Product(34543, "product", 42);
    Instant now = Instant.now();
    persistenceStore.saveSuccess(JsonConfig.get().getObjectMapper().valueToTree(event), product, now);
    assertThat(dr.getStatus()).isEqualTo(DataRecord.Status.COMPLETED);
    assertThat(dr.getExpiryTimestamp()).isEqualTo(now.plus(3600, ChronoUnit.SECONDS).getEpochSecond());
    assertThat(dr.getResponseData()).isEqualTo(JsonConfig.get().getObjectMapper().writeValueAsString(product));
    assertThat(dr.getIdempotencyKey()).isEqualTo("testFunction#47261bd5b456f400f8d191cfb3a7482f");
    assertThat(dr.getPayloadHash()).isEqualTo("");
    assertThat(status).isEqualTo(2);
    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) Product(software.amazon.lambda.powertools.idempotency.model.Product) Test(org.junit.jupiter.api.Test)

Example 5 with LRUCache

use of software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache in project aws-lambda-powertools-java by awslabs.

the class BasePersistenceStoreTest method getRecord_shouldReturnRecordFromPersistence.

// </editor-fold>
// =================================================================
// =================================================================
// <editor-fold desc="getRecord">
@Test
public void getRecord_shouldReturnRecordFromPersistence() throws IdempotencyItemNotFoundException, IdempotencyValidationException {
    APIGatewayProxyRequestEvent event = EventLoader.loadApiGatewayRestEvent("apigw_event.json");
    LRUCache<String, DataRecord> cache = new LRUCache<>(2);
    persistenceStore.configure(IdempotencyConfig.builder().build(), "myfunc", cache);
    Instant now = Instant.now();
    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);
}
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)

Aggregations

APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)8 Test (org.junit.jupiter.api.Test)8 LRUCache (software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache)8 Instant (java.time.Instant)7 Product (software.amazon.lambda.powertools.idempotency.model.Product)2