From 7953b0c286d07a2200c69f7d69538e683873a463 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Fri, 3 Mar 2023 16:38:47 +0500 Subject: [PATCH] Increase code coverage --- .../org/smartregister/util/FormUtilsTest.java | 33 ++++++++ .../util/OpenSRPImageLoaderTest.java | 82 +++++++++++++++++++ 2 files changed, 115 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..4f6d5e97f 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/FormUtilsTest.java @@ -1,5 +1,7 @@ package org.smartregister.util; +import static org.mockito.ArgumentMatchers.eq; + import android.content.res.AssetManager; import android.util.Xml; @@ -16,7 +18,9 @@ import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + import androidx.test.core.app.ApplicationProvider; + import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; @@ -27,6 +31,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; @@ -198,4 +204,31 @@ public void getJsonFieldFromArrayShouldReturnObjectWithCorrectNameProperty() thr Assert.assertEquals("Doe", resultJson.getString("value")); } + @Test + public void testGetSubForShouldReturnCOrrectValuem() throws JSONException { + String form = "{\"form_data_definition_version\":\"2\",\"form\":{\"default_bind_path\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp\",\"bind_type\":\"mcaremother\",\"ec_bind_type\":\"ec_mcaremother\",\"fields\":[{\"name\":\"id\",\"shouldLoadValue\":true},{\"name\":\"bnf_current_formStatus\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/bnf_current_formStatus\"},{\"name\":\"relationalid\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/relationalid\",\"shouldLoadValue\":true},{\"name\":\"changes\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/changes\"},{\"name\":\"GOBHHID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_gobhhid\",\"shouldLoadValue\":true},{\"name\":\"JiVitAHHID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_jivhhid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMBID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_wom_bid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMNID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_wom_nid\",\"shouldLoadValue\":true},{\"name\":\"FWWOMFNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_first_name\",\"shouldLoadValue\":true},{\"name\":\"FWHUSNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_husname\",\"shouldLoadValue\":true},{\"name\":\"FWPSRLMP\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_psrlmp\",\"shouldLoadValue\":true},{\"name\":\"existing_location\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/existing_location\"},{\"name\":\"today\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/today\"},{\"name\":\"start\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/start\"},{\"name\":\"end\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/end\"},{\"name\":\"FWBNFDATE\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWBNFDATE\"},{\"name\":\"FWCONFIRMATION\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWCONFIRMATION\"},{\"name\":\"FWGESTATIONALAGE\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWGESTATIONALAGE\"},{\"name\":\"FWEDD\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWEDD\"},{\"name\":\"FWBNFSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWBNFSTS\"},{\"name\":\"FWDISPLAYTEXT1\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/FWDISPLAYTEXT1\"},{\"name\":\"Is_PNC\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/Is_PNC\"},{\"name\":\"user_type\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/user_type\"},{\"name\":\"external_user_ID\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/external_user_ID\"},{\"name\":\"FWBNFWOMVITSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFWOMVITSTS\"},{\"name\":\"FWBNFDTOO\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFDTOO\"},{\"name\":\"FWBNFLB\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFLB\"},{\"name\":\"FWBNFSMSRSN\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNFSMSRSN\"}],\"sub_forms\":[{\"name\":\"child_registration\",\"bind_type\":\"mcarechild\",\"ec_bind_type\":\"ec_mcarechild\",\"default_bind_path\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS\",\"fields\":[{\"name\":\"id\",\"shouldLoadValue\":true},{\"name\":\"relationalid\",\"shouldLoadValue\":true},{\"name\":\"FWBNFGEN\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFGEN\"},{\"name\":\"FWBNFCHLDVITSTS\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFCHLDVITSTS\"},{\"name\":\"FWBNFNAMECHECK\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFNAMECHECK\"},{\"name\":\"FWBNFNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFNAME\"},{\"name\":\"FWBNFCHILDNAME\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFCHILDNAME\"},{\"name\":\"FWBNFDOB\",\"bind\":\"/model/instance/BirthNotificationPregnancyStatusFollowUp/outcome_occured/FWBNCHLDVITSTS/FWBNFDOB\"},{\"name\":\"mother_entity_id\",\"source\":\"mcaremother.id\",\"shouldLoadValue\":true}]}]}}"; + String entity = "{\n" + + " \"name\": \"relationalid\",\n" + + " \"relationalid\": \"123-123\",\n" + + " \"bind\": \"/model/instance/BirthNotificationPregnancyStatusFollowUp/relationalid\",\n" + + " \"shouldLoadValue\": true\n" + + " }"; + String NodeNameValue = ReflectionHelpers.callInstanceMethod(formUtils, "retrieveValueForNodeName" + , ReflectionHelpers.ClassParameter.from(String.class, "relationalid") + , ReflectionHelpers.ClassParameter.from(JSONObject.class, new JSONObject(entity)) + , ReflectionHelpers.ClassParameter.from(JSONObject.class, new JSONObject(form))); + Assert.assertEquals("123-123", NodeNameValue); + } + + @Test + public void testHasChildElementsShouldReturnTrueWhenNodeHasElement() { + Node node = Mockito.mock(Node.class); + Node childNode = Mockito.mock(Node.class); + NodeList nodeList = Mockito.mock(NodeList.class); + Mockito.doReturn(nodeList).when(node).getChildNodes(); + Mockito.doReturn(1).when(nodeList).getLength(); + Mockito.doReturn(childNode).when(nodeList).item(eq(0)); + Mockito.doReturn(Node.ELEMENT_NODE).when(childNode).getNodeType(); + Assert.assertTrue(FormUtils.hasChildElements(node)); + } } diff --git a/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java b/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java index 3972c6b1f..eed851991 100644 --- a/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java +++ b/opensrp-core/src/test/java/org/smartregister/util/OpenSRPImageLoaderTest.java @@ -1,22 +1,35 @@ package org.smartregister.util; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import android.content.Intent; +import android.graphics.Bitmap; import androidx.test.core.app.ApplicationProvider; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import org.mockito.Mockito; import org.robolectric.Robolectric; import org.robolectric.android.controller.ActivityController; import org.robolectric.util.ReflectionHelpers; import org.smartregister.BaseUnitTest; import org.smartregister.Context; import org.smartregister.CoreLibrary; +import org.smartregister.domain.ProfileImage; +import org.smartregister.domain.SyncStatus; +import org.smartregister.repository.ImageRepository; import org.smartregister.util.mock.OpenSRPImageLoaderTestActivity; +import org.smartregister.view.activity.DrishtiApplication; + +import java.io.File; +import java.io.IOException; /** * Created by kaderchowdhury on 14/11/17. @@ -33,6 +46,8 @@ public class OpenSRPImageLoaderTest extends BaseUnitTest { private ActivityController controller; + private File testFile; + @Before public void setUp() throws Exception { Intent intent = new Intent(ApplicationProvider.getApplicationContext(), OpenSRPImageLoaderTestActivity.class); @@ -59,4 +74,71 @@ public void assertFragmentActivityConstructorInitializationNotNull() throws Exce OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(activity, -1); Assert.assertNotNull(openSRPImageLoader); } + + @Test + public void testGetCompressFormatShouldReturnProperCompressFormat() { + String filePath = "/User/images/file.jpg"; + Bitmap.CompressFormat format = ReflectionHelpers.callStaticMethod(OpenSRPImageLoader.class, "getCompressFormat", + ReflectionHelpers.ClassParameter.from(String.class, filePath)); + Assert.assertEquals(Bitmap.CompressFormat.JPEG, format); + + filePath = "/User/images/file.png"; + format = ReflectionHelpers.callStaticMethod(OpenSRPImageLoader.class, "getCompressFormat", + ReflectionHelpers.ClassParameter.from(String.class, filePath)); + Assert.assertEquals(Bitmap.CompressFormat.PNG, format); + + } + + @Test + public void testMoveSyncedImageAndSaveProfilePicReturnsSuccessful() throws IOException { + String path = DrishtiApplication.getAppDir() + File.separator + "123-123.JPEG"; + testFile = new File(path); + testFile.createNewFile(); + + CoreLibrary library = Mockito.mock(CoreLibrary.class); + ReflectionHelpers.setField(CoreLibrary.getInstance(), "instance", library); + Context context = Mockito.mock(Context.class); + doReturn(context).when(library).context(); + ImageRepository imageRepository = Mockito.mock(ImageRepository.class); + doNothing().when(imageRepository).add(any()); + doReturn(imageRepository).when(context).imageRepository(); + + + boolean success = OpenSRPImageLoader.moveSyncedImageAndSaveProfilePic(SyncStatus.SYNCED.value(), "123-123", testFile); + Assert.assertTrue(success); + } + + @Test + public void testSaveStaticImageToDisk() throws IOException { + Bitmap bitmap = Mockito.mock(Bitmap.class); + doReturn(true).when(bitmap).compress(any(), anyInt(), any()); + + CoreLibrary library = Mockito.mock(CoreLibrary.class); + ReflectionHelpers.setField(CoreLibrary.getInstance(), "instance", library); + Context context = Mockito.mock(Context.class); + doReturn(context).when(library).context(); + ImageRepository imageRepository = Mockito.mock(ImageRepository.class); + doNothing().when(imageRepository).add(any()); + doReturn(imageRepository).when(context).imageRepository(); + + OpenSRPImageLoader.saveStaticImageToDisk( "123-123", bitmap); + Mockito.verify(imageRepository).add(any(ProfileImage.class)); + } + + @Test + public void testSetMaxImageSizeShouldSetCorrectImageSize() { + OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(activity.getInstance()); + openSRPImageLoader.setMaxImageSize(200); + int width = ReflectionHelpers.getField(openSRPImageLoader, "mMaxImageWidth"); + int height = ReflectionHelpers.getField(openSRPImageLoader, "mMaxImageHeight"); + Assert.assertEquals(200, width); + Assert.assertEquals(200, height); + } + + @After + public void destroy() { + ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "imageRepository", null); + if (testFile != null && testFile.exists()) + testFile.delete(); + } }