Search in sources :

Example 1 with Expected

use of com.amazonaws.services.dynamodbv2.document.Expected in project camel by apache.

the class DeleteItemCommandTest 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, ExpectedAttributeValue> updateCondition = new HashMap<String, ExpectedAttributeValue>();
    updateCondition.put("name", new ExpectedAttributeValue(new AttributeValue("expected value")));
    exchange.getIn().setHeader(DdbConstants.UPDATE_CONDITION, updateCondition);
    exchange.getIn().setHeader(DdbConstants.RETURN_VALUES, "ALL_OLD");
    command.execute();
    assertEquals("DOMAIN1", ddbClient.deleteItemRequest.getTableName());
    assertEquals(key, ddbClient.deleteItemRequest.getKey());
    assertEquals(updateCondition, ddbClient.deleteItemRequest.getExpected());
    assertEquals("ALL_OLD", ddbClient.deleteItemRequest.getReturnValues());
    assertEquals(new AttributeValue("attrValue"), exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class).get("attrName"));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) HashMap(java.util.HashMap) ExpectedAttributeValue(com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue) Test(org.junit.Test)

Example 2 with Expected

use of com.amazonaws.services.dynamodbv2.document.Expected in project iep by Netflix.

the class PaginationTest method route53ResourceRecordSets.

@Test
public void route53ResourceRecordSets() throws Exception {
    SortedSet<String> pages = newPageSet(5);
    final Iterator<String> reqIt = pages.iterator();
    final Iterator<String> resIt = pages.iterator();
    Function<ListResourceRecordSetsRequest, ListResourceRecordSetsResult> f = r -> {
        if (r.getStartRecordName() != null) {
            String expected = reqIt.next();
            Assert.assertEquals(expected + "-id", r.getStartRecordIdentifier());
            Assert.assertEquals(expected + "-name", r.getStartRecordName());
            Assert.assertEquals(expected + "-type", r.getStartRecordType());
        }
        String next = resIt.hasNext() ? resIt.next() : null;
        return new ListResourceRecordSetsResult().withNextRecordIdentifier((next != null) ? next + "-id" : null).withNextRecordName((next != null) ? next + "-name" : null).withNextRecordType((next != null) ? next + "-type" : null);
    };
    Publisher<ListResourceRecordSetsResult> publisher = Pagination.createPublisher(new ListResourceRecordSetsRequest(), f);
    Iterable<String> iter = Flowable.fromPublisher(publisher).filter(r -> r.getNextRecordName() != null).map(ListResourceRecordSetsResult::getNextRecordName).blockingIterable();
    SortedSet<String> results = new TreeSet<>();
    for (String s : iter) {
        results.add(s.substring(0, 5));
    }
    Assert.assertEquals(pages, results);
    Assert.assertFalse(reqIt.hasNext());
}
Also used : ListHostedZonesResult(com.amazonaws.services.route53.model.ListHostedZonesResult) ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) SortedSet(java.util.SortedSet) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ListMetricsResult(com.amazonaws.services.cloudwatch.model.ListMetricsResult) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest) Function(java.util.function.Function) TreeSet(java.util.TreeSet) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) ListClustersResult(com.amazonaws.services.elasticmapreduce.model.ListClustersResult) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) Iterator(java.util.Iterator) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) Publisher(org.reactivestreams.Publisher) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) ListResourceRecordSetsResult(com.amazonaws.services.route53.model.ListResourceRecordSetsResult) ListHostedZonesRequest(com.amazonaws.services.route53.model.ListHostedZonesRequest) ListClustersRequest(com.amazonaws.services.elasticmapreduce.model.ListClustersRequest) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult) PutMetricDataResult(com.amazonaws.services.cloudwatch.model.PutMetricDataResult) Assert(org.junit.Assert) ListResourceRecordSetsRequest(com.amazonaws.services.route53.model.ListResourceRecordSetsRequest) ListResourceRecordSetsRequest(com.amazonaws.services.route53.model.ListResourceRecordSetsRequest) TreeSet(java.util.TreeSet) ListResourceRecordSetsResult(com.amazonaws.services.route53.model.ListResourceRecordSetsResult) Test(org.junit.Test)

Example 3 with Expected

use of com.amazonaws.services.dynamodbv2.document.Expected in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method putIfAbsent.

@Override
public String putIfAbsent(String key, String value) {
    Assert.hasText(key, "'key' must not be empty.");
    Assert.hasText(value, "'value' must not be empty.");
    awaitForActive();
    try {
        this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key).withAttributeUpdate(new AttributeUpdate(VALUE).put(value)).withExpected(new Expected(KEY).notExist()));
        return null;
    } catch (ConditionalCheckFailedException e) {
        return get(key);
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) Expected(com.amazonaws.services.dynamodbv2.document.Expected) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 4 with Expected

use of com.amazonaws.services.dynamodbv2.document.Expected in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method replace.

@Override
public boolean replace(String key, String oldValue, String newValue) {
    Assert.hasText(key, "'key' must not be empty.");
    Assert.hasText(oldValue, "'value' must not be empty.");
    Assert.hasText(newValue, "'newValue' must not be empty.");
    awaitForActive();
    try {
        return this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key).withAttributeUpdate(new AttributeUpdate(VALUE).put(newValue)).withExpected(new Expected(VALUE).eq(oldValue)).withReturnValues(ReturnValue.UPDATED_NEW)).getItem() != null;
    } catch (ConditionalCheckFailedException e) {
        return false;
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) Expected(com.amazonaws.services.dynamodbv2.document.Expected) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 5 with Expected

use of com.amazonaws.services.dynamodbv2.document.Expected in project thunder by RohanNagar.

the class UsersDao method insert.

/**
 * Insert a new PilotUser into the data store.
 *
 * @param user The object to insert.
 * @return The PilotUser object that was created.
 * @throws DatabaseException If the user already exists or if the database is down.
 */
public PilotUser insert(PilotUser user) {
    checkNotNull(user);
    long now = Instant.now().toEpochMilli();
    Item item = new Item().withPrimaryKey("email", user.getEmail().getAddress()).withString("id", UUID.randomUUID().toString()).withString("version", UUID.randomUUID().toString()).withLong("creation_time", now).withLong("update_time", now).withJSON("document", toJson(mapper, user));
    try {
        table.putItem(item, new Expected("email").notExist());
    } catch (ConditionalCheckFailedException e) {
        LOG.error("The user {} already exists in the database. Throwing DatabaseException.", user.getEmail());
        throw new DatabaseException("The user already exists.", DatabaseError.CONFLICT);
    } catch (AmazonServiceException e) {
        LOG.error("The database rejected the create request.", e);
        throw new DatabaseException("The database rejected the create request.", DatabaseError.REQUEST_REJECTED);
    } catch (AmazonClientException e) {
        LOG.error("The database is currently unresponsive.", e);
        throw new DatabaseException("The database is currently unavailable.", DatabaseError.DATABASE_DOWN);
    }
    return user;
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Expected(com.amazonaws.services.dynamodbv2.document.Expected) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 Test (org.junit.Test)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Expected (com.amazonaws.services.dynamodbv2.document.Expected)5 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)5 AmazonClientException (com.amazonaws.AmazonClientException)4 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)4 HashMap (java.util.HashMap)4 Item (com.amazonaws.services.dynamodbv2.document.Item)3 ExpectedAttributeValue (com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AttributeUpdate (com.amazonaws.services.dynamodbv2.document.AttributeUpdate)2 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)2 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)1 DescribeAutoScalingGroupsResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult)1 ListMetricsRequest (com.amazonaws.services.cloudwatch.model.ListMetricsRequest)1 ListMetricsResult (com.amazonaws.services.cloudwatch.model.ListMetricsResult)1 PutMetricDataRequest (com.amazonaws.services.cloudwatch.model.PutMetricDataRequest)1 PutMetricDataResult (com.amazonaws.services.cloudwatch.model.PutMetricDataResult)1