diff --git a/opencga-analysis/src/test/java/org/opencb/opencga/analysis/family/FamilyAnalysisTest.java b/opencga-analysis/src/test/java/org/opencb/opencga/analysis/family/FamilyAnalysisTest.java index 10e3c1fc475..7ce1fe10c2b 100644 --- a/opencga-analysis/src/test/java/org/opencb/opencga/analysis/family/FamilyAnalysisTest.java +++ b/opencga-analysis/src/test/java/org/opencb/opencga/analysis/family/FamilyAnalysisTest.java @@ -170,15 +170,17 @@ public void test2Member2GenerationFamilyTest() throws CatalogException { public void updateTest() throws CatalogException { FamilyUpdateParams updateParams = new FamilyUpdateParams(); + Family prevFamily = catalogManager.getFamilyManager().get(studyId, family.getId(), null, sessionIdUser).first(); + assertEquals(prevFamily.getPedigreeGraph().getBase64(), family.getPedigreeGraph().getBase64()); + QueryOptions queryOptions = new QueryOptions() .append(ParamConstants.FAMILY_UPDATE_ROLES_PARAM, true) .append(ParamConstants.INCLUDE_RESULT_PARAM, true); Family updatedFamily = catalogManager.getFamilyManager().update(studyId, family.getId(), updateParams, queryOptions, sessionIdUser) .first(); - PedigreeGraph pedigreeGraph = updatedFamily.getPedigreeGraph(); - MatcherAssert.assertThat(pedigreeGraph.getBase64(), CoreMatchers.startsWith("iVBORw0KGgoAAAANSUhEUgAAAeAAAAHg=")); - MatcherAssert.assertThat(pedigreeGraph.getBase64(), CoreMatchers.endsWith("5UIf81hI8AAAAASUVORK5CYII=")); + assertEquals(prevFamily.getPedigreeGraph().getBase64(), updatedFamily.getPedigreeGraph().getBase64()); + assertEquals(prevFamily.getVersion() + 1, updatedFamily.getVersion()); } @Test diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/converters/FamilyConverter.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/converters/FamilyConverter.java index 16372fe2db9..b747e5b8e36 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/converters/FamilyConverter.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/converters/FamilyConverter.java @@ -16,17 +16,16 @@ package org.opencb.opencga.catalog.db.mongodb.converters; +import org.apache.commons.collections4.CollectionUtils; import org.bson.Document; import org.opencb.biodata.models.clinical.Phenotype; -import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.db.api.FamilyDBAdaptor; import org.opencb.opencga.core.models.family.Family; -import org.opencb.opencga.core.models.individual.Individual; import org.opencb.opencga.core.models.study.VariableSet; -import java.util.HashMap; +import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; /** @@ -55,40 +54,41 @@ public Document convertToStorageType(Family object, List variableSe } public void validateDocumentToUpdate(Document document) { - List memberList = (List) document.get("members"); + List memberList = document.getList("members", Document.class); if (memberList != null) { // We make sure we don't store duplicates - Map individualMap = new HashMap<>(); + Set individualSet = new HashSet<>(); for (Document individual : memberList) { - long id = individual.getInteger("uid").longValue(); - int version = individual.getInteger("version"); - if (id > 0) { - Individual tmpIndividual = new Individual() - .setVersion(version); - tmpIndividual.setUid(id); - individualMap.put(id, tmpIndividual); + long uid = individual.get("uid", Number.class).longValue(); + if (uid <= 0) { + throw new IllegalArgumentException("Missing uid value for member '" + individual.getString("id") + "'."); } + if (individualSet.contains(uid)) { + throw new IllegalArgumentException("Duplicated member '" + individual.getString("id") + " (" + uid + ")' found."); + } + individualSet.add(uid); } document.put("members", - individualMap.entrySet().stream() + memberList.stream() .map(entry -> new Document() - .append("uid", entry.getValue().getUid()) - .append("version", entry.getValue().getVersion())) + .append("uid", entry.get("uid", Number.class).longValue()) + .append("version", entry.get("version", Number.class).intValue()) + ) .collect(Collectors.toList())); } - List disorderList = (List) document.get("disorders"); + List disorderList = document.getList("disorders", Document.class); if (disorderList != null) { for (Document disorder : disorderList) { - fixPhenotypeFields((List) disorder.get("evidences")); + fixPhenotypeFields(disorder.getList("evidences", Document.class)); } } - fixPhenotypeFields((List) document.get("phenotypes")); + fixPhenotypeFields(document.getList("phenotypes", Document.class)); } private void fixPhenotypeFields(List phenotypeList) { - if (ListUtils.isNotEmpty(phenotypeList)) { + if (CollectionUtils.isNotEmpty(phenotypeList)) { for (Document phenotype : phenotypeList) { phenotype.put("status", Phenotype.Status.UNKNOWN.name()); phenotype.put("ageOfOnset", "-1");