Skip to content

Commit

Permalink
RESTWS-939: DiagnosisResource should support formFieldPath property
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMujuziMoses committed Jun 7, 2024
1 parent 193af52 commit 8761201
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.openmrs.Diagnosis;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.ModuleUtil;
import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
Expand Down Expand Up @@ -98,6 +99,9 @@ public void purge(Diagnosis diagnosis, RequestContext requestContext) throws Res
public DelegatingResourceDescription getRepresentationDescription(Representation representation) {
if (representation instanceof DefaultRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
if (supportsFormNamespaceAndPath()) {
description.addProperty("formNamespaceAndPath");
}
description.addProperty("uuid");
description.addProperty("diagnosis", Representation.REF);
description.addProperty("condition", Representation.REF);
Expand All @@ -112,6 +116,9 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
return description;
} else if (representation instanceof FullRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
if (supportsFormNamespaceAndPath()) {
description.addProperty("formNamespaceAndPath");
}
description.addProperty("uuid");
description.addProperty("diagnosis");
description.addProperty("patient", Representation.REF);
Expand All @@ -135,6 +142,9 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
public Model getGETModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getGETModel(rep);
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
if (supportsFormNamespaceAndPath()) {
model.property("formNamespaceAndPath", new StringProperty());
}
model
.property("uuid", new StringProperty())
.property("diagnosis", new StringProperty())
Expand Down Expand Up @@ -169,7 +179,10 @@ public String getDisplayString(Diagnosis diagnosis) {
@Override
public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException {
DelegatingResourceDescription description = new DelegatingResourceDescription();

if (supportsFormNamespaceAndPath()) {
description.addProperty("formNamespaceAndPath");
}

description.addRequiredProperty("diagnosis");
description.addRequiredProperty("encounter");
description.addRequiredProperty("condition");
Expand All @@ -185,15 +198,18 @@ public DelegatingResourceDescription getCreatableProperties() throws ResourceDoe
*/
@Override
public Model getCREATEModel(Representation rep) {

return new ModelImpl()
ModelImpl model = (ModelImpl) super.getCREATEModel(rep);
if (supportsFormNamespaceAndPath()) {
model.property("formNamespaceAndPath", new StringProperty());
}
model
.property("diagnosis", new StringProperty())
.property("encounter", new StringProperty())
.property("condition", new StringProperty())
.property("certainty", new StringProperty())
.property("patient", new StringProperty().example("uuid"))
.property("rank", new IntegerProperty());

return model;
}

/**
Expand All @@ -202,14 +218,17 @@ public Model getCREATEModel(Representation rep) {
@Override
public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException {
DelegatingResourceDescription description = new DelegatingResourceDescription();
if (supportsFormNamespaceAndPath()) {
description.addProperty("formNamespaceAndPath");
}
description.addRequiredProperty("diagnosis");
description.addRequiredProperty("condition");
description.addRequiredProperty("rank");
description.removeProperty("patient");
description.addRequiredProperty("voided");
description.addRequiredProperty("certainty");
description.addRequiredProperty("encounter");

return description;
}

Expand All @@ -218,13 +237,18 @@ public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoe
*/
@Override
public Model getUPDATEModel(Representation rep) {
return new ModelImpl()
ModelImpl model = (ModelImpl) super.getUPDATEModel(rep);
if (supportsFormNamespaceAndPath()) {
model.property("formNamespaceAndPath", new StringProperty());
}
model
.property("diagnosis", new StringProperty())
.property("condition", new StringProperty())
.property("encounter", new StringProperty())
.property("certainty", new EnumProperty(ConditionVerificationStatus.class))
.property("rank", new IntegerProperty())
.property("voided", new BooleanProperty());
return model;
}

@Override
Expand All @@ -242,4 +266,13 @@ protected PageableResult doSearch(RequestContext context) {
}
return new NeedsPaging<Diagnosis>(Context.getDiagnosisService().getDiagnoses(patient, dateFrom), context);
}

/**
* Helper method to check if openmrs version running is form recordable
*
* @return boolean value after check
*/
private boolean supportsFormNamespaceAndPath() {
return ModuleUtil.isOpenmrsVersionInVersions("2.5.* - 9.*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@

import static org.openmrs.ConditionVerificationStatus.CONFIRMED;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openmrs.Concept;
import org.openmrs.ConceptName;
import org.openmrs.Condition;
Expand All @@ -27,10 +32,14 @@
import org.openmrs.Patient;
import org.openmrs.api.DiagnosisService;
import org.openmrs.api.context.Context;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.ModuleUtil;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.response.ConversionException;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.v1_0.RestTestConstants2_2;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.openmrs.util.OpenmrsConstants;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestMethod;

Expand All @@ -50,6 +59,9 @@ public class DiagnosisController2_2Test extends MainResourceControllerTest {
private Concept concept;

private ConceptName conceptName;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void before() throws Exception {
Expand All @@ -61,6 +73,11 @@ public void before() throws Exception {
this.concept = Context.getConceptService().getConcept(1);
this.conceptName = Context.getConceptService().getConceptName(1);
}

@After
public void after() throws Exception {
setCurrentOpenmrsVersion("2.2");
}

/**
* @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI()
Expand Down Expand Up @@ -331,4 +348,39 @@ public void shouldReturnPatientDiagnosis() throws Exception {
List<Diagnosis> diagnoses = result.get("results");
Assert.assertEquals(2, diagnoses.size());
}

/**
* Helper method to set the current OpenMRS version for tests.
*
* @param currentOpenmrsVersion the openmrs version to set the current version to
*/
private void setCurrentOpenmrsVersion(final String currentOpenmrsVersion) throws NoSuchFieldException,
IllegalAccessException {

Field versionField = OpenmrsConstants.class.getDeclaredField("OPENMRS_VERSION_SHORT");

Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(versionField, versionField.getModifiers() & ~Modifier.FINAL);
versionField.set(null, currentOpenmrsVersion);
}

@Test
public void testFormNamespaceAndPath() throws Exception {
final String nonCoded = "Some condition";
String json = "{ \"diagnosis\":{\"coded\":null,\"specificName\":null,\"nonCoded\":\"" + nonCoded
+ "\"},\"condition\":\"" + condition.getUuid()
+ "\",\"certainty\":\"" + "CONFIRMED" + "\",\"encounter\":\""
+ encounter.getUuid() + "\",\"rank\":\"" + 2
+ "\",\"voided\":\"" + false + "\"}";

handle(newPostRequest(getURI() + "/" + RestTestConstants2_2.UPDATABLE_CODED_DIAGNOSIS_UUID, json));
Diagnosis newDiagnosis = diagnosisService.getDiagnosisByUuid(RestTestConstants2_2.UPDATABLE_CODED_DIAGNOSIS_UUID);
Assert.assertNotNull(newDiagnosis);
Assert.assertEquals("Some condition", newDiagnosis.getDiagnosis().getNonCoded());

setCurrentOpenmrsVersion("2.5.0");
expectedException.expect(ConversionException.class);
handle(newPostRequest(getURI() + "/" + RestTestConstants2_2.UPDATABLE_CODED_DIAGNOSIS_UUID, json));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void validateDefaultRepresentation() throws Exception {
assertPropEquals("rank", getObject().getRank());
assertPropEquals("voided", getObject().getVoided());
assertPropPresent("display");
assertPropNotPresent("formNamespaceAndPath");
}

@Override
Expand All @@ -66,5 +67,6 @@ public void validateFullRepresentation() throws Exception {
assertPropEquals("voided", getObject().getVoided());
assertPropPresent("auditInfo");
assertPropPresent("display");
assertPropNotPresent("formNamespaceAndPath");
}
}
Loading

0 comments on commit 8761201

Please sign in to comment.