Permalink
Browse files

ATT-27: REST end point for file upload should allow to specify encounter

  • Loading branch information...
ridmal authored and mks-d committed Jul 19, 2018
1 parent fb128fe commit c4d6b9181e2f08e755e41c6b091944526de2dcf1
@@ -86,6 +86,7 @@ public Object upload(MultipartFile file, RequestContext context) throws Response
// Prepare Parameters
Patient patient = Context.getPatientService().getPatientByUuid(context.getParameter("patient"));
Visit visit = Context.getVisitService().getVisitByUuid(context.getParameter("visit"));
Encounter encounter = Context.getEncounterService().getEncounterByUuid(context.getParameter("encounter"));
Provider provider = Context.getProviderService().getProviderByUuid(context.getParameter("provider"));
String fileCaption = context.getParameter("fileCaption");
String instructions = context.getParameter("instructions");
@@ -103,11 +104,22 @@ public Object upload(MultipartFile file, RequestContext context) throws Response
if (StringUtils.isEmpty(instructions))
instructions = ValueComplex.INSTRUCTIONS_DEFAULT;
Encounter encounter = null;
if (visit != null) {
// Verify Parameters
if (encounter != null && visit != null) {
if (encounter.getVisit() != visit) {
throw new IllegalRequestException(
"The specified encounter does not belong to the provided visit, upload aborted.");
}
}
if (visit != null && encounter == null) {
encounter = attachmentsContext.getAttachmentEncounter(patient, visit, provider);
}
if (encounter != null && visit == null) {
visit = encounter.getVisit();
}
// Save Obs
Obs obs;
switch (getContentFamily(file.getContentType())) {
@@ -16,6 +16,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.Visit;
@@ -104,7 +105,7 @@ public void saveAttachment_shouldUpdateObsComment() throws Exception {
// Replay
Object doc = deserialize(handle(newPostRequest(getURI() + "/" + getUuid(), json)));
// Verif
// Verify
String comment = (String) PropertyUtils.getProperty(doc, "comment");
assertEquals(editedComment, comment);
}
@@ -117,7 +118,7 @@ public void deleteAttachment_shouldVoidObs() throws Exception {
// Replay
handle(newDeleteRequest(getURI() + "/" + getUuid()));
// Verif
// Verify
assertTrue(obsService.getObsByUuid(getUuid()).isVoided());
}
@@ -131,7 +132,7 @@ public void deleteAttachmentWithMissingFile_shouldVoidObs() throws Exception {
// Replay
handle(newDeleteRequest(getURI() + "/" + getUuid()));
// Verif
// Verify
assertTrue(obsService.getObsByUuid(getUuid()).isVoided());
}
@@ -144,7 +145,7 @@ public void purgeAttachment_shouldPurgeObsAndRemoveFile() throws Exception {
// Replay
handle(newDeleteRequest(getURI() + "/" + getUuid(), new Parameter("purge", "")));
// Verif
// Verify
assertNull(obsService.getObsByUuid(getUuid()));
assertFalse(file.exists());
}
@@ -159,7 +160,7 @@ public void purgeAttachmentWithMissingFile_shouldPurgeObs() throws Exception {
// Replay
handle(newDeleteRequest(getURI() + "/" + getUuid(), new Parameter("purge", "")));
// Verif
// Verify
assertNull(obsService.getObsByUuid(getUuid()));
}
@@ -188,10 +189,8 @@ public void purgeAttachment_shouldPurgeObsWithoutAssociatedEncounter() throws Ex
}
@Test
public void postAttachment_shouldUploadFile() throws Exception {
public void postAttachment_shouldUploadFileToVisit() throws Exception {
String fileCaption = "Test file caption";
{
// Setup
String fileName = "testFile1.dat";
@@ -220,33 +219,88 @@ public void postAttachment_shouldUploadFile() throws Exception {
Assert.assertNotNull(obs.getEncounter());
Assert.assertEquals(obs.getEncounter().getEncounterType(), attachmentsContext.getEncounterType());
}
}
@Test
public void postAttachment_shouldUploadFileAsEncounterless() throws Exception {
String fileCaption = "Test file caption";
// File upload should not require visit
{
// Setup
String fileName = "testFile2.dat";
Patient patient = Context.getPatientService().getPatient(2);
MockMultipartHttpServletRequest request = newUploadRequest(getURI());
MockMultipartFile file = new MockMultipartFile("file", fileName, "application/octet-stream", randomData);
request.addFile(file);
request.addParameter("patient", patient.getUuid());
request.addParameter("fileCaption", fileCaption);
// Replay
SimpleObject response = deserialize(handle(request));
Obs obs = Context.getObsService().getObsByUuid((String) response.get("uuid"));
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), null);
ComplexData complexData = complexObs.getComplexData();
// Verify
Assert.assertEquals(obs.getComment(), fileCaption);
Assert.assertEquals(complexData.getTitle(), fileName);
Assert.assertArrayEquals(randomData, (byte[]) complexData.getData());
Assert.assertNull(obs.getEncounter());
}
// Setup
String fileName = "testFile2.dat";
Patient patient = Context.getPatientService().getPatient(2);
MockMultipartHttpServletRequest request = newUploadRequest(getURI());
MockMultipartFile file = new MockMultipartFile("file", fileName, "application/octet-stream", randomData);
request.addFile(file);
request.addParameter("patient", patient.getUuid());
request.addParameter("fileCaption", fileCaption);
// Replay
SimpleObject response = deserialize(handle(request));
Obs obs = Context.getObsService().getObsByUuid((String) response.get("uuid"));
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), null);
ComplexData complexData = complexObs.getComplexData();
// Verify
Assert.assertEquals(obs.getComment(), fileCaption);
Assert.assertEquals(complexData.getTitle(), fileName);
Assert.assertArrayEquals(randomData, (byte[]) complexData.getData());
Assert.assertNull(obs.getEncounter());
}
@Test
public void postAttachment_shouldUploadFileToEncounter() throws Exception {
String fileCaption = "Test file caption";
// Setup
String fileName = "testFile3.dat";
Patient patient = Context.getPatientService().getPatient(2);
Encounter encounter = testHelper.getTestEncounter();
MockMultipartHttpServletRequest request = newUploadRequest(getURI());
MockMultipartFile file = new MockMultipartFile("file", fileName, "application/octet-stream", randomData);
request.addFile(file);
request.addParameter("patient", patient.getUuid());
request.addParameter("encounter", encounter.getUuid());
request.addParameter("fileCaption", fileCaption);
// Replay
SimpleObject response = deserialize(handle(request));
Obs obs = Context.getObsService().getObsByUuid((String) response.get("uuid"));
Obs complexObs = Context.getObsService().getComplexObs(obs.getObsId(), null);
ComplexData complexData = complexObs.getComplexData();
// Verify
Assert.assertEquals(obs.getComment(), fileCaption);
Assert.assertEquals(complexData.getTitle(), fileName);
Assert.assertArrayEquals(randomData, (byte[]) complexData.getData());
Assert.assertEquals(obs.getEncounter().getUuid(), encounter.getUuid());
}
@Test(expected = IllegalRequestException.class)
public void postAttachment_shouldThrowWhenVisitAndEncounterDoNotMatch() throws Exception {
// Setup
String fileCaption = "Test file caption";
String fileName = "testFile1.dat";
Patient patient = Context.getPatientService().getPatient(2);
Visit visit = Context.getVisitService().getVisit(1);
Encounter encounter = Context.getEncounterService().getEncounter(3);
MockMultipartHttpServletRequest request = newUploadRequest(getURI());
MockMultipartFile file = new MockMultipartFile("file", fileName, "application/octet-stream", randomData);
request.addFile(file);
request.addParameter("patient", patient.getUuid());
request.addParameter("visit", visit.getUuid());
request.addParameter("encounter", encounter.getUuid());
request.addParameter("fileCaption", fileCaption);
// Replay
SimpleObject response = deserialize(handle(request));
}
@Test(expected = IllegalRequestException.class)

0 comments on commit c4d6b91

Please sign in to comment.