diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 5cd382ca2..b383af10b 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -21,13 +21,14 @@ jobs: JAVA_VERSION: ${{ matrix.java-version }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up JDK - uses: actions/setup-java@v1 + uses: actions/setup-java@v4 with: java-version: ${{ matrix.java-version }} + distribution: 'temurin' - name: Cache local Maven repository - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java index 9ab4a6c8e..664122971 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java @@ -168,4 +168,21 @@ public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception assertNotNull(PropertyUtils.getProperty(result, "auditInfo")); } + + @Test + public void shouldReturnCustomRepresentationForAuditInfo() throws Exception { + SimpleObject result = deserialize( + handle(newGetRequest(getURI() + "/" + getUuid(), + new Parameter("v", "custom:(auditInfo:(creator:(uuid),dateCreated))")))); + + assertNotNull(PropertyUtils.getProperty(result, "auditInfo")); + assertNotNull(PropertyUtils.getProperty(result, "auditInfo.creator")); + assertNotNull(PropertyUtils.getProperty(result, "auditInfo.creator.uuid")); + assertNotNull(PropertyUtils.getProperty(result, "auditInfo.dateCreated")); + + assertNull(PropertyUtils.getProperty(result, "name")); + assertNull(PropertyUtils.getProperty(result, "links")); + assertNull(PropertyUtils.getProperty(result, "auditInfo.creator.display")); + assertNull(PropertyUtils.getProperty(result, "auditInfo.creator.links")); + } } diff --git a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java index e19e6d1fd..b8e065bdc 100644 --- a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java +++ b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java @@ -132,7 +132,7 @@ public void delete(HttpServletRequest request) { * * @return Provider if the user is authenticated */ - private Provider getCurrentProvider() { + protected Provider getCurrentProvider() { Provider currentProvider = null; User currentUser = Context.getAuthenticatedUser(); if (currentUser != null) { diff --git a/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/SessionController2_0.java b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/SessionController2_0.java new file mode 100644 index 000000000..4443db8db --- /dev/null +++ b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/SessionController2_0.java @@ -0,0 +1,60 @@ +/** + * 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.webservices.rest.web.v1_0.controller.openmrs2_0; + +import java.util.Collection; +import java.util.HashSet; + +import org.openmrs.Provider; +import org.openmrs.User; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9.SessionController1_9; +import org.openmrs.util.PrivilegeConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * @see SessionController1_9 + */ +@Controller +@RequestMapping +public class SessionController2_0 extends SessionController1_9 { + + private static final Logger log = LoggerFactory.getLogger(SessionController2_0.class); + + /** + * @see SessionController1_9#getCurrentProvider() + */ + @Override + protected Provider getCurrentProvider() { + Provider currentProvider = null; + User currentUser = Context.getAuthenticatedUser(); + if (currentUser != null) { + Collection providers = new HashSet(); + try { + Context.addProxyPrivilege(PrivilegeConstants.GET_PROVIDERS); + if (currentUser.getPerson() != null) { + providers = Context.getProviderService().getProvidersByPerson(currentUser.getPerson(), false); + } + } + finally { + Context.removeProxyPrivilege(PrivilegeConstants.GET_PROVIDERS); + } + if (providers.size() > 1) { + log.warn("Can't handle users with multiple provider accounts"); + } else if (providers.size() == 1) { + currentProvider = providers.iterator().next(); + } + } + return currentProvider; + } +} diff --git a/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/SessionController2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/SessionController2_0Test.java new file mode 100644 index 000000000..c82ebfee3 --- /dev/null +++ b/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/SessionController2_0Test.java @@ -0,0 +1,43 @@ +/** + * 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.webservices.rest.web.v1_0.controller.openmrs2_0; + +import org.apache.commons.beanutils.PropertyUtils; +import org.junit.Assert; +import org.junit.Test; +import org.openmrs.api.context.Context; +import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; + +/** + * Tests functionality of {@link SessionController2_0} + */ +public class SessionController2_0Test extends BaseModuleWebContextSensitiveTest { + + /** + * @see SessionController2_0#get() + * @verifies return the session with current provider if the user doesn't have Get Providers privilege + */ + @Test + public void get_shouldReturnCurrentProviderIfTheUserDoesNotHaveGetProvidersPrivilege() throws Exception { + executeDataSet("sessionControllerTestDataset.xml"); + + // authenticate new user without privileges + Context.logout(); + Context.authenticate("test_user", "test"); + Assert.assertTrue(Context.isAuthenticated()); + + SessionController2_0 controller = Context.getRegisteredComponents(SessionController2_0.class).iterator().next(); + + Object ret = controller.get(); + Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider"); + Assert.assertNotNull(currentProvider); + Assert.assertTrue(currentProvider.toString().contains("Test Provider")); + } +} diff --git a/omod-2.0/src/test/resources/sessionControllerTestDataset.xml b/omod-2.0/src/test/resources/sessionControllerTestDataset.xml new file mode 100644 index 000000000..17a71e1f1 --- /dev/null +++ b/omod-2.0/src/test/resources/sessionControllerTestDataset.xml @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/DiagnosisResource2_2.java b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/DiagnosisResource2_2.java index 87437965e..4be563061 100644 --- a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/DiagnosisResource2_2.java +++ b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/DiagnosisResource2_2.java @@ -169,7 +169,7 @@ public String getDisplayString(Diagnosis diagnosis) { @Override public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException { DelegatingResourceDescription description = new DelegatingResourceDescription(); - + description.addRequiredProperty("diagnosis"); description.addRequiredProperty("encounter"); description.addRequiredProperty("condition"); @@ -185,7 +185,7 @@ public DelegatingResourceDescription getCreatableProperties() throws ResourceDoe */ @Override public Model getCREATEModel(Representation rep) { - + return new ModelImpl() .property("diagnosis", new StringProperty()) .property("encounter", new StringProperty()) @@ -193,7 +193,7 @@ public Model getCREATEModel(Representation rep) { .property("certainty", new StringProperty()) .property("patient", new StringProperty().example("uuid")) .property("rank", new IntegerProperty()); - + } /** @@ -209,7 +209,7 @@ public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoe description.addRequiredProperty("voided"); description.addRequiredProperty("certainty"); description.addRequiredProperty("encounter"); - + return description; } diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml index 46153c3e0..957ea4318 100644 --- a/omod-2.5/pom.xml +++ b/omod-2.5/pom.xml @@ -1,19 +1,21 @@ - - + + 4.0.0 - webservices.rest org.openmrs.module + webservices.rest 2.45.0-SNAPSHOT - 4.0.0 webservices.rest-omod-2.5 + jar Rest Web Services 2.5 OMOD + OpenMRS module project for Rest Web Services 2.5.0 + 1.8 + 1.8 @@ -93,6 +95,14 @@ ${project.parent.version} + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.0 + ${project.parent.version} + tests + test + + ${project.parent.groupId} ${project.parent.artifactId}-omod-2.2 @@ -135,14 +145,6 @@ test - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.0 - ${project.parent.version} - tests - test - - org.openmrs.api openmrs-api @@ -160,13 +162,7 @@ org.openmrs.web openmrs-web - ${openmrs.version.2.5.0} - - - javax.servlet - servlet-api - - + ${openmrs.version.2.5.0} @@ -174,7 +170,7 @@ openmrs-web test-jar test - ${openmrs.version.2.5.0} + ${openmrs.version.2.5.0} @@ -182,7 +178,7 @@ openmrs-test pom test - ${openmrs.version.2.5.0} + ${openmrs.version.2.5.0} @@ -195,7 +191,7 @@ org.apache.tomcat jasper - 6.0.18 + ${apacheTomcatVersion} provided diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5.java index af674b733..d57505164 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5.java @@ -9,12 +9,20 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.StringProperty; + import org.openmrs.Diagnosis; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.annotation.Resource; import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation; import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; import org.openmrs.module.webservices.rest.web.representation.Representation; + +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; + import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.DiagnosisResource2_2; @@ -71,4 +79,5 @@ public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoe return description; } + } diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5Test.java new file mode 100644 index 000000000..d260cbd48 --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/DiagnosisResource2_5Test.java @@ -0,0 +1,95 @@ +/** + * 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.webservices.rest.web.v1_0.resource.openmrs2_5; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.Diagnosis; +import org.openmrs.api.DiagnosisService; +import org.openmrs.api.EncounterService; +import org.openmrs.api.PatientService; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.openmrs.module.webservices.rest.web.v1_0.RestTestConstants2_2; + +/** + * Tests functionality of {@link DiagnosisResource2_5}. + */ +public class DiagnosisResource2_5Test extends BaseDelegatingResourceTest { + + + private DiagnosisService diagnosisService; + + private EncounterService encounterService; + + private PatientService patientService; + + + @Before + public void before() { + this.diagnosisService = Context.getDiagnosisService(); + this.encounterService = Context.getEncounterService(); + this.patientService = Context.getPatientService(); + executeDataSet("DiagnosisResourceTestDataset.xml"); + } + + @Override + public Diagnosis newObject() { + return diagnosisService.getDiagnosisByUuid(getUuidProperty()); + } + + @Override + public String getUuidProperty() { + return RestTestConstants2_2.DIAGNOSIS_UUID; + } + + @Override + public void validateDefaultRepresentation() throws Exception { + super.validateDefaultRepresentation(); + assertPropEquals("formFieldNamespace", getObject().getFormFieldNamespace()); + assertPropEquals("formFieldPath", getObject().getFormFieldPath()); + } + + @Override + public void validateFullRepresentation() throws Exception { + super.validateFullRepresentation(); + assertPropEquals("formFieldNamespace", getObject().getFormFieldNamespace()); + assertPropEquals("formFieldPath", getObject().getFormFieldPath()); + } + + @Override + public String getDisplayProperty() { + return ""; + } + + @Test + public void testFormFieldNamespaceAndPath() { + String uuid = "a303bbfb-w5w4-25d1-9f11-4f33f99d456r"; + Diagnosis diagnosis = new Diagnosis(); + diagnosis.setUuid(uuid); + diagnosis.setEncounter(encounterService.getEncounter(1)); + diagnosis.setPatient(patientService.getPatient(1)); + diagnosis.setRank(2); + + final String NAMESPACE = "namespace"; + final String FORMFIELD_PATH = "formFieldPath"; + diagnosis.setFormField(NAMESPACE, FORMFIELD_PATH); + diagnosisService.save(diagnosis); + + Diagnosis savedDiagnosis = diagnosisService.getDiagnosisByUuid(uuid); + String formFieldNameSpace = savedDiagnosis.getFormFieldNamespace(); + String formFieldPath = savedDiagnosis.getFormFieldPath(); + + Assert.assertEquals("namespace", formFieldNameSpace); + Assert.assertEquals("formFieldPath", formFieldPath); + Assert.assertNotNull(savedDiagnosis.getFormNamespaceAndPath()); + } +} diff --git a/omod-2.5/src/test/resources/DiagnosisResourceTestDataset.xml b/omod-2.5/src/test/resources/DiagnosisResourceTestDataset.xml new file mode 100644 index 000000000..ca779465b --- /dev/null +++ b/omod-2.5/src/test/resources/DiagnosisResourceTestDataset.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java index 1d47c9336..044d8c341 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java @@ -389,6 +389,9 @@ public static Object convertToRepresentation(S o, Representation rep, Conver } return ret; } else if (o instanceof Map) { + if (rep instanceof CustomRepresentation) { + return convertToCustomRepresentation(o, (CustomRepresentation) rep); + } SimpleObject ret = new SimpleObject(); for (Map.Entry entry : ((Map) o).entrySet()) { ret.put(entry.getKey().toString(), @@ -414,6 +417,24 @@ public static Object convertToRepresentation(S o, Representation rep, Conver } } + /** + * Converts an object to its custom representation + * This could be used to convert any domain objects that does not have any specific converter associated with them + * such as SimpleObject's, Map's, etc + */ + private static SimpleObject convertToCustomRepresentation(Object o, CustomRepresentation rep) { + DelegatingResourceDescription drd = ConversionUtil.getCustomRepresentationDescription(rep); + + SimpleObject result = new SimpleObject(); + for (String propertyName : drd.getProperties().keySet()) { + DelegatingResourceDescription.Property property = drd.getProperties().get(propertyName); + Object propertyValue = ConversionUtil.getPropertyWithRepresentation(o, propertyName, property.getRep()); + result.add(propertyName, propertyValue); + } + + return result; + } + /** * Gets the type for the specified generic type variable. * diff --git a/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java b/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java index ea4dd901b..71b3fae12 100644 --- a/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java +++ b/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java @@ -10,7 +10,7 @@ package org.openmrs.module.webservices.rest.web; import static org.hamcrest.core.Is.is; -import org.junit.Assert; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import java.lang.reflect.Method; @@ -25,8 +25,11 @@ import java.util.TimeZone; import org.apache.commons.beanutils.PropertyUtils; +import org.junit.Assert; import org.junit.Test; import org.openmrs.api.ConceptNameType; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; import org.openmrs.module.webservices.rest.web.representation.Representation; import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; @@ -145,6 +148,26 @@ public void convert_shouldConvertToAClass() throws Exception { Assert.assertTrue(converted.isAssignableFrom(String.class)); } + @Test + public void convert_shouldConvertSimpleObjectToCustomRepresentation() throws Exception { + + SimpleObject child = new SimpleObject(); + child.put("child_key_1", "child_val_1"); + child.put("child_key_2", "child_val_2"); + SimpleObject parent = new SimpleObject(); + parent.put("parent_key_1", child); + parent.put("parent_key_2", "parent_val_2"); + + Object o = ConversionUtil.convertToRepresentation(parent, new CustomRepresentation("parent_key_1:(child_key_1)")); + + SimpleObject expectedChild = new SimpleObject(); + expectedChild.put("child_key_1", "child_val_1"); + SimpleObject expectedParent = new SimpleObject(); + expectedParent.put("parent_key_1", expectedChild); + + assertEquals(expectedParent, o); + } + public void convert_shouldConvertIntToDouble() throws Exception { assertThat((Double) ConversionUtil.convert(5, Double.class), is(5d)); } diff --git a/omod/pom.xml b/omod/pom.xml index e0cd4bfff..e2a42d1bc 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -143,6 +143,18 @@ tests test + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.5 + ${project.parent.version} + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.5 + ${project.parent.version} + tests + test + diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 0b2d6222f..2b0fc0cc7 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -50,6 +50,10 @@ /lib/webservices.rest-omod-2.4.* 2.4.* - 9.* + + /lib/webservices.rest-omod-2.5.* + 2.5.* - 9.* +