Search in sources :

Example 16 with RangeKeyCondition

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

the class DynamoSurveyDaoTest method getSurveyIncludeElementsByIdentifer.

@Test
public void getSurveyIncludeElementsByIdentifer() {
    DynamoSurvey survey = new DynamoSurvey();
    survey.setAppId(TEST_APP_ID);
    survey.setGuid(GUID);
    survey.setIdentifier(SURVEY_ID);
    survey.setCreatedOn(CREATED_ON);
    mockSurveyMapper(survey);
    // There is a question that should be retrieved.
    DynamoSurveyQuestion element = new DynamoSurveyQuestion();
    element.setType(SurveyElementConstants.SURVEY_QUESTION_TYPE);
    element.setUiHint(UIHint.BLOODPRESSURE);
    element.setConstraints(new BloodPressureConstraints());
    when(mockSurveyElementMapper.queryPage(eq(DynamoSurveyElement.class), any())).thenReturn(mockElementResultsPage);
    when(mockElementResultsPage.getResults()).thenReturn(ImmutableList.of(element));
    Survey result = dao.getSurvey(TEST_APP_ID, SURVEY_IDENTIFIER_KEYS, true);
    assertSame(result, survey);
    assertEquals(result.getElements().size(), 1);
    verify(mockSurveyMapper).queryPage(eq(DynamoSurvey.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurvey> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getAppId(), TEST_APP_ID);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), EQ.name());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), SURVEY_ID);
    assertEquals(query.getQueryFilter().size(), 1);
    verifyCreatedOnQueryCondition(query);
    verify(mockSurveyElementMapper).queryPage(eq(DynamoSurveyElement.class), elementQueryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurveyElement> elementQuery = elementQueryCaptor.getValue();
    assertEquals(elementQuery.getHashKeyValues().getSurveyCompoundKey(), GUID + ":" + CREATED_ON);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Survey(org.sagebionetworks.bridge.models.surveys.Survey) BloodPressureConstraints(org.sagebionetworks.bridge.models.surveys.BloodPressureConstraints) Test(org.testng.annotations.Test)

Example 17 with RangeKeyCondition

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

the class DynamoSurveyDaoTest method getSurveyMostRecentVersionByIdentifier.

@Test
public void getSurveyMostRecentVersionByIdentifier() {
    // Survey 1A
    DynamoSurvey survey1A = new DynamoSurvey();
    survey1A.setGuid(GUID);
    survey1A.setIdentifier(SURVEY_ID);
    survey1A.setCreatedOn(CREATED_ON);
    // Survey 1B
    DynamoSurvey survey1B = new DynamoSurvey();
    survey1B.setGuid(GUID);
    survey1B.setIdentifier(SURVEY_ID);
    survey1B.setCreatedOn(CREATED_ON - 2000L);
    mockSurveyMapper(survey1B, survey1A);
    mockSurveyElementMapper();
    Survey survey = dao.getSurveyMostRecentVersion(TEST_APP_ID, SURVEY_IDENTIFIER);
    assertSame(survey, survey1A);
    verify(mockSurveyMapper).queryPage(eq(DynamoSurvey.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurvey> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getAppId(), TEST_APP_ID);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), EQ.name());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), SURVEY_ID);
    assertEquals(query.getQueryFilter().size(), 1);
    verifyIsDeletedQueryCondition(query);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Survey(org.sagebionetworks.bridge.models.surveys.Survey) Test(org.testng.annotations.Test)

Example 18 with RangeKeyCondition

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

the class DynamoSurveyDaoTest method getSurveyAllVersionsIncludeDeletedByIdentifier.

@Test
public void getSurveyAllVersionsIncludeDeletedByIdentifier() {
    // Survey 1A
    DynamoSurvey survey1A = new DynamoSurvey();
    survey1A.setGuid(GUID);
    survey1A.setIdentifier(SURVEY_ID);
    survey1A.setCreatedOn(CREATED_ON);
    survey1A.setDeleted(true);
    // Survey 1B
    DynamoSurvey survey1B = new DynamoSurvey();
    survey1B.setGuid(GUID);
    survey1B.setIdentifier(SURVEY_ID);
    survey1B.setCreatedOn(CREATED_ON - 2000L);
    survey1B.setDeleted(true);
    mockSurveyMapper(survey1B, survey1A);
    mockSurveyElementMapper();
    List<Survey> results = dao.getSurveyAllVersions(TEST_APP_ID, SURVEY_IDENTIFIER, true);
    assertEquals(results, ImmutableList.of(survey1A, survey1B));
    verify(mockSurveyMapper).queryPage(eq(DynamoSurvey.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoSurvey> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getAppId(), TEST_APP_ID);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), EQ.name());
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), SURVEY_ID);
    assertNull(query.getQueryFilter());
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) Survey(org.sagebionetworks.bridge.models.surveys.Survey) Test(org.testng.annotations.Test)

Example 19 with RangeKeyCondition

use of com.amazonaws.services.dynamodbv2.document.RangeKeyCondition 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 20 with RangeKeyCondition

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

the class DynamoHealthDataDocumentationDaoTest method getDocumentationForParentId.

@SuppressWarnings("unchecked")
@Test
public void getDocumentationForParentId() {
    // mock dependencies
    DynamoHealthDataDocumentation doc = new DynamoHealthDataDocumentation();
    doReturn(ImmutableList.of(doc)).when(dao).queryHelper(any());
    // execute
    ForwardCursorPagedResourceList<HealthDataDocumentation> resultList = dao.getDocumentationForParentId(TEST_APP_ID, API_DEFAULT_PAGE_SIZE, OFFSET_KEY);
    assertEquals(resultList.getItems().size(), 1);
    assertSame(resultList.getItems().get(0), doc);
    assertNull(resultList.getNextPageOffsetKey());
    // validate
    ArgumentCaptor<DynamoDBQueryExpression<DynamoHealthDataDocumentation>> queryCaptor = ArgumentCaptor.forClass(DynamoDBQueryExpression.class);
    verify(dao).queryHelper(queryCaptor.capture());
    DynamoDBQueryExpression<DynamoHealthDataDocumentation> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getParentId(), TEST_APP_ID);
    assertEquals(query.getLimit().intValue(), API_DEFAULT_PAGE_SIZE + 1);
    assertEquals(query.getRangeKeyConditions().size(), 1);
    Condition rangeKeyCondition = query.getRangeKeyConditions().get("identifier");
    assertEquals(rangeKeyCondition.getComparisonOperator(), ComparisonOperator.GE.toString());
    assertEquals(rangeKeyCondition.getAttributeValueList().size(), 1);
    assertEquals(rangeKeyCondition.getAttributeValueList().get(0).getS(), OFFSET_KEY);
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) HealthDataDocumentation(org.sagebionetworks.bridge.models.HealthDataDocumentation) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) Test(org.testng.annotations.Test)

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