Search in sources :

Example 1 with Product

use of software.amazon.lambda.powertools.idempotency.model.Product in project aws-lambda-powertools-java by awslabs.

the class IdempotencyAspectTest method secondCall_notExpired_shouldGetFromStore.

@Test
public void secondCall_notExpired_shouldGetFromStore() throws JsonProcessingException {
    // GIVEN
    Idempotency.config().withPersistenceStore(store).withConfig(IdempotencyConfig.builder().withEventKeyJMESPath("id").build()).configure();
    doThrow(IdempotencyItemAlreadyExistsException.class).when(store).saveInProgress(any(), any());
    Product p = new Product(42, "fake product", 12);
    Basket b = new Basket(p);
    DataRecord record = new DataRecord("42", DataRecord.Status.COMPLETED, Instant.now().plus(356, SECONDS).getEpochSecond(), JsonConfig.get().getObjectMapper().writer().writeValueAsString(b), null);
    doReturn(record).when(store).getRecord(any(), any());
    // WHEN
    IdempotencyEnabledFunction function = new IdempotencyEnabledFunction();
    Basket basket = function.handleRequest(p, context);
    // THEN
    assertThat(basket).isEqualTo(b);
    assertThat(function.handlerCalled()).isFalse();
}
Also used : Product(software.amazon.lambda.powertools.idempotency.model.Product) Basket(software.amazon.lambda.powertools.idempotency.model.Basket) DataRecord(software.amazon.lambda.powertools.idempotency.persistence.DataRecord) Test(org.junit.jupiter.api.Test)

Example 2 with Product

use of software.amazon.lambda.powertools.idempotency.model.Product in project aws-lambda-powertools-java by awslabs.

the class IdempotencyAspectTest method idempotencyOnSubMethodAnnotated_firstCall_shouldPutInStore.

@Test
public void idempotencyOnSubMethodAnnotated_firstCall_shouldPutInStore() {
    Idempotency.config().withPersistenceStore(store).configure();
    // WHEN
    IdempotencyInternalFunction function = new IdempotencyInternalFunction();
    Product p = new Product(42, "fake product", 12);
    Basket basket = function.handleRequest(p, context);
    // THEN
    assertThat(basket.getProducts()).hasSize(2);
    assertThat(function.subMethodCalled()).isTrue();
    ArgumentCaptor<JsonNode> nodeCaptor = ArgumentCaptor.forClass(JsonNode.class);
    verify(store).saveInProgress(nodeCaptor.capture(), any());
    assertThat(nodeCaptor.getValue().asText()).isEqualTo("fake");
    ArgumentCaptor<Basket> resultCaptor = ArgumentCaptor.forClass(Basket.class);
    verify(store).saveSuccess(any(), resultCaptor.capture(), any());
    assertThat(resultCaptor.getValue().getProducts()).contains(basket.getProducts().get(0), new Product(0, "fake", 0));
}
Also used : Product(software.amazon.lambda.powertools.idempotency.model.Product) Basket(software.amazon.lambda.powertools.idempotency.model.Basket) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 3 with Product

use of software.amazon.lambda.powertools.idempotency.model.Product in project aws-lambda-powertools-java by awslabs.

the class IdempotencyAspectTest method idempotencyOnSubMethodVoid_shouldThrowException.

@Test
public void idempotencyOnSubMethodVoid_shouldThrowException() {
    Idempotency.config().withPersistenceStore(store).withConfig(IdempotencyConfig.builder().build()).configure();
    // WHEN
    IdempotencyInternalFunctionVoid function = new IdempotencyInternalFunctionVoid();
    Product p = new Product(42, "fake product", 12);
    // THEN
    assertThatThrownBy(() -> function.handleRequest(p, context)).isInstanceOf(IdempotencyConfigurationException.class);
}
Also used : Product(software.amazon.lambda.powertools.idempotency.model.Product) Test(org.junit.jupiter.api.Test)

Example 4 with Product

use of software.amazon.lambda.powertools.idempotency.model.Product in project aws-lambda-powertools-java by awslabs.

the class IdempotencyAspectTest method firstCall_shouldPutInStore.

@Test
public void firstCall_shouldPutInStore() {
    Idempotency.config().withPersistenceStore(store).withConfig(IdempotencyConfig.builder().withEventKeyJMESPath("id").build()).configure();
    IdempotencyEnabledFunction function = new IdempotencyEnabledFunction();
    Product p = new Product(42, "fake product", 12);
    Basket basket = function.handleRequest(p, context);
    assertThat(basket.getProducts()).hasSize(1);
    assertThat(function.handlerCalled()).isTrue();
    ArgumentCaptor<JsonNode> nodeCaptor = ArgumentCaptor.forClass(JsonNode.class);
    verify(store).saveInProgress(nodeCaptor.capture(), any());
    assertThat(nodeCaptor.getValue().get("id").asLong()).isEqualTo(p.getId());
    assertThat(nodeCaptor.getValue().get("name").asText()).isEqualTo(p.getName());
    assertThat(nodeCaptor.getValue().get("price").asDouble()).isEqualTo(p.getPrice());
    ArgumentCaptor<Basket> resultCaptor = ArgumentCaptor.forClass(Basket.class);
    verify(store).saveSuccess(any(), resultCaptor.capture(), any());
    assertThat(resultCaptor.getValue()).isEqualTo(basket);
}
Also used : Product(software.amazon.lambda.powertools.idempotency.model.Product) Basket(software.amazon.lambda.powertools.idempotency.model.Basket) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 5 with Product

use of software.amazon.lambda.powertools.idempotency.model.Product in project aws-lambda-powertools-java by awslabs.

the class IdempotencyAspectTest method functionThrowException_shouldDeleteRecord_andThrowFunctionException.

@Test
public void functionThrowException_shouldDeleteRecord_andThrowFunctionException() {
    // GIVEN
    Idempotency.config().withPersistenceStore(store).withConfig(IdempotencyConfig.builder().withEventKeyJMESPath("id").build()).configure();
    // WHEN / THEN
    IdempotencyWithErrorFunction function = new IdempotencyWithErrorFunction();
    Product p = new Product(42, "fake product", 12);
    assertThatThrownBy(() -> function.handleRequest(p, context)).isInstanceOf(IndexOutOfBoundsException.class);
    verify(store).deleteRecord(any(), any(IndexOutOfBoundsException.class));
}
Also used : Product(software.amazon.lambda.powertools.idempotency.model.Product) Test(org.junit.jupiter.api.Test)

Aggregations

Product (software.amazon.lambda.powertools.idempotency.model.Product)15 Test (org.junit.jupiter.api.Test)13 Basket (software.amazon.lambda.powertools.idempotency.model.Basket)8 DataRecord (software.amazon.lambda.powertools.idempotency.persistence.DataRecord)4 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Instant (java.time.Instant)2 Idempotent (software.amazon.lambda.powertools.idempotency.Idempotent)2 LRUCache (software.amazon.lambda.powertools.idempotency.internal.cache.LRUCache)2 SetEnvironmentVariable (org.junitpioneer.jupiter.SetEnvironmentVariable)1 BasePersistenceStore (software.amazon.lambda.powertools.idempotency.persistence.BasePersistenceStore)1