Skip to content

Commit

Permalink
Merge pull request #207 from OpenSRP/ndegwa-tt-11-24
Browse files Browse the repository at this point in the history
BaseChildRegistrationDataFragment Unit tests
  • Loading branch information
ndegwamartin authored Nov 27, 2020
2 parents 22bee3f + 70d4d5c commit c9e011f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://travis-ci.org/OpenSRP/opensrp-client-child.svg?branch=master)](https://travis-ci.org/OpenSRP/opensrp-client-child) [![Coverage Status](https://coveralls.io/repos/github/OpenSRP/opensrp-client-child/badge.svg?branch=master)](https://coveralls.io/github/OpenSRP/opensrp-client-child?branch=master)
![Build status](https://github.com/OpenSRP/opensrp-client-reveal/workflows/Android%20CI%20with%20Gradle/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/OpenSRP/opensrp-client-child/badge.svg?branch=master)](https://coveralls.io/github/OpenSRP/opensrp-client-child?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b8b5e3c6e9284bffb993d07b235a8691)](https://www.codacy.com/app/OpenSRP/opensrp-client-child?utm_source=github.com&utm_medium=referral&utm_content=OpenSRP/opensrp-client-child&utm_campaign=Badge_Grade)

# opensrp-client-child
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void resetAdapterData(Map<String, String> detailsMap) {
}

//TODO Temporary fix for spinner setting value as hint when nothing is selected
if (field.getType().equalsIgnoreCase(JsonFormConstants.SPINNER) && field.getHint().equalsIgnoreCase(value)) {
if (JsonFormConstants.SPINNER.equalsIgnoreCase(field.getType()) && value != null && value.equalsIgnoreCase(field.getHint())) {
value = null;
}

Expand Down Expand Up @@ -220,16 +220,16 @@ private void updateOtherField(String actualField, String value) {
private String getFieldValue(Map<String, String> detailsMap, Field field, String key) {
String value;
value = detailsMap.get(field.getKey().toLowerCase(Locale.getDefault()));
value = !TextUtils.isEmpty(value) ? value : detailsMap.get(getPrefix(field.getEntityId()) + key.toLowerCase(Locale.getDefault()));
value = !TextUtils.isEmpty(value) ? value : detailsMap.get(getPrefix(field.getEntityId()) + cleanOpenMRSEntityId(field.getOpenmrsEntityId().toLowerCase(Locale.getDefault())));
value = !TextUtils.isEmpty(value) ? value : detailsMap.get(key.toLowerCase(Locale.getDefault()));
value = !StringUtils.isBlank(value) ? value : detailsMap.get(getPrefix(field.getEntityId()) + key.toLowerCase(Locale.getDefault()));
value = !StringUtils.isBlank(value) ? value : detailsMap.get(getPrefix(field.getEntityId()) + cleanOpenMRSEntityId(field.getOpenmrsEntityId().toLowerCase(Locale.getDefault())));
value = !StringUtils.isBlank(value) ? value : detailsMap.get(key.toLowerCase(Locale.getDefault()));
return value;
}

public String getPrefix(String entityId) {
if (!TextUtils.isEmpty(entityId) && entityId.equalsIgnoreCase(Constants.KEY.MOTHER))
if (!StringUtils.isBlank(entityId) && entityId.equalsIgnoreCase(Constants.KEY.MOTHER))
return "mother_";
else if (!TextUtils.isEmpty(entityId) && entityId.equalsIgnoreCase(Constants.KEY.FATHER))
else if (!StringUtils.isBlank(entityId) && entityId.equalsIgnoreCase(Constants.KEY.FATHER))
return "father_";
else return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Utils.class})
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*"})
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*", "javax.management.*", "org.xmlpull.v1.*"})
public abstract class BasePowerMockUnitTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
import android.view.View;
import android.view.ViewGroup;

import com.google.common.collect.ImmutableMap;
import com.vijay.jsonwizard.constants.JsonFormConstants;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
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;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.child.BasePowerMockUnitTest;
import org.smartregister.child.ChildLibrary;
import org.smartregister.child.R;
import org.smartregister.child.activity.BaseChildDetailTabbedActivity;
Expand Down Expand Up @@ -54,9 +54,8 @@
/**
* Created by ndegwamartin on 04/08/2020.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({CoreLibrary.class, ChildLibrary.class})
public class BaseChildRegistrationDataFragmentTest {
public class BaseChildRegistrationDataFragmentTest extends BasePowerMockUnitTest {

private BaseChildRegistrationDataFragment baseChildRegistrationDataFragment;

Expand Down Expand Up @@ -118,9 +117,11 @@ public void setUp() {
Mockito.doReturn(step).when(form).getStep1();
fields = generateFormFieldsForTest();
List<String> unformattedNumberFields = new ArrayList<>();
unformattedNumberFields.add("key2");

Whitebox.setInternalState(baseChildRegistrationDataFragment, "fields", fields);
Whitebox.setInternalState(baseChildRegistrationDataFragment, "mAdapter", adapter);
Assert.assertNotNull(baseChildRegistrationDataFragment.getmAdapter());
Whitebox.setInternalState(baseChildRegistrationDataFragment, "unformattedNumberFields", unformattedNumberFields);
Mockito.doReturn(fields).when(step).getFields();

Expand All @@ -130,6 +131,18 @@ public void setUp() {
PowerMockito.doReturn(appProperties).when(childLibrary).getProperties();
}

@Test
public void testGetPrefix() {
String prefix = baseChildRegistrationDataFragment.getPrefix(Constants.KEY.MOTHER);
Assert.assertEquals("mother_", prefix);

prefix = baseChildRegistrationDataFragment.getPrefix(Constants.KEY.FATHER);
Assert.assertEquals("father_", prefix);

prefix = baseChildRegistrationDataFragment.getPrefix("other-random-value");
Assert.assertEquals("", prefix);
}

@Test
public void testAddUnformattedKeys() {
Assert.assertEquals(baseChildRegistrationDataFragment.addUnFormattedNumberFields(ArgumentMatchers.anyString(), ArgumentMatchers.anyString()).size(), 2);
Expand Down Expand Up @@ -314,6 +327,33 @@ public void testCleanValueRetrievesValueFromKeysListForOtherSpinner() {
Assert.assertEquals("Male", value);
}

@Test
public void testCleanValueRetrievesValueForSpinnerUsingNewMLSApproach() {

ChildAppProperties appProperties = new ChildAppProperties();
appProperties.setProperty(ChildAppProperties.KEY.MULTI_LANGUAGE_SUPPORT, "true");

PowerMockito.doReturn(appProperties).when(childLibrary).getProperties();

String rawValue = "Fr";

Field f = new Field();
f.setHint("Select Language");

List<Map<String, String>> options = new ArrayList<>();
options.add(ImmutableMap.of(JsonFormConstants.KEY, "En", JsonFormConstants.TEXT, "English"));
options.add(ImmutableMap.of(JsonFormConstants.KEY, "Fr", JsonFormConstants.TEXT, "French"));
Whitebox.setInternalState(f, "options", options);

Whitebox.setInternalState(f, "key", "language_spinner");
Whitebox.setInternalState(f, "type", JsonFormConstants.SPINNER);

String value = baseChildRegistrationDataFragment.cleanValue(f, rawValue);
Mockito.verify(baseChildRegistrationDataFragment).formatRenderValue(ArgumentMatchers.any(Field.class), ArgumentMatchers.any(String.class));
Assert.assertEquals("French", value);
}


@Test
public void testCleanValueRetrievesDatabaseValueForSpinnerSubtypeLocation() {
String rawValue = "";
Expand Down Expand Up @@ -382,22 +422,64 @@ public void testResetAdapterDataPopulatesAdapterWithMap() {
Assert.assertEquals(detailsMap.size(), adapterCaptor.getAllValues().get(0).getItemCount());
}

@Test
public void testResetAdapterDataPopulatesAdapterWithMapForFildsWithAliasNameOnQuery() {
List<Field> fieldsList = new ArrayList<>();
List<Field> formFields = generateFormFieldsForTest();
fieldsList.add(formFields.get(0));
fieldsList.add(formFields.get(1));
Map<String, String> fieldNameAliasMap = new HashMap<>();
fieldNameAliasMap.put("key1", "key-1-alias");
fieldNameAliasMap.put("key2", "key-2-alias");
Whitebox.setInternalState(baseChildRegistrationDataFragment, "fields", fieldsList);
Whitebox.setInternalState(baseChildRegistrationDataFragment, "stringResourceIds", baseChildRegistrationDataFragment.getDataRowLabelResourceIds());
Whitebox.setInternalState(baseChildRegistrationDataFragment, "fieldNameAliasMap", fieldNameAliasMap);

Map<String, String> detailsMap = new HashMap<>();
detailsMap.put("key-1-alias", "Value 1");
detailsMap.put("key-2-alias", "Value 2");

baseChildRegistrationDataFragment.resetAdapterData(detailsMap);

ArgumentCaptor<ChildRegistrationDataAdapter> adapterCaptor = ArgumentCaptor.forClass(ChildRegistrationDataAdapter.class);

Mockito.verify(baseChildRegistrationDataFragment, Mockito.atLeast(detailsMap.size())).getResourceLabel(ArgumentMatchers.any(String.class));
Mockito.verify(baseChildRegistrationDataFragment).setmAdapter(adapterCaptor.capture());
Assert.assertEquals(detailsMap.size(), adapterCaptor.getAllValues().get(0).getItemCount());
}

@Test
public void testFormatRenderValue() {
Field field = new Field();
field.setHint("OpenSRP ID");
Whitebox.setInternalState(field, "key", "key1");
Whitebox.setInternalState(field, "renderType", "id");
Whitebox.setInternalState(field, "type", JsonFormConstants.EDIT_TEXT);

String formatted = baseChildRegistrationDataFragment.formatRenderValue(field, "839340034921");
Assert.assertNotNull(formatted);
Assert.assertEquals("8393-4003-4921", formatted);
}

private List<Field> generateFormFieldsForTest() {
fields = new ArrayList<>();
Field f = new Field();
f.setHint("Hint A");
Whitebox.setInternalState(f, "key", "key1");
Whitebox.setInternalState(f, "type", JsonFormConstants.EDIT_TEXT);
fields.add(f);

f = new Field();
f.setHint("Hint B");
Whitebox.setInternalState(f, "key", "key2");
Whitebox.setInternalState(f, "type", JsonFormConstants.EDIT_TEXT);
fields.add(f);

f = new Field();
f.setHint("OpenSRP ID");
Whitebox.setInternalState(f, "key", "key3");
Whitebox.setInternalState(f, "renderType", "id");
Whitebox.setInternalState(f, "type", JsonFormConstants.EDIT_TEXT);
fields.add(f);

f = new Field();
Expand Down

0 comments on commit c9e011f

Please sign in to comment.