Skip to content

Commit

Permalink
BAH-3407 | Refactor. used conceptsByName method and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SanoferSameera committed Jan 3, 2024
1 parent a5e46c2 commit 12a257b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.openmrs.Patient;
import org.openmrs.PatientProgram;
import org.openmrs.Visit;
import org.openmrs.ConceptSearchResult;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.episodes.Episode;
Expand All @@ -22,16 +21,14 @@
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.InvalidSearchException;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.LocaleUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.openmrs.util.LocaleUtility;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;

Expand Down Expand Up @@ -63,7 +60,7 @@ public PageableResult search(RequestContext context) throws ResponseException {
}
List<String> conceptNamesList = new ArrayList<>();
if (conceptNames == null) {
List<Concept> concepts = getConceptsByNameAndLocale(ALL_OBSERVATION_TEMPLATES,searchLocale);
List<Concept> concepts = Context.getConceptService().getConceptsByName(ALL_OBSERVATION_TEMPLATES, searchLocale, null);
if(!concepts.isEmpty()){
for (Concept concept : concepts) {
conceptNamesList = getConcepts(concept.getSetMembers());
Expand All @@ -84,15 +81,6 @@ public PageableResult search(RequestContext context) throws ResponseException {

return new NeedsPaging<Obs>(finalObsList, context);
}
private List<Concept> getConceptsByNameAndLocale(String conceptName, Locale searchLocale) {
if (conceptName != null) {
List<Locale> localeList = Collections.singletonList(searchLocale);
List<ConceptSearchResult> conceptsSearchResult = Context.getConceptService().getConcepts(conceptName, localeList, false, null, null, null, null, null, 0, null);

return conceptsSearchResult.stream().map(ConceptSearchResult::getConcept).collect(Collectors.toList());
}
return new ArrayList<Concept>();
}

private Locale getLocale(String locale) {
if (locale != null && !locale.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import org.bahmni.module.bahmnicore.service.BahmniProgramWorkflowService;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.DynamicTest;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.mockito.stubbing.OngoingStubbing;
import org.openmrs.*;
import org.openmrs.Concept;
import org.openmrs.ConceptName;
import org.openmrs.Encounter;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.PatientProgram;
import org.openmrs.Visit;
import org.openmrs.api.ConceptService;
import org.openmrs.api.EncounterService;
import org.openmrs.api.ObsService;
Expand All @@ -23,23 +27,23 @@
import org.openmrs.module.webservices.rest.web.resource.api.SearchConfig;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.InvalidSearchException;
import org.openmrs.util.LocaleUtility;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.AdditionalAnswers.returnsElementsOf;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
Expand All @@ -48,7 +52,6 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.openmrs.util.LocaleUtility.fromSpecification;

@PrepareForTest(Context.class)
@RunWith(PowerMockRunner.class)
Expand All @@ -74,11 +77,10 @@ public class VisitFormsSearchHandlerTest {
private EpisodeService episodeService;

private Patient patient;
List<Locale> localeList = Collections.singletonList(new Locale("en"));
Locale locale = new Locale("en");
private Concept concept;
private Obs obs;
private ConceptSearchResult conceptsSearchResult;
private List<ConceptSearchResult> conceptsSearchResultList;
private final List<Concept> concepts = new ArrayList<>();
private final String conceptNames = null;


Expand Down Expand Up @@ -173,8 +175,10 @@ public void shouldReturnAllObsIfConceptNameIsNotSpecified() throws Exception {
parentConcept.addSetMember(concept);
Concept historyConcept = createConcept("History and Examination", "en");
parentConcept.addSetMember(historyConcept);
conceptList.add(parentConcept);

when(Context.getConceptService().getConceptsByName("All Observation Templates", new Locale("en"), null)).thenReturn(conceptList);

PowerMockito.when(conceptService.getConcept("All Observation Templates")).thenReturn(parentConcept);
Obs obs2 = createObs(historyConcept);

PowerMockito.when(obsService.getObservations(any(List.class), any(List.class), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null), eq(null), eq(false))).thenReturn(Arrays.asList(obs, obs2));
Expand Down Expand Up @@ -212,9 +216,9 @@ public void shouldThrowExceptionIfThePatienUuidIsNull(){
}

@Test
public void shouldGetObservationsWithinThePatientProgramIfThePatientProgramUuidIsPassed() throws Exception {
public void shouldGetObservationsWithinThePatientProgramIfThePatientProgramUuidIsPassed() {
when(context.getRequest().getParameterValues("conceptNames")).thenReturn(null);
when(Context.getConceptService().getConcepts(conceptNames, localeList, false, null, null, null, null, null, 0, null)).thenReturn(conceptsSearchResultList);
when(Context.getConceptService().getConceptsByName("conceptNames",locale,null)).thenReturn(concepts);
String patientProgramUuid = "patient-program-uuid";
when(context.getRequest().getParameter("patientProgramUuid")).thenReturn(patientProgramUuid);
when(Context.getService(BahmniProgramWorkflowService.class)).thenReturn(programWorkflowService);
Expand All @@ -229,7 +233,7 @@ public void shouldGetObservationsWithinThePatientProgramIfThePatientProgramUuidI

visitFormsSearchHandler.search(context);

verify(conceptService, times(1)).getConcepts("All Observation Templates", localeList, false, null, null, null, null, null, 0, null);
verify(conceptService, times(1)).getConceptsByName("All Observation Templates", locale, null);
verify(programWorkflowService, times(1)).getPatientProgramByUuid(patientProgramUuid);
verify(episodeService, times(1)).getEpisodeForPatientProgram(patientProgram);
verify(visitService, never()).getVisitsByPatient(patient);
Expand All @@ -240,7 +244,7 @@ public void shouldGetObservationsWithinThePatientProgramIfThePatientProgramUuidI
@Test
public void shouldNotFetchAnyObservationsIfThereIsNoEpisodeForTheProgram() throws Exception {
when(context.getRequest().getParameterValues("conceptNames")).thenReturn(null);
when(Context.getConceptService().getConcepts("conceptNames", localeList, false, null, null, null, null, null, 0, null)).thenReturn(conceptsSearchResultList);
when(Context.getConceptService().getConceptsByName("conceptNames", locale, null)).thenReturn(concepts);
String patientProgramUuid = "patient-program-uuid";
when(context.getRequest().getParameter("patientProgramUuid")).thenReturn(patientProgramUuid);
when(Context.getService(BahmniProgramWorkflowService.class)).thenReturn(programWorkflowService);
Expand All @@ -253,7 +257,7 @@ public void shouldNotFetchAnyObservationsIfThereIsNoEpisodeForTheProgram() throw

visitFormsSearchHandler.search(context);

verify(conceptService, times(1)).getConcepts("All Observation Templates", localeList, false, null, null, null, null, null, 0, null);
verify(conceptService, times(1)).getConceptsByName("All Observation Templates", locale, null);
verify(programWorkflowService, times(1)).getPatientProgramByUuid(patientProgramUuid);
verify(episodeService, times(1)).getEpisodeForPatientProgram(patientProgram);
verify(visitService, never()).getVisitsByPatient(patient);
Expand All @@ -262,8 +266,8 @@ public void shouldNotFetchAnyObservationsIfThereIsNoEpisodeForTheProgram() throw
}

@Test
public void shouldNotFetchAnyObservationsIfThereAreNoEncountersInEpisode() throws Exception {
when(Context.getConceptService().getConcepts(conceptNames, localeList, false, null, null, null, null, null, 0, null)).thenReturn(conceptsSearchResultList);
public void shouldNotFetchAnyObservationsIfThereAreNoEncountersInEpisode() {
when(Context.getConceptService().getConceptsByName(conceptNames, locale, null)).thenReturn(concepts);
when(context.getRequest().getParameterValues("conceptNames")).thenReturn(null);
String patientProgramUuid = "patient-program-uuid";
when(context.getRequest().getParameter("patientProgramUuid")).thenReturn(patientProgramUuid);
Expand All @@ -278,7 +282,7 @@ public void shouldNotFetchAnyObservationsIfThereAreNoEncountersInEpisode() throw

visitFormsSearchHandler.search(context);

verify(conceptService, times(1)).getConcepts("All Observation Templates", localeList, false, null, null, null, null, null, 0, null);
verify(conceptService, times(1)).getConceptsByName("All Observation Templates", locale, null);
verify(programWorkflowService, times(1)).getPatientProgramByUuid(patientProgramUuid);
verify(episodeService, times(1)).getEpisodeForPatientProgram(patientProgram);
verify(visitService, never()).getVisitsByPatient(patient);
Expand Down

0 comments on commit 12a257b

Please sign in to comment.