private Variable getVariable(VariableSet variableSet, String variableId) throws CatalogDBException { for (Variable variable : variableSet.getVariables()) { if (variable.getId().equals(variableId)) { return variable; } } return null; }
public static void checkVariableSet(VariableSet variableSet) throws CatalogException { Set<String> variableIdSet = new HashSet<>(); for (Variable variable : variableSet.getVariables()) { if (variableIdSet.contains(variable.getId())) { throw new CatalogException("Duplicated variable Id"); } variableIdSet.add(variable.getId()); } for (Variable variable : variableSet.getVariables()) { checkVariable(variable); } }
@Test public void checkStringList2Test() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(stringList.getId(), Arrays.asList("value1", "value2", "value3")); AnnotationUtils.checkAnnotations(Collections.singletonMap(stringList.getId(), stringList), annotation); }
@Test public void checkObjectTest() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(object.getId(), new ObjectMap(string.getId(), "OneString") .append(numberList.getId(), Arrays.asList(1, "2", 3))); AnnotationUtils.checkAnnotations(Collections.singletonMap(object.getId(), object), annotation); }
@Test public void checkNestedObjectTest() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(nestedObject.getId(), new ObjectMap(stringList.getId(), Arrays.asList("v1", "v2", "v3")) .append(object.getId(), new ObjectMap(string.getId(), "OneString") .append(numberList.getId(), Arrays.asList (1, 2, "3"))) ); AnnotationUtils.checkAnnotations(Collections.singletonMap(nestedObject.getId(), nestedObject), annotation); }
@Test public void checkNestedObjectTest3() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(nestedObject.getId(), new ObjectMap(stringList.getId(), Arrays.asList("v1", "v2", "v3")) .append(object .getId(), new ObjectMap(string.getId(), "OneString"))); thrown.expect(CatalogException.class); //Numeric list is required AnnotationUtils.checkAnnotations(Collections.singletonMap(nestedObject.getId(), nestedObject), annotation); }
@Test public void checkNestedObjectTest2() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(nestedObject.getId(), new ObjectMap(stringList.getId(), Arrays.asList("v1", "v2", "v3")) .append(object.getId(), new ObjectMap(string.getId(), "OneString") .append(numberList.getId(), Arrays.asList(1, "K", "3"))) ); thrown.expect(CatalogException.class); //Bad value for numericList "K" AnnotationUtils.checkAnnotations(Collections.singletonMap(nestedObject.getId(), nestedObject), annotation); }
@Test public void checkObjectTest2() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(object.getId(), new ObjectMap(string.getId(), "OneString") .append(numberList.getId(), Arrays.asList(1, 2, "K"))); thrown.expect(CatalogException.class); AnnotationUtils.checkAnnotations(Collections.singletonMap(object.getId(), object), annotation); }
@Test public void checkStringTest2() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(string.getId(), Arrays.asList("value1", "value2", "value3")); thrown.expect(CatalogException.class); AnnotationUtils.checkAnnotations(Collections.singletonMap(string.getId(), string), annotation); }
@Test public void checkNumberListTest2() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(stringList.getId(), Arrays.asList(1, "NOT_A_NUMBER", 3.3)); thrown.expect(CatalogException.class); AnnotationUtils.checkAnnotations(Collections.singletonMap(numberList.getId(), numberList), annotation); }
@Test public void checkObjectTest3() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(object.getId(), new ObjectBean("string Key", Arrays.<Double>asList(3.3, 4.0, 5.0))); AnnotationUtils.checkAnnotations(Collections.singletonMap(object.getId(), object), annotation); }
@Test public void checkNullValues2Test() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(string.getId(), null); thrown.expect(CatalogException.class); //Numeric list is required AnnotationUtils.checkAnnotations(Collections.singletonMap(string.getId(), string), annotation); }
@Test public void checkFreeObjectTest() throws CatalogException { Map<String, Object> annotation = new HashMap<>(); annotation.put(freeObject.getId(), new ObjectMap("ANY_KEY", "SOME_VALUE").append("A_BOOLEAN", false)); AnnotationUtils.checkAnnotations(Collections.singletonMap(freeObject.getId(), freeObject), annotation); }
@Test public void testRemoveFieldFromVariableSet() throws CatalogDBException, CatalogAuthorizationException { QueryResult<VariableSet> variableSetQueryResult = createExampleVariableSet("VARSET_1", false); QueryResult<VariableSet> queryResult = catalogStudyDBAdaptor.removeFieldFromVariableSet(variableSetQueryResult.first().getUid(), "NAME", user3.getId()); assertTrue(queryResult.first().getVariables() .stream() .filter(v -> "NAME".equals(v.getId())) .collect(Collectors.toList()).isEmpty()); }
public QueryResult<VariableSet> addFieldToVariableSet(String studyStr, String variableSetStr, Variable variable, String sessionId) throws CatalogException { if (StringUtils.isEmpty(variable.getId())) { if (StringUtils.isEmpty(variable.getName())) { throw new CatalogException("Missing variable id"); } variable.setId(variable.getName()); } MyResourceId resource = getVariableSetId(variableSetStr, studyStr, sessionId); String userId = resource.getUser(); authorizationManager.checkCanCreateUpdateDeleteVariableSets(resource.getStudyId(), userId); QueryResult<VariableSet> queryResult = studyDBAdaptor.addFieldToVariableSet(resource.getResourceId(), variable, userId); auditManager.recordDeletion(AuditRecord.Resource.variableSet, resource.getResourceId(), userId, queryResult.first(), null, null); return queryResult; }
/** * Creates a new variable once and attempts to create the same one again. * @throws CatalogDBException */ @Test public void addFieldToVariableSetTest1() throws CatalogDBException, CatalogAuthorizationException { createExampleVariableSet("VARSET_1", false); createExampleVariableSet("VARSET_2", true); Variable variable = new Variable("NAM", "", Variable.VariableType.TEXT, "", true, false, Collections.emptyList(), 0, "", "", null, Collections.emptyMap()); QueryResult<VariableSet> queryResult = catalogStudyDBAdaptor.addFieldToVariableSet(18, variable, user3.getId()); // Check that the new variable has been inserted in the variableSet assertTrue(queryResult.first().getVariables().stream().filter(variable1 -> variable.getId().equals(variable1.getId())).findAny() .isPresent()); // We try to insert the same one again. thrown.expect(CatalogDBException.class); thrown.expectMessage("already exist"); catalogStudyDBAdaptor.addFieldToVariableSet(18, variable, user3.getId()); }