Search in sources :

Example 1 with BatchWriteResult

use of software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult in project aws-sdk-java-v2 by aws.

the class BatchWriteItemOperationTest method transformResults_multipleUnprocessedOperations_extensionTransformsPutsNotDeletes.

@Test
public void transformResults_multipleUnprocessedOperations_extensionTransformsPutsNotDeletes() {
    BatchWriteItemOperation operation = BatchWriteItemOperation.create(emptyRequest());
    List<WriteRequest> writeRequests1 = Arrays.asList(putRequest(FAKE_ITEM_MAPS.get(0)), deleteRequest(FAKE_ITEM_MAPS.get(1)), deleteRequest(FAKE_ITEM_MAPS.get(2)));
    List<WriteRequest> writeRequests2 = Arrays.asList(deleteRequest(FAKESORT_ITEM_MAPS.get(0)), putRequest(FAKESORT_ITEM_MAPS.get(1)), putRequest(FAKESORT_ITEM_MAPS.get(2)));
    Map<String, List<WriteRequest>> writeRequests = new HashMap<>();
    writeRequests.put(TABLE_NAME, writeRequests1);
    writeRequests.put(TABLE_NAME_2, writeRequests2);
    BatchWriteItemResponse response = BatchWriteItemResponse.builder().unprocessedItems(writeRequests).build();
    // Use the mock extension to transform every item based on table name
    IntStream.range(0, 3).forEach(i -> {
        doReturn(ReadModification.builder().transformedItem(FAKE_ITEM_MAPS.get(i + 3)).build()).when(mockExtension).afterRead(argThat(extensionContext -> extensionContext.operationContext().tableName().equals(TABLE_NAME) && extensionContext.items().equals(FAKE_ITEM_MAPS.get(i))));
        doReturn(ReadModification.builder().transformedItem(FAKESORT_ITEM_MAPS.get(i + 3)).build()).when(mockExtension).afterRead(argThat(extensionContext -> extensionContext.operationContext().tableName().equals(TABLE_NAME_2) && extensionContext.items().equals(FAKESORT_ITEM_MAPS.get(i))));
    });
    BatchWriteResult results = operation.transformResponse(response, mockExtension);
    assertThat(results.unprocessedDeleteItemsForTable(fakeItemMappedTableWithExtension), containsInAnyOrder(FAKE_ITEM_KEYS.get(1), FAKE_ITEM_KEYS.get(2)));
    assertThat(results.unprocessedPutItemsForTable(fakeItemMappedTableWithExtension), containsInAnyOrder(FAKE_ITEMS.get(3)));
    assertThat(results.unprocessedDeleteItemsForTable(fakeItemWithSortMappedTableWithExtension), containsInAnyOrder(FAKESORT_ITEM_KEYS.get(0)));
    assertThat(results.unprocessedPutItemsForTable(fakeItemWithSortMappedTableWithExtension), containsInAnyOrder(FAKESORT_ITEMS.get(4), FAKESORT_ITEMS.get(5)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) BatchWriteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest) RunWith(org.junit.runner.RunWith) FakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort) HashMap(java.util.HashMap) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) Mockito.lenient(org.mockito.Mockito.lenient) Collections.singletonList(java.util.Collections.singletonList) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) Map(java.util.Map) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) Collections.singletonMap(java.util.Collections.singletonMap) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PutRequest(software.amazon.awssdk.services.dynamodb.model.PutRequest) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) DeleteRequest(software.amazon.awssdk.services.dynamodb.model.DeleteRequest) Collections.emptyMap(java.util.Collections.emptyMap) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) Collections.emptyList(java.util.Collections.emptyList) BatchWriteItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemRequest) Key(software.amazon.awssdk.enhanced.dynamodb.Key) ReadModification(software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WriteModification(software.amazon.awssdk.enhanced.dynamodb.extensions.WriteModification) Mockito.verify(org.mockito.Mockito.verify) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) BatchWriteResult(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult) FakeItemWithSort.createUniqueFakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort.createUniqueFakeItemWithSort) WriteBatch(software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Matchers.is(org.hamcrest.Matchers.is) DynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) BatchWriteResult(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult) HashMap(java.util.HashMap) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) Test(org.junit.Test)

Example 2 with BatchWriteResult

use of software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult in project aws-sdk-java-v2 by aws.

the class BatchWriteItemOperationTest method transformResults_noUnprocessedOperations.

@Test
public void transformResults_noUnprocessedOperations() {
    BatchWriteItemOperation operation = BatchWriteItemOperation.create(emptyRequest());
    BatchWriteItemResponse response = BatchWriteItemResponse.builder().unprocessedItems(emptyMap()).build();
    BatchWriteResult results = operation.transformResponse(response, mockExtension);
    assertThat(results.unprocessedDeleteItemsForTable(fakeItemMappedTable), is(emptyList()));
    assertThat(results.unprocessedPutItemsForTable(fakeItemMappedTable), is(emptyList()));
}
Also used : BatchWriteResult(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) Test(org.junit.Test)

Example 3 with BatchWriteResult

use of software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult in project aws-sdk-java-v2 by aws.

the class BatchWriteItemOperationTest method transformResults_multipleUnprocessedOperations.

@Test
public void transformResults_multipleUnprocessedOperations() {
    BatchWriteItemOperation operation = BatchWriteItemOperation.create(emptyRequest());
    List<WriteRequest> writeRequests1 = Arrays.asList(putRequest(FAKE_ITEM_MAPS.get(0)), deleteRequest(FAKE_ITEM_MAPS.get(1)), deleteRequest(FAKE_ITEM_MAPS.get(2)));
    List<WriteRequest> writeRequests2 = Arrays.asList(deleteRequest(FAKESORT_ITEM_MAPS.get(0)), putRequest(FAKESORT_ITEM_MAPS.get(1)), putRequest(FAKESORT_ITEM_MAPS.get(2)));
    Map<String, List<WriteRequest>> writeRequests = new HashMap<>();
    writeRequests.put(TABLE_NAME, writeRequests1);
    writeRequests.put(TABLE_NAME_2, writeRequests2);
    BatchWriteItemResponse response = BatchWriteItemResponse.builder().unprocessedItems(writeRequests).build();
    BatchWriteResult results = operation.transformResponse(response, mockExtension);
    assertThat(results.unprocessedDeleteItemsForTable(fakeItemMappedTableWithExtension), containsInAnyOrder(FAKE_ITEM_KEYS.get(1), FAKE_ITEM_KEYS.get(2)));
    assertThat(results.unprocessedPutItemsForTable(fakeItemMappedTableWithExtension), containsInAnyOrder(FAKE_ITEMS.get(0)));
    assertThat(results.unprocessedDeleteItemsForTable(fakeItemWithSortMappedTableWithExtension), containsInAnyOrder(FAKESORT_ITEM_KEYS.get(0)));
    assertThat(results.unprocessedPutItemsForTable(fakeItemWithSortMappedTableWithExtension), containsInAnyOrder(FAKESORT_ITEMS.get(1), FAKESORT_ITEMS.get(2)));
}
Also used : BatchWriteResult(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult) HashMap(java.util.HashMap) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) BatchWriteItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 BatchWriteResult (software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteResult)3 BatchWriteItemResponse (software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse)3 Collections.emptyList (java.util.Collections.emptyList)2 Collections.singletonList (java.util.Collections.singletonList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 WriteRequest (software.amazon.awssdk.services.dynamodb.model.WriteRequest)2 Arrays (java.util.Arrays)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Map (java.util.Map)1 IntStream (java.util.stream.IntStream)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)1 Before (org.junit.Before)1 RunWith (org.junit.runner.RunWith)1