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()));
}
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();
}
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()));
}
Aggregations