Search in sources :

Example 1 with FakeItem

use of software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem in project aws-sdk-java-v2 by aws.

the class UpdateItemOperationTest method generateRequest_withExtension_modifiesUpdateExpression.

@Test
public void generateRequest_withExtension_modifiesUpdateExpression() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> baseMap = new HashMap<>(FakeItem.getTableSchema().itemToMap(fakeItem, true));
    Map<String, AttributeValue> fakeMap = new HashMap<>(baseMap);
    fakeMap.put("subclass_attribute", AttributeValue.builder().s("1").build());
    when(mockDynamoDbEnhancedClientExtension.beforeWrite(any(DynamoDbExtensionContext.BeforeWrite.class))).thenReturn(WriteModification.builder().transformedItem(fakeMap).build());
    UpdateItemOperation<FakeItem> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).ignoreNulls(true).build());
    UpdateItemRequest request = updateItemOperation.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, mockDynamoDbEnhancedClientExtension);
    assertThat(request.updateExpression(), is("SET " + SUBCLASS_ATTRIBUTE_NAME + " = " + SUBCLASS_ATTRIBUTE_VALUE));
    assertThat(request.expressionAttributeValues(), hasEntry(SUBCLASS_ATTRIBUTE_VALUE, AttributeValue.builder().s("1").build()));
    assertThat(request.expressionAttributeNames(), hasEntry(SUBCLASS_ATTRIBUTE_NAME, "subclass_attribute"));
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 FakeItem

use of software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem in project aws-sdk-java-v2 by aws.

the class UpdateItemOperationTest method generateTransactWriteItem_basicRequest.

@Test
public void generateTransactWriteItem_basicRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
    doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
    TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
    TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 FakeItem

use of software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem in project aws-sdk-java-v2 by aws.

the class UpdateItemOperationTest method transformResponse_mapsAttributesReturnedInResponse.

@Test
public void transformResponse_mapsAttributesReturnedInResponse() {
    FakeItem fakeItem1 = FakeItem.createUniqueFakeItem();
    FakeItem fakeItem2 = FakeItem.createUniqueFakeItem();
    Map<String, AttributeValue> fakeItem2Attributes = FakeItem.getTableSchema().itemToMap(fakeItem2, true);
    UpdateItemOperation<FakeItem> updateItemOperation = UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem1).build());
    FakeItem result = updateItemOperation.transformResponse(UpdateItemResponse.builder().attributes(fakeItem2Attributes).build(), FakeItem.getTableSchema(), PRIMARY_CONTEXT, null).attributes();
    assertThat(result, is(fakeItem2));
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 FakeItem

use of software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem in project aws-sdk-java-v2 by aws.

the class UpdateItemOperationTest method generateTransactWriteItem_conditionalRequest.

@Test
public void generateTransactWriteItem_conditionalRequest() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    UpdateItemOperation<FakeItem> updateItemOperation = spy(UpdateItemOperation.create(UpdateItemEnhancedRequest.builder(FakeItem.class).item(fakeItem).build()));
    OperationContext context = DefaultOperationContext.create(TABLE_NAME, TableMetadata.primaryIndexName());
    String updateExpression = "update-expression";
    String conditionExpression = "condition-expression";
    Map<String, AttributeValue> attributeValues = Collections.singletonMap("key", stringValue("value1"));
    Map<String, String> attributeNames = Collections.singletonMap("key", "value2");
    UpdateItemRequest updateItemRequest = UpdateItemRequest.builder().tableName(TABLE_NAME).key(fakeItemMap).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeValues(attributeValues).expressionAttributeNames(attributeNames).build();
    doReturn(updateItemRequest).when(updateItemOperation).generateRequest(any(), any(), any());
    TransactWriteItem actualResult = updateItemOperation.generateTransactWriteItem(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
    TransactWriteItem expectedResult = TransactWriteItem.builder().update(Update.builder().key(fakeItemMap).tableName(TABLE_NAME).updateExpression(updateExpression).conditionExpression(conditionExpression).expressionAttributeNames(attributeNames).expressionAttributeValues(attributeValues).build()).build();
    assertThat(actualResult, is(expectedResult));
    verify(updateItemOperation).generateRequest(FakeItem.getTableSchema(), context, mockDynamoDbEnhancedClientExtension);
}
Also used : OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) TransactWriteItem(software.amazon.awssdk.services.dynamodb.model.TransactWriteItem) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 5 with FakeItem

use of software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem in project aws-sdk-java-v2 by aws.

the class VersionedRecordExtensionTest method beforeRead_doesNotTransformObject.

@Test
public void beforeRead_doesNotTransformObject() {
    FakeItem fakeItem = createUniqueFakeItem();
    Map<String, AttributeValue> fakeItemMap = FakeItem.getTableSchema().itemToMap(fakeItem, true);
    ReadModification result = versionedRecordExtension.afterRead(DefaultDynamoDbExtensionContext.builder().items(fakeItemMap).tableMetadata(FakeItem.getTableMetadata()).operationContext(PRIMARY_CONTEXT).build());
    assertThat(result, is(ReadModification.builder().build()));
}
Also used : 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)

Aggregations

FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)180 Test (org.junit.Test)165 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)153 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)99 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)59 HashMap (java.util.HashMap)57 Map (java.util.Map)48 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)43 Matchers.is (org.hamcrest.Matchers.is)43 RunWith (org.junit.runner.RunWith)43 Mock (org.mockito.Mock)43 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)43 OperationContext (software.amazon.awssdk.enhanced.dynamodb.OperationContext)41 Matchers.nullValue (org.hamcrest.Matchers.nullValue)39 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)36 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)35 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)34 Collections.singletonMap (java.util.Collections.singletonMap)33 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)31 Collections (java.util.Collections)30