Search in sources :

Example 11 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoScheduledActivityDaoTest method getActivityHistoryV2.

@SuppressWarnings("unchecked")
@Test
public void getActivityHistoryV2() {
    DynamoIndexHelper indexHelper = mock(DynamoIndexHelper.class);
    QueryResultPage<DynamoScheduledActivity> queryResult = mock(QueryResultPage.class);
    activityDao.setReferentIndex(indexHelper);
    ArgumentCaptor<DynamoDBQueryExpression<DynamoScheduledActivity>> queryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    DynamoScheduledActivity activity = new DynamoScheduledActivity();
    List<DynamoScheduledActivity> list = ImmutableList.of(activity);
    when(queryResult.getResults()).thenReturn(list);
    when(mapper.queryPage(eq(DynamoScheduledActivity.class), any())).thenReturn(queryResult);
    Map<String, AttributeValue> map = ImmutableMap.of("guid", new AttributeValue(SCHEDULED_ON_START.toLocalDate() + ":baz"));
    when(queryResult.getLastEvaluatedKey()).thenReturn(map);
    ForwardCursorPagedResourceList<ScheduledActivity> results = activityDao.getActivityHistoryV2(HEALTH_CODE, ACTIVITY_GUID, SCHEDULED_ON_START, SCHEDULED_ON_END, OFFSET_KEY, PAGE_SIZE);
    verify(mapper).queryPage(eq(DynamoScheduledActivity.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoScheduledActivity> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getHealthCode(), HEALTH_CODE);
    assertEquals((int) query.getLimit(), PAGE_SIZE);
    assertEquals(query.getExclusiveStartKey().get("guid").getS(), ACTIVITY_GUID + ":" + OFFSET_KEY);
    assertEquals(query.getExclusiveStartKey().get("healthCode").getS(), HEALTH_CODE);
    Condition condition = query.getRangeKeyConditions().get("guid");
    assertEquals(condition.getAttributeValueList().get(0).getS(), ACTIVITY_GUID + ":" + SCHEDULED_ON_START.toLocalDateTime().toString());
    assertEquals(condition.getAttributeValueList().get(1).getS(), ACTIVITY_GUID + ":" + SCHEDULED_ON_END.toLocalDateTime().toString());
    assertEquals(results.getItems().size(), 1);
    assertEquals(results.getNextPageOffsetKey(), "baz");
    assertEquals((String) results.getRequestParams().get("offsetKey"), OFFSET_KEY);
    assertEquals((int) results.getRequestParams().get("pageSize"), PAGE_SIZE);
    assertEquals(results.getRequestParams().get("scheduledOnStart"), SCHEDULED_ON_START.toString());
    assertEquals(results.getRequestParams().get("scheduledOnEnd"), SCHEDULED_ON_END.toString());
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) Test(org.testng.annotations.Test)

Example 12 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoScheduledActivityDaoTest method mockMapperResults.

@SuppressWarnings("unchecked")
private void mockMapperResults(final List<ScheduledActivity> activities) {
    // Mocks loading one of the supplied activities.
    when(mapper.load(any())).thenAnswer(invocation -> {
        DynamoScheduledActivity thisSchActivity = invocation.getArgument(0);
        for (ScheduledActivity schActivity : activities) {
            if (thisSchActivity.getGuid().equals(schActivity.getGuid()) && thisSchActivity.getHealthCode().equals(schActivity.getHealthCode())) {
                return thisSchActivity;
            }
        }
        return null;
    });
    // Mocks a query that returns all of the activities.
    final PaginatedQueryList<DynamoScheduledActivity> queryResults = (PaginatedQueryList<DynamoScheduledActivity>) mock(PaginatedQueryList.class);
    when(queryResults.iterator()).thenReturn(((List<DynamoScheduledActivity>) (List<?>) activities).iterator());
    when(queryResults.toArray()).thenReturn(activities.toArray());
    when(mapper.query((Class<DynamoScheduledActivity>) any(Class.class), (DynamoDBQueryExpression<DynamoScheduledActivity>) any(DynamoDBQueryExpression.class))).thenReturn(queryResults);
    List<DynamoScheduledActivity> dynamoActivities = Lists.newArrayListWithCapacity(activities.size());
    for (ScheduledActivity activity : activities) {
        dynamoActivities.add((DynamoScheduledActivity) activity);
    }
    QueryResultPage<DynamoScheduledActivity> queryResultPage = (QueryResultPage<DynamoScheduledActivity>) mock(QueryResultPage.class);
    when(queryResultPage.getResults()).thenReturn(dynamoActivities);
    when(queryResultPage.getLastEvaluatedKey()).thenReturn(null);
    when(mapper.queryPage((Class<DynamoScheduledActivity>) any(Class.class), (DynamoDBQueryExpression<DynamoScheduledActivity>) any(DynamoDBQueryExpression.class))).thenReturn(queryResultPage);
    // Mock a batch load of the activities
    Map<String, List<Object>> results = Maps.newHashMap();
    results.put("some-table-name", new ArrayList<Object>(activities));
    when(mapper.batchLoad(any(List.class))).thenReturn(results);
}
Also used : PaginatedQueryList(com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) List(java.util.List) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PaginatedQueryList(com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList) QueryResultPage(com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage)

Example 13 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoScheduledActivityDaoTest method getActivityHistoryV3NoOffsetKey.

@SuppressWarnings("unchecked")
@Test
public void getActivityHistoryV3NoOffsetKey() {
    DynamoIndexHelper indexHelper = mock(DynamoIndexHelper.class);
    QueryResultPage<DynamoScheduledActivity> queryResultPage = mock(QueryResultPage.class);
    QueryOutcome outcome = mock(QueryOutcome.class);
    activityDao.setReferentIndex(indexHelper);
    Activity activity = TestUtils.getActivity1();
    String referentGuid = BridgeUtils.createReferentGuidIndex(activity, SCHEDULED_ON_START.toLocalDateTime());
    String guid = activity.getGuid() + ":" + SCHEDULED_ON_START.toLocalDateTime();
    // Need less than an entire page of records
    List<Item> items = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Item item = new Item().withString("guid", guid + ((i == 0) ? "" : i)).with("referentGuid", referentGuid).with("healthCode", HEALTH_CODE);
        items.add(item);
    }
    when(outcome.getItems()).thenReturn(items);
    when(indexHelper.query(any())).thenReturn(outcome);
    List<Object> activities = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        DynamoScheduledActivity oneActivity = new DynamoScheduledActivity();
        // These values are mostly to get us through sorting, and are not that important
        oneActivity.setGuid(guid + ((i == 0) ? "" : i));
        oneActivity.setReferentGuid(referentGuid);
        activities.add(oneActivity);
    }
    Map<String, List<Object>> resultMap = ImmutableMap.of("guid", activities);
    when(mapper.batchLoad(any(List.class))).thenReturn(resultMap);
    when(mapper.queryPage((Class<DynamoScheduledActivity>) any(Class.class), (DynamoDBQueryExpression<DynamoScheduledActivity>) any(DynamoDBQueryExpression.class))).thenReturn(queryResultPage);
    ForwardCursorPagedResourceList<ScheduledActivity> results = activityDao.getActivityHistoryV3(HEALTH_CODE, ActivityType.TASK, activity.getGuid(), SCHEDULED_ON_START, SCHEDULED_ON_END, null, PAGE_SIZE);
    assertEquals(results.getItems().size(), 10);
    assertNull(results.getNextPageOffsetKey());
    assertNull(results.getRequestParams().get("offsetKey"));
    assertEquals((int) results.getRequestParams().get("pageSize"), PAGE_SIZE);
    assertEquals(results.getRequestParams().get("scheduledOnStart"), SCHEDULED_ON_START.toString());
    assertEquals(results.getRequestParams().get("scheduledOnEnd"), SCHEDULED_ON_END.toString());
}
Also used : ArrayList(java.util.ArrayList) Activity(org.sagebionetworks.bridge.models.schedules.Activity) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) ScheduledActivity(org.sagebionetworks.bridge.models.schedules.ScheduledActivity) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Item(com.amazonaws.services.dynamodbv2.document.Item) List(java.util.List) ForwardCursorPagedResourceList(org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) PaginatedQueryList(com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList) Test(org.testng.annotations.Test)

