Search in sources :

Example 91 with Item

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

the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByService.

@Test
public void testGetWorkloadRecordsByService() {
    long currTime = System.currentTimeMillis();
    Mockito.doReturn("1234").when(item).getString("instanceId");
    Mockito.doReturn("openstack").when(item).getString("provider");
    Mockito.doReturn("10.10.10.11").when(item).getString("ip");
    Mockito.doReturn("test-host").when(item).getString("hostname");
    Mockito.doReturn(true).when(item).hasAttribute("hostname");
    Mockito.doReturn(currTime).when(item).get("creationTime");
    Mockito.doReturn(currTime).when(item).get("updateTime");
    Mockito.doReturn(currTime).when(item).get("certExpiryTime");
    Mockito.doReturn(currTime).when(item).getLong("creationTime");
    Mockito.doReturn(currTime).when(item).getLong("updateTime");
    Mockito.doReturn(currTime).when(item).getLong("certExpiryTime");
    Mockito.doReturn(true).when(item).hasAttribute("certExpiryTime");
    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(item);
    Mockito.doReturn(itemCollection).when(serviceIndex).query(Mockito.any(QuerySpec.class));
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    dbConn.setOperationTimeout(10);
    List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByService("athenz", "api");
    Assert.assertNotNull(wlRecordList);
    Assert.assertEquals(wlRecordList.get(0).getInstanceId(), "1234");
    Assert.assertEquals(wlRecordList.get(0).getProvider(), "openstack");
    Assert.assertEquals(wlRecordList.get(0).getIp(), "10.10.10.11");
    Assert.assertEquals(wlRecordList.get(0).getHostname(), "test-host");
    Assert.assertEquals(wlRecordList.get(0).getUpdateTime(), new Date(currTime));
    Assert.assertEquals(wlRecordList.get(0).getCertExpiryTime(), new Date(currTime));
    dbConn.close();
}
Also used : QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 92 with Item

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

the class DynamoDBWorkloadRecordStoreConnectionTest method testUpdateWorkloadRecord.

@Test
public void testUpdateWorkloadRecord() {
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    WorkloadRecord workloadRecord = new WorkloadRecord();
    workloadRecord.setProvider("openstack");
    long currTime = System.currentTimeMillis();
    Date currDate = new Date(currTime);
    workloadRecord.setUpdateTime(currDate);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.api#1234#10.0.0.1").withAttributeUpdate(new AttributeUpdate("provider").put(workloadRecord.getProvider()), new AttributeUpdate("updateTime").put(workloadRecord.getUpdateTime()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateWorkloadRecord(workloadRecord);
    Assert.assertTrue(requestSuccess);
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 93 with Item

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

the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByIpNotFoundException.

@Test
public void testGetWorkloadRecordsByIpNotFoundException() {
    Mockito.doThrow(new AmazonDynamoDBException("item not found")).when(ipIndex).query(Mockito.any(QuerySpec.class));
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByIp("10.0.0.1");
    Assert.assertTrue(wlRecordList.isEmpty());
    dbConn.close();
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Test(org.testng.annotations.Test)

Example 94 with Item

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

the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509RecordNullableColumns.

@Test
public void testUpdateX509RecordNullableColumns() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date();
    X509CertRecord certRecord = getRecordNonNullableColumns(now);
    certRecord.setLastNotifiedTime(null);
    certRecord.setLastNotifiedServer(null);
    certRecord.setExpiryTime(now);
    certRecord.setHostName(null);
    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(null));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
    assertTrue(requestSuccess);
    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 95 with Item

use of com.amazonaws.services.dynamodbv2.document.Item 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();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)107 Item (com.amazonaws.services.dynamodbv2.document.Item)95 HashMap (java.util.HashMap)60 Table (com.amazonaws.services.dynamodbv2.document.Table)53 Test (org.testng.annotations.Test)49 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)42 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)35 AmazonServiceException (com.amazonaws.AmazonServiceException)31 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)27 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)26 ArrayList (java.util.ArrayList)25 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 List (java.util.List)23 Test (org.junit.Test)23 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)22 IOException (java.io.IOException)21 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)20 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)18 Date (java.util.Date)18 Map (java.util.Map)17