Search in sources :

Example 1 with BatchGetItemRequest

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

the class DynamoServiceIntegrationTest method testRequestEntityTooLargeErrorHandling.

/**
 * Tests that we properly handle error responses for request entities that
 * are too large.
 */
// DISABLED because DynamoDB apparently upped their max request size; we
// should be hitting this with a unit test that simulates an appropriate
// SdkServiceException.
// @Test
public void testRequestEntityTooLargeErrorHandling() throws Exception {
    Map<String, KeysAndAttributes> items = new HashMap<String, KeysAndAttributes>();
    for (int i = 0; i < 1024; i++) {
        KeysAndAttributes kaa = KeysAndAttributes.builder().build();
        StringBuilder bigString = new StringBuilder();
        for (int j = 0; j < 1024; j++) {
            bigString.append("a");
        }
        bigString.append(i);
        items.put(bigString.toString(), kaa);
    }
    BatchGetItemRequest request = BatchGetItemRequest.builder().requestItems(items).build();
    try {
        dynamo.batchGetItem(request);
    } catch (AwsServiceException exception) {
        assertNotNull(exception.getMessage());
        assertEquals("Request entity too large", exception.awsErrorDetails().errorCode());
        assertEquals(413, exception.statusCode());
    }
}
Also used : BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) HashMap(java.util.HashMap) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes)

Example 2 with BatchGetItemRequest

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

the class BatchGetItemOperationTest method getServiceCall_usingShortcutForm_makesTheRightCallAndReturnsResponse.

@Test
public void getServiceCall_usingShortcutForm_makesTheRightCallAndReturnsResponse() {
    BatchGetItemEnhancedRequest batchGetItemEnhancedRequest = BatchGetItemEnhancedRequest.builder().readBatches(ReadBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTable).addGetItem(FAKE_ITEM_KEYS.get(0)).build()).build();
    BatchGetItemOperation operation = BatchGetItemOperation.create(batchGetItemEnhancedRequest);
    BatchGetItemRequest batchGetItemRequest = BatchGetItemRequest.builder().requestItems(singletonMap("test-table", KeysAndAttributes.builder().keys(singletonList(FAKE_ITEM_MAPS.get(0))).build())).build();
    BatchGetItemIterable expectedResponse = mock(BatchGetItemIterable.class);
    when(mockDynamoDbClient.batchGetItemPaginator(any(BatchGetItemRequest.class))).thenReturn(expectedResponse);
    SdkIterable<BatchGetItemResponse> response = operation.serviceCall(mockDynamoDbClient).apply(batchGetItemRequest);
    assertThat(response, sameInstance(expectedResponse));
    verify(mockDynamoDbClient).batchGetItemPaginator(batchGetItemRequest);
}
Also used : BatchGetItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) BatchGetItemIterable(software.amazon.awssdk.services.dynamodb.paginators.BatchGetItemIterable) 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 BatchGetItemRequest

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

the class BatchGetItemOperationTest method generateRequest_multipleBatches_multipleTableSchemas.

@Test
public void generateRequest_multipleBatches_multipleTableSchemas() {
    BatchGetItemEnhancedRequest batchGetItemEnhancedRequest = BatchGetItemEnhancedRequest.builder().readBatches(ReadBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTable).addGetItem(r -> r.key(FAKE_ITEM_KEYS.get(0))).addGetItem(r -> r.key(FAKE_ITEM_KEYS.get(1))).addGetItem(r -> r.key(FAKE_ITEM_KEYS.get(2))).build(), ReadBatch.builder(FakeItemWithSort.class).mappedTableResource(fakeItemWithSortMappedTable).addGetItem(r -> r.key(FAKESORT_ITEM_KEYS.get(0))).addGetItem(r -> r.key(FAKESORT_ITEM_KEYS.get(1))).addGetItem(r -> r.key(FAKESORT_ITEM_KEYS.get(2))).build()).build();
    BatchGetItemOperation operation = BatchGetItemOperation.create(batchGetItemEnhancedRequest);
    BatchGetItemRequest batchGetItemRequest = operation.generateRequest(mockExtension);
    KeysAndAttributes keysAndAttributes1 = batchGetItemRequest.requestItems().get(TABLE_NAME);
    KeysAndAttributes keysAndAttributes2 = batchGetItemRequest.requestItems().get(TABLE_NAME_2);
    assertThat(keysAndAttributes1.keys(), containsInAnyOrder(FAKE_ITEM_MAPS.subList(0, 3).toArray()));
    assertThat(keysAndAttributes2.keys(), containsInAnyOrder(FAKESORT_ITEM_MAPS.subList(0, 3).toArray()));
    assertThat(keysAndAttributes1.consistentRead(), is(nullValue()));
    assertThat(keysAndAttributes2.consistentRead(), is(nullValue()));
    verifyNoMoreInteractions(mockExtension);
}
Also used : BatchGetItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) Arrays(java.util.Arrays) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) BatchGetItemIterable(software.amazon.awssdk.services.dynamodb.paginators.BatchGetItemIterable) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) Mockito.doReturn(org.mockito.Mockito.doReturn) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Collections.emptyList(java.util.Collections.emptyList) SdkIterable(software.amazon.awssdk.core.pagination.sync.SdkIterable) Key(software.amazon.awssdk.enhanced.dynamodb.Key) ReadModification(software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification) List(java.util.List) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) FakeItemWithSort.createUniqueFakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort.createUniqueFakeItemWithSort) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Matchers.is(org.hamcrest.Matchers.is) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) ReadBatch(software.amazon.awssdk.enhanced.dynamodb.model.ReadBatch) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) FakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort) HashMap(java.util.HashMap) BatchGetItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) Collections.singletonMap(java.util.Collections.singletonMap) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.empty(org.hamcrest.Matchers.empty) DynamoDbTable(software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable) GetItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.GetItemEnhancedRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BatchGetResultPage(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetResultPage) Mockito.verify(org.mockito.Mockito.verify) Collectors.toList(java.util.stream.Collectors.toList) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Collections(java.util.Collections) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) FakeItemWithSort.createUniqueFakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort.createUniqueFakeItemWithSort) FakeItemWithSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithSort) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) 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 BatchGetItemRequest

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