Example 14 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoHealthDataDaoTest method getRecordsByHealthCodeCreatedOnSchemaId.

@Test
public void getRecordsByHealthCodeCreatedOnSchemaId() {
    // Mock mapper with record.
    DynamoHealthDataRecord record = new DynamoHealthDataRecord();
    record.setHealthCode(TEST_HEALTH_CODE);
    record.setId("test ID");
    record.setCreatedOn(TEST_CREATED_ON);
    record.setSchemaId(TEST_SCHEMA_ID);
    QueryResultPage<DynamoHealthDataRecord> resultPage = new QueryResultPage<>();
    resultPage.setResults(ImmutableList.of(record));
    DynamoDBMapper mockMapper = mock(DynamoDBMapper.class);
    when(mockMapper.queryPage(eq(DynamoHealthDataRecord.class), any())).thenReturn(resultPage);
    DynamoHealthDataDao dao = new DynamoHealthDataDao();
    dao.setMapper(mockMapper);
    // Execute and validate.
    List<HealthDataRecord> retVal = dao.getRecordsByHealthCodeCreatedOn(TEST_HEALTH_CODE, TEST_CREATED_ON, TEST_CREATED_ON_END);
    assertEquals(retVal.size(), 1);
    assertSame(retVal.get(0), record);
    // Verify query.
    ArgumentCaptor<DynamoDBQueryExpression> queryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    verify(mockMapper).queryPage(eq(DynamoHealthDataRecord.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoHealthDataRecord> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getHealthCode(), TEST_HEALTH_CODE);
    assertFalse(query.isConsistentRead());
    assertEquals(query.getLimit().intValue(), BridgeConstants.DUPE_RECORDS_MAX_COUNT);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("createdOn");
    assertEquals(rangeKeyCondition.getComparisonOperator(), ComparisonOperator.BETWEEN.toString());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getN(), String.valueOf(TEST_CREATED_ON));
    assertEquals(rangeKeyCondition.getAttributeValueList().get(1).getN(), String.valueOf(TEST_CREATED_ON_END));
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) QueryResultPage(com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage) Test(org.testng.annotations.Test)

Example 15 with QueryResultPage

use of com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage in project BridgeServer2 by Sage-Bionetworks.

the class DynamoParticipantVersionDaoTest method getLatestForHealthCode.

@Test
public void getLatestForHealthCode() {
    // Mock dependencies.
    DynamoParticipantVersion participantVersion = new DynamoParticipantVersion();
    QueryResultPage<DynamoParticipantVersion> queryResultPage = new QueryResultPage<>();
    queryResultPage.setResults(ImmutableList.of(participantVersion));
    when(mockMapper.queryPage(eq(DynamoParticipantVersion.class), any())).thenReturn(queryResultPage);
    // Execute and validate.
    Optional<ParticipantVersion> resultOpt = dao.getLatestParticipantVersionForHealthCode(TestConstants.TEST_APP_ID, TestConstants.HEALTH_CODE);
    assertTrue(resultOpt.isPresent());
    assertSame(resultOpt.get(), participantVersion);
    ArgumentCaptor<DynamoDBQueryExpression<DynamoParticipantVersion>> queryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    verify(mockMapper).queryPage(eq(DynamoParticipantVersion.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoParticipantVersion> query = queryCaptor.getValue();
    assertFalse(query.isScanIndexForward());
    assertEquals(query.getLimit().intValue(), 1);
    ParticipantVersion queryHashKey = query.getHashKeyValues();
    assertEquals(queryHashKey.getAppId(), TestConstants.TEST_APP_ID);
    assertEquals(queryHashKey.getHealthCode(), TestConstants.HEALTH_CODE);
}
Also used : ParticipantVersion(org.sagebionetworks.bridge.models.accounts.ParticipantVersion) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) QueryResultPage(com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage) Test(org.testng.annotations.Test)

Aggregations

DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)10 Condition (com.amazonaws.services.dynamodbv2.model.Condition)8 Test (org.testng.annotations.Test)8 QueryResultPage (com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage)7 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)7 ForwardCursorPagedResourceList (org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)5 ScheduledActivity (org.sagebionetworks.bridge.models.schedules.ScheduledActivity)4 PaginatedQueryList (com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList)2 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SmsMessage (org.sagebionetworks.bridge.models.sms.SmsMessage)2 UploadSchema (org.sagebionetworks.bridge.models.upload.UploadSchema)2 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)1 Item (com.amazonaws.services.dynamodbv2.document.Item)1 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)1 RangeKeyCondition (com.amazonaws.services.dynamodbv2.document.RangeKeyCondition)1 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)1 QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)1 DateTime (org.joda.time.DateTime)1