From 1bf5cea837cbab6fb130d75abcf1c002b00e11b4 Mon Sep 17 00:00:00 2001 From: Mark Goodrich Date: Thu, 3 Oct 2024 15:28:34 -0400 Subject: [PATCH] =?UTF-8?q?PROV-108:=20Add=20Rest=20Search=20Handler=20to?= =?UTF-8?q?=20support=20searching=20for=20providers=20=E2=80=A6=20(#51)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/pom.xml | 71 --------- omod/pom.xml | 67 ++++---- .../rest/search/ProviderSearchHandler.java | 64 ++++++++ omod/src/main/resources/config.xml | 1 + .../ProviderSearchFragmentControllerTest.java | 8 +- .../search/ProviderSearchHandlerTest.java | 80 ++++++++++ pom.xml | 146 ++++++++---------- 7 files changed, 256 insertions(+), 181 deletions(-) create mode 100644 omod/src/main/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandler.java rename omod/src/test/java/org/openmrs/module/providermanagement/{ => fragment/controller}/ProviderSearchFragmentControllerTest.java (96%) create mode 100644 omod/src/test/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandlerTest.java diff --git a/api/pom.xml b/api/pom.xml index 0bd7afb..6a8c84e 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -11,60 +11,6 @@ jar Provider Management Module API API project for ProviderManagement - - - - - - org.openmrs.api - openmrs-api - jar - - - - org.openmrs.web - openmrs-web - jar - - - - org.openmrs.api - openmrs-api - test-jar - test - - - - org.openmrs.web - openmrs-web - test-jar - test - - - - org.openmrs.test - openmrs-test - pom - test - - - - - - - org.codehaus.groovy - groovy - 1.7.6 - jar - provided - - - - org.openmrs.module - uiframework-api - - - @@ -82,21 +28,4 @@ - - - - openmrs-1.9 - - 1.9.0 - - - - openmrs-1.10 - - 1.10.0-SNAPSHOT - - - - - diff --git a/omod/pom.xml b/omod/pom.xml index b9f132d..539ad71 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -14,14 +14,6 @@ - - @@ -36,45 +28,66 @@ - org.openmrs.api - openmrs-api + org.openmrs.web + openmrs-web + ${openMRSVersion} jar + provided org.openmrs.web openmrs-web - jar + ${openMRSVersion} + test-jar + test - + + + org.openmrs.test + openmrs-test + ${openMRSVersion} + pom + test + + org.openmrs.api openmrs-api + ${openMRSVersion} test-jar test + + + javassist + javassist + + + + - org.openmrs.web - openmrs-web - test-jar - test + org.openmrs.module + webservices.rest-omod + ${webservicesRestVersion} + provided - org.openmrs.test - openmrs-test - pom - test + org.openmrs.module + webservices.rest-omod-common + ${webservicesRestVersion} + provided - - - - - org.openmrs.module - uiframework-api - + + org.openmrs.module + webservices.rest-omod-common + ${webservicesRestVersion} + test-jar + test + diff --git a/omod/src/main/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandler.java b/omod/src/main/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandler.java new file mode 100644 index 0000000..50ffa02 --- /dev/null +++ b/omod/src/main/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandler.java @@ -0,0 +1,64 @@ +package org.openmrs.module.providermanagement.rest.search; + +import org.openmrs.api.APIException; +import org.openmrs.api.context.Context; +import org.openmrs.module.providermanagement.Provider; +import org.openmrs.module.providermanagement.ProviderRole; +import org.openmrs.module.providermanagement.api.ProviderManagementService; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.resource.api.PageableResult; +import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig; +import org.openmrs.module.webservices.rest.web.resource.api.SearchHandler; +import org.openmrs.module.webservices.rest.web.resource.api.SearchQuery; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@Component +public class ProviderSearchHandler implements SearchHandler { + + protected final String PROVIDER_ROLES_PARAM = "providerRoles"; + + private final SearchConfig searchConfig = new SearchConfig("providerByRole", RestConstants.VERSION_1 + "/provider", + Arrays.asList("1.9.* - 9.*"), + new SearchQuery.Builder( + "Allows you to find providers by provider role uuid").withRequiredParameters(PROVIDER_ROLES_PARAM).build()); + + /** + * @see org.openmrs.module.webservices.rest.web.resource.api.SearchHandler#getSearchConfig() + */ + @Override + public SearchConfig getSearchConfig() { + return searchConfig; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.api.SearchHandler#search(org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + public PageableResult search(RequestContext context) throws ResponseException { + + String[] providerRoleUuidArray = context.getRequest().getParameterValues(PROVIDER_ROLES_PARAM); + List providerRoles = new ArrayList(); + + // supports both providerRoles=uuid1,uuid2 and providerRoles=uuid1&providerRoles=uuid2 + for (String providerRoleUuidString : providerRoleUuidArray) { + for (String providerRoleUuid : providerRoleUuidString.split(",")) { + ProviderRole providerRole = Context.getService(ProviderManagementService.class).getProviderRoleByUuid(providerRoleUuid); + if (providerRole != null) { + providerRoles.add(providerRole); + } else { + throw new APIException("Unable to find provider role with uuid: " + providerRoleUuid); + } + } + } + + List providers = Context.getService(ProviderManagementService.class).getProvidersByRoles(providerRoles); + return new NeedsPaging(providers, context); + } +} diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index ae1446a..9ab45c2 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -27,6 +27,7 @@ org.openmrs.module.uiframework org.openmrs.module.uilibrary + org.openmrs.module.webservices.rest diff --git a/omod/src/test/java/org/openmrs/module/providermanagement/ProviderSearchFragmentControllerTest.java b/omod/src/test/java/org/openmrs/module/providermanagement/fragment/controller/ProviderSearchFragmentControllerTest.java similarity index 96% rename from omod/src/test/java/org/openmrs/module/providermanagement/ProviderSearchFragmentControllerTest.java rename to omod/src/test/java/org/openmrs/module/providermanagement/fragment/controller/ProviderSearchFragmentControllerTest.java index 240b062..7212a3a 100644 --- a/omod/src/test/java/org/openmrs/module/providermanagement/ProviderSearchFragmentControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/providermanagement/fragment/controller/ProviderSearchFragmentControllerTest.java @@ -12,7 +12,7 @@ * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ -package org.openmrs.module.providermanagement; +package org.openmrs.module.providermanagement.fragment.controller; import org.junit.Assert; import org.junit.Before; @@ -20,20 +20,20 @@ import org.mockito.Mockito; import org.openmrs.GlobalProperty; import org.openmrs.api.context.Context; +import org.openmrs.module.providermanagement.Provider; import org.openmrs.module.providermanagement.api.ProviderManagementService; -import org.openmrs.module.providermanagement.fragment.controller.ProviderSearchFragmentController; -import org.openmrs.test.BaseModuleContextSensitiveTest; import org.openmrs.ui.framework.Formatter; import org.openmrs.ui.framework.FormatterImpl; import org.openmrs.ui.framework.SimpleObject; import org.openmrs.ui.framework.UiUtils; import org.openmrs.ui.framework.formatter.FormatterService; import org.openmrs.ui.framework.fragment.FragmentActionUiUtils; +import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; import java.util.List; import java.util.Map; -public class ProviderSearchFragmentControllerTest extends BaseModuleContextSensitiveTest { +public class ProviderSearchFragmentControllerTest extends BaseModuleWebContextSensitiveTest{ protected static final String XML_DATASET_PATH = "org/openmrs/module/providermanagement/include/"; diff --git a/omod/src/test/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandlerTest.java b/omod/src/test/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandlerTest.java new file mode 100644 index 0000000..156f1a3 --- /dev/null +++ b/omod/src/test/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandlerTest.java @@ -0,0 +1,80 @@ +package org.openmrs.module.providermanagement.rest.search; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.Provider; +import org.openmrs.api.APIException; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.annotation.ExpectedException; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.util.List; + +public class ProviderSearchHandlerTest extends MainResourceControllerTest { + + protected static final String XML_DATASET_PATH = "org/openmrs/module/providermanagement/include/"; + + protected static final String XML_DATASET = "providerManagement-dataset.xml"; + + @Before + public void init() throws Exception { + executeDataSet(XML_DATASET_PATH + XML_DATASET); + } + + @Override + public String getURI() { + return "provider"; + } + + @Override + public String getUuid() { + return "da7f523f-cca9-11e0-9572-0800200c9a66"; // from providerManagement-dataset.xml + } + + @Override + public long getAllCount() { + return 9; // one from standard test dataset, 8 from providerManagement-dataset.xml + } + + @Test + public void shouldReturnProvidersBySingleRole() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI()); + req.addParameter("providerRoles", "da7f523f-27ce-4bb2-86d6-6d1d05312bd5"); //binome role + SimpleObject result = deserialize(handle(req)); + List providers = (List) result.get("results"); + Assert.assertEquals(3, providers.size()); + } + + @Test + public void shouldReturnProvidersByMultipleRolesByCommaSeparatedSingleParameter() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI()); + req.addParameter("providerRoles", "da7f523f-27ce-4bb2-86d6-6d1d05312bd5,ea7f523f-27ce-4bb2-86d6-6d1d05312bd5"); //binome and binome superviser roles + SimpleObject result = deserialize(handle(req)); + List providers = (List) result.get("results"); + Assert.assertEquals(4, providers.size()); + } + + @Test + public void shouldReturnProvidersByMultipleRolesByMultipleParameters() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI()); + req.addParameter("providerRoles", "da7f523f-27ce-4bb2-86d6-6d1d05312bd5"); + req.addParameter("providerRoles", "ea7f523f-27ce-4bb2-86d6-6d1d05312bd5"); + SimpleObject result = deserialize(handle(req)); + List providers = (List) result.get("results"); + Assert.assertEquals(4, providers.size()); + } + + + @Test + @ExpectedException(APIException.class) + public void shouldThrowExceptionIfInvalidRole() throws Exception { + MockHttpServletRequest req = request(RequestMethod.GET, getURI()); + req.addParameter("providerRoles", "bogus"); + SimpleObject result = deserialize(handle(req)); + List providers = (List) result.get("results"); + } + +} diff --git a/pom.xml b/pom.xml index b534898..35c1f63 100644 --- a/pom.xml +++ b/pom.xml @@ -50,80 +50,76 @@ 1.9.9 3.4 1.5 + 2.5 UTF-8 - - - - - - - - - - - org.openmrs.api - openmrs-api - ${openMRSVersion} - jar - provided - - - - org.openmrs.web - openmrs-web - ${openMRSVersion} - jar - provided - - - - org.openmrs.api - openmrs-api - ${openMRSVersion} - test-jar - test - - - - org.openmrs.web - openmrs-web - ${openMRSVersion} - test-jar - test - - - - org.openmrs.test - openmrs-test - ${openMRSVersion} - pom - test - - - - - - - org.openmrs.module - uiframework-api - ${uiframeworkVersion} - jar - provided - - - - + + + + + + org.openmrs.api + openmrs-api + ${openMRSVersion} + jar + provided + + + + org.openmrs.web + openmrs-web + ${openMRSVersion} + jar + provided + + + + org.openmrs.api + openmrs-api + ${openMRSVersion} + test-jar + test + + + + org.openmrs.web + openmrs-web + ${openMRSVersion} + test-jar + test + + + + org.openmrs.test + openmrs-test + ${openMRSVersion} + pom + test + + + + + + + org.openmrs.module + uiframework-api + ${uiframeworkVersion} + jar + provided + + + + + + org.codehaus.groovy + groovy + 1.7.6 + jar + provided + + + @@ -146,14 +142,6 @@ maven-dependency-plugin 2.4 - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.1 - - -Djdk.net.URLClassPath.disableClassPathURLCheck=true - - org.apache.maven.plugins maven-release-plugin