DynamoDBWorkerUtils { public static String getStoredETag(final AmazonDynamoDB dynamoDB, final String table, final String resource) { String oldETag; final Map<String, AttributeValue> resourceKey = new HashMap<String, AttributeValue>(); resourceKey.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource)); final GetItemResult result = dynamoDB.getItem(table, resourceKey); final Map<String, AttributeValue> item = result.getItem(); if (item != null && item.containsKey(ETAG_KEY)) { oldETag = item.get(ETAG_KEY).getS(); } else { oldETag = null; } return oldETag; } private  DynamoDBWorkerUtils(); static String getStoredETag(final AmazonDynamoDB dynamoDB, final String table, final String resource); static void updateETag(final AmazonDynamoDB dynamoDB, final String table, final String resource,
        final String eTag); static final String ETAG_KEY; }
@Test public void testGetStoredETagExists() { AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class); Map<String, AttributeValue> resourceKey = new HashMap<String, AttributeValue>(); resourceKey.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource)); dynamoDB.getItem(table, resourceKey); Map<String, AttributeValue> resourceResult = new HashMap<String, AttributeValue>(); resourceResult.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource)); resourceResult.put(DynamoDBWorkerUtils.ETAG_KEY, new AttributeValue(eTag)); GetItemResult result = new GetItemResult().withItem(resourceResult); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); String resultETag = DynamoDBWorkerUtils.getStoredETag(dynamoDB, table, resource); assertEquals(eTag, resultETag); PowerMock.verifyAll(); }
@Test public void testGetStoredETagDoesNotExist() { AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class); Map<String, AttributeValue> resourceKey = new HashMap<String, AttributeValue>(); resourceKey.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource)); dynamoDB.getItem(table, resourceKey); GetItemResult result = new GetItemResult(); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); String resultETag = DynamoDBWorkerUtils.getStoredETag(dynamoDB, table, resource); assertEquals(null, resultETag); PowerMock.verifyAll(); }