Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-481: Clean up parameter passing for Condition Service Search #510

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
*/
package org.openmrs.module.fhir2.api.impl;

import java.util.HashSet;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.annotation.Sort;
import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.QuantityAndListParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -30,7 +21,7 @@
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude_2_2;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.api.translators.ConditionTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -56,22 +47,11 @@ public class FhirConditionServiceImpl_2_2 extends BaseFhirService<Condition, org
private SearchQuery<org.openmrs.Condition, Condition, FhirConditionDao<org.openmrs.Condition>, ConditionTranslator<org.openmrs.Condition>, SearchQueryInclude<Condition>> searchQuery;

@Override
public IBundleProvider searchConditions(ReferenceAndListParam patientParam, TokenAndListParam code,
TokenAndListParam clinicalStatus, DateRangeParam onsetDate, QuantityAndListParam onsetAge,
DateRangeParam recordedDate, TokenAndListParam id, DateRangeParam lastUpdated, @Sort SortSpec sort,
HashSet<Include> includes) {

SearchParameterMap theParams = new SearchParameterMap()
.addParameter(FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER, patientParam)
.addParameter(FhirConstants.CODED_SEARCH_HANDLER, code)
.addParameter(FhirConstants.CONDITION_CLINICAL_STATUS_HANDLER, clinicalStatus)
.addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAge)
.addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "onsetDate", onsetDate)
.addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "dateCreated", recordedDate)
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.ID_PROPERTY, id)
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.LAST_UPDATED_PROPERTY, lastUpdated)
.addParameter(FhirConstants.INCLUDE_SEARCH_HANDLER, includes).setSortSpec(sort);
public IBundleProvider searchConditions(ConditionSearchParams conditionSearchParams) {
conditionSearchParams.toSearchParameterMap().addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "onsetDate",
conditionSearchParams.getOnsetDate());

return searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
return searchQuery.getQueryResults(conditionSearchParams.toSearchParameterMap(), dao, translator,
searchQueryInclude);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openmrs.module.fhir2.api.search.SearchQueryBundleProvider;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude_2_2;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.fhir2.api.translators.ConditionTranslator;

Expand Down Expand Up @@ -262,8 +263,8 @@ public void searchConditions_shouldReturnTranslatedConditionReturnedByDao() {
when(searchQueryInclude.getIncludedResources(any(), any())).thenReturn(Collections.emptySet());
when(conditionTranslator.toFhirResource(openmrsCondition)).thenReturn(fhirCondition);

IBundleProvider result = conditionService.searchConditions(patientReference, codeList, clinicalList, onsetDate,
onsetAge, recordDate, uuid, lastUpdated, sort, includes);
IBundleProvider result = conditionService.searchConditions(new ConditionSearchParams(patientReference, codeList,
clinicalList, onsetDate, onsetAge, recordDate, uuid, lastUpdated, sort, includes));

List<IBaseResource> resultList = get(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,11 @@
*/
package org.openmrs.module.fhir2.api;

import java.util.HashSet;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.annotation.Sort;
import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.QuantityAndListParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import org.hl7.fhir.r4.model.Condition;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;

public interface FhirConditionService extends FhirService<Condition> {

IBundleProvider searchConditions(ReferenceAndListParam patientParam, TokenAndListParam code,
TokenAndListParam clinicalStatus, DateRangeParam onsetDate, QuantityAndListParam onsetAge,
DateRangeParam recordedDate, TokenAndListParam id, DateRangeParam lastUpdated, @Sort SortSpec sort,
HashSet<Include> includes);
IBundleProvider searchConditions(ConditionSearchParams conditionSearchParams);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@

import javax.transaction.Transactional;

import java.util.HashSet;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.annotation.Sort;
import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.api.server.IBundleProvider;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.QuantityAndListParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -32,7 +23,7 @@
import org.openmrs.module.fhir2.api.dao.FhirConditionDao;
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.api.translators.ConditionTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -57,20 +48,11 @@ public class FhirConditionServiceImpl extends BaseFhirService<Condition, Obs> im
private SearchQuery<org.openmrs.Obs, Condition, FhirConditionDao<org.openmrs.Obs>, ConditionTranslator<org.openmrs.Obs>, SearchQueryInclude<Condition>> searchQuery;

@Override
public IBundleProvider searchConditions(ReferenceAndListParam patientParam, TokenAndListParam code,
TokenAndListParam clinicalStatus, DateRangeParam onsetDate, QuantityAndListParam onsetAge,
DateRangeParam recordedDate, TokenAndListParam id, DateRangeParam lastUpdated, @Sort SortSpec sort,
HashSet<Include> includes) {
public IBundleProvider searchConditions(ConditionSearchParams conditionSearchParams) {
conditionSearchParams.toSearchParameterMap().addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "obsDatetime",
conditionSearchParams.getOnsetDate());

SearchParameterMap theParams = new SearchParameterMap()
.addParameter(FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER, patientParam)
.addParameter(FhirConstants.CODED_SEARCH_HANDLER, code)
.addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, onsetAge)
.addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "obsDatetime", onsetDate)
.addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "dateCreated", recordedDate)
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.ID_PROPERTY, id)
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.LAST_UPDATED_PROPERTY, lastUpdated)
.addParameter(FhirConstants.INCLUDE_SEARCH_HANDLER, includes).setSortSpec(sort);
return searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
return searchQuery.getQueryResults(conditionSearchParams.toSearchParameterMap(), dao,
translator, searchQueryInclude);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.search.param;

import java.util.HashSet;

import ca.uhn.fhir.model.api.Include;
import ca.uhn.fhir.rest.api.SortSpec;
import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.QuantityAndListParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import ca.uhn.fhir.rest.param.TokenAndListParam;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.openmrs.module.fhir2.FhirConstants;

@Data
@NoArgsConstructor

Check warning on line 27 in api/src/main/java/org/openmrs/module/fhir2/api/search/param/ConditionSearchParams.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/api/search/param/ConditionSearchParams.java#L26-L27

Added lines #L26 - L27 were not covered by tests
@EqualsAndHashCode(callSuper = true)
public class ConditionSearchParams extends BaseResourceSearchParams {

private ReferenceAndListParam patientParam;

private TokenAndListParam code;

private TokenAndListParam clinicalStatus;

private DateRangeParam onsetDate;

private QuantityAndListParam onsetAge;

private DateRangeParam recordedDate;

@Builder

Check warning on line 43 in api/src/main/java/org/openmrs/module/fhir2/api/search/param/ConditionSearchParams.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/fhir2/api/search/param/ConditionSearchParams.java#L43

Added line #L43 was not covered by tests
public ConditionSearchParams(ReferenceAndListParam patientParam, TokenAndListParam code,
TokenAndListParam clinicalStatus, DateRangeParam onsetDate, QuantityAndListParam onsetAge,
DateRangeParam recordedDate, TokenAndListParam id, DateRangeParam lastUpdated, SortSpec sort,
HashSet<Include> includes) {

super(id, lastUpdated, sort, includes, null);

this.patientParam = patientParam;
this.code = code;
this.clinicalStatus = clinicalStatus;
this.onsetDate = onsetDate;
this.onsetAge = onsetAge;
this.recordedDate = recordedDate;
}

@Override
public SearchParameterMap toSearchParameterMap() {
return baseSearchParameterMap().addParameter(FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER, getPatientParam())
.addParameter(FhirConstants.CODED_SEARCH_HANDLER, getCode())
.addParameter(FhirConstants.CONDITION_CLINICAL_STATUS_HANDLER, getClinicalStatus())
.addParameter(FhirConstants.QUANTITY_SEARCH_HANDLER, getOnsetAge())
.addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, "dateCreated", getRecordedDate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.openmrs.module.fhir2.api.FhirConditionService;
import org.openmrs.module.fhir2.api.annotations.R3Provider;
import org.openmrs.module.fhir2.api.search.SearchQueryBundleProviderR3Wrapper;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -121,7 +122,7 @@ public IBundleProvider searchConditions(
includes = null;
}

return new SearchQueryBundleProviderR3Wrapper(conditionService.searchConditions(patientParam, code, clinicalStatus,
onsetDate, onsetAge, recordedDate, id, lastUpdated, sort, includes));
return new SearchQueryBundleProviderR3Wrapper(conditionService.searchConditions(new ConditionSearchParams(
patientParam, code, clinicalStatus, onsetDate, onsetAge, recordedDate, id, lastUpdated, sort, includes)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.hl7.fhir.r4.model.Patient;
import org.openmrs.module.fhir2.api.FhirConditionService;
import org.openmrs.module.fhir2.api.annotations.R4Provider;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.providers.util.FhirProviderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -130,8 +131,8 @@ public IBundleProvider searchConditions(
includes = null;
}

return conditionService.searchConditions(patientParam, code, clinicalStatus, onsetDate, onsetAge, recordedDate, id,
lastUpdated, sort, includes);
return conditionService.searchConditions(new ConditionSearchParams(patientParam, code, clinicalStatus, onsetDate,
onsetAge, recordedDate, id, lastUpdated, sort, includes));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryBundleProvider;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.fhir2.api.translators.ConditionTranslator;

Expand Down Expand Up @@ -200,9 +201,6 @@ public void searchConditions_shouldReturnTranslatedConditionReturnedByDao() {
TokenAndListParam codeList = new TokenAndListParam();
codeList.addValue(new TokenOrListParam().add(new TokenParam("test code")));

TokenAndListParam clinicalList = new TokenAndListParam();
clinicalList.addValue(new TokenOrListParam().add(new TokenParam("test clinical")));

DateRangeParam onsetDate = new DateRangeParam().setLowerBound("gt2020-05-01").setUpperBound("lt2021-05-01");

QuantityAndListParam onsetAge = new QuantityAndListParam();
Expand Down Expand Up @@ -234,8 +232,8 @@ public void searchConditions_shouldReturnTranslatedConditionReturnedByDao() {
when(searchQueryInclude.getIncludedResources(any(), any())).thenReturn(Collections.emptySet());
when(translator.toFhirResource(obsCondition)).thenReturn(condition);

IBundleProvider result = fhirConditionService.searchConditions(patientReference, codeList, clinicalList, onsetDate,
onsetAge, recordDate, uuid, lastUpdated, sort, includes);
IBundleProvider result = fhirConditionService.searchConditions(new ConditionSearchParams(patientReference, codeList,
null, onsetDate, onsetAge, recordDate, uuid, lastUpdated, sort, includes));

List<IBaseResource> resultList = get(result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirConditionService;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.providers.r4.MockIBundleProvider;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -181,9 +182,9 @@ public void searchConditions_shouldReturnConditionReturnedByService() {

HashSet<Include> includes = new HashSet<>();

when(conditionService.searchConditions(patientReference, codeList, clinicalList, onsetDate, onsetAge, recordDate,
uuid, lastUpdated, sort, null))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));
when(conditionService.searchConditions(new ConditionSearchParams(patientReference, codeList, clinicalList, onsetDate,
onsetAge, recordDate, uuid, lastUpdated, sort, null)))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));

IBundleProvider result = resourceProvider.searchConditions(patientReference, subjectReference, codeList,
clinicalList, onsetDate, onsetAge, recordDate, uuid, lastUpdated, sort, includes);
Expand Down Expand Up @@ -221,9 +222,9 @@ public void searchConditions_shouldReturnConditionReturnedByServiceWhenPatientIs

HashSet<Include> includes = new HashSet<>();

when(conditionService.searchConditions(subjectReference, codeList, clinicalList, onsetDate, onsetAge, recordDate,
uuid, lastUpdated, sort, null))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));
when(conditionService.searchConditions(new ConditionSearchParams(subjectReference, codeList, clinicalList, onsetDate,
onsetAge, recordDate, uuid, lastUpdated, sort, null)))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));

IBundleProvider result = resourceProvider.searchConditions(subjectReference, subjectReference, codeList,
clinicalList, onsetDate, onsetAge, recordDate, uuid, lastUpdated, sort, includes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirConditionService;
import org.openmrs.module.fhir2.api.search.param.ConditionSearchParams;
import org.openmrs.module.fhir2.providers.BaseFhirProvenanceResourceTest;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -175,9 +176,9 @@ public void searchConditions_shouldReturnConditionReturnedByService() {

HashSet<Include> includes = new HashSet<>();

when(conditionService.searchConditions(patientReference, codeList, clinicalList, onsetDate, onsetAge, recordDate,
uuid, lastUpdated, sort, null))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));
when(conditionService.searchConditions(new ConditionSearchParams(patientReference, codeList, clinicalList, onsetDate,
onsetAge, recordDate, uuid, lastUpdated, sort, null)))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));

IBundleProvider result = resourceProvider.searchConditions(patientReference, subjectReference, codeList,
clinicalList, onsetDate, onsetAge, recordDate, uuid, lastUpdated, sort, includes);
Expand Down Expand Up @@ -215,9 +216,9 @@ public void searchConditions_shouldReturnConditionReturnedByServiceWhenPatientIs

HashSet<Include> includes = new HashSet<>();

when(conditionService.searchConditions(subjectReference, codeList, clinicalList, onsetDate, onsetAge, recordDate,
uuid, lastUpdated, sort, null))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));
when(conditionService.searchConditions(new ConditionSearchParams(subjectReference, codeList, clinicalList, onsetDate,
onsetAge, recordDate, uuid, lastUpdated, sort, null)))
.thenReturn(new MockIBundleProvider<>(Collections.singletonList(condition), 10, 1));

IBundleProvider result = resourceProvider.searchConditions(subjectReference, subjectReference, codeList,
clinicalList, onsetDate, onsetAge, recordDate, uuid, lastUpdated, sort, includes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ public void shouldSearchForAllConditionsAsJson() throws Exception {

@Test
public void shouldReturnSortedAndFilteredSearchResultsForConditionsAsJson() throws Exception {
MockHttpServletResponse response = get("/Condition?clinical-status=active&onset-date=2008&_sort=-onset-date")
.accept(FhirMediaTypes.JSON).go();
MockHttpServletResponse response = get("/Condition?clinical-status=active?onset-date=2008&_sort=-onset-date").accept(FhirMediaTypes.JSON)
.go();

assertThat(response, isOk());
assertThat(response.getContentType(), is(FhirMediaTypes.JSON.toString()));
Expand Down Expand Up @@ -410,8 +410,8 @@ public void shouldSearchForAllConditionsAsXML() throws Exception {

@Test
public void shouldReturnSortedAndFilteredSearchResultsForConditionsAsXML() throws Exception {
MockHttpServletResponse response = get("/Condition?clinical-status=active&onset-date=2008&_sort=-onset-date")
.accept(FhirMediaTypes.XML).go();
MockHttpServletResponse response = get("/Condition?onset-date=2008&_sort=-onset-date").accept(FhirMediaTypes.XML)
.go();

assertThat(response, isOk());
assertThat(response.getContentType(), is(FhirMediaTypes.XML.toString()));
Expand Down
Loading
Loading