From 73b867fe046ac98c47a6e6f7e6fa7f1210d772ce Mon Sep 17 00:00:00 2001 From: alisman Date: Mon, 11 Nov 2024 13:13:36 -0500 Subject: [PATCH] Add tests --- .../impl/StudyViewColumnarServiceImpl.java | 21 ++----- .../util/StudyViewColumnarServiceUtil.java | 35 +++++++++++ .../StudyViewColumnarServiceUtilTest.java | 58 +++++++++++++++++++ 3 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java create mode 100644 src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java diff --git a/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java b/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java index 35de5e9aec2..49cafd324ed 100644 --- a/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java +++ b/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java @@ -19,6 +19,7 @@ import org.cbioportal.service.StudyViewColumnarService; import org.cbioportal.service.exception.StudyNotFoundException; import org.cbioportal.service.treatment.TreatmentCountReportService; +import org.cbioportal.service.util.StudyViewColumnarServiceUtil; import org.cbioportal.web.parameter.ClinicalDataType; import org.cbioportal.web.parameter.CustomSampleIdentifier; import org.cbioportal.web.parameter.GenericAssayDataBinFilter; @@ -159,24 +160,12 @@ public List getClinicalDataCounts(StudyViewFilter studyVi // fetch the samples by using the provided study view filter List filteredSamples = getFilteredSamples(studyViewFilter); - Map map = - result.stream().collect(Collectors.toMap(ClinicalDataCountItem::getAttributeId, item -> item)); - - filteredAttributes.stream().forEach(attr -> { - if (!map.containsKey(attr)) { - ClinicalDataCountItem newItem = new ClinicalDataCountItem(); - newItem.setAttributeId(attr); - ClinicalDataCount count = new ClinicalDataCount(); - count.setCount(filteredSamples.size()); - count.setValue("NA"); - count.setAttributeId(attr); - newItem.setCounts(Arrays.asList(count)); - result.add(newItem); - } - }); + var resultWithAllAttributes = StudyViewColumnarServiceUtil.addClinicalDataCountsForMissingAttributes(result, filteredAttributes, filteredSamples); - return result; + return resultWithAllAttributes; } + + @Cacheable( cacheResolver = "staticRepositoryCacheOneResolver", diff --git a/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java b/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java new file mode 100644 index 00000000000..e4210ebbcb4 --- /dev/null +++ b/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java @@ -0,0 +1,35 @@ +package org.cbioportal.service.util; + +import org.cbioportal.model.ClinicalDataCount; +import org.cbioportal.model.ClinicalDataCountItem; +import org.cbioportal.model.Sample; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class StudyViewColumnarServiceUtil { + + public static List addClinicalDataCountsForMissingAttributes(List counts, List filteredAttributes, List filteredSamples) { + Map map = counts.stream() + .collect(Collectors.toMap(ClinicalDataCountItem::getAttributeId, item -> item)); + + List result = new ArrayList<>(counts); + + filteredAttributes.forEach(attr -> { + if (!map.containsKey(attr)) { + ClinicalDataCountItem newItem = new ClinicalDataCountItem(); + newItem.setAttributeId(attr); + ClinicalDataCount count = new ClinicalDataCount(); + count.setCount(filteredSamples.size()); + count.setValue("NA"); + count.setAttributeId(attr); + newItem.setCounts(List.of(count)); + result.add(newItem); + } + }); + + return result; + } +} \ No newline at end of file diff --git a/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java b/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java new file mode 100644 index 00000000000..18894827ac1 --- /dev/null +++ b/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java @@ -0,0 +1,58 @@ +package org.cbioportal.service.util; + +import org.cbioportal.model.ClinicalDataCount; +import org.cbioportal.model.ClinicalDataCountItem; +import org.cbioportal.model.Sample; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.*; + +public class StudyViewColumnarServiceUtilTest { + + @Test + public void testAddClinicalDataCountsForMissingAttributes() { + // Prepare the input data + ClinicalDataCountItem existingItem = new ClinicalDataCountItem(); + existingItem.setAttributeId("existingAttr"); + ClinicalDataCount existingCount = new ClinicalDataCount(); + existingCount.setCount(1); + existingCount.setValue("value1"); + existingItem.setCounts(Collections.singletonList(existingCount)); + List result = Collections.singletonList(existingItem); + + List filteredAttributes = Arrays.asList("existingAttr","attr1", "attr2"); + List filteredSamples = Arrays.asList(new Sample(), new Sample()); + + // Call the method under test + List updatedResult = StudyViewColumnarServiceUtil.addClinicalDataCountsForMissingAttributes(result, filteredAttributes, filteredSamples); + + // code adds missing attributes from filteredAttributes + Assert.assertEquals(3, updatedResult.size()); + + ClinicalDataCountItem item1 = updatedResult.get(0); + Assert.assertEquals("existingAttr", item1.getAttributeId()); + Assert.assertEquals(1, item1.getCounts().size()); + Assert.assertEquals("value1", item1.getCounts().get(0).getValue()); + + // the added attributes (they were filtered out) + // have counts for NA value equal to the total sample count + ClinicalDataCountItem item2 = updatedResult.get(1); + Assert.assertEquals("attr1", item2.getAttributeId()); + Assert.assertEquals(1, item2.getCounts().size()); + Assert.assertEquals("NA", item2.getCounts().get(0).getValue()); + Assert.assertEquals(2, item2.getCounts().get(0).getCount().intValue()); + + // ditto the third item + ClinicalDataCountItem item3 = updatedResult.get(2); + Assert.assertEquals("attr2", item3.getAttributeId()); + Assert.assertEquals(1, item3.getCounts().size()); + Assert.assertEquals("NA", item3.getCounts().get(0).getValue()); + Assert.assertEquals(2, item3.getCounts().get(0).getCount().intValue()); + } + +} \ No newline at end of file