Search in sources :

Example 1 with ReadModification

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

the class ScanOperationTest method scanItem_withExtension_correctlyTransformsItems.

@Test
public void scanItem_withExtension_correctlyTransformsItems() {
    List<FakeItem> scanResultItems = generateFakeItemList();
    List<FakeItem> modifiedResultItems = generateFakeItemList();
    List<Map<String, AttributeValue>> scanResultMaps = scanResultItems.stream().map(ScanOperationTest::getAttributeValueMap).collect(toList());
    ReadModification[] readModifications = modifiedResultItems.stream().map(ScanOperationTest::getAttributeValueMap).map(attributeMap -> ReadModification.builder().transformedItem(attributeMap).build()).collect(Collectors.toList()).toArray(new ReadModification[] {});
    when(mockDynamoDbEnhancedClientExtension.afterRead(any(DynamoDbExtensionContext.AfterRead.class))).thenReturn(readModifications[0], Arrays.copyOfRange(readModifications, 1, readModifications.length));
    ScanResponse scanResponse = generateFakeScanResults(scanResultMaps);
    Page<FakeItem> scanResultPage = scanOperation.transformResponse(scanResponse, FakeItem.getTableSchema(), PRIMARY_CONTEXT, mockDynamoDbEnhancedClientExtension);
    assertThat(scanResultPage.items(), is(modifiedResultItems));
    InOrder inOrder = Mockito.inOrder(mockDynamoDbEnhancedClientExtension);
    scanResultMaps.forEach(attributeMap -> inOrder.verify(mockDynamoDbEnhancedClientExtension).afterRead(DefaultDynamoDbExtensionContext.builder().tableMetadata(FakeItem.getTableMetadata()).operationContext(PRIMARY_CONTEXT).tableSchema(FakeItem.getTableSchema()).items(attributeMap).build()));
}
Also used : InOrder(org.mockito.InOrder) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) ReadModification(software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification) 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 ReadModification

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

the class ChainExtension method afterRead.

/**
 * Implementation of the {@link DynamoDbEnhancedClientExtension} interface that will call all the chained extensions
 * in reverse order, passing the results of each one to the next and coalescing the results into a single modification.
 *
 * @param context A {@link DynamoDbExtensionContext.AfterRead} context
 * @return A single {@link ReadModification} representing the final transformation of all the chained extensions.
 */
@Override
public ReadModification afterRead(DynamoDbExtensionContext.AfterRead context) {
    Map<String, AttributeValue> transformedItem = null;
    Iterator<DynamoDbEnhancedClientExtension> iterator = extensionChain.descendingIterator();
    while (iterator.hasNext()) {
        Map<String, AttributeValue> itemToTransform = transformedItem == null ? context.items() : transformedItem;
        DynamoDbExtensionContext.AfterRead afterRead = DefaultDynamoDbExtensionContext.builder().items(itemToTransform).operationContext(context.operationContext()).tableMetadata(context.tableMetadata()).tableSchema(context.tableSchema()).build();
        ReadModification readModification = iterator.next().afterRead(afterRead);
        if (readModification.transformedItem() != null) {
            transformedItem = readModification.transformedItem();
        }
    }
    return ReadModification.builder().transformedItem(transformedItem).build();
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbExtensionContext(software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext) DynamoDbEnhancedClientExtension(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension) ReadModification(software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification)

Example 3 with ReadModification

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

the class QueryOperationTest method queryItem_withExtension_correctlyTransformsItem.

@Test
public void queryItem_withExtension_correctlyTransformsItem() {
    List<FakeItem> queryResultItems = generateFakeItemList();
    List<FakeItem> modifiedResultItems = generateFakeItemList();
    List<Map<String, AttributeValue>> queryResultMap = queryResultItems.stream().map(QueryOperationTest::getAttributeValueMap).collect(toList());
    ReadModification[] readModifications = modifiedResultItems.stream().map(QueryOperationTest::getAttributeValueMap).map(attributeMap -> ReadModification.builder().transformedItem(attributeMap).build()).collect(Collectors.toList()).toArray(new ReadModification[] {});
    when(mockDynamoDbEnhancedClientExtension.afterRead(any(DynamoDbExtensionContext.AfterRead.class))).thenReturn(readModifications[0], Arrays.copyOfRange(readModifications, 1, readModifications.length));
    QueryResponse queryResponse = generateFakeQueryResults(queryResultMap);
    Page<FakeItem> queryResultPage = queryOperation.transformResponse(queryResponse, FakeItem.getTableSchema(), PRIMARY_CONTEXT, mockDynamoDbEnhancedClientExtension);
    assertThat(queryResultPage.items(), is(modifiedResultItems));
    InOrder inOrder = Mockito.inOrder(mockDynamoDbEnhancedClientExtension);
    queryResultMap.forEach(attributeMap -> inOrder.verify(mockDynamoDbEnhancedClientExtension).afterRead(DefaultDynamoDbExtensionContext.builder().tableMetadata(FakeItem.getTableMetadata()).operationContext(PRIMARY_CONTEXT).tableSchema(FakeItem.getTableSchema()).items(attributeMap).build()));
}
Also used : InOrder(org.mockito.InOrder) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) ReadModification(software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification) 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

ReadModification (software.amazon.awssdk.enhanced.dynamodb.extensions.ReadModification)3 Collections.singletonMap (java.util.Collections.singletonMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 InOrder (org.mockito.InOrder)2 FakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem)2 FakeItem.createUniqueFakeItem (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem.createUniqueFakeItem)2 HashMap (java.util.HashMap)1 DynamoDbEnhancedClientExtension (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClientExtension)1 DynamoDbExtensionContext (software.amazon.awssdk.enhanced.dynamodb.DynamoDbExtensionContext)1 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)1 QueryResponse (software.amazon.awssdk.services.dynamodb.model.QueryResponse)1 ScanResponse (software.amazon.awssdk.services.dynamodb.model.ScanResponse)1