Search in sources :

Example 21 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoHealthDataEx3DaoTest method getRecordsForAppAndStudy.

@Test
public void getRecordsForAppAndStudy() {
    // Mock dependencies.
    DynamoHealthDataRecordEx3 record = new DynamoHealthDataRecordEx3();
    doReturn(ImmutableList.of(record)).when(dao).queryHelper(any());
    // Execute.
    ForwardCursorPagedResourceList<HealthDataRecordEx3> resultList = dao.getRecordsForAppAndStudy(TestConstants.TEST_APP_ID, STUDY_ID, CREATED_ON_START, CREATED_ON_END, BridgeConstants.API_DEFAULT_PAGE_SIZE, null);
    assertEquals(resultList.getItems().size(), 1);
    assertSame(resultList.getItems().get(0), record);
    assertNull(resultList.getNextPageOffsetKey());
    // Validate.
    ArgumentCaptor<DynamoDBQueryExpression<DynamoHealthDataRecordEx3>> queryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    verify(dao).queryHelper(queryCaptor.capture());
    DynamoDBQueryExpression<DynamoHealthDataRecordEx3> query = queryCaptor.getValue();
    assertFalse(query.isConsistentRead());
    assertEquals(query.getIndexName(), DynamoHealthDataRecordEx3.APPSTUDYKEY_CREATEDON_INDEX);
    assertEquals(query.getHashKeyValues().getAppId(), TestConstants.TEST_APP_ID);
    assertEquals(query.getHashKeyValues().getStudyId(), STUDY_ID);
    assertEquals(query.getLimit().intValue(), BridgeConstants.API_DEFAULT_PAGE_SIZE + 1);
    assertEquals(query.getRangeKeyConditions().size(), 1);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("createdOn");
    assertEquals(rangeKeyCondition.getComparisonOperator(), ComparisonOperator.BETWEEN.toString());
    assertEquals(rangeKeyCondition.getAttributeValueList().size(), 2);
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getN(), String.valueOf(CREATED_ON_START));
    assertEquals(rangeKeyCondition.getAttributeValueList().get(1).getN(), String.valueOf(CREATED_ON_END));
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) HealthDataRecordEx3(org.sagebionetworks.bridge.models.healthdata.HealthDataRecordEx3) Test(org.testng.annotations.Test)

Example 22 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoSurveyDao method getSurveyGuidForIdentifier.

/**
 * {@inheritDoc}
 */
@Override
public String getSurveyGuidForIdentifier(String appId, String surveyId) {
    // Hash key.
    DynamoSurvey hashKey = new DynamoSurvey();
    hashKey.setAppId(appId);
    // Range key.
    Condition rangeKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList(new AttributeValue().withS(surveyId));
    // Construct query.
    DynamoDBQueryExpression<DynamoSurvey> expression = new DynamoDBQueryExpression<DynamoSurvey>().withConsistentRead(false).withHashKeyValues(hashKey).withRangeKeyCondition("identifier", rangeKeyCondition).withLimit(1);
    // Execute query.
    QueryResultPage<DynamoSurvey> resultPage = surveyMapper.queryPage(DynamoSurvey.class, expression);
    List<DynamoSurvey> surveyList = resultPage.getResults();
    if (surveyList.isEmpty()) {
        return null;
    }
    return surveyList.get(0).getGuid();
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)

Example 23 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition in project BridgeServer2 by Sage-Bionetworks.

the class DynamoUploadDao method createCountQuery.

/**
 * Create a query for records applying the filter values if they exist.
 */
private DynamoDBQueryExpression<DynamoUpload2> createCountQuery(String appId, DateTime startTime, DateTime endTime) {
    Condition rangeKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN.toString()).withAttributeValueList(new AttributeValue().withN(String.valueOf(startTime.getMillis())), new AttributeValue().withN(String.valueOf(endTime.getMillis())));
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setAppId(appId);
    DynamoDBQueryExpression<DynamoUpload2> query = new DynamoDBQueryExpression<>();
    query.withIndexName(STUDY_ID_REQUESTED_ON_INDEX);
    query.withHashKeyValues(upload);
    query.withConsistentRead(false);
    query.setScanIndexForward(false);
    query.withRangeKeyCondition(REQUESTED_ON, rangeKeyCondition);
    return query;
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) RangeKeyCondition(com.amazonaws.services.dynamodbv2.document.RangeKeyCondition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)

Aggregations

Condition (com.amazonaws.services.dynamodbv2.model.Condition)17 Test (org.testng.annotations.Test)14 DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)10 Survey (org.sagebionetworks.bridge.models.surveys.Survey)5 Item (com.amazonaws.services.dynamodbv2.document.Item)4 RangeKeyCondition (com.amazonaws.services.dynamodbv2.document.RangeKeyCondition)4 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 HealthDataRecordEx3 (org.sagebionetworks.bridge.models.healthdata.HealthDataRecordEx3)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)3 ForwardCursorPagedResourceList (org.sagebionetworks.bridge.models.ForwardCursorPagedResourceList)3 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)2 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)2 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)2 ImmutableList (com.google.common.collect.ImmutableList)2 HashMap (java.util.HashMap)2 ResourceList (org.sagebionetworks.bridge.models.ResourceList)2 ScheduledActivity (org.sagebionetworks.bridge.models.schedules.ScheduledActivity)2 QueryResultPage (com.amazonaws.services.dynamodbv2.datamodeling.QueryResultPage)1