use of com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome in project athenz by yahoo.
the class DynamoDBCertRecordStoreConnectionTest method testUpdateUnrefreshedCertificatesNotificationTimestamp.
@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestamp() {
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
Date now = new Date(1591706189000L);
long nowL = now.getTime();
long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;
long sevenDaysAgo = nowL - 7 * 24 * 60 * 60 * 1000;
Map<String, AttributeValue> unNotified = ZTSTestUtils.generateAttributeValues("home.test.service2", "unNotified", null, null, null, null, "testHost1");
Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
Map<String, AttributeValue> rebootstrapped = ZTSTestUtils.generateAttributeValues("home.test.service3", "rebootstrapped", Long.toString(sevenDaysAgo), Long.toString(sevenDaysAgo), "testServer", null, "testHost2");
Map<String, AttributeValue> willBeUpdatedByOtherZts = ZTSTestUtils.generateAttributeValues("home.test.service4", "willBeUpdatedByOtherZts", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost3");
Item item1 = ItemUtils.toItem(unNotified);
Item item2 = ItemUtils.toItem(reNotified);
Item item3 = ItemUtils.toItem(willBeUpdatedByOtherZts);
Item item4 = ItemUtils.toItem(rebootstrapped);
ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
when(itemCollection.iterator()).thenReturn(iteratorSupport);
when(iteratorSupport.hasNext()).thenReturn(true, true, true, true, false);
when(iteratorSupport.next()).thenReturn(item1).thenReturn(item2).thenReturn(item3).thenReturn(item4);
Mockito.doReturn(itemCollection).when(currentTimeIndex).query(any(QuerySpec.class));
ItemCollection<QueryOutcome> itemCollection2 = Mockito.mock(ItemCollection.class);
IteratorSupport<Item, QueryOutcome> iteratorSupport2 = Mockito.mock(IteratorSupport.class);
when(itemCollection2.iterator()).thenReturn(iteratorSupport2);
when(iteratorSupport2.hasNext()).thenReturn(// One record with host testHost1
true, // One record with host testHost1
false, // Two records with host testHost2
true, // Two records with host testHost2
true, // Two records with host testHost2
false, // One record with host testHost3
true, // One record with host testHost3
false, true, true, // Two records with host testHost2
false);
when(iteratorSupport2.next()).thenReturn(item1).thenReturn(item2).thenReturn(item4).thenReturn(item3).thenReturn(item2).thenReturn(item4);
Mockito.doReturn(itemCollection2).when(hostNameIndex).query(any(QuerySpec.class));
AttributeValue lastNotifiedTimeAttrValue = new AttributeValue();
lastNotifiedTimeAttrValue.setN(Long.toString(nowL));
AttributeValue lastNotifiedServerAttrValue = new AttributeValue();
lastNotifiedServerAttrValue.setS("localhost");
AttributeValue lastNotifiedOtherServerAttrValue = new AttributeValue();
lastNotifiedOtherServerAttrValue.setS("SomeOtherZTS");
unNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
unNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);
reNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
reNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);
willBeUpdatedByOtherZts.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
willBeUpdatedByOtherZts.put("lastNotifiedServer", lastNotifiedOtherServerAttrValue);
Item updatedItem1 = ItemUtils.toItem(unNotified);
Item updatedItem2 = ItemUtils.toItem(reNotified);
Item updatedItem3 = ItemUtils.toItem(willBeUpdatedByOtherZts);
UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
when(updateItemOutcome1.getItem()).thenReturn(updatedItem1);
UpdateItemOutcome updateItemOutcome2 = Mockito.mock(UpdateItemOutcome.class);
when(updateItemOutcome2.getItem()).thenReturn(updatedItem2);
UpdateItemOutcome updateItemOutcome3 = Mockito.mock(UpdateItemOutcome.class);
when(updateItemOutcome3.getItem()).thenReturn(updatedItem3);
when(table.updateItem(any(UpdateItemSpec.class))).thenReturn(updateItemOutcome1).thenReturn(updateItemOutcome2).thenReturn(updateItemOutcome3);
List<X509CertRecord> records = dbConn.updateUnrefreshedCertificatesNotificationTimestamp("localhost", nowL, "provider");
ArgumentCaptor<UpdateItemSpec> updateArguments = ArgumentCaptor.forClass(UpdateItemSpec.class);
Mockito.verify(table, Mockito.times(3)).updateItem(updateArguments.capture());
// Assert get filtered records
List<UpdateItemSpec> allUpdateArguments = updateArguments.getAllValues();
assertEquals(3, allUpdateArguments.size());
assertEquals("{primaryKey: provider:home.test.service2:unNotified}", allUpdateArguments.get(0).getKeyComponents().toArray()[0].toString());
assertEquals("{primaryKey: provider:home.test.service3:reNotified}", allUpdateArguments.get(1).getKeyComponents().toArray()[0].toString());
assertEquals("{primaryKey: provider:home.test.service4:willBeUpdatedByOtherZts}", allUpdateArguments.get(2).getKeyComponents().toArray()[0].toString());
// Assert Update
assertEquals(records.size(), 2);
assertNull(records.get(0).getCurrentTime());
assertEquals(records.get(0).getService(), "home.test.service2");
assertEquals(records.get(0).getLastNotifiedTime(), now);
assertEquals(records.get(1).getCurrentTime().getTime(), fiveDaysAgo);
assertEquals(records.get(1).getService(), "home.test.service3");
assertEquals(records.get(1).getLastNotifiedTime(), now);
}
use of com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome in project athenz by yahoo.
the class DynamoDBNotificationsHelperTest method testUpdateLastNotifiedItemRetryFailed.
@Test
public void testUpdateLastNotifiedItemRetryFailed() throws InterruptedException {
Date now = new Date(1591706189000L);
long lastNotifiedTime = now.getTime();
long yesterday = lastNotifiedTime - TimeUnit.DAYS.toMillis(1);
long fiveDaysAgo = lastNotifiedTime - 5 * 24 * 60 * 60 * 1000;
System.setProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_RETRIES, "2");
System.setProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_RETRIES_SLEEP_MILLIS, "1000");
DynamoDBNotificationsHelper dynamoDBNotificationsHelper = new DynamoDBNotificationsHelper();
Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
Item item = ItemUtils.toItem(reNotified);
UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
when(updateItemOutcome1.getItem()).thenReturn(item);
Table table = Mockito.mock(Table.class);
// After getting this error twice, we stop retrying
Mockito.when(table.updateItem(any(UpdateItemSpec.class))).thenThrow(new ProvisionedThroughputExceededException("Provisioned Throughput Exceeded")).thenThrow(new ProvisionedThroughputExceededException("Provisioned Throughput Exceeded"));
try {
dynamoDBNotificationsHelper.updateLastNotifiedItem("lastNotifiedServer", lastNotifiedTime, yesterday, item, "primaryKey", table);
fail();
} catch (TimeoutException ex) {
assertEquals("Failed too many retries. Check table provisioned throughput settings.", ex.getMessage());
}
System.clearProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_RETRIES);
System.clearProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_RETRIES_SLEEP_MILLIS);
}
use of com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome in project athenz by yahoo.
the class DynamoDBNotificationsHelperTest method testUpdateLastNotifiedItemRetry.
@Test
public void testUpdateLastNotifiedItemRetry() throws TimeoutException, InterruptedException {
Date now = new Date(1591706189000L);
long lastNotifiedTime = now.getTime();
long yesterday = lastNotifiedTime - TimeUnit.DAYS.toMillis(1);
long fiveDaysAgo = lastNotifiedTime - 5 * 24 * 60 * 60 * 1000;
DynamoDBNotificationsHelper dynamoDBNotificationsHelper = new DynamoDBNotificationsHelper();
Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
Item item = ItemUtils.toItem(reNotified);
UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
when(updateItemOutcome1.getItem()).thenReturn(item);
Table table = Mockito.mock(Table.class);
Mockito.when(table.updateItem(any(UpdateItemSpec.class))).thenThrow(new ProvisionedThroughputExceededException("Provisioned Throughput Exceeded")).thenReturn(updateItemOutcome1);
Item updatedItem = dynamoDBNotificationsHelper.updateLastNotifiedItem("lastNotifiedServer", lastNotifiedTime, yesterday, item, "primaryKey", table);
assertEquals(updatedItem, item);
}
use of com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome in project aws-doc-sdk-examples by awsdocs.
the class MoviesItemOps03 method main.
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("Movies");
int year = 2015;
String title = "The Big New Movie";
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year", year, "title", title).withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a").withValueMap(new ValueMap().withNumber(":r", 5.5).withString(":p", "Everything happens all at once.").withList(":a", Arrays.asList("Larry", "Moe", "Curly"))).withReturnValues(ReturnValue.UPDATED_NEW);
try {
System.out.println("Updating the item...");
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
} catch (Exception e) {
System.err.println("Unable to update item: " + year + " " + title);
System.err.println(e.getMessage());
}
}
use of com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome in project aws-doc-sdk-examples by awsdocs.
the class DocumentAPIItemCRUDExample method updateMultipleAttributes.
private static void updateMultipleAttributes() {
Table table = dynamoDB.getTable(tableName);
try {
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 120).withUpdateExpression("add #a :val1 set #na=:val2").withNameMap(new NameMap().with("#a", "Authors").with("#na", "NewAttribute")).withValueMap(new ValueMap().withStringSet(":val1", "Author YY", "Author ZZ").withString(":val2", "someValue")).withReturnValues(ReturnValue.ALL_NEW);
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
// Check the response.
System.out.println("Printing item after multiple attribute update...");
System.out.println(outcome.getItem().toJSONPretty());
} catch (Exception e) {
System.err.println("Failed to update multiple attributes in " + tableName);
System.err.println(e.getMessage());
}
}
Aggregations