the class DynamoServiceIntegrationTest method testNullQueryKeyErrorHandling.

@Test
@SuppressWarnings("unchecked")
public void testNullQueryKeyErrorHandling() {
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    // Put a valid item first
    item.put(HASH_KEY_NAME, AttributeValue.builder().s("bar").build());
    item.put("age", AttributeValue.builder().s("30").build());
    PutItemRequest putItemRequest = PutItemRequest.builder().tableName(tableName).item(item).returnValues(ReturnValue.ALL_OLD.toString()).build();
    dynamo.putItem(putItemRequest);
    Map<String, KeysAndAttributes> items = new HashMap<String, KeysAndAttributes>();
    // Put a valid key and a null one
    items.put(tableName, KeysAndAttributes.builder().keys(mapKey(HASH_KEY_NAME, AttributeValue.builder().s("bar").build()), null).build());
    BatchGetItemRequest request = BatchGetItemRequest.builder().requestItems(items).build();
    try {
        dynamo.batchGetItem(request);
    } catch (AwsServiceException exception) {
        assertEquals("ValidationException", exception.awsErrorDetails().errorCode());
    }
    Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>();
    List<WriteRequest> writeRequests = new ArrayList<WriteRequest>();
    Map<String, AttributeValue> writeAttributes = new HashMap<String, AttributeValue>();
    writeAttributes.put(HASH_KEY_NAME, AttributeValue.builder().s("" + System.currentTimeMillis()).build());
    writeAttributes.put("bar", AttributeValue.builder().s("" + System.currentTimeMillis()).build());
    writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(writeAttributes).build()).build());
    writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(null).build()).build());
    requestItems.put(tableName, writeRequests);
    try {
        dynamo.batchWriteItem(BatchWriteItemRequest.builder().requestItems(requestItems).build());
    } catch (AwsServiceException exception) {
        assertEquals("ValidationException", exception.awsErrorDetails().errorCode());
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) WriteRequest(software.amazon.awssdk.services.dynamodb.model.WriteRequest) ArrayList(java.util.ArrayList) KeysAndAttributes(software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) AwsServiceException(software.amazon.awssdk.awscore.exception.AwsServiceException) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with BatchGetItemRequest

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

the class BatchGetItemOperationTest method getServiceCall_usingKeyItemForm_makesTheRightCallAndReturnsResponse.

@Test
public void getServiceCall_usingKeyItemForm_makesTheRightCallAndReturnsResponse() {
    BatchGetItemEnhancedRequest batchGetItemEnhancedRequest = BatchGetItemEnhancedRequest.builder().readBatches(ReadBatch.builder(FakeItem.class).mappedTableResource(fakeItemMappedTable).addGetItem(FAKE_ITEMS.get(0)).build()).build();
    BatchGetItemOperation operation = BatchGetItemOperation.create(batchGetItemEnhancedRequest);
    BatchGetItemRequest batchGetItemRequest = BatchGetItemRequest.builder().requestItems(singletonMap("test-table", KeysAndAttributes.builder().keys(singletonList(FAKE_ITEM_MAPS.get(0))).build())).build();
    BatchGetItemIterable expectedResponse = mock(BatchGetItemIterable.class);
    when(mockDynamoDbClient.batchGetItemPaginator(any(BatchGetItemRequest.class))).thenReturn(expectedResponse);
    SdkIterable<BatchGetItemResponse> response = operation.serviceCall(mockDynamoDbClient).apply(batchGetItemRequest);
    assertThat(response, sameInstance(expectedResponse));
    verify(mockDynamoDbClient).batchGetItemPaginator(batchGetItemRequest);
}
Also used : BatchGetItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest) BatchGetItemRequest(software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest) BatchGetItemResponse(software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse) BatchGetItemIterable(software.amazon.awssdk.services.dynamodb.paginators.BatchGetItemIterable) FakeItem.createUniqueFakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Aggregations

BatchGetItemRequest (software.amazon.awssdk.services.dynamodb.model.BatchGetItemRequest)8 KeysAndAttributes (software.amazon.awssdk.services.dynamodb.model.KeysAndAttributes)6 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 BatchGetItemResponse (software.amazon.awssdk.services.dynamodb.model.BatchGetItemResponse)5 List (java.util.List)4 FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)4 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)4 BatchGetItemEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.BatchGetItemEnhancedRequest)4 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Map (java.util.Map)3 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)3 SiteIdKeyGenerator.createDatabaseKey (com.formkiq.stacks.dynamodb.SiteIdKeyGenerator.createDatabaseKey)2 Collection (java.util.Collection)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)2 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)2 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)2