Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-vd committed Mar 3, 2023
1 parent 25d2d6c commit debece5
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.smartregister.util;

import static org.mockito.ArgumentMatchers.eq;

import android.content.res.AssetManager;
import android.util.Xml;

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
package org.smartregister.util;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;

import android.app.Service;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

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.
Expand All @@ -33,6 +50,8 @@ public class OpenSRPImageLoaderTest extends BaseUnitTest {

private ActivityController<OpenSRPImageLoaderTestActivity> controller;

private File testFile;

@Before
public void setUp() throws Exception {
Intent intent = new Intent(ApplicationProvider.getApplicationContext(), OpenSRPImageLoaderTestActivity.class);
Expand All @@ -59,4 +78,84 @@ 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);
}

@Test
public void testConstructorWithServiceInitializedCorrectly() {
Service service = Mockito.mock(Service.class);
android.content.Context context = Mockito.mock(android.content.Context.class);
Resources resources = Mockito.mock(Resources.class);
doReturn(context).when(service).getApplicationContext();
doReturn(resources).when(service).getResources();
doReturn(Mockito.mock(Drawable.class)).when(resources).getDrawable(eq(0));
OpenSRPImageLoader openSRPImageLoader = new OpenSRPImageLoader(service, 0);
Assert.assertNotNull(ReflectionHelpers.getField(openSRPImageLoader, "contextWeakReference"));
Assert.assertNotNull(ReflectionHelpers.getField(openSRPImageLoader, "mPlaceHolderDrawables"));
}

@After
public void destroy() {
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "imageRepository", null);
if (testFile != null && testFile.exists())
testFile.delete();
}
}

0 comments on commit debece5

Please sign in to comment.