Permalink
Browse files

ATT-24: AttachmentResource's doSearch() mock-based tests

  • Loading branch information...
ridmal authored and mks-d committed Aug 9, 2018
1 parent 8f31b76 commit c237dec8aeb77c85d969fee06543ca3abd99737e
@@ -76,6 +76,8 @@
@Qualifier(AttachmentsConstants.COMPONENT_VISIT_COMPATIBILITY)
protected VisitCompatibility visitCompatibility;
@Autowired
protected AttachmentsService attachmentsService;
/*
* Exposing all needed services through OUR context
*/
@@ -120,6 +122,10 @@ public AdministrationService getAdministrationService() {
return administrationService;
}
public AttachmentsService getAttachmentsService() {
return attachmentsService;
}
public boolean doAllowEmptyCaption() {
return this.getBooleanByGlobalProperty(AttachmentsConstants.GP_ALLOW_NO_CAPTION);
}
@@ -1,5 +1,6 @@
package org.openmrs.module.attachments.rest;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.openmrs.Patient;
import org.openmrs.Encounter;
@@ -10,6 +11,7 @@
import org.openmrs.api.context.Context;
import org.openmrs.module.attachments.AttachmentsConstants;
import org.openmrs.module.attachments.AttachmentsContext;
import org.openmrs.module.attachments.AttachmentsService;
import org.openmrs.module.attachments.ComplexObsSaver;
import org.openmrs.module.attachments.obs.Attachment;
import org.openmrs.module.attachments.obs.ValueComplex;
@@ -19,15 +21,20 @@
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
import org.openmrs.module.webservices.rest.web.resource.api.Uploadable;
import org.openmrs.module.webservices.rest.web.resource.impl.DataDelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.GenericRestException;
import org.openmrs.module.webservices.rest.web.response.IllegalRequestException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.openmrs.module.attachments.AttachmentsContext.getContentFamily;
@@ -155,7 +162,7 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
/**
* Voids the encounter if it contains no non-voided obs.
*
*
* @param encounterService
* @param encounterUuid
*/
@@ -165,4 +172,81 @@ public static void voidEncounterIfEmpty(EncounterService encounterService, Strin
encounterService.voidEncounter(encounter, "foo");
}
}
/**
* Get the Attachments using AttachmentService.
*
* @param as specifies the AttachmentService instance.
* @param patient
* @param visit
* @param encounter
* @param includeEncounterless
* @param includeVoided
*/
public List<Attachment> search(AttachmentsService as, Patient patient, Visit visit, Encounter encounter,
String includeEncounterless, boolean includeVoided) {
List<Attachment> attachmentList = new ArrayList<>();
if (includeEncounterless != null) {
if (includeEncounterless.equals("only")) {
attachmentList = as.getEncounterlessAttachments(patient, includeVoided);
} else {
attachmentList = as.getAttachments(patient, BooleanUtils.toBoolean(includeEncounterless), includeVoided);
}
} else {
if (encounter != null && visit == null) {
attachmentList = as.getAttachments(patient, encounter, includeVoided);
}
if (visit != null && encounter == null) {
attachmentList = as.getAttachments(patient, visit, includeVoided);
}
if (encounter == null && visit == null) {
attachmentList = as.getAttachments(patient, includeVoided);
}
}
return attachmentList;
}
/**
* Get Attachments by given parameters (paged according to context if necessary) only if a patient
* parameter exists in the request set on the {@link RequestContext}, optional encounter, visit ,
* includeEncounterless , includeVoided request parameters can be specified to filter the
* attachments.
*
* @param context
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext)
* @return Attachments based on the user parameters
*/
@Override
protected PageableResult doSearch(RequestContext context) {
// 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"));
String includeEncounterless = context.getParameter("includeEncounterless");
Boolean includeVoided = BooleanUtils.toBoolean(context.getParameter("includeVoided"));
// Verify Parameters
if (patient == null) {
throw new IllegalRequestException("A patient parameter must be provided when searching the attachments.");
}
if (includeVoided == null) {
includeVoided = false;
}
// Search Attachments
List<Attachment> attachmentList = search(attachmentsContext.getAttachmentsService(), patient, visit, encounter,
includeEncounterless, includeVoided);
if (attachmentList != null) {
return new NeedsPaging<Attachment>(attachmentList, context);
}
return new EmptySearchResult();
}
}
@@ -0,0 +1,104 @@
package org.openmrs.module.attachments.rest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openmrs.Encounter;
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.api.context.Context;
import org.openmrs.module.attachments.AttachmentsService;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Context.class)
public class AttachmentResourceTest {
@Before
public void setup() {
initMocks(this);
PowerMockito.mockStatic(Context.class);
}
@Test
public void search_shouldInvokeApiForEncounterAttachments() {
// Setup
AttachmentResource1_10 res = new AttachmentResource1_10();
AttachmentsService attachmentsService = mock(AttachmentsService.class);
Patient patient = new Patient();
Encounter encounter = new Encounter();
// Replay
res.search(attachmentsService, patient, null, encounter, null, true);
// Verify
verify(attachmentsService, times(1)).getAttachments(patient, encounter, true);
verifyNoMoreInteractions(attachmentsService);
}
@Test
public void search_shouldInvokeApiForVisitAttachments() {
// Setup
AttachmentResource1_10 res = new AttachmentResource1_10();
AttachmentsService attachmentsService = mock(AttachmentsService.class);
Patient patient = new Patient();
Visit visit = new Visit();
// Replay
res.search(attachmentsService, patient, visit, null, null, true);
// Verify
verify(attachmentsService, times(1)).getAttachments(patient, visit, true);
verifyNoMoreInteractions(attachmentsService);
}
@Test
public void search_shouldInvokeApiForAllAttachments() {
// Setup
AttachmentResource1_10 res = new AttachmentResource1_10();
AttachmentsService attachmentsService = mock(AttachmentsService.class);
Patient patient = new Patient();
// Replay
res.search(attachmentsService, patient, null, null, null, true);
// Verify
verify(attachmentsService, times(1)).getAttachments(patient, true);
verifyNoMoreInteractions(attachmentsService);
}
@Test
public void search_shouldInvokeApiForEncounterlessAttachments() {
// Setup
AttachmentResource1_10 res = new AttachmentResource1_10();
AttachmentsService attachmentsService = mock(AttachmentsService.class);
Patient patient = new Patient();
// Replay
res.search(attachmentsService, patient, null, null, "only", true);
// Verify
verify(attachmentsService, times(1)).getEncounterlessAttachments(patient, true);
verifyNoMoreInteractions(attachmentsService);
}
@Test
public void search_shouldInvokeApiForAllAttachmentsButEncounterless() {
// Setup
AttachmentResource1_10 res = new AttachmentResource1_10();
AttachmentsService attachmentsService = mock(AttachmentsService.class);
Patient patient = new Patient();
// Replay
res.search(attachmentsService, patient, null, null, "false", true);
// Verify
verify(attachmentsService, times(1)).getAttachments(patient, false, true);
verifyNoMoreInteractions(attachmentsService);
}
}

0 comments on commit c237dec

Please sign in to comment.