diff --git a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImpl.java b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImpl.java index c6d369459f..cb09203f9c 100644 --- a/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImpl.java +++ b/bahmnicore-api/src/main/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImpl.java @@ -57,6 +57,12 @@ public BahmniFormDetailsServiceImpl(PatientService patientService, VisitService public Collection getFormDetails(String patientUuid, FormType formType, int numberOfVisits) { Patient patient = getPatient(patientUuid); List visits = visitService.getVisitsByPatient(patient); + + //Warning: This check is needed to avoid getEncounters returning ALL non-voided encounters of all patients leading to OutOfMemoryError:Java Heap + //Refer: https://bahmni.atlassian.net/browse/BAH-3513 + if(visits.isEmpty()) + return Collections.emptyList(); + List limitedVisits = limitVisits(visits, numberOfVisits); List encounters = getEncounters(limitedVisits); diff --git a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImplTest.java b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImplTest.java index 5ec1a9a612..84d96f4bc6 100644 --- a/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImplTest.java +++ b/bahmnicore-api/src/test/java/org/bahmni/module/bahmnicore/forms2/service/impl/BahmniFormDetailsServiceImplTest.java @@ -166,14 +166,36 @@ public void shouldReturnFormDetailsGivenPatientUuidFormTypeAsV2AndNumberOfVisits @Test public void shouldReturnEmptyCollectionsOfFormDetailsIfPatientDoesNotHaveVisits() { when(visitService.getVisitsByPatient(patient)).thenReturn(Collections.emptyList()); - shouldReturnEmptyCollectionsOfFormDetailsIfPatientDoesNotHaveVisitsOrEncounters(); + Collection formDetailsCollection = bahmniFormDetailsService.getFormDetails(patientUuid, FormType.FORMS2, -1); + + assertEquals(0, formDetailsCollection.size()); + + verify(patientService, times(1)).getPatientByUuid(patientUuid); + verify(visitService, times(1)).getVisitsByPatient(patient); + verify(encounterService, times(0)).getEncounters(any(EncounterSearchCriteria.class)); + + verify(patient, times(0)).getPerson(); + verify(obsService, times(0)).getObservations(anyListOf(Person.class), + anyListOf(Encounter.class), any(), any(), any(), any(), any(), any(), any(), any(), any(), + any(Boolean.class)); } @Test public void shouldReturnEmptyCollectionsOfFormDetailsIfPatientDoesNotHaveEncounters() { when(encounterService.getEncounters(any(EncounterSearchCriteria.class))).thenReturn(Collections.emptyList()); - shouldReturnEmptyCollectionsOfFormDetailsIfPatientDoesNotHaveVisitsOrEncounters(); + Collection formDetailsCollection = bahmniFormDetailsService.getFormDetails(patientUuid, FormType.FORMS2, -1); + + assertEquals(0, formDetailsCollection.size()); + + verify(patientService, times(1)).getPatientByUuid(patientUuid); + verify(visitService, times(1)).getVisitsByPatient(patient); + verify(encounterService, times(1)).getEncounters(any(EncounterSearchCriteria.class)); + + verify(patient, times(0)).getPerson(); + verify(obsService, times(0)).getObservations(anyListOf(Person.class), + anyListOf(Encounter.class), any(), any(), any(), any(), any(), any(), any(), any(), any(), + any(Boolean.class)); } @Test @@ -314,21 +336,4 @@ private void verifyCommonMockCalls() { any(Boolean.class)); } - private void shouldReturnEmptyCollectionsOfFormDetailsIfPatientDoesNotHaveVisitsOrEncounters() { - - Collection formDetailsCollection = bahmniFormDetailsService.getFormDetails(patientUuid, FormType.FORMS2, -1); - - assertEquals(0, formDetailsCollection.size()); - - verify(patientService, times(1)).getPatientByUuid(patientUuid); - verify(visitService, times(1)).getVisitsByPatient(patient); - verify(encounterService, times(1)).getEncounters(any(EncounterSearchCriteria.class)); - - verify(patient, times(0)).getPerson(); - verify(obsService, times(0)).getObservations(anyListOf(Person.class), - anyListOf(Encounter.class), any(), any(), any(), any(), any(), any(), any(), any(), any(), - any(Boolean.class)); - - } - }