Search in sources :

Example 1 with DeleteRequest

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

the class WriteBatchTest method builder_maximal_consumer_style.

@Test
public void builder_maximal_consumer_style() {
    FakeItem fakeItem = createUniqueFakeItem();
    WriteBatch builtObject = WriteBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTable).addPutItem(r -> r.item(fakeItem)).addDeleteItem(r -> r.key(k -> k.partitionValue(fakeItem.getId()))).build();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, FakeItem.getTableMetadata().primaryKeys());
    assertThat(builtObject.tableName(), is(TABLE_NAME));
    assertThat(builtObject.writeRequests(), containsInAnyOrder(putRequest(fakeItemMap), deleteRequest(fakeItemMap)));
}
Also used : DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) Matchers.is(org.hamcrest.Matchers.is) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PutRequest(software.amazon.awssdk.services.dynamodb.model.PutRequest) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Before(org.junit.Before) DeleteRequest(software.amazon.awssdk.services.dynamodb.model.DeleteRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 2 with DeleteRequest

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

the class BatchWriteItemOperationTest method generateRequest_multipleTables_extensionOnlyTransformsPutsAndNotDeletes.

@Test
public void generateRequest_multipleTables_extensionOnlyTransformsPutsAndNotDeletes() {
    // Use the mock extension to transform every item based on table name
    IntStream.range(0, 3).forEach(i -> {
        lenient().doReturn(WriteModification.builder().transformedItem(FAKE_ITEM_MAPS.get(i + 3)).build()).when(mockExtension).beforeWrite(argThat(extensionContext -> extensionContext.operationContext().tableName().equals(TABLE_NAME) && extensionContext.items().equals(FAKE_ITEM_MAPS.get(i))));
        lenient().doReturn(WriteModification.builder().transformedItem(FAKESORT_ITEM_MAPS.get(i + 3)).build()).when(mockExtension).beforeWrite(argThat(extensionContext -> extensionContext.operationContext().tableName().equals(TABLE_NAME_2) && extensionContext.items().equals(FAKESORT_ITEM_MAPS.get(i))));
    });
    BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder().writeBatches(WriteBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTableWithExtension).addPutItem(r -> r.item(FAKE_ITEMS.get(0))).addDeleteItem(r -> r.key(FAKE_ITEM_KEYS.get(1))).addPutItem(r -> r.item(FAKE_ITEMS.get(2))).build(), WriteBatch.builder(FakeItemWithSort.class).mappedTableResource(fakeItemWithSortMappedTableWithExtension).addDeleteItem(r -> r.key(FAKESORT_ITEM_KEYS.get(0))).addPutItem(r -> r.item(FAKESORT_ITEMS.get(1))).addDeleteItem(r -> r.key(FAKESORT_ITEM_KEYS.get(2))).build()).build();
    BatchWriteItemOperation operation = BatchWriteItemOperation.create(batchWriteItemEnhancedRequest);
    BatchWriteItemRequest request = operation.generateRequest(mockExtension);
    List<WriteRequest> writeRequests1 = request.requestItems().get(TABLE_NAME);
    List<WriteRequest> writeRequests2 = request.requestItems().get(TABLE_NAME_2);
    // Only PutItem requests should have their attributes transformed
    assertThat(writeRequests1, containsInAnyOrder(putRequest(FAKE_ITEM_MAPS.get(3)), deleteRequest(FAKE_ITEM_MAPS.get(1)), putRequest(FAKE_ITEM_MAPS.get(5))));
    assertThat(writeRequests2, containsInAnyOrder(deleteRequest(FAKESORT_ITEM_MAPS.get(0)), putRequest(FAKESORT_ITEM_MAPS.get(4)), deleteRequest(FAKESORT_ITEM_MAPS.get(2))));
}
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) FakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort) FakeItemWithSort.createUniqueFakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort.createUniqueFakeItemWithSort) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) BatchWriteItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest) BatchWriteItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchWriteItemRequest) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 3 with DeleteRequest

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

the class WriteBatchTest method builder_maximal_builder_style.

@Test
public void builder_maximal_builder_style() {
    FakeItem fakeItem = createUniqueFakeItem();
    PutItemEnhancedRequest<FakeItem> putItem = PutItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build();
    DeleteItemEnhancedRequest deleteItem = DeleteItemEnhancedRequest.builder().key(k -> k.partitionValue(fakeItem.getId())).build();
    WriteBatch builtObject = WriteBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTable).addPutItem(putItem).addDeleteItem(deleteItem).build();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, FakeItem.getTableMetadata().primaryKeys());
    assertThat(builtObject.tableName(), is(TABLE_NAME));
    assertThat(builtObject.writeRequests(), containsInAnyOrder(putRequest(fakeItemMap), deleteRequest(fakeItemMap)));
}
Also used : DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) Matchers.is(org.hamcrest.Matchers.is) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PutRequest(software.amazon.awssdk.services.dynamodb.model.PutRequest) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Before(org.junit.Before) DeleteRequest(software.amazon.awssdk.services.dynamodb.model.DeleteRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 4 with DeleteRequest

use of software.amazon.awssdk.services.dynamodb.model.DeleteRequest 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)

Aggregations

Map (java.util.Map)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)4 Matchers.is (org.hamcrest.Matchers.is)4 Before (org.junit.Before)4 Test (org.junit.Test)4 RunWith (org.junit.runner.RunWith)4 Mock (org.mockito.Mock)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)4 DynamoDbTable (software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable)4 FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)4 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)4 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)4 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)4 DeleteRequest (software.amazon.awssdk.services.dynamodb.model.DeleteRequest)4 PutRequest (software.amazon.awssdk.services.dynamodb.model.PutRequest)4 WriteRequest (software.amazon.awssdk.services.dynamodb.model.WriteRequest)4 Arrays (java.util.Arrays)2 Collections.emptyList (java.util.Collections.emptyList)2