use of com.amazonaws.services.dynamodbv2.document.Expected in project athenz by yahoo.
the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509RecordNoHostName.
@Test
public void testUpdateX509RecordNoHostName() {
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
Date now = new Date();
X509CertRecord certRecord = getRecordNonNullableColumns(now);
certRecord.setLastNotifiedTime(now);
certRecord.setLastNotifiedServer("last-notified-server");
certRecord.setExpiryTime(now);
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()));
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();
// Check everyone except the hostname (it will be filled with the primaryKey value as the hostName index doesn't allow nulls)
for (int i = 0; i < capturedAttributes.size() - 1; ++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());
}
// Make sure hostName received the value of the primaryKey
System.out.println("expected attr: hostName, value: athenz.provider:cn:1234");
assertEquals(capturedAttributes.get(capturedAttributes.size() - 1).getAttributeName(), "hostName");
assertEquals(capturedAttributes.get(capturedAttributes.size() - 1).getValue(), "athenz.provider:cn:1234");
dbConn.close();
}
use of com.amazonaws.services.dynamodbv2.document.Expected in project camel by apache.
the class UpdateItemCommandTest method execute.
@Test
public void execute() {
Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("1", new AttributeValue("Key_1"));
exchange.getIn().setHeader(DdbConstants.KEY, key);
Map<String, AttributeValueUpdate> attributeMap = new HashMap<String, AttributeValueUpdate>();
AttributeValueUpdate attributeValue = new AttributeValueUpdate(new AttributeValue("new value"), AttributeAction.ADD);
attributeMap.put("name", attributeValue);
exchange.getIn().setHeader(DdbConstants.UPDATE_VALUES, attributeMap);
Map<String, ExpectedAttributeValue> expectedAttributeValueMap = new HashMap<String, ExpectedAttributeValue>();
expectedAttributeValueMap.put("name", new ExpectedAttributeValue(new AttributeValue("expected value")));
exchange.getIn().setHeader(DdbConstants.UPDATE_CONDITION, expectedAttributeValueMap);
exchange.getIn().setHeader(DdbConstants.RETURN_VALUES, "ALL_OLD");
command.execute();
assertEquals("DOMAIN1", ddbClient.updateItemRequest.getTableName());
assertEquals(attributeMap, ddbClient.updateItemRequest.getAttributeUpdates());
assertEquals(key, ddbClient.updateItemRequest.getKey());
assertEquals(expectedAttributeValueMap, ddbClient.updateItemRequest.getExpected());
assertEquals("ALL_OLD", ddbClient.updateItemRequest.getReturnValues());
assertEquals(new AttributeValue("attrValue"), exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class).get("attrName"));
}
use of com.amazonaws.services.dynamodbv2.document.Expected in project jcabi-dynamo by jcabi.
the class AwsItem method put.
@Override
public Map<String, AttributeValue> put(final Map<String, AttributeValueUpdate> attrs) throws IOException {
final AmazonDynamoDB aws = this.credentials.aws();
final Attributes expected = this.attributes.only(this.keys);
try {
final UpdateItemRequest request = new UpdateItemRequest().withTableName(this.name).withExpected(expected.asKeys()).withKey(expected).withAttributeUpdates(attrs).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL).withReturnValues(ReturnValue.UPDATED_NEW);
final long start = System.currentTimeMillis();
final UpdateItemResult result = aws.updateItem(request);
Logger.info(this, "#put('%s'): updated item to DynamoDB, %s, in %[ms]s", attrs, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
return result.getAttributes();
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to put %s into \"%s\" with %s", attrs, this.name, this.keys), ex);
} finally {
aws.shutdown();
}
}
use of com.amazonaws.services.dynamodbv2.document.Expected in project beam by apache.
the class AttributeValueCoderTest method shouldPassForListType.
@Test
public void shouldPassForListType() throws IOException {
AttributeValue expected = new AttributeValue();
List<AttributeValue> listAttr = new ArrayList<>();
listAttr.add(new AttributeValue("innerMapValue1"));
listAttr.add(new AttributeValue().withN("8976234"));
expected.setL(listAttr);
AttributeValueCoder coder = AttributeValueCoder.of();
ByteArrayOutputStream output = new ByteArrayOutputStream();
coder.encode(expected, output);
ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
AttributeValue actual = coder.decode(in);
Assert.assertEquals(expected, actual);
}
use of com.amazonaws.services.dynamodbv2.document.Expected in project beam by apache.
the class AttributeValueCoderTest method shouldPassForMapType.
@Test
public void shouldPassForMapType() throws IOException {
AttributeValue expected = new AttributeValue();
Map<String, AttributeValue> attrMap = new HashMap<>();
attrMap.put("innerMapAttr1", new AttributeValue("innerMapValue1"));
attrMap.put("innerMapAttr2", new AttributeValue().withB(ByteBuffer.wrap("8976234".getBytes(StandardCharsets.UTF_8))));
expected.setM(attrMap);
AttributeValueCoder coder = AttributeValueCoder.of();
ByteArrayOutputStream output = new ByteArrayOutputStream();
coder.encode(expected, output);
ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
AttributeValue actual = coder.decode(in);
Assert.assertEquals(expected, actual);
}
Aggregations