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();
}
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));
}
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);
}
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);
}
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));
}
Aggregations