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 Jul 2, 2024
1 parent 5f3ca92 commit 05b4100
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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 @@ -99,9 +98,6 @@ 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 @@ -116,9 +112,6 @@ 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 @@ -142,9 +135,6 @@ 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 @@ -179,9 +169,6 @@ 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");
Expand All @@ -198,18 +185,15 @@ public DelegatingResourceDescription getCreatableProperties() throws ResourceDoe
*/
@Override
public Model getCREATEModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getCREATEModel(rep);
if (supportsFormNamespaceAndPath()) {
model.property("formNamespaceAndPath", new StringProperty());
}
model

return new ModelImpl()
.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 @@ -218,9 +202,6 @@ 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");
Expand All @@ -237,18 +218,13 @@ public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoe
*/
@Override
public Model getUPDATEModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getUPDATEModel(rep);
if (supportsFormNamespaceAndPath()) {
model.property("formNamespaceAndPath", new StringProperty());
}
model
return new ModelImpl()
.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 @@ -266,13 +242,4 @@ 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,19 +11,14 @@

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 @@ -32,14 +27,10 @@
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 @@ -59,9 +50,6 @@ 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 @@ -73,11 +61,6 @@ 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 @@ -348,39 +331,4 @@ 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,7 +52,6 @@ public void validateDefaultRepresentation() throws Exception {
assertPropEquals("rank", getObject().getRank());
assertPropEquals("voided", getObject().getVoided());
assertPropPresent("display");
assertPropNotPresent("formNamespaceAndPath");
}

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

0 comments on commit 05b4100

Please sign in to comment.