Search in sources :

Example 11 with QueryOutcome

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

the class DynamoDBCertRecordStoreConnectionTest method testUpdateUnrefreshedCertificatesNotificationTimestamp.

@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestamp() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;
    long sevenDaysAgo = nowL - 7 * 24 * 60 * 60 * 1000;
    Map<String, AttributeValue> unNotified = ZTSTestUtils.generateAttributeValues("home.test.service2", "unNotified", null, null, null, null, "testHost1");
    Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
    Map<String, AttributeValue> rebootstrapped = ZTSTestUtils.generateAttributeValues("home.test.service3", "rebootstrapped", Long.toString(sevenDaysAgo), Long.toString(sevenDaysAgo), "testServer", null, "testHost2");
    Map<String, AttributeValue> willBeUpdatedByOtherZts = ZTSTestUtils.generateAttributeValues("home.test.service4", "willBeUpdatedByOtherZts", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost3");
    Item item1 = ItemUtils.toItem(unNotified);
    Item item2 = ItemUtils.toItem(reNotified);
    Item item3 = ItemUtils.toItem(willBeUpdatedByOtherZts);
    Item item4 = ItemUtils.toItem(rebootstrapped);
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, true, true, true, false);
    when(iteratorSupport.next()).thenReturn(item1).thenReturn(item2).thenReturn(item3).thenReturn(item4);
    Mockito.doReturn(itemCollection).when(currentTimeIndex).query(any(QuerySpec.class));
    ItemCollection<QueryOutcome> itemCollection2 = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport2 = Mockito.mock(IteratorSupport.class);
    when(itemCollection2.iterator()).thenReturn(iteratorSupport2);
    when(iteratorSupport2.hasNext()).thenReturn(// One record with host testHost1
    true, // One record with host testHost1
    false, // Two records with host testHost2
    true, // Two records with host testHost2
    true, // Two records with host testHost2
    false, // One record with host testHost3
    true, // One record with host testHost3
    false, true, true, // Two records with host testHost2
    false);
    when(iteratorSupport2.next()).thenReturn(item1).thenReturn(item2).thenReturn(item4).thenReturn(item3).thenReturn(item2).thenReturn(item4);
    Mockito.doReturn(itemCollection2).when(hostNameIndex).query(any(QuerySpec.class));
    AttributeValue lastNotifiedTimeAttrValue = new AttributeValue();
    lastNotifiedTimeAttrValue.setN(Long.toString(nowL));
    AttributeValue lastNotifiedServerAttrValue = new AttributeValue();
    lastNotifiedServerAttrValue.setS("localhost");
    AttributeValue lastNotifiedOtherServerAttrValue = new AttributeValue();
    lastNotifiedOtherServerAttrValue.setS("SomeOtherZTS");
    unNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    unNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);
    reNotified.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    reNotified.put("lastNotifiedServer", lastNotifiedServerAttrValue);
    willBeUpdatedByOtherZts.put("lastNotifiedTime", lastNotifiedTimeAttrValue);
    willBeUpdatedByOtherZts.put("lastNotifiedServer", lastNotifiedOtherServerAttrValue);
    Item updatedItem1 = ItemUtils.toItem(unNotified);
    Item updatedItem2 = ItemUtils.toItem(reNotified);
    Item updatedItem3 = ItemUtils.toItem(willBeUpdatedByOtherZts);
    UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome1.getItem()).thenReturn(updatedItem1);
    UpdateItemOutcome updateItemOutcome2 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome2.getItem()).thenReturn(updatedItem2);
    UpdateItemOutcome updateItemOutcome3 = Mockito.mock(UpdateItemOutcome.class);
    when(updateItemOutcome3.getItem()).thenReturn(updatedItem3);
    when(table.updateItem(any(UpdateItemSpec.class))).thenReturn(updateItemOutcome1).thenReturn(updateItemOutcome2).thenReturn(updateItemOutcome3);
    List<X509CertRecord> records = dbConn.updateUnrefreshedCertificatesNotificationTimestamp("localhost", nowL, "provider");
    ArgumentCaptor<UpdateItemSpec> updateArguments = ArgumentCaptor.forClass(UpdateItemSpec.class);
    Mockito.verify(table, Mockito.times(3)).updateItem(updateArguments.capture());
    // Assert get filtered records
    List<UpdateItemSpec> allUpdateArguments = updateArguments.getAllValues();
    assertEquals(3, allUpdateArguments.size());
    assertEquals("{primaryKey: provider:home.test.service2:unNotified}", allUpdateArguments.get(0).getKeyComponents().toArray()[0].toString());
    assertEquals("{primaryKey: provider:home.test.service3:reNotified}", allUpdateArguments.get(1).getKeyComponents().toArray()[0].toString());
    assertEquals("{primaryKey: provider:home.test.service4:willBeUpdatedByOtherZts}", allUpdateArguments.get(2).getKeyComponents().toArray()[0].toString());
    // Assert Update
    assertEquals(records.size(), 2);
    assertNull(records.get(0).getCurrentTime());
    assertEquals(records.get(0).getService(), "home.test.service2");
    assertEquals(records.get(0).getLastNotifiedTime(), now);
    assertEquals(records.get(1).getCurrentTime().getTime(), fiveDaysAgo);
    assertEquals(records.get(1).getService(), "home.test.service3");
    assertEquals(records.get(1).getLastNotifiedTime(), now);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) Test(org.testng.annotations.Test)

Example 12 with QueryOutcome

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

the class DynamoDBCertRecordStoreConnectionTest method testUpdateUnrefreshedCertificatesNotificationTimestampHostException.

@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestampHostException() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;
    Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
    Item item1 = ItemUtils.toItem(reNotified);
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, false);
    when(iteratorSupport.next()).thenReturn(item1);
    Mockito.doReturn(itemCollection).when(currentTimeIndex).query(any(QuerySpec.class));
    Mockito.doThrow(new TransactionConflictException("error")).when(hostNameIndex).query(any(QuerySpec.class));
    List<X509CertRecord> result = dbConn.updateUnrefreshedCertificatesNotificationTimestamp("serverTest", 1591706189000L, "providerTest");
    assertEquals(result.size(), 0);
    dbConn.close();
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) TransactionConflictException(com.amazonaws.services.dynamodbv2.model.TransactionConflictException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) Test(org.testng.annotations.Test)

Example 13 with QueryOutcome

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

the class DynamoDBCertRecordStoreConnectionTest method testUpdateUnrefreshedCertificatesNotificationTimestampUpdateDynamoDBException.

@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestampUpdateDynamoDBException() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date(1591706189000L);
    long nowL = now.getTime();
    long fiveDaysAgo = nowL - 5 * 24 * 60 * 60 * 1000;
    Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
    Item item1 = ItemUtils.toItem(reNotified);
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, false);
    when(iteratorSupport.next()).thenReturn(item1);
    Mockito.doReturn(itemCollection).when(currentTimeIndex).query(any(QuerySpec.class));
    ItemCollection<QueryOutcome> itemCollection2 = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport2 = Mockito.mock(IteratorSupport.class);
    when(itemCollection2.iterator()).thenReturn(iteratorSupport2);
    when(iteratorSupport2.hasNext()).thenReturn(true, false);
    when(iteratorSupport2.next()).thenReturn(item1);
    Mockito.doReturn(itemCollection2).when(hostNameIndex).query(any(QuerySpec.class));
    Mockito.doThrow(new AmazonDynamoDBException("invalid operation")).when(table).updateItem(any(UpdateItemSpec.class));
    List<X509CertRecord> result = dbConn.updateUnrefreshedCertificatesNotificationTimestamp("serverTest", 1591706189000L, "providerTest");
    assertEquals(result.size(), 0);
    dbConn.close();
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) Test(org.testng.annotations.Test)

Example 14 with QueryOutcome

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

the class DocumentAPIGlobalSecondaryIndexExample method queryIndex.

public static void queryIndex(String indexName) {
    Table table = dynamoDB.getTable(tableName);
    System.out.println("\n***********************************************************\n");
    System.out.print("Querying index " + indexName + "...");
    Index index = table.getIndex(indexName);
    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();
    if (indexName == "CreateDateIndex") {
        System.out.println("Issues filed on 2013-11-01");
        querySpec.withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_date", "2013-11-01").withString(":v_issue", "A-"));
        items = index.query(querySpec);
    } else if (indexName == "TitleIndex") {
        System.out.println("Compilation errors");
        querySpec.withKeyConditionExpression("Title = :v_title and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_title", "Compilation error").withString(":v_issue", "A-"));
        items = index.query(querySpec);
    } else if (indexName == "DueDateIndex") {
        System.out.println("Items that are due on 2013-11-30");
        querySpec.withKeyConditionExpression("DueDate = :v_date").withValueMap(new ValueMap().withString(":v_date", "2013-11-30"));
        items = index.query(querySpec);
    } else {
        System.out.println("\nNo valid index name provided");
        return;
    }
    Iterator<Item> iterator = items.iterator();
    System.out.println("Query: printing results...");
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) Index(com.amazonaws.services.dynamodbv2.document.Index) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 15 with QueryOutcome

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

the class DocumentAPILocalSecondaryIndexExample method query.

public static void query(String indexName) {
    Table table = dynamoDB.getTable(tableName);
    System.out.println("\n***********************************************************\n");
    System.out.println("Querying table " + tableName + "...");
    QuerySpec querySpec = new QuerySpec().withConsistentRead(true).withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
    if (indexName == "IsOpenIndex") {
        System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open.");
        System.out.println("Only a user-specified list of attributes are returned\n");
        Index index = table.getIndex(indexName);
        querySpec.withKeyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_isopen", 1));
        querySpec.withProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");
        ItemCollection<QueryOutcome> items = index.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    } else if (indexName == "OrderCreationDateIndex") {
        System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2015.");
        System.out.println("Only the projected attributes are returned\n");
        Index index = table.getIndex(indexName);
        querySpec.withKeyConditionExpression("CustomerId = :v_custid and OrderCreationDate >= :v_orddate").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_orddate", 20150131));
        querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);
        ItemCollection<QueryOutcome> items = index.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    } else {
        System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
        querySpec.withKeyConditionExpression("CustomerId = :v_custid").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com"));
        ItemCollection<QueryOutcome> items = table.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) Iterator(java.util.Iterator) LocalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex) Index(com.amazonaws.services.dynamodbv2.document.Index) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) ItemCollection(com.amazonaws.services.dynamodbv2.document.ItemCollection) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Aggregations

QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)18 Item (com.amazonaws.services.dynamodbv2.document.Item)10 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10 Table (com.amazonaws.services.dynamodbv2.document.Table)10 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)10 Test (org.testng.annotations.Test)6 Date (java.util.Date)5 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Index (com.amazonaws.services.dynamodbv2.document.Index)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2 TransactionConflictException (com.amazonaws.services.dynamodbv2.model.TransactionConflictException)2 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)2 HashMap (java.util.HashMap)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)1 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)1 ItemCollection (com.amazonaws.services.dynamodbv2.document.ItemCollection)1