use of com.amazonaws.services.dynamodbv2.document.PrimaryKey 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.PrimaryKey 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.PrimaryKey in project athenz by yahoo.
the class DynamoDBSSHRecordStoreConnectionTest method testUpdateSSHRecord.
@Test
public void testUpdateSSHRecord() {
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
SSHCertRecord certRecord = new SSHCertRecord();
certRecord.setInstanceId("1234");
certRecord.setService("cn");
certRecord.setPrincipals("host1,host2");
certRecord.setClientIP("10.10.10.11");
certRecord.setPrivateIP("10.10.10.12");
UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("principals").put(certRecord.getPrincipals()), new AttributeUpdate("clientIP").put(certRecord.getClientIP()), new AttributeUpdate("privateIP").put(certRecord.getPrivateIP()));
Mockito.doReturn(updateOutcome).when(table).updateItem(item);
boolean requestSuccess = dbConn.updateSSHCertRecord(certRecord);
assertTrue(requestSuccess);
dbConn.close();
}
use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.
the class DynamoDBSSHRecordStoreConnectionTest method testGetSSHCertRecordNotFoundException.
@Test
public void testGetSSHCertRecordNotFoundException() {
Mockito.doThrow(new AmazonDynamoDBException("item not found")).when(table).getItem("primaryKey", "cn:1234");
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
SSHCertRecord certRecord = dbConn.getSSHCertRecord("1234", "cn");
assertNull(certRecord);
dbConn.close();
}
use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project aws-doc-sdk-examples by awsdocs.
the class SampleDataLoad method loadSampleThreads.
private static void loadSampleThreads(String tableName) {
try {
// 7
long time1 = (new Date()).getTime() - (7 * 24 * 60 * 60 * 1000);
// days
// ago
// 14
long time2 = (new Date()).getTime() - (14 * 24 * 60 * 60 * 1000);
// days
// ago
// 21
long time3 = (new Date()).getTime() - (21 * 24 * 60 * 60 * 1000);
// days
// ago
Date date1 = new Date();
date1.setTime(time1);
Date date2 = new Date();
date2.setTime(time2);
Date date3 = new Date();
date3.setTime(time3);
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Table table = dynamoDB.getTable(tableName);
System.out.println("Adding data to " + tableName);
Item item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 1").withString("Message", "DynamoDB thread 1 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date2)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "primarykey", "table")));
table.putItem(item);
item = new Item().withPrimaryKey("ForumName", "Amazon DynamoDB").withString("Subject", "DynamoDB Thread 2").withString("Message", "DynamoDB thread 2 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date3)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("index", "partitionkey", "sortkey")));
table.putItem(item);
item = new Item().withPrimaryKey("ForumName", "Amazon S3").withString("Subject", "S3 Thread 1").withString("Message", "S3 Thread 3 message").withString("LastPostedBy", "User A").withString("LastPostedDateTime", dateFormatter.format(date1)).withNumber("Views", 0).withNumber("Replies", 0).withNumber("Answered", 0).withStringSet("Tags", new HashSet<String>(Arrays.asList("largeobjects", "multipart upload")));
table.putItem(item);
} catch (Exception e) {
System.err.println("Failed to create item in " + tableName);
System.err.println(e.getMessage());
}
}
Aggregations