Search in sources :

Example 16 with PrimaryKey

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);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) ProvisionedThroughputExceededException(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputExceededException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 17 with PrimaryKey

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);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) ProvisionedThroughputExceededException(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputExceededException) Test(org.testng.annotations.Test)

Example 18 with PrimaryKey

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();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) SSHCertRecord(com.yahoo.athenz.common.server.ssh.SSHCertRecord) Test(org.testng.annotations.Test)

Example 19 with PrimaryKey

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();
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) SSHCertRecord(com.yahoo.athenz.common.server.ssh.SSHCertRecord) Test(org.testng.annotations.Test)

Example 20 with PrimaryKey

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());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) HashSet(java.util.HashSet)

Aggregations

Test (org.testng.annotations.Test)15 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)11 Item (com.amazonaws.services.dynamodbv2.document.Item)9 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)8 Table (com.amazonaws.services.dynamodbv2.document.Table)7 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)5 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)5 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)3 Date (java.util.Date)3 AmazonClientException (com.amazonaws.AmazonClientException)2 ClientConfiguration (com.amazonaws.ClientConfiguration)2 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)2 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)2 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2