Search in sources :

Example 1 with AppConfigElement

use of org.sagebionetworks.bridge.models.appconfig.AppConfigElement in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppConfigElementDao method getMostRecentElements.

@Override
public List<AppConfigElement> getMostRecentElements(String appId, boolean includeDeleted) {
    DynamoAppConfigElement key = new DynamoAppConfigElement();
    key.setAppId(appId);
    DynamoDBQueryExpression<DynamoAppConfigElement> query = new DynamoDBQueryExpression<DynamoAppConfigElement>().withIndexName(STUDY_ID_INDEX_NAME).withHashKeyValues(key).withConsistentRead(false).withScanIndexForward(false);
    List<DynamoAppConfigElement> elementIndices = mapper.query(DynamoAppConfigElement.class, query);
    Map<String, AppConfigElement> versionMap = Maps.newHashMap();
    Map<String, List<Object>> resultMap = mapper.batchLoad(elementIndices);
    for (List<Object> resultList : resultMap.values()) {
        for (Object oneResult : resultList) {
            if (!(oneResult instanceof DynamoAppConfigElement)) {
                // This should never happen, but just in case.
                throw new BridgeServiceException("DynamoDB returned objects of type " + oneResult.getClass().getName() + " instead of DynamoAppConfigElement");
            }
            DynamoAppConfigElement oneElement = (DynamoAppConfigElement) oneResult;
            if (!includeDeleted && oneElement.isDeleted()) {
                continue;
            }
            AppConfigElement existingElement = versionMap.get(oneElement.getId());
            Long existingRevision = (existingElement == null) ? null : existingElement.getRevision();
            if (existingRevision == null || oneElement.getRevision() > existingRevision) {
                versionMap.put(oneElement.getId(), oneElement);
            }
        }
    }
    List<AppConfigElement> elements = Lists.newArrayList(versionMap.values());
    Collections.sort(elements, Comparator.comparing(AppConfigElement::getId));
    return elements;
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) DynamoDBQueryExpression(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression) AppConfigElement(org.sagebionetworks.bridge.models.appconfig.AppConfigElement) List(java.util.List) PaginatedQueryList(com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList)

Example 2 with AppConfigElement

use of org.sagebionetworks.bridge.models.appconfig.AppConfigElement in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppConfigElementDaoTest method getMostRecentElement.

@Test
public void getMostRecentElement() {
    DynamoAppConfigElement element = new DynamoAppConfigElement();
    when(mockMapper.query(eq(DynamoAppConfigElement.class), any())).thenReturn(mockResults);
    when(mockResults.get(0)).thenReturn(element);
    AppConfigElement returned = dao.getMostRecentElement(TEST_APP_ID, "id");
    assertEquals(returned, element);
    verify(mockMapper).query(eq(DynamoAppConfigElement.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoAppConfigElement> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getKey(), TEST_APP_ID + ":id");
    assertFalse(query.isScanIndexForward());
    assertEquals(query.getLimit(), new Integer(1));
    Condition deleteCondition = query.getQueryFilter().get("deleted");
    assertEquals(deleteCondition.getComparisonOperator(), "EQ");
    assertFalse(deleteCondition.getAttributeValueList().get(0).getBOOL());
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AppConfigElement(org.sagebionetworks.bridge.models.appconfig.AppConfigElement) Test(org.testng.annotations.Test)

Example 3 with AppConfigElement

use of org.sagebionetworks.bridge.models.appconfig.AppConfigElement in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppConfigElementDaoTest method saveElementRevisionThrowsConditionalCheckFailedException.

// As will happen if version attribute isn't returned or is wrong
@Test(expectedExceptions = ConcurrentModificationException.class)
public void saveElementRevisionThrowsConditionalCheckFailedException() {
    doThrow(new ConditionalCheckFailedException("")).when(mockMapper).save(any());
    AppConfigElement element = TestUtils.getAppConfigElement();
    dao.saveElementRevision(element);
}
Also used : AppConfigElement(org.sagebionetworks.bridge.models.appconfig.AppConfigElement) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException) Test(org.testng.annotations.Test)

Example 4 with AppConfigElement

use of org.sagebionetworks.bridge.models.appconfig.AppConfigElement in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppConfigElementDaoTest method getElementRevisionsExcludesDeleted.

@Test
public void getElementRevisionsExcludesDeleted() {
    when(mockMapper.query(eq(DynamoAppConfigElement.class), any())).thenReturn(mockResults);
    when(mockResults.stream()).thenReturn(appConfigElementListId1().stream());
    List<AppConfigElement> returned = dao.getElementRevisions(TEST_APP_ID, ID_1, false);
    assertEquals(returned, appConfigElementListId1());
    verify(mockMapper).query(eq(DynamoAppConfigElement.class), queryCaptor.capture());
    DynamoDBQueryExpression<DynamoAppConfigElement> query = queryCaptor.getValue();
    assertEquals(query.getHashKeyValues().getKey(), TEST_APP_ID + ":id1");
    assertFalse(query.isScanIndexForward());
    Condition deleteCondition = query.getQueryFilter().get("deleted");
    assertEquals(deleteCondition.getComparisonOperator(), "EQ");
    assertFalse(deleteCondition.getAttributeValueList().get(0).getBOOL());
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AppConfigElement(org.sagebionetworks.bridge.models.appconfig.AppConfigElement) Test(org.testng.annotations.Test)

Example 5 with AppConfigElement

use of org.sagebionetworks.bridge.models.appconfig.AppConfigElement in project BridgeServer2 by Sage-Bionetworks.

the class DynamoAppConfigElementDaoTest method saveElementRevision.

@Test
public void saveElementRevision() {
    AppConfigElement element = AppConfigElement.create();
    element.setVersion(1L);
    VersionHolder returned = dao.saveElementRevision(element);
    assertEquals(returned.getVersion(), new Long(1));
    verify(mockMapper).save(element);
}
Also used : AppConfigElement(org.sagebionetworks.bridge.models.appconfig.AppConfigElement) VersionHolder(org.sagebionetworks.bridge.models.VersionHolder) Test(org.testng.annotations.Test)

Aggregations

AppConfigElement (org.sagebionetworks.bridge.models.appconfig.AppConfigElement)46 Test (org.testng.annotations.Test)29 VersionHolder (org.sagebionetworks.bridge.models.VersionHolder)8 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)4 AppConfig (org.sagebionetworks.bridge.models.appconfig.AppConfig)4 ConfigReference (org.sagebionetworks.bridge.models.schedules.ConfigReference)4 Condition (com.amazonaws.services.dynamodbv2.model.Condition)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2 CriteriaContext (org.sagebionetworks.bridge.models.CriteriaContext)2 Survey (org.sagebionetworks.bridge.models.surveys.Survey)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)1 PaginatedQueryList (com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Mockito.anyLong (org.mockito.Mockito.anyLong)1