diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java index 4b0dc4000c..de89c6605a 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/dao/impl/PatientDaoImpl.java @@ -122,6 +122,10 @@ private PatientResponse toPatientResponse(PatientResponseMapper patientResponseM } private List getPatientsByNameAndGender(String name, String gender, Integer length) { + if(isNullOrEmpty(name, gender)) { + return new ArrayList<>(); + } + HibernatePatientDAO patientDAO = new HibernatePatientDAO(); patientDAO.setSessionFactory(sessionFactory); List patients = new ArrayList(); @@ -139,6 +143,10 @@ && checkGender(personName.getPerson(), gender) return patients; } + private Boolean isNullOrEmpty(String name, String gender) { + return (name == null || name.trim().isEmpty()) && (gender == null || gender.isEmpty()); + } + private Boolean checkGender(Person person, String gender) { if(gender != null && !gender.isEmpty()){ diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java index 29dc45426d..580103a0ae 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/dao/impl/BahmniPatientDaoImplLuceneIT.java @@ -248,4 +248,18 @@ public void shouldSearchSimilarPatientByPatientNameAndGender() { assertEquals(patient1.getMiddleName(), "Peeter"); assertEquals(patient1.getFamilyName(), "Sinha"); } + + @Test + public void shouldReturnEmptyListIfAllSearchTermsAreEmpty() { + List patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "", "c36006e5-9fbb-4f20-866b-0ece245615a1", 5); + + assertEquals(0, patients.size()); + } + + @Test + public void shouldReturnResultsIfOneFieldIsSet() { + List patients = patientDao.getSimilarPatientsUsingLuceneSearch("", "F", "c36006e5-9fbb-4f20-866b-0ece245615a1", 5); + + assertEquals(5, patients.size()); + } } diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java index 3bff7677c5..2a6eba22c6 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/service/impl/BahmniPatientServiceImplTest.java @@ -76,11 +76,10 @@ public void shouldGetPatientByPartialIdentifier() throws Exception { } @Test - public void shouldCallgetSimilarPatientsUsingLuceneSearch() { + public void shouldCallGetSimilarPatientsUsingLuceneSearch() { HttpServletRequest request = mock(HttpServletRequest.class); when(requestContext.getRequest()).thenReturn(request); when(request.getParameterMap()).thenReturn(new HashMap<>()); - PatientSearchParameters patientSearchParameters = new PatientSearchParameters(requestContext); patientSearchParameters.setName("John"); patientSearchParameters.setGender("M"); diff --git a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java index 6e5b0ba76f..9885526517 100644 --- a/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java +++ b/bahmnicore-omod/src/main/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchController.java @@ -1,14 +1,8 @@ package org.bahmni.module.bahmnicore.web.v1_0.controller.search; import org.bahmni.module.bahmnicore.contract.patient.PatientSearchParameters; -import org.bahmni.module.bahmnicore.contract.patient.mapper.PatientResponseMapper; import org.bahmni.module.bahmnicore.contract.patient.response.PatientResponse; import org.bahmni.module.bahmnicore.service.BahmniPatientService; -import org.openmrs.Patient; -import org.openmrs.PatientIdentifier; -import org.openmrs.Person; -import org.openmrs.api.context.Context; -import org.openmrs.module.bahmniemrapi.visitlocation.BahmniVisitLocationServiceImpl; import org.openmrs.module.webservices.rest.web.RequestContext; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.RestUtil; @@ -21,7 +15,6 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; diff --git a/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchControllerIT.java b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchControllerIT.java new file mode 100644 index 0000000000..a64bd1bb2f --- /dev/null +++ b/bahmnicore-omod/src/test/java/org/bahmni/module/bahmnicore/web/v1_0/controller/search/BahmniPatientSearchControllerIT.java @@ -0,0 +1,45 @@ +package org.bahmni.module.bahmnicore.web.v1_0.controller.search; + +import org.bahmni.module.bahmnicore.web.v1_0.BaseIntegrationTest; +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class BahmniPatientSearchControllerIT extends BaseIntegrationTest { + + @Before + public void setup() throws Exception { + executeDataSet("apiTestData.xml"); + updateSearchIndex(); + } + + @Test + public void shouldReturnPatientDetailsWhenUsingLuceneSearch() throws Exception { + MockHttpServletRequest request = newGetRequest("/rest/v1/bahmnicore/search/patient/lucene"); + request.setParameter("loginLocationUuid", "c36006e5-9fbb-4f20-866b-0ece245615a1"); + request.setParameter("q", "GAN200001"); + + MockHttpServletResponse response = handle(request); + + assertEquals(200, response.getStatus()); + assertTrue("Expected response to contain patient uuid", response.getContentAsString().contains("uuid")); + } + + @Test + public void shouldReturnPatientDetailsWhenUsingSimilarSearch() throws Exception { + MockHttpServletRequest request = newGetRequest("/rest/v1/bahmnicore/search/patient/similar"); + request.setParameter("gender", "M"); + request.setParameter("loginLocationUuid", "c36006e5-9fbb-4f20-866b-0ece245615a1"); + request.setParameter("q", "John"); + + MockHttpServletResponse response = handle(request); + + assertEquals(200, response.getStatus()); + assertTrue("Expected response to contain list of results", response.getContentAsString().contains("pageOfResults")); + assertTrue("Expected response to contain patient uuid", response.getContentAsString().contains("uuid")); + } +} \ No newline at end of file