Search in sources :

Example 21 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project aws-doc-sdk-examples by awsdocs.

the class MoviesItemOps05 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(new PrimaryKey("year", year, "title", title)).withUpdateExpression("remove info.actors[0]").withConditionExpression("size(info.actors) > :num").withValueMap(new ValueMap().withNumber(":num", 3)).withReturnValues(ReturnValue.UPDATED_NEW);
    // Conditional update (we expect this to fail)
    try {
        System.out.println("Attempting a conditional update...");
        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());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) PrimaryKey(com.amazonaws.services.dynamodbv2.document.PrimaryKey) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 22 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509Record.

@Test
public void testUpdateX509Record() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date();
    X509CertRecord certRecord = getRecordNonNullableColumns(now);
    certRecord.setLastNotifiedTime(now);
    certRecord.setLastNotifiedServer("last-notified-server");
    certRecord.setExpiryTime(now);
    certRecord.setHostName("hostname");
    certRecord.setSvcDataUpdateTime(now);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.provider:cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("provider").put(certRecord.getProvider()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("currentSerial").put(certRecord.getCurrentSerial()), new AttributeUpdate("currentIP").put(certRecord.getCurrentIP()), new AttributeUpdate("currentTime").put(certRecord.getCurrentTime().getTime()), new AttributeUpdate("currentDate").put(DynamoDBUtils.getIso8601FromDate(certRecord.getCurrentTime())), new AttributeUpdate("prevSerial").put(certRecord.getPrevSerial()), new AttributeUpdate("prevIP").put(certRecord.getPrevIP()), new AttributeUpdate("prevTime").put(certRecord.getPrevTime().getTime()), new AttributeUpdate("clientCert").put(certRecord.getClientCert()), new AttributeUpdate("ttl").put(certRecord.getCurrentTime().getTime() / 1000L + 3660 * 720), new AttributeUpdate("svcDataUpdateTime").put(certRecord.getSvcDataUpdateTime().getTime()), new AttributeUpdate("expiryTime").put(certRecord.getExpiryTime().getTime()), new AttributeUpdate("hostName").put(certRecord.getHostName()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
    assertTrue(requestSuccess);
    ArgumentCaptor<UpdateItemSpec> itemCaptor = ArgumentCaptor.forClass(UpdateItemSpec.class);
    Mockito.verify(table, times(1)).updateItem(itemCaptor.capture());
    List<UpdateItemSpec> allValues = itemCaptor.getAllValues();
    assertEquals(1, allValues.size());
    UpdateItemSpec capturedItem = allValues.get(0);
    assertEquals(capturedItem.getKeyComponents().toArray()[0].toString(), item.getKeyComponents().toArray()[0].toString());
    List<AttributeUpdate> capturedAttributes = capturedItem.getAttributeUpdate();
    List<AttributeUpdate> expectedAttributes = item.getAttributeUpdate();
    for (int i = 0; i < expectedAttributes.size(); ++i) {
        System.out.println("expected attr: " + expectedAttributes.get(i).getAttributeName() + ", value: " + expectedAttributes.get(i).getValue());
        assertEquals(capturedAttributes.get(i).getAttributeName(), expectedAttributes.get(i).getAttributeName());
        assertEquals(capturedAttributes.get(i).getValue(), expectedAttributes.get(i).getValue());
    }
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Example 23 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnectionTest method testGetX509CertRecordNotFoundException.

@Test
public void testGetX509CertRecordNotFoundException() {
    Mockito.doThrow(new AmazonDynamoDBException("item not found")).when(table).getItem("primaryKey", "athenz.provider:cn:1234");
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    X509CertRecord certRecord = dbConn.getX509CertRecord("athenz.provider", "1234", "cn");
    assertNull(certRecord);
    dbConn.close();
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Example 24 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.

the class DynamoDBNotificationsHelperTest method testIsMostUpdatedHostRecord.

@Test
public void testIsMostUpdatedHostRecord() {
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long threeDaysAgo = nowL - 3 * 24 * 60 * 60 * 1000;
    long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;
    Map<String, AttributeValue> noCurrentTime = ZTSTestUtils.generateAttributeValues("home.test.service2", "testInstance2", null, null, null, null, "testHost1");
    Map<String, AttributeValue> emptyCurrentTime = ZTSTestUtils.generateAttributeValues("home.test.service2", "testInstance2", "", null, null, null, "testHost1");
    Map<String, AttributeValue> threeDaysAgoMap = ZTSTestUtils.generateAttributeValues("home.test.service4", "testInstance4", Long.toString(threeDaysAgo), Long.toString(threeDaysAgo), "testServer", null, "testHost1");
    Map<String, AttributeValue> fiveDaysAgoMap = ZTSTestUtils.generateAttributeValues("home.test.service3", "testInstance3", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost1");
    Item itemNoCurrentTime = ItemUtils.toItem(noCurrentTime);
    Item itemEmptyCurrentTime = ItemUtils.toItem(emptyCurrentTime);
    Item itemFiveDaysAgo = ItemUtils.toItem(fiveDaysAgoMap);
    Item itemThreeDaysAgo = ItemUtils.toItem(threeDaysAgoMap);
    List<Item> allItems = Arrays.asList(itemEmptyCurrentTime, itemFiveDaysAgo, itemThreeDaysAgo, itemNoCurrentTime);
    DynamoDBNotificationsHelper dynamoDBNotificationsHelper = new DynamoDBNotificationsHelper();
    assertFalse(dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(itemFiveDaysAgo, allItems, "currentTime", "primaryKey"));
    assertFalse(dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(itemNoCurrentTime, allItems, "currentTime", "primaryKey"));
    assertFalse(dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(itemEmptyCurrentTime, allItems, "currentTime", "primaryKey"));
    assertTrue(dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(itemThreeDaysAgo, allItems, "currentTime", "primaryKey"));
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Test(org.testng.annotations.Test)

Example 25 with PrimaryKey

use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.

the class DynamoDBNotificationsHelperTest method testIsMostUpdatedHostRecordSingleRecord.

@Test
public void testIsMostUpdatedHostRecordSingleRecord() {
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long threeDaysAgo = nowL - 3 * 24 * 60 * 60 * 1000;
    Map<String, AttributeValue> threeDaysAgoMap = ZTSTestUtils.generateAttributeValues("home.test.service4", "testInstance4", Long.toString(threeDaysAgo), Long.toString(threeDaysAgo), "testServer", null, "testHost1");
    Item itemThreeDaysAgo = ItemUtils.toItem(threeDaysAgoMap);
    List<Item> allItems = Collections.singletonList(itemThreeDaysAgo);
    DynamoDBNotificationsHelper dynamoDBNotificationsHelper = new DynamoDBNotificationsHelper();
    assertTrue(dynamoDBNotificationsHelper.isMostUpdatedRecordBasedOnAttribute(itemThreeDaysAgo, allItems, "currentTime", "primaryKey"));
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Test(org.testng.annotations.Test)

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