diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandler.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandler.java index df34f07f7..afaddc154 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandler.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandler.java @@ -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; @@ -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; @@ -63,7 +60,7 @@ public PageableResult search(RequestContext context) throws ResponseException { } List conceptNamesList = new ArrayList<>(); if (conceptNames == null) { - List concepts = getConceptsByNameAndLocale(ALL_OBSERVATION_TEMPLATES,searchLocale); + List concepts = Context.getConceptService().getConceptsByName(ALL_OBSERVATION_TEMPLATES, searchLocale, null); if(!concepts.isEmpty()){ for (Concept concept : concepts) { conceptNamesList = getConcepts(concept.getSetMembers()); @@ -84,15 +81,6 @@ public PageableResult search(RequestContext context) throws ResponseException { return new NeedsPaging(finalObsList, context); } - private List getConceptsByNameAndLocale(String conceptName, Locale searchLocale) { - if (conceptName != null) { - List localeList = Collections.singletonList(searchLocale); - List 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(); - } private Locale getLocale(String locale) { if (locale != null && !locale.isEmpty()) { diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandlerTest.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandlerTest.java index 151ee348d..609cdaa98 100644 --- a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandlerTest.java +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/search/VisitFormsSearchHandlerTest.java @@ -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; @@ -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; @@ -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) @@ -74,11 +77,10 @@ public class VisitFormsSearchHandlerTest { private EpisodeService episodeService; private Patient patient; - List localeList = Collections.singletonList(new Locale("en")); + Locale locale = new Locale("en"); private Concept concept; private Obs obs; - private ConceptSearchResult conceptsSearchResult; - private List conceptsSearchResultList; + private final List concepts = new ArrayList<>(); private final String conceptNames = null; @@ -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)); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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);