Skip to content

Commit

Permalink
PROV-108: Add Rest Search Handler to support searching for providers … (
Browse files Browse the repository at this point in the history
  • Loading branch information
mogoodrich authored Oct 3, 2024
1 parent 07d43d2 commit 1bf5cea
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 181 deletions.
71 changes: 0 additions & 71 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,6 @@
<packaging>jar</packaging>
<name>Provider Management Module API</name>
<description>API project for ProviderManagement</description>

<dependencies>
<!-- Begin OpenMRS core -->

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openmrs.test</groupId>
<artifactId>openmrs-test</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>

<!-- End OpenMRS core -->

<!-- include groovy, but mark as provided since it is included in core OpenMRS -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.7.6</version>
<type>jar</type>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>uiframework-api</artifactId>
</dependency>

</dependencies>

<build>
<resources>
Expand All @@ -82,21 +28,4 @@
</testResources>
</build>

<!-- profiles for testing against different versions of Openmrs -->
<profiles>
<profile>
<id>openmrs-1.9</id>
<properties>
<openMRSVersion>1.9.0</openMRSVersion>
</properties>
</profile>
<profile>
<id>openmrs-1.10</id>
<properties>
<openMRSVersion>1.10.0-SNAPSHOT</openMRSVersion>
</properties>
</profile>
</profiles>


</project>
67 changes: 40 additions & 27 deletions omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@

<dependencies>

<!--
Add other dependencies from parent's pom:
<dependency>
<groupId>org.other.library</groupId>
<artifactId>library-name</artifactId>
</dependency>
-->

<!-- Begin OpenMRS modules -->

<dependency>
Expand All @@ -36,45 +28,66 @@
<!-- Begin OpenMRS core -->

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<version>${openMRSVersion}</version>
<type>jar</type>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<type>jar</type>
<version>${openMRSVersion}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.openmrs.test</groupId>
<artifactId>openmrs-test</artifactId>
<version>${openMRSVersion}</version>
<type>pom</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.openmrs.api</groupId>
<artifactId>openmrs-api</artifactId>
<version>${openMRSVersion}</version>
<type>test-jar</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- Begin OpenMRS modules -->

<dependency>
<groupId>org.openmrs.web</groupId>
<artifactId>openmrs-web</artifactId>
<type>test-jar</type>
<scope>test</scope>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod</artifactId>
<version>${webservicesRestVersion}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.openmrs.test</groupId>
<artifactId>openmrs-test</artifactId>
<type>pom</type>
<scope>test</scope>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
<version>${webservicesRestVersion}</version>
<scope>provided</scope>
</dependency>

<!-- End OpenMRS core -->

<!-- depends on ui framework module -->
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>uiframework-api</artifactId>
</dependency>
<dependency>
<groupId>org.openmrs.module</groupId>
<artifactId>webservices.rest-omod-common</artifactId>
<version>${webservicesRestVersion}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
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);
}
}
1 change: 1 addition & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<require_modules>
<require_module version="${uiframeworkVersion}">org.openmrs.module.uiframework</require_module>
<require_module version="${uilibraryVersion}">org.openmrs.module.uilibrary</require_module>
<require_module version="${webservicesRestVersion}">org.openmrs.module.webservices.rest</require_module>
</require_modules>

<!-- Aware Modules -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@
* 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;
import org.junit.Test;
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/";

Expand Down
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");
}

}
Loading

0 comments on commit 1bf5cea

Please sign in to comment.