Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-942 - Obs for a Concept Numeric with allowDecimal = false shou… #612

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.StringProperty;
import org.apache.commons.lang.BooleanUtils;
import org.openmrs.Concept;
import org.openmrs.ConceptNumeric;
import org.openmrs.Obs;
import org.openmrs.api.db.hibernate.HibernateUtil;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertySetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
Expand Down Expand Up @@ -92,4 +96,22 @@ public static void setFormFieldNamespace(Obs obs, Object namespace) {
public String getResourceVersion() {
return RestConstants1_11.RESOURCE_VERSION;
}

/**
* @return a Double if the ConceptNumeric is configured as allowDecimal, but an Integer otherwise
* This changes the implementation from the pre-1.11 field named precise.
*/
protected Number getValueNumeric(Obs obs) {
if (obs.getValueNumeric() == null) {
return null;
}
Concept concept = HibernateUtil.getRealObjectFromProxy(obs.getConcept());
if (concept instanceof ConceptNumeric) {
ConceptNumeric conceptNumeric = (ConceptNumeric) concept;
if (BooleanUtils.isFalse(conceptNumeric.getAllowDecimal())) {
return obs.getValueNumeric().intValue();
}
}
return obs.getValueNumeric();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11;

import org.junit.Assert;
import org.junit.Test;
import org.openmrs.ConceptNumeric;
import org.openmrs.Obs;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest;

public class ObsResource1_11Test extends BaseDelegatingResourceTest<ObsResource1_11, Obs> {
Expand Down Expand Up @@ -44,4 +49,22 @@ public String getDisplayProperty() {
public String getUuidProperty() {
return RestTestConstants1_8.OBS_UUID;
}

@Test
public void asRepresentation_shouldReturnNumericValueBasedOnConceptNumericAllowDecimal() {
Obs obs = getObject();
obs.setValueNumeric(20.0);
ConceptNumeric cn = Context.getConceptService().getConceptNumeric(5497);
obs.setConcept(cn);
cn.setAllowDecimal(true);
SimpleObject rep = getResource().asRepresentation(getObject(), Representation.DEFAULT);
Object value = rep.get("value");
Assert.assertEquals(20.0, value);
Assert.assertEquals(Double.class, value.getClass());
cn.setAllowDecimal(false);
rep = getResource().asRepresentation(getObject(), Representation.DEFAULT);
value = rep.get("value");
Assert.assertEquals(20, value);
Assert.assertEquals(Integer.class, value.getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public String getDisplayString(Obs obs) {
* @return
*/
@PropertyGetter("value")
public static Object getValue(Obs obs) throws ConversionException {
public Object getValue(Obs obs) throws ConversionException {
if (obs.isComplex()) {
//Note that complex obs value is handled by ObsComplexValueController1_8
SimpleObject so = new SimpleObject();
Expand Down Expand Up @@ -322,13 +322,20 @@ public static Object getValue(Obs obs) throws ConversionException {
}

}

if (obs.getValueNumeric() != null) {
return obs.getValueNumeric();
return getValueNumeric(obs);
}

return null;
}

/**
* @return the valueNumeric from the obs
*/
protected Number getValueNumeric(Obs obs) {
return obs.getValueNumeric();
}

/**
* Sets the members of an obs group
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public void setValue_shouldReturnUuidForConceptTrue() throws Exception {
Obs obs = new Obs();
obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID));
ObsResource1_8.setValue(obs, trueConcept);
assertEquals(trueConcept, ObsResource1_8.getValue(obs));
assertEquals(trueConcept, new ObsResource1_8().getValue(obs));
}

@Test
Expand All @@ -232,15 +232,15 @@ public void setValue_shouldReturnDrug() throws Exception {
String drugUuid = "3cfcf118-931c-46f7-8ff6-7b876f0d4202";
Drug drug = Context.getConceptService().getDrugByUuid(drugUuid);
ObsResource1_8.setValue(obs, drugUuid);
assertEquals(drug, ObsResource1_8.getValue(obs));
assertEquals(drug, new ObsResource1_8().getValue(obs));
}

@Test
public void setValue_shouldReturnUuidForConceptFalse() throws Exception {
Obs obs = new Obs();
obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID));
ObsResource1_8.setValue(obs, falseConcept);
assertEquals(falseConcept, ObsResource1_8.getValue(obs));
assertEquals(falseConcept, new ObsResource1_8().getValue(obs));
}

@Test(expected = ConversionException.class)
Expand All @@ -255,15 +255,15 @@ public void setValue_shouldReturnUuidForPrimitiveTrue() throws Exception {
Obs obs = new Obs();
obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID));
ObsResource1_8.setValue(obs, true);
assertEquals(trueConcept, ObsResource1_8.getValue(obs));
assertEquals(trueConcept, new ObsResource1_8().getValue(obs));
}

@Test
public void setValue_shouldReturnUuidForPrimitiveFalse() throws Exception {
Obs obs = new Obs();
obs.setConcept(Context.getConceptService().getConceptByUuid(BOOLEAN_CONCEPT_UUID));
ObsResource1_8.setValue(obs, false);
assertEquals(falseConcept, ObsResource1_8.getValue(obs));
assertEquals(falseConcept, new ObsResource1_8().getValue(obs));
}

private void clearAndSetValue(Obs obs, ObsType type, Object value) {
Expand All @@ -287,6 +287,6 @@ public void setConvertedProperties_shouldAllowAnyPropertyOrder() throws Exceptio
propertyMap.put("obsDatetime", "2013-12-09T00:00:00.000+0100");

resource.setConvertedProperties(obs, propertyMap, resource.getUpdatableProperties(), false);
org.springframework.util.Assert.isTrue(((Double) ObsResource1_8.getValue(obs)) == 10.0);
org.springframework.util.Assert.isTrue(((Double) new ObsResource1_8().getValue(obs)) == 10.0);
}
}
Loading