From 7f9de003aef14e4f8bc44106d80581826231fb39 Mon Sep 17 00:00:00 2001 From: vend Date: Tue, 23 May 2023 14:44:31 +0500 Subject: [PATCH 01/14] updated SyncSettingsServiceHelper to Exception Handling --- gradle.properties | 2 +- .../smartregister/sync/helper/SyncSettingsServiceHelper.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index adc40ab8b..1f85ef9df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=6.1.1-SNAPSHOT +VERSION_NAME=6.1.2-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-core/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java b/opensrp-core/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java index db355f208..d532dff3e 100644 --- a/opensrp-core/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java +++ b/opensrp-core/src/main/java/org/smartregister/sync/helper/SyncSettingsServiceHelper.java @@ -58,6 +58,8 @@ public int processIntent() throws JSONException { } catch (JSONException e) { Timber.e(e); + } catch (NullPointerException e) { + Timber.e(e); } JSONArray settings = getSettings(); From dc7ab7da5163f0fd7eb21a3492ff16f6015b4bc6 Mon Sep 17 00:00:00 2001 From: vend Date: Wed, 24 May 2023 15:43:48 +0500 Subject: [PATCH 02/14] updated testcases for coverage check --- .../org/smartregister/clientandeventmodel/DateUtilTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java b/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java index 718146c52..0ba5a477f 100644 --- a/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java +++ b/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java @@ -1,5 +1,7 @@ package org.smartregister.clientandeventmodel; +import android.content.res.AssetManager; + import org.junit.Assert; import org.joda.time.LocalDate; @@ -42,6 +44,8 @@ public void assertFakeIt() throws Exception { Assert.assertNotNull(DateUtil.toDate(new Date(0l))); Assert.assertNotNull(DateUtil.fromDate(new Date(0l))); Assert.assertNotNull(DateUtil.getDateFromString("1985-07-24T00:00:00.000Z")); + Assert.assertNull(DateUtil.toDate("test date")); + Assert.assertNotNull(DateUtil.today()); } } From 76d2d2707ac391e2b70675c87eeca9c2e9ee6a55 Mon Sep 17 00:00:00 2001 From: vend Date: Wed, 24 May 2023 16:10:15 +0500 Subject: [PATCH 03/14] removed unused import --- .../org/smartregister/clientandeventmodel/DateUtilTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java b/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java index 0ba5a477f..5472dd15f 100644 --- a/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java +++ b/opensrp-core/src/test/java/org/smartregister/clientandeventmodel/DateUtilTest.java @@ -1,6 +1,6 @@ package org.smartregister.clientandeventmodel; -import android.content.res.AssetManager; + import org.junit.Assert; From 2e202196ba460d0ac9e55135ef8dd61c5e01e1d4 Mon Sep 17 00:00:00 2001 From: vend Date: Wed, 24 May 2023 19:37:25 +0500 Subject: [PATCH 04/14] updated new tests --- .../org/smartregister/util/UtilsTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java index 792ae7a4e..c9a6a73dd 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java @@ -23,7 +23,9 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.content.res.AssetManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; @@ -69,6 +71,9 @@ import org.smartregister.repository.AllSharedPreferences; import org.smartregister.service.UserService; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -763,5 +768,35 @@ public void testTryParseLongShouldParseCorrectly() { public void testTryParseLongShouldParseShoouldReturnDefaultValueOnException() { assertEquals(0L, (Long) Utils.tryParseLong("xyz", 0), 0); } + + @Test + public void testGetAssetFileInputStream() throws IOException { + Context mockContext = Mockito.mock(Context.class); + AssetManager mockAssetManager = Mockito.mock(AssetManager.class); + InputStream mockInputStream = Mockito.mock(InputStream.class); + Mockito.when(mockContext.getAssets()).thenReturn(mockAssetManager); + + Mockito.when(mockAssetManager.open(Mockito.eq("file_path"))).thenReturn(mockInputStream); + InputStream result = Utils.getAssetFileInputStream(mockContext, "file_path"); + Assert.assertEquals(mockInputStream, result); + } + + @Test + public void testDeleteRoomDb() { + Context mockContext = Mockito.mock(Context.class); + ApplicationInfo mockApplicationInfo = Mockito.mock(ApplicationInfo.class); + + File mockDatabasesFolder = Mockito.mock(File.class); + File mockDb = Mockito.mock(File.class); + mockApplicationInfo.dataDir = "testPath"; + + Mockito.when(mockContext.getApplicationInfo()).thenReturn(mockApplicationInfo); + Mockito.when(mockDatabasesFolder.getAbsolutePath()).thenReturn("/data/data/com.example.app/databases"); + + boolean result = Utils.deleteRoomDb(mockContext, "databaseName"); + Assert.assertFalse(result); + } + + } From 9c58a4c1464a05c4103793e207f972d3e0b00f43 Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 15:35:13 +0500 Subject: [PATCH 05/14] updated new tests --- .../org/smartregister/util/FormUtilsTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index 070704950..4b4499267 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -198,4 +198,30 @@ public void getJsonFieldFromArrayShouldReturnObjectWithCorrectNameProperty() thr Assert.assertEquals("Doe", resultJson.getString("value")); } + @Test + public void testGetObjectAtPath() throws Exception { + + String[] path1 = {"key1", "key2"}; + JSONObject jsonObject1 = new JSONObject("{\"key1\": {\"key2\": \"value\"}}"); + Object result1 = formUtils.getObjectAtPath(path1, jsonObject1); + Assert.assertEquals("value", result1); + + String[] path2 = {"key1", "key3"}; + JSONObject jsonObject2 = new JSONObject("{\"key1\": {\"key2\": \"value\"}}"); + Object result2 = formUtils.getObjectAtPath(path2, jsonObject2); + Assert.assertNull(result2); + + + String[] path3 = {"key1", "key4","key5"}; + JSONObject jsonObject3 = new JSONObject("{\"key1\": {\"key4\": [{\"key5\": \"value\"}]}}"); + Object result3 = formUtils.getObjectAtPath(path3, jsonObject3); + Assert.assertEquals("value", result3); + + + String[] path4 = {"key1", "key4"}; + JSONObject jsonObject4 = new JSONObject("{\"key1\": {\"key4\": []}}"); + Object result4 = formUtils.getObjectAtPath(path4, jsonObject4); + Assert.assertEquals("[]", result4.toString()); + } + } From b664640b6072a1d3b89437186d4e80ad06395ba3 Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 15:45:15 +0500 Subject: [PATCH 06/14] unecessasry references to assert removed --- .../org/smartregister/util/FormUtilsTest.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index 4b4499267..946fa3add 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -35,6 +35,9 @@ import java.net.URL; import java.util.HashMap; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + /** * Created by kaderchowdhury on 14/11/17. */ @@ -144,25 +147,25 @@ public InputStream answer(InvocationOnMock invocation) throws Throwable { xmlMockedStatic.when(Xml::newSerializer).thenReturn(xmlSerializer); } - Assert.assertNotNull(formUtils.generateXMLInputForFormWithEntityId("baseEntityId", FORMNAME, null)); + assertNotNull(formUtils.generateXMLInputForFormWithEntityId("baseEntityId", FORMNAME, null)); } @Test public void assertWithEntityIdReturnsFormSubmissionBuilder() { FormSubmissionBuilder builder = new FormSubmissionBuilder(); - Assert.assertNotNull(builder.withEntityId("baseEntityId")); + assertNotNull(builder.withEntityId("baseEntityId")); } @Test public void assertWithSyncStatusReturnsFormSubmissionBuilder() { FormSubmissionBuilder builder = new FormSubmissionBuilder(); SyncStatus syncStatus = null; - Assert.assertNotNull(builder.withSyncStatus(syncStatus)); + assertNotNull(builder.withSyncStatus(syncStatus)); } @Test public void assertConstructorInitializationNotNull() throws Exception { - Assert.assertNotNull(new FormUtils(context_)); + assertNotNull(new FormUtils(context_)); } public String getStringFromStream(InputStream is) throws Exception { @@ -179,13 +182,13 @@ public String getStringFromStream(InputStream is) throws Exception { public void getFormJsonShouldReturnCorrectFormWithSameLength() throws IOException { Mockito.doReturn(ApplicationProvider.getApplicationContext().getResources()).when(context_).getResources(); Mockito.doReturn(ApplicationProvider.getApplicationContext().getApplicationContext()).when(context_).getApplicationContext(); - Assert.assertEquals(10011, formUtils.getFormJson("test_basic_form").toString().length()); + assertEquals(10011, formUtils.getFormJson("test_basic_form").toString().length()); } @Test public void getIndexForFormNameShouldReturnCorrectIndex() { String[] formNames = new String[]{"Birth Reg", "Immunisation Reg", "Death Form"}; - Assert.assertEquals(1, formUtils.getIndexForFormName("Immunisation Reg", formNames)); + assertEquals(1, formUtils.getIndexForFormName("Immunisation Reg", formNames)); } @Test @@ -195,7 +198,7 @@ public void getJsonFieldFromArrayShouldReturnObjectWithCorrectNameProperty() thr JSONObject resultJson = ReflectionHelpers.callInstanceMethod(formUtils, "getJsonFieldFromArray" , ReflectionHelpers.ClassParameter.from(String.class, "last_name") , ReflectionHelpers.ClassParameter.from(JSONArray.class, jsonArray)); - Assert.assertEquals("Doe", resultJson.getString("value")); + assertEquals("Doe", resultJson.getString("value")); } @Test @@ -204,7 +207,7 @@ public void testGetObjectAtPath() throws Exception { String[] path1 = {"key1", "key2"}; JSONObject jsonObject1 = new JSONObject("{\"key1\": {\"key2\": \"value\"}}"); Object result1 = formUtils.getObjectAtPath(path1, jsonObject1); - Assert.assertEquals("value", result1); + assertEquals("value", result1); String[] path2 = {"key1", "key3"}; JSONObject jsonObject2 = new JSONObject("{\"key1\": {\"key2\": \"value\"}}"); @@ -215,13 +218,13 @@ public void testGetObjectAtPath() throws Exception { String[] path3 = {"key1", "key4","key5"}; JSONObject jsonObject3 = new JSONObject("{\"key1\": {\"key4\": [{\"key5\": \"value\"}]}}"); Object result3 = formUtils.getObjectAtPath(path3, jsonObject3); - Assert.assertEquals("value", result3); + assertEquals("value", result3); String[] path4 = {"key1", "key4"}; JSONObject jsonObject4 = new JSONObject("{\"key1\": {\"key4\": []}}"); Object result4 = formUtils.getObjectAtPath(path4, jsonObject4); - Assert.assertEquals("[]", result4.toString()); + assertEquals("[]", result4.toString()); } } From fde35c61d7b9dff9b36370658f043840fab76fcc Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 16:00:17 +0500 Subject: [PATCH 07/14] unecessasry references to mock removed --- .../java/org/smartregister/util/UtilsTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java index c9a6a73dd..e603ed4d2 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java @@ -771,9 +771,9 @@ public void testTryParseLongShouldParseShoouldReturnDefaultValueOnException() { @Test public void testGetAssetFileInputStream() throws IOException { - Context mockContext = Mockito.mock(Context.class); - AssetManager mockAssetManager = Mockito.mock(AssetManager.class); - InputStream mockInputStream = Mockito.mock(InputStream.class); + Context mockContext = mock(Context.class); + AssetManager mockAssetManager = mock(AssetManager.class); + InputStream mockInputStream = mock(InputStream.class); Mockito.when(mockContext.getAssets()).thenReturn(mockAssetManager); Mockito.when(mockAssetManager.open(Mockito.eq("file_path"))).thenReturn(mockInputStream); @@ -783,18 +783,17 @@ public void testGetAssetFileInputStream() throws IOException { @Test public void testDeleteRoomDb() { - Context mockContext = Mockito.mock(Context.class); - ApplicationInfo mockApplicationInfo = Mockito.mock(ApplicationInfo.class); + Context mockContext = mock(Context.class); + ApplicationInfo mockApplicationInfo =mock(ApplicationInfo.class); - File mockDatabasesFolder = Mockito.mock(File.class); - File mockDb = Mockito.mock(File.class); + File mockDatabasesFolder = mock(File.class); mockApplicationInfo.dataDir = "testPath"; Mockito.when(mockContext.getApplicationInfo()).thenReturn(mockApplicationInfo); Mockito.when(mockDatabasesFolder.getAbsolutePath()).thenReturn("/data/data/com.example.app/databases"); boolean result = Utils.deleteRoomDb(mockContext, "databaseName"); - Assert.assertFalse(result); + assertFalse(result); } From c419c28daaae0dd4b52efd83d85d0a33828d8996 Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 16:10:17 +0500 Subject: [PATCH 08/14] unecessasry references to mock removed --- .../src/test/java/org/smartregister/util/UtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java index e603ed4d2..0bb34eb0a 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/UtilsTest.java @@ -778,7 +778,7 @@ public void testGetAssetFileInputStream() throws IOException { Mockito.when(mockAssetManager.open(Mockito.eq("file_path"))).thenReturn(mockInputStream); InputStream result = Utils.getAssetFileInputStream(mockContext, "file_path"); - Assert.assertEquals(mockInputStream, result); + assertEquals(mockInputStream, result); } @Test From 0cb004c6cf0c3fc59214b93e0a739f2d5a3fc5e2 Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 16:50:59 +0500 Subject: [PATCH 09/14] update test --- .../org/smartregister/util/FormUtilsTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index 946fa3add..381019f36 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -36,6 +36,7 @@ import java.util.HashMap; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; /** @@ -227,4 +228,24 @@ public void testGetObjectAtPath() throws Exception { assertEquals("[]", result4.toString()); } + @Test + public void testGetFieldsArrayForSubFormDefinition() throws Exception { + + JSONArray fieldsArray2 = new JSONArray("[{\"name\": \"field1\"}, {\"name\": \"field2\", \"source\": \"source2\"}]"); + String bindPath2 = "example"; + JSONObject fieldsDefinition2 = new JSONObject(); + fieldsDefinition2.put("fields", fieldsArray2); + fieldsDefinition2.put("bind_type", bindPath2); + JSONArray result2 = formUtils.getFieldsArrayForSubFormDefinition(fieldsDefinition2); + assertNotNull(result2); + assertEquals(2, result2.length()); + + JSONObject item1 = result2.getJSONObject(0); + assertEquals("field1", item1.getString("name")); + assertEquals(bindPath2 + ".field1", item1.getString("source")); + + JSONObject item2 = result2.getJSONObject(1); + assertEquals("field2", item2.getString("name")); + assertEquals(bindPath2 + ".source2", item2.getString("source")); + } } From ba006e90b2c2c50800bdcfd8208a9752707618d7 Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 19:03:54 +0500 Subject: [PATCH 10/14] update test --- .../service/DrishtiServiceTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java b/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java index 5d52eafa6..2668e82cc 100644 --- a/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java +++ b/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java @@ -1,5 +1,9 @@ package org.smartregister.service; +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; + import org.apache.commons.io.IOUtils; import org.ei.drishti.dto.Action; import org.ei.drishti.dto.AlertStatus; @@ -14,6 +18,7 @@ import org.smartregister.BaseUnitTest; import org.smartregister.domain.Response; import org.smartregister.domain.ResponseStatus; +import org.smartregister.sync.DrishtiSyncScheduler; import org.smartregister.util.ActionBuilder; import java.util.Arrays; @@ -100,4 +105,16 @@ public void shouldReturnFailureResponseWhenJsonIsMalformed() { Assert.assertTrue(actions.payload().isEmpty()); Assert.assertEquals(ResponseStatus.failure, actions.status()); } + + @Test + public void testStartOnlyIfConnectedToNetwork() { + Context context = Mockito.mock(Context.class); + ConnectivityManager connectivityManager = Mockito.mock(ConnectivityManager.class); + NetworkInfo networkInfo = Mockito.mock(NetworkInfo.class); + Mockito.when(networkInfo.isConnected()).thenReturn(true); + Mockito.when(context.getSystemService(Context.CONNECTIVITY_SERVICE)) + .thenReturn(connectivityManager); + Mockito.when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); + DrishtiSyncScheduler.startOnlyIfConnectedToNetwork(context); + } } From af0236b109f5b0e81d4b34a46be4b8ae31b096ea Mon Sep 17 00:00:00 2001 From: vend Date: Fri, 26 May 2023 20:31:15 +0500 Subject: [PATCH 11/14] update test --- .../org/smartregister/util/FormUtilsTest.java | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index 381019f36..fbf7a8fd4 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -10,6 +10,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentMatchers; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -27,6 +28,8 @@ import org.smartregister.repository.FormDataRepository; import org.smartregister.service.ANMService; import org.smartregister.util.mock.XmlSerializerMock; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import java.io.File; import java.io.FileInputStream; @@ -38,6 +41,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.smartregister.util.JsonFormUtils.generateRandomUUIDString; /** * Created by kaderchowdhury on 14/11/17. @@ -213,7 +219,7 @@ public void testGetObjectAtPath() throws Exception { String[] path2 = {"key1", "key3"}; JSONObject jsonObject2 = new JSONObject("{\"key1\": {\"key2\": \"value\"}}"); Object result2 = formUtils.getObjectAtPath(path2, jsonObject2); - Assert.assertNull(result2); + assertNull(result2); String[] path3 = {"key1", "key4","key5"}; @@ -248,4 +254,52 @@ public void testGetFieldsArrayForSubFormDefinition() throws Exception { assertEquals("field2", item2.getString("name")); assertEquals(bindPath2 + ".source2", item2.getString("source")); } + + @Test + public void testHasChildElements() { + Node element = Mockito.mock(Node.class); + NodeList children = Mockito.mock(NodeList.class); + Mockito.when(element.getChildNodes()).thenReturn(children); + Mockito.when(children.getLength()).thenReturn(0); + boolean result1 = formUtils.hasChildElements(element); + assertFalse(result1); + + + + Mockito.when(children.getLength()).thenReturn(1); + Mockito.when(children.item(ArgumentMatchers.anyInt())).thenReturn(element); + Mockito.when(element.getNodeType()).thenReturn(Node.TEXT_NODE); + boolean result3 = formUtils.hasChildElements(element); + assertFalse(result3); + } + + @Test + public void testGetValueForPath() throws Exception { + + + String[] path2 = {"key1", "key2"}; + JSONObject jsonObject2 = new JSONObject(); + String result2 = formUtils.getValueForPath(path2, jsonObject2); + assertNull(result2); + + JSONObject innerObject3 = new JSONObject("{\"content\": \"value\"}"); + JSONArray innerArray3 = new JSONArray("[\"value\"]"); + JSONObject jsonObject3 = new JSONObject(); + jsonObject3.put("key1", innerObject3); + jsonObject3.put("key2", innerArray3); + jsonObject3.put("key3", "value3"); + + String[] path3 = {"key1", "content"}; + String result3 = formUtils.getValueForPath(path3, jsonObject3); + assertEquals("value", result3); + + String[] path4 = {"key2"}; + String result4 = formUtils.getValueForPath(path4, jsonObject3); + assertEquals("value", result4); + + String[] path5 = {"key3"}; + String result5 = formUtils.getValueForPath(path5, jsonObject3); + assertEquals("value3", result5); + + } } From 113dcf3d5189fd98d0bef52b7b01b66deb8fb638 Mon Sep 17 00:00:00 2001 From: vend Date: Mon, 29 May 2023 18:45:40 +0500 Subject: [PATCH 12/14] updated test --- .../org/smartregister/util/FormUtilsTest.java | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index fbf7a8fd4..9882be74a 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -7,7 +7,6 @@ import org.json.JSONException; import org.json.JSONObject; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentMatchers; @@ -42,8 +41,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.smartregister.util.JsonFormUtils.generateRandomUUIDString; /** * Created by kaderchowdhury on 14/11/17. @@ -302,4 +299,44 @@ public void testGetValueForPath() throws Exception { assertEquals("value3", result5); } + + @Test + public void testGetSubForms() throws Exception { + JSONArray subFormDataArray = new JSONArray(); + String entity_id = "123"; + JSONObject subFormDefinition = new JSONObject(); + JSONObject overrides = new JSONObject(); + subFormDefinition.put("fields",new JSONArray()); + subFormDefinition.put("bind_type","/bind/type"); + + JSONArray result = ReflectionHelpers.callInstanceMethod(formUtils, "getSubForms" + , ReflectionHelpers.ClassParameter.from(JSONArray.class, subFormDataArray) + , ReflectionHelpers.ClassParameter.from(String.class, entity_id) + , ReflectionHelpers.ClassParameter.from(JSONObject.class, subFormDefinition) + , ReflectionHelpers.ClassParameter.from(JSONObject.class, overrides)); + + assertNotNull(result); + assertEquals(1, result.length()); + } + + @Test + public void testRetrieveSubformDefinitionForBindPath() throws Exception { + // Create sample input data + JSONArray subForms = new JSONArray(); + JSONObject subForm1 = new JSONObject(); + subForm1.put("default_bind_path", "path/to/SubForm1"); + JSONObject subForm2 = new JSONObject(); + subForm2.put("default_bind_path", "path/to/SubForm2"); + subForms.put(subForm1); + subForms.put(subForm2); + String fieldName = "SubForm1"; + + + JSONObject result = ReflectionHelpers.callInstanceMethod(formUtils, "retriveSubformDefinitionForBindPath" + , ReflectionHelpers.ClassParameter.from(JSONArray.class, subForms) + , ReflectionHelpers.ClassParameter.from(String.class, fieldName)); + + assertNotNull(result); + assertEquals("path/to/SubForm1", result.getString("default_bind_path")); + } } From 949c667a47dd282840ee39df810bd105054e0cd6 Mon Sep 17 00:00:00 2001 From: vend Date: Mon, 29 May 2023 19:04:44 +0500 Subject: [PATCH 13/14] updated test --- .../org/smartregister/service/DrishtiServiceTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java b/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java index 2668e82cc..c5380f2e5 100644 --- a/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java +++ b/opensrp-core/src/test/java/org/smartregister/service/DrishtiServiceTest.java @@ -14,7 +14,6 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; import org.smartregister.BaseUnitTest; import org.smartregister.domain.Response; import org.smartregister.domain.ResponseStatus; @@ -24,6 +23,8 @@ import java.util.Arrays; import java.util.List; +import static org.junit.Assert.assertTrue; + public class DrishtiServiceTest extends BaseUnitTest { @Mock private HTTPAgent httpAgent; @@ -69,7 +70,7 @@ public void shouldFetchNoAlertActionsWhenJsonIsForEmptyList() throws Exception { Response> actions = drishtiService.fetchNewActions("anm1", "0"); - Assert.assertTrue(actions.payload().isEmpty()); + assertTrue(actions.payload().isEmpty()); } @Test @@ -78,7 +79,7 @@ public void shouldFetchNoAlertActionsWhenHTTPCallFails() throws Exception { Response> actions = drishtiService.fetchNewActions("anm1", "0"); - Assert.assertTrue(actions.payload().isEmpty()); + assertTrue(actions.payload().isEmpty()); Assert.assertEquals(ResponseStatus.failure, actions.status()); } @@ -102,7 +103,7 @@ public void shouldReturnFailureResponseWhenJsonIsMalformed() { Response> actions = drishtiService.fetchNewActions("anm1", "0"); - Assert.assertTrue(actions.payload().isEmpty()); + assertTrue(actions.payload().isEmpty()); Assert.assertEquals(ResponseStatus.failure, actions.status()); } @@ -116,5 +117,6 @@ public void testStartOnlyIfConnectedToNetwork() { .thenReturn(connectivityManager); Mockito.when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); DrishtiSyncScheduler.startOnlyIfConnectedToNetwork(context); + assertTrue(networkInfo.isConnected()); } } From 33437e8bf789b5ecb7bd6fd9924eb137efbca0a9 Mon Sep 17 00:00:00 2001 From: vend Date: Mon, 29 May 2023 19:31:31 +0500 Subject: [PATCH 14/14] updated test --- .../org/smartregister/util/FormUtilsTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java index 9882be74a..c5a1d4560 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -36,11 +36,13 @@ import java.io.InputStream; import java.net.URL; import java.util.HashMap; +import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; /** * Created by kaderchowdhury on 14/11/17. @@ -339,4 +341,27 @@ public void testRetrieveSubformDefinitionForBindPath() throws Exception { assertNotNull(result); assertEquals("path/to/SubForm1", result.getString("default_bind_path")); } + + @Test + public void testGetSubFormNames() throws Exception { + // Create sample input data + JSONObject formDefinition = new JSONObject(); + JSONObject form = new JSONObject(); + JSONArray subForms = new JSONArray(); + JSONObject subForm1 = new JSONObject(); + subForm1.put("default_bind_path", "path/to/SubForm1"); + JSONObject subForm2 = new JSONObject(); + subForm2.put("default_bind_path", "path/to/SubForm2"); + subForms.put(subForm1); + subForms.put(subForm2); + form.put("sub_forms", subForms); + formDefinition.put("form", form); + + List result = ReflectionHelpers.callInstanceMethod(formUtils, "getSubFormNames" + , ReflectionHelpers.ClassParameter.from(JSONObject.class, formDefinition)); + assertNotNull(result); + assertEquals(2, result.size()); + assertTrue(result.contains("SubForm1")); + assertTrue(result.contains("SubForm2")); + } }