-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PROV-108: Add Rest Search Handler to support searching for providers … (
#51)
- Loading branch information
1 parent
07d43d2
commit 1bf5cea
Showing
7 changed files
with
256 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...rc/main/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ProviderRole> providerRoles = new ArrayList<ProviderRole>(); | ||
|
||
// 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<Provider> providers = Context.getService(ProviderManagementService.class).getProvidersByRoles(providerRoles); | ||
return new NeedsPaging<Provider>(providers, context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...est/java/org/openmrs/module/providermanagement/rest/search/ProviderSearchHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Provider> providers = (List<Provider>) 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<Provider> providers = (List<Provider>) 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<Provider> providers = (List<Provider>) 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<Provider> providers = (List<Provider>) result.get("results"); | ||
} | ||
|
||
} |
Oops, something went wrong.