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());
}
}
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();
}
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();
}
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"));
}
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"));
}
Aggregations