From f14e36a6b23e9d0a1b21bb8d3bbdc44b1e18dc4a Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 18 May 2021 19:34:57 +0300 Subject: [PATCH] Add unit tests --- .../ChildAdvancedSearchInteractorTest.java | 198 +++++++++++++++++- 1 file changed, 189 insertions(+), 9 deletions(-) diff --git a/opensrp-child/src/test/java/org/smartregister/child/interactor/ChildAdvancedSearchInteractorTest.java b/opensrp-child/src/test/java/org/smartregister/child/interactor/ChildAdvancedSearchInteractorTest.java index 9dee4ea1c..5e9a7608d 100644 --- a/opensrp-child/src/test/java/org/smartregister/child/interactor/ChildAdvancedSearchInteractorTest.java +++ b/opensrp-child/src/test/java/org/smartregister/child/interactor/ChildAdvancedSearchInteractorTest.java @@ -1,9 +1,12 @@ package org.smartregister.child.interactor; +import org.json.JSONArray; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @@ -16,18 +19,17 @@ import org.smartregister.child.ChildLibrary; import org.smartregister.child.util.AppExecutors; import org.smartregister.child.util.ChildAppProperties; +import org.smartregister.child.util.Constants; import org.smartregister.domain.Response; import org.smartregister.domain.ResponseStatus; import org.smartregister.service.HTTPAgent; +import org.smartregister.util.AppProperties; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.List; import java.util.Map; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - @RunWith(PowerMockRunner.class) @PrepareForTest({CoreLibrary.class, ChildLibrary.class}) public class ChildAdvancedSearchInteractorTest { @@ -47,6 +49,9 @@ public class ChildAdvancedSearchInteractorTest { @Mock private HTTPAgent httpAgent; + @Mock + private AppProperties appProperties; + private ChildAdvancedSearchInteractor childAdvancedSearchInteractor; @Before @@ -54,9 +59,8 @@ public void setUp() { MockitoAnnotations.initMocks(this); PowerMockito.mockStatic(ChildLibrary.class); PowerMockito.when(ChildLibrary.getInstance()).thenReturn(childLibrary); - ChildAppProperties appProperties = new ChildAppProperties(); PowerMockito.doReturn(appProperties).when(childLibrary).getProperties(); - childAdvancedSearchInteractor = new ChildAdvancedSearchInteractor(Mockito.mock(AppExecutors.class)); + childAdvancedSearchInteractor = Mockito.spy(new ChildAdvancedSearchInteractor(Mockito.mock(AppExecutors.class))); } @Test @@ -77,16 +81,192 @@ public void testGlobalSearch() throws Exception { PowerMockito.when(coreLibrary.context()).thenReturn(context); PowerMockito.when(context.configuration()).thenReturn(dristhiConfiguration); - ChildAdvancedSearchInteractor interactor = spy(ChildAdvancedSearchInteractor.class); + ChildAdvancedSearchInteractor interactor = Mockito.spy(ChildAdvancedSearchInteractor.class); PowerMockito.when(interactor.getHttpAgent()).thenReturn(httpAgent); Response resp = new Response<>(ResponseStatus.success, "success"); - PowerMockito.when(httpAgent.fetch(anyString())).thenReturn(resp); + PowerMockito.when(httpAgent.fetch(Mockito.anyString())).thenReturn(resp); Map map = new HashMap<>(); map.put("key", "test"); globalSearchMethod.invoke(childAdvancedSearchInteractor, map); - verify(interactor).getHttpAgent(); + Mockito.verify(interactor).getHttpAgent(); + } + + @Test + public void testGlobalSearchWhenUseNewAdvanceSearchApproachIsTrue() throws Exception { + + String expectedMotherClientSearchUri = "https://www.test-opensrp.smartregister.org/rest/client/search?name=Momma&attribute=mother_guardian_number:+2546718880001&searchRelationship=mother"; + String expectedChildClientSearchUri = "https://www.test-opensrp.smartregister.org/rest/client/search?identifier=F2103003K&relationships=mother"; + + Method globalSearchMethod = ChildAdvancedSearchInteractor.class.getDeclaredMethod("globalSearch", Map.class); + globalSearchMethod.setAccessible(true); + + PowerMockito.mockStatic(CoreLibrary.class); + PowerMockito.when(CoreLibrary.getInstance()).thenReturn(coreLibrary); + PowerMockito.when(coreLibrary.context()).thenReturn(context); + + Mockito.doReturn("https://www.test-opensrp.smartregister.org").when(dristhiConfiguration).dristhiBaseURL(); + PowerMockito.when(context.configuration()).thenReturn(dristhiConfiguration); + + ChildAdvancedSearchInteractor interactor = Mockito.spy(ChildAdvancedSearchInteractor.class); + PowerMockito.when(interactor.getHttpAgent()).thenReturn(httpAgent); + + Response motherResponse = new Response<>(ResponseStatus.success, "[{\"zeir_id\":\"M_F2103003KL\",\"last_name\":\"Madea\",\"first_name\":\"Momma\"}]"); + PowerMockito.when(httpAgent.fetch(expectedMotherClientSearchUri)).thenReturn(motherResponse); + + Response childResponse = new Response<>(ResponseStatus.success, "[{\"zeir_id\":\"F2103003KL\",\"last_name\":\"Mwanza\",\"first_name\":\"Sylvia\"},{\"zeir_id\":\"F2103003K\",\"last_name\":\"Banda\",\"first_name\":\"Sylvia\"}]"); + PowerMockito.when(httpAgent.fetch(expectedChildClientSearchUri)).thenReturn(childResponse); + + Mockito.doReturn(true).when(appProperties).isTrue(ChildAppProperties.KEY.USE_NEW_ADVANCE_SEARCH_APPROACH); + + Map searchParametersMap = new HashMap<>(); + + searchParametersMap.put(Constants.KEY.FIRST_NAME, "Sylvia"); + searchParametersMap.put(Constants.KEY.LAST_NAME, "Banda"); + searchParametersMap.put(Constants.KEY.MOTHER_FIRST_NAME, "Momma"); + searchParametersMap.put(Constants.KEY.MOTHER_LAST_NAME, "Madea"); + searchParametersMap.put(Constants.KEY.ZEIR_ID, "F2103003K"); + searchParametersMap.put(Constants.KEY.MOTHER_GUARDIAN_NUMBER, "+2546718880001"); + + Response response = (Response) globalSearchMethod.invoke(childAdvancedSearchInteractor, searchParametersMap); + + //Verify crucial method invocations + ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); + Mockito.verify(httpAgent, Mockito.times(2)).fetch(stringArgumentCaptor.capture()); + + List capturedParamValue = stringArgumentCaptor.getAllValues(); + Assert.assertNotNull(capturedParamValue); + Assert.assertEquals(2, capturedParamValue.size()); + Assert.assertEquals(expectedMotherClientSearchUri, capturedParamValue.get(0)); + Assert.assertEquals(expectedChildClientSearchUri, capturedParamValue.get(1)); + + //Assert payload + Assert.assertNotNull(response); + + String payload = response.payload(); + Assert.assertNotNull(payload); + + JSONArray payloadArray = new JSONArray(payload); + Assert.assertEquals(3, payloadArray.length()); + + Assert.assertEquals("Sylvia", payloadArray.getJSONObject(0).getString(Constants.KEY.FIRST_NAME)); + Assert.assertEquals("Mwanza", payloadArray.getJSONObject(0).getString(Constants.KEY.LAST_NAME)); + + Assert.assertEquals("Sylvia", payloadArray.getJSONObject(1).getString(Constants.KEY.FIRST_NAME)); + Assert.assertEquals("Banda", payloadArray.getJSONObject(1).getString(Constants.KEY.LAST_NAME)); + + //Mother + Assert.assertEquals("Momma", payloadArray.getJSONObject(2).getString(Constants.KEY.FIRST_NAME)); + Assert.assertEquals("Madea", payloadArray.getJSONObject(2).getString(Constants.KEY.LAST_NAME)); + } + + @Test + public void testGlobalSearchWhenUseNewAdvanceSearchApproachIsTrueAndOnlyChildParamsProvided() throws Exception { + + String expectedChildClientSearchUri = "https://www.test-opensrp.smartregister.org/rest/client/search?name=Sylvia&birthdate=2015-02-10:2021-02-25&attribute=lost_to_follow_up:true&relationships=mother"; + + Method globalSearchMethod = ChildAdvancedSearchInteractor.class.getDeclaredMethod("globalSearch", Map.class); + globalSearchMethod.setAccessible(true); + + PowerMockito.mockStatic(CoreLibrary.class); + PowerMockito.when(CoreLibrary.getInstance()).thenReturn(coreLibrary); + PowerMockito.when(coreLibrary.context()).thenReturn(context); + + Mockito.doReturn("https://www.test-opensrp.smartregister.org").when(dristhiConfiguration).dristhiBaseURL(); + PowerMockito.when(context.configuration()).thenReturn(dristhiConfiguration); + + ChildAdvancedSearchInteractor interactor = Mockito.spy(ChildAdvancedSearchInteractor.class); + PowerMockito.when(interactor.getHttpAgent()).thenReturn(httpAgent); + + Response childResponse = new Response<>(ResponseStatus.success, "[{\"zeir_id\":\"F2103003KL\",\"last_name\":\"Mwanza\",\"first_name\":\"Sylvia\"}]"); + PowerMockito.when(httpAgent.fetch(ArgumentMatchers.anyString())).thenReturn(childResponse); + + Mockito.doReturn(true).when(appProperties).isTrue(ChildAppProperties.KEY.USE_NEW_ADVANCE_SEARCH_APPROACH); + + Map searchParametersMap = new HashMap<>(); + + searchParametersMap.put(Constants.KEY.FIRST_NAME, "Sylvia"); + searchParametersMap.put(Constants.KEY.LAST_NAME, "Mwanza"); + searchParametersMap.put(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP, "true"); + + searchParametersMap.put(Constants.KEY.BIRTH_DATE, "2015-02-10:2021-02-25"); + + Response response = (Response) globalSearchMethod.invoke(childAdvancedSearchInteractor, searchParametersMap); + + //Verify crucial method invocations + ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); + Mockito.verify(httpAgent, Mockito.atMostOnce()).fetch(stringArgumentCaptor.capture()); + + List capturedParamValue = stringArgumentCaptor.getAllValues(); + Assert.assertNotNull(capturedParamValue); + Assert.assertEquals(1, capturedParamValue.size()); + Assert.assertEquals(expectedChildClientSearchUri, capturedParamValue.get(0)); + + //Assert payload + Assert.assertNotNull(response); + + String payload = response.payload(); + Assert.assertNotNull(payload); + + JSONArray payloadArray = new JSONArray(payload); + Assert.assertEquals(1, payloadArray.length()); + + Assert.assertEquals("Sylvia", payloadArray.getJSONObject(0).getString(Constants.KEY.FIRST_NAME)); + Assert.assertEquals("Mwanza", payloadArray.getJSONObject(0).getString(Constants.KEY.LAST_NAME)); + } + + @Test + public void testGlobalSearchWhenUseNewAdvanceSearchApproachIsTrueAndOnlyMotherParamsProvided() throws Exception { + + String expectedMotherClientSearchUri = "https://www.test-opensrp.smartregister.org/rest/client/search?name=Madea&attribute=mother_guardian_number:+2546718880001&searchRelationship=mother"; + + Method globalSearchMethod = ChildAdvancedSearchInteractor.class.getDeclaredMethod("globalSearch", Map.class); + globalSearchMethod.setAccessible(true); + + PowerMockito.mockStatic(CoreLibrary.class); + PowerMockito.when(CoreLibrary.getInstance()).thenReturn(coreLibrary); + PowerMockito.when(coreLibrary.context()).thenReturn(context); + + Mockito.doReturn("https://www.test-opensrp.smartregister.org").when(dristhiConfiguration).dristhiBaseURL(); + PowerMockito.when(context.configuration()).thenReturn(dristhiConfiguration); + + ChildAdvancedSearchInteractor interactor = Mockito.spy(ChildAdvancedSearchInteractor.class); + PowerMockito.when(interactor.getHttpAgent()).thenReturn(httpAgent); + + Response motherResponse = new Response<>(ResponseStatus.success, "[{\"zeir_id\":\"M_F2103003KL\",\"last_name\":\"Madea\",\"first_name\":\"Momma\"}]"); + PowerMockito.when(httpAgent.fetch(ArgumentMatchers.anyString())).thenReturn(motherResponse); + + Mockito.doReturn(true).when(appProperties).isTrue(ChildAppProperties.KEY.USE_NEW_ADVANCE_SEARCH_APPROACH); + + Map searchParametersMap = new HashMap<>(); + + searchParametersMap.put(Constants.KEY.MOTHER_LAST_NAME, "Madea"); + searchParametersMap.put(Constants.KEY.MOTHER_GUARDIAN_NUMBER, "+2546718880001"); + + Response response = (Response) globalSearchMethod.invoke(childAdvancedSearchInteractor, searchParametersMap); + + //Verify crucial method invocations + ArgumentCaptor stringArgumentCaptor = ArgumentCaptor.forClass(String.class); + Mockito.verify(httpAgent, Mockito.atMostOnce()).fetch(stringArgumentCaptor.capture()); + + List capturedParamValue = stringArgumentCaptor.getAllValues(); + Assert.assertNotNull(capturedParamValue); + Assert.assertEquals(1, capturedParamValue.size()); + Assert.assertEquals(expectedMotherClientSearchUri, capturedParamValue.get(0)); + + //Assert payload + Assert.assertNotNull(response); + + String payload = response.payload(); + Assert.assertNotNull(payload); + + JSONArray payloadArray = new JSONArray(payload); + Assert.assertEquals(1, payloadArray.length()); + + //Mother + Assert.assertEquals("Momma", payloadArray.getJSONObject(0).getString(Constants.KEY.FIRST_NAME)); + Assert.assertEquals("Madea", payloadArray.getJSONObject(0).getString(Constants.KEY.LAST_NAME)); } }