Skip to content

Commit

Permalink
Should be able to create a new allergy with reactions in one call -
Browse files Browse the repository at this point in the history
RESTWS-668
  • Loading branch information
dkayiwa committed Jul 9, 2017
1 parent cb6450d commit 41d46ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@
import java.util.ArrayList;
import java.util.List;

import org.openmrs.Allergy;
import org.openmrs.Allergies;
import org.openmrs.Allergy;
import org.openmrs.AllergyReaction;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.annotation.SubResource;
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.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.module.webservices.rest.web.response.ObjectNotFoundException;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.PatientResource1_9;

@SubResource(parent = PatientResource1_9.class, path = "allergy", supportedClass = Allergy.class, supportedOpenmrsVersions = {
Expand Down Expand Up @@ -202,6 +201,12 @@ public void purge(String parentUniqueId, String uuid, RequestContext context) th
*/
@Override
public Allergy save(Allergy newAllergy) {
List<AllergyReaction> reactions = newAllergy.getReactions();
for (AllergyReaction reaction : reactions) {
if (reaction.getAllergy() == null) {
reaction.setAllergy(newAllergy);
}
}
Context.getPatientService().saveAllergy(newAllergy);
return Context.getPatientService().getAllergyByUuid(newAllergy.getUuid());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,47 @@ public void shouldSetNoKnownAllergiesIfAllergiesEmpty() throws Exception {
allergies = Context.getPatientService().getAllergies(patient);
Assert.assertEquals(Allergies.NO_KNOWN_ALLERGIES, allergies.getAllergyStatus());
}

/**
* Save a new Allergy with its reactions in one call
*/
@Test
public void shouldSaveNewAllergyWithReactionsInOneCall() throws Exception {
Allergy allergy = Context.getPatientService().getAllergyByUuid(getUuid());
Patient patient = allergy.getPatient();
Allergies allergies = Context.getPatientService().getAllergies(patient);
Assert.assertEquals(allergies.size(), 4);

// save allergy with coded allergen
String json = "{" + " \"comment\" : \"allergy comment\","
+ " \"severity\" : { \"uuid\" : \"35d3346a-6769-4d52-823f-b4b234bac3e3\"}," + " \"allergen\" : "
+ " { \"allergenType\" : \"DRUG\", "
+ " \"codedAllergen\" : { \"uuid\" : \"35d3346a-6769-4d52-823f-b4b234bac3e3\"} " + " }, "
+ "\"reactions\" : " + "[" + "{" + " \"reaction\" : { \"uuid\" : \"35d3346a-6769-4d52-823f-b4b234bac3e3\" }"
+ "}," + "{" + " \"reaction\" : { \"uuid\" : \"5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\" },"
+ " \"reactionNonCoded\" : \"test non coded reaction\"" + "}" + "]" + "}";

// save allergy
SimpleObject savedAllergy = deserialize(handle(newPostRequest(getURI(), json)));

Assert.assertEquals("allergy comment", Util.getByPath(savedAllergy, "comment"));
Assert.assertEquals("35d3346a-6769-4d52-823f-b4b234bac3e3", Util.getByPath(savedAllergy, "severity/uuid"));

Assert.assertEquals("35d3346a-6769-4d52-823f-b4b234bac3e3",
Util.getByPath(savedAllergy, "allergen/codedAllergen/uuid"));

Assert.assertEquals("DRUG", Util.getByPath(savedAllergy, "allergen/allergenType"));

Assert.assertEquals("35d3346a-6769-4d52-823f-b4b234bac3e3",
Util.getByPath(savedAllergy, "reactions[0]/reaction/uuid"));

Assert.assertEquals("5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
Util.getByPath(savedAllergy, "reactions[1]/reaction/uuid"));

Assert.assertEquals("test non coded reaction", Util.getByPath(savedAllergy, "reactions[1]/reactionNonCoded"));

// assert that a new allergy has been added
allergies = Context.getPatientService().getAllergies(patient);
Assert.assertEquals(allergies.size(), 5);
}
}

0 comments on commit 41d46ef

Please sign in to comment.