Skip to content

Commit

Permalink
wip - fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cbioportal import user committed Jan 3, 2025
1 parent 82cc2b6 commit 96e2bd8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.cbioportal.model.ClinicalAttribute;
import org.cbioportal.model.ClinicalDataBin;
import org.cbioportal.model.ClinicalDataCount;
import org.cbioportal.model.ClinicalDataCountItem;
import org.cbioportal.persistence.fedapi.FederatedDataSource;
import org.cbioportal.persistence.fedapi.FederatedDataSourceImpl;
Expand All @@ -26,6 +27,9 @@
@Service
@ConditionalOnProperty(name = "fedapi.mode", havingValue = "DATASOURCE")
public class FederatedDataSourceService implements FederatedService {

@Value("fedapi.datasource.display-name")
private String displayName;

@Value("#{'${fedapi.datasource.visible-studies}'.split(',')}")
private List<String> visibleStudies;
Expand All @@ -47,7 +51,24 @@ public class FederatedDataSourceService implements FederatedService {

@Override
public List<ClinicalAttribute> fetchClinicalAttributes() {
return clinicalAttributeService.fetchClinicalAttributes(this.visibleStudies, "SUMMARY");
var result = clinicalAttributeService.fetchClinicalAttributes(visibleStudies, "SUMMARY");
return addDataSourceToClinicalAttributes(result);
}

private List<ClinicalAttribute> addDataSourceToClinicalAttributes(List<ClinicalAttribute> result) {
for (String studyId : visibleStudies) {
var dataSourceAttr = new ClinicalAttribute();
dataSourceAttr.setAttrId("DATA_SOURCE");
dataSourceAttr.setPatientAttribute(false);
dataSourceAttr.setPriority("1");
dataSourceAttr.setCancerStudyIdentifier(studyId);
dataSourceAttr.setDatatype("STRING");
dataSourceAttr.setDescription("Name of the federated data source this sample originated from.");
dataSourceAttr.setDisplayName("Data Source");

result.add(dataSourceAttr);
}
return result;
}

@Override
Expand All @@ -58,6 +79,23 @@ public List<ClinicalDataCountItem> fetchClinicalDataCounts(
filter.getStudyViewFilter().setStudyIds(visibleStudies);
return cachedClinicalDataCounts(filter);
}

private List<ClinicalDataCountItem> addDataSourceToClinicalDataCounts(
List<ClinicalDataCountItem> result,
int sampleCount
) {
var dataSourceCount = new ClinicalDataCount();
dataSourceCount.setAttributeId("DATA_SOURCE");
dataSourceCount.setValue(displayName);
dataSourceCount.setCount(sampleCount);

var dataSourceCountItem = new ClinicalDataCountItem();
dataSourceCountItem.setAttributeId("DATA_SOURCE");
dataSourceCountItem.setCounts(List.of(dataSourceCount));

result.add(dataSourceCountItem);
return result;
}

public List<ClinicalDataCountItem> cachedClinicalDataCounts(ClinicalDataCountFilter interceptedClinicalDataCountFilter) {
List<ClinicalDataFilter> attributes = interceptedClinicalDataCountFilter.getAttributes();
Expand All @@ -77,6 +115,7 @@ public List<ClinicalDataCountItem> cachedClinicalDataCounts(ClinicalDataCountFil
List<ClinicalDataCountItem> result = clinicalDataService.fetchClinicalDataCounts(
studyIds, sampleIds, attributes.stream().map(a -> a.getAttributeId()).collect(Collectors.toList()));

result = addDataSourceToClinicalDataCounts(result, sampleIds.size());
return result;
}

Expand All @@ -97,14 +136,4 @@ public List<ClinicalDataBin> cachedFetchClinicalDataBinCounts(ClinicalDataBinCou
false
);
}

private List<String> filterStudyIds(List<String> requestedStudyIds) {
var result = new ArrayList<String>();
for (String studyId : requestedStudyIds) {
if (visibleStudies.contains(studyId)) {
result.add(studyId);
}
}
return requestedStudyIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,41 +128,6 @@ public void setup() {
// This is necessary for initializing Mockito mocks pre-JUnit 4.5
// See: https://stackoverflow.com/a/15494996/4077294
MockitoAnnotations.initMocks(this);

// List<Sample> samples = mockSamples();
//
// // Methods used by fetchClinicalDataCounts
//
// // When cross-checking list of attributes requested
// when(
// clinicalAttributeRepository.getClinicalAttributesByStudyIdsAndAttributeIds(
// /* studyIds */ any(),
// /* attributeIds */ any()
// )
// ).thenReturn(mockClinicalAttributes());

// // When applying the study view filter to get a list of samples
// when(
// sampleRepository.fetchSamples(
// /* studyIds */ any(),
// /* sampleIds */ any(),
// /* projection */ any()
// )
// ).thenReturn(samples);
//
// // When tallying the counts
// when(
// clinicalDataRepository.fetchClinicalDataCounts(
// /* studyIds */ any(),
// /* sampleIds */ any(),
// /* attributeIds */ any(),
// /* clinicalDataType */ any(),
// /* projection */ any()
// )
// ).thenReturn(...);
//
// // Methods used by fetchClinicalDataBinCounts

}

private List<ClinicalAttribute> mockPatientAttributes() {
Expand Down Expand Up @@ -443,7 +408,7 @@ public void fetchClinicalAttributes() {
/* studyIds */ eq(VISIBLE_STUDIES),
/* projection */ eq("SUMMARY")
)
).thenReturn(attribs);
).thenReturn(new ArrayList<>(attribs)); // create a copy so the original does not get modified

var result = federatedDataSourceService.fetchClinicalAttributes();

Expand All @@ -468,7 +433,7 @@ public void fetchClinicalDataCounts() {
/* studyIds */ any(),
/* attributeIds */ any()
)
).thenReturn(attribs);
).thenReturn(new ArrayList<>(attribs));

// When applying the study view filter to get a list of samples
when(
Expand All @@ -480,7 +445,7 @@ public void fetchClinicalDataCounts() {
/* sortBy */ isNull(),
/* direction */ isNull()
)
).thenReturn(samples);
).thenReturn(new ArrayList<>(samples));

// When tallying the counts
when(
Expand All @@ -499,7 +464,7 @@ public void fetchClinicalDataCounts() {
/* studyIds */ any(),
/* sampleIds */ any()
)
).thenReturn(patients);
).thenReturn(new ArrayList<>(patients));

var result = federatedDataSourceService.fetchClinicalDataCounts(filter);

Expand Down Expand Up @@ -532,7 +497,7 @@ public void fetchClinicalDataBinCounts() {
/* studyIds */ any(),
/* attributeIds */ any()
)
).thenReturn(attribs);
).thenReturn(new ArrayList<>(attribs));

// When applying the study view filter to get a list of samples
when(
Expand All @@ -544,7 +509,7 @@ public void fetchClinicalDataBinCounts() {
/* sortBy */ isNull(),
/* direction */ isNull()
)
).thenReturn(samples);
).thenReturn(new ArrayList<>(samples));

// When fetching clinical data for the samples pre-binning
when(
Expand All @@ -563,7 +528,7 @@ public void fetchClinicalDataBinCounts() {
/* studyIds */ any(),
/* sampleIds */ any()
)
).thenReturn(patients);
).thenReturn(new ArrayList<>(patients));

var result = federatedDataSourceService.fetchClinicalDataBinCounts(filter);

Expand Down

0 comments on commit 96e2bd8

Please sign in to comment.