From 6d7cf6e945a2294335b62738045c30ea422ceff8 Mon Sep 17 00:00:00 2001 From: Ephraim Kigamba Date: Wed, 1 Jul 2020 18:44:44 +0300 Subject: [PATCH] Fix tests --- .../vijay/jsonwizard/NativeFormLibrary.java | 20 ++- ...FormConfigurationJsonFormActivityTest.java | 40 +++-- .../utils/FormRollbackDialogUtilTest.java | 158 +++--------------- .../vijay/jsonwizard/utils/FormUtilsTest.java | 41 +++-- .../jsonwizard/utils/TestClientForm.java | 111 ++++++++++++ 5 files changed, 203 insertions(+), 167 deletions(-) create mode 100644 android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/TestClientForm.java diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/NativeFormLibrary.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/NativeFormLibrary.java index ac87f02b9..298edb5cb 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/NativeFormLibrary.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/NativeFormLibrary.java @@ -9,16 +9,28 @@ */ public class NativeFormLibrary { + private ClientFormContract.Dao clientFormDao; + private static NativeFormLibrary instance; + @NonNull - public static final NativeFormLibrary getInstance() { - return null; + public static NativeFormLibrary getInstance() { + if (instance == null) { + instance = new NativeFormLibrary(); + } + + return instance; } +/* public static final boolean init() { return false; - } + }*/ public ClientFormContract.Dao getClientFormDao() { - return null; + return clientFormDao; + } + + public void setClientFormDao(@NonNull ClientFormContract.Dao clientFormDao) { + this.clientFormDao = clientFormDao; } } diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/activities/FormConfigurationJsonFormActivityTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/activities/FormConfigurationJsonFormActivityTest.java index d99388848..a60b837c9 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/activities/FormConfigurationJsonFormActivityTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/activities/FormConfigurationJsonFormActivityTest.java @@ -6,6 +6,7 @@ import android.support.v7.app.AlertDialog; import android.view.Window; +import com.vijay.jsonwizard.NativeFormLibrary; import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.interfaces.OnFormFetchedCallback; @@ -23,6 +24,7 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.shadows.ShadowDialog; import org.robolectric.util.ReflectionHelpers; +import org.smartregister.client.utils.contract.ClientFormContract; import java.io.BufferedReader; import java.io.StringReader; @@ -36,7 +38,6 @@ public class FormConfigurationJsonFormActivityTest extends BaseActivityTest { @Before public void setUp() throws Exception { - //formConfigurationJsonFormActivity = new FormConfigurationJsonFormActivity(); formConfigurationJsonFormActivity = Robolectric.buildActivity(FormConfigurationJsonFormActivity.class) .get(); } @@ -49,11 +50,20 @@ public void getRulesShouldReturnCallFormUtils() throws Exception { ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application); String rulesFileIdentifier = "registration_calculation.yml"; - Mockito.doReturn(new BufferedReader(new StringReader(""))).when(formUtils).getRulesFromRepository(Mockito.eq(rulesFileIdentifier)); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); + + NativeFormLibrary.getInstance().setClientFormDao(clientFormRepository); + + Mockito.doReturn(new BufferedReader(new StringReader(""))).when(formUtils).getRulesFromRepository( + Mockito.eq(RuntimeEnvironment.application), + Mockito.eq(clientFormRepository), + rulesFileIdentifier); formConfigurationJsonFormActivity.getRules(RuntimeEnvironment.application, rulesFileIdentifier); - Mockito.verify(formUtils).getRulesFromRepository(Mockito.eq(rulesFileIdentifier)); + Mockito.verify(formUtils).getRulesFromRepository( + Mockito.eq(RuntimeEnvironment.application), + Mockito.eq(clientFormRepository),Mockito.eq(rulesFileIdentifier)); } @Test @@ -65,11 +75,12 @@ public void getSubFormShouldCallFormUtils() throws Exception { String subFormIdentifier = "tuberculosis_test"; JSONObject jsonObject = new JSONObject(); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); - Mockito.doReturn(jsonObject).when(formUtils).getSubFormJsonFromRepository(subFormIdentifier, null, RuntimeEnvironment.application, false); + Mockito.doReturn(jsonObject).when(formUtils).getSubFormJsonFromRepository(RuntimeEnvironment.application, clientFormRepository, subFormIdentifier, null, false); formConfigurationJsonFormActivity.getSubForm(subFormIdentifier, null, RuntimeEnvironment.application, false); - Mockito.verify(formUtils).getSubFormJsonFromRepository(subFormIdentifier, null, RuntimeEnvironment.application, false); + Mockito.verify(formUtils).getSubFormJsonFromRepository(RuntimeEnvironment.application, clientFormRepository, subFormIdentifier, null, false); } @Test @@ -77,8 +88,8 @@ public void getSubFormShouldCallHandleFormErrorWhenFormReturnedIsCorrupted() thr FormUtils formUtils = Mockito.mock(FormUtils.class); FormConfigurationJsonFormActivity spiedActivity = Mockito.spy(formConfigurationJsonFormActivity); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); ReflectionHelpers.setStaticField(FormUtils.class, "instance", formUtils); - ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application); String subFormIdentifier = "tuberculosis_test.json"; Mockito.doAnswer(new Answer() { @@ -86,7 +97,7 @@ public void getSubFormShouldCallHandleFormErrorWhenFormReturnedIsCorrupted() thr public Object answer(InvocationOnMock invocation) throws Throwable { throw new JSONException("Test exception"); } - }).when(formUtils).getSubFormJsonFromRepository(subFormIdentifier, null, RuntimeEnvironment.application, false); + }).when(formUtils).getSubFormJsonFromRepository(RuntimeEnvironment.application, clientFormRepository, subFormIdentifier, null, false); Assert.assertNull(spiedActivity.getSubForm(subFormIdentifier, null, RuntimeEnvironment.application, false)); @@ -99,6 +110,7 @@ public void handleFormErrorShouldCallFormUtilsHandleError() { FormUtils formUtils = Mockito.mock(FormUtils.class); //formConfigurationJsonFormActivity.contex FormConfigurationJsonFormActivity spiedActivity = Mockito.spy(formConfigurationJsonFormActivity); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); ReflectionHelpers.setStaticField(FormUtils.class, "instance", formUtils); ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application); @@ -109,10 +121,10 @@ public Object answer(InvocationOnMock invocation) throws Throwable { onFormFetchedCallback.onFormFetched(""); return null; } - }).when(formUtils).handleJsonFormOrRulesError(Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class)); + }).when(formUtils).handleJsonFormOrRulesError(RuntimeEnvironment.application, clientFormRepository, Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class)); spiedActivity.handleFormError(false, formIdentifier); - Mockito.verify(formUtils).handleJsonFormOrRulesError(Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class)); + Mockito.verify(formUtils).handleJsonFormOrRulesError(RuntimeEnvironment.application, clientFormRepository, Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class)); Mockito.verify(spiedActivity).finish(); } @@ -130,7 +142,7 @@ public void showFormVersionUpdateDialogShouldCreateAlertDialogWithTitleAndMessag Mockito.doReturn(jsonObject).when(spiedActivity).getmJSONObject(); - spiedActivity.showFormVersionUpdateDialog(title, message); + spiedActivity.showFormVersionUpdateDialog(jsonObject, title, message); AlertDialog alertDialog = (AlertDialog) ShadowDialog.getLatestDialog(); Object alertDialogController = ReflectionHelpers.getField(alertDialog, "mAlert"); @@ -154,7 +166,7 @@ public void formUpdateAlertDialogShouldCallNegateIsNewClientForm() throws JSONEx Mockito.doReturn(jsonObject).when(spiedActivity).getmJSONObject(); - spiedActivity.showFormVersionUpdateDialog(title, message); + spiedActivity.showFormVersionUpdateDialog(jsonObject, title, message); AlertDialog alertDialog = (AlertDialog) ShadowDialog.getLatestDialog(); alertDialog.getButton(Dialog.BUTTON_POSITIVE).callOnClick(); @@ -173,12 +185,12 @@ public void onCreateShouldCallShowFormVersionUpdateDialog() throws JSONException spiedActivity.setTheme(R.style.Theme_AppCompat_Light_Dialog_Alert); Mockito.doReturn(jsonObject).when(spiedActivity).getmJSONObject(); - Mockito.doNothing().when(spiedActivity).init(Mockito.any()); - Mockito.doNothing().when(spiedActivity).showFormVersionUpdateDialog(getString(R.string.form_update_title), getString(R.string.form_update_message)); + Mockito.doNothing().when(spiedActivity).init(Mockito.anyString()); + Mockito.doNothing().when(spiedActivity).showFormVersionUpdateDialog(jsonObject, getString(R.string.form_update_title), getString(R.string.form_update_message)); spiedActivity.onCreate(new Bundle()); - Mockito.verify(spiedActivity).showFormVersionUpdateDialog(getString(R.string.form_update_title), getString(R.string.form_update_message)); + Mockito.verify(spiedActivity).showFormVersionUpdateDialog(jsonObject, getString(R.string.form_update_title), getString(R.string.form_update_message)); } protected String getString(int stringId) { diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormRollbackDialogUtilTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormRollbackDialogUtilTest.java index e9563da8e..06e423ccc 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormRollbackDialogUtilTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormRollbackDialogUtilTest.java @@ -2,6 +2,7 @@ import com.vijay.jsonwizard.BaseTest; +import com.vijay.jsonwizard.R; import com.vijay.jsonwizard.constants.JsonFormConstants; import com.vijay.jsonwizard.interfaces.RollbackDialogCallback; @@ -9,13 +10,12 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.robolectric.RuntimeEnvironment; -import org.robolectric.util.ReflectionHelpers; -import org.smartregister.client.utils.contract.ClientFormDao; +import org.smartregister.client.utils.contract.ClientFormContract; -import java.util.Date; +import java.util.ArrayList; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** @@ -25,181 +25,77 @@ public class FormRollbackDialogUtilTest extends BaseTest { @Test public void selectFormShouldReturnFalseWhenCurrentCorruptedFormIsChosen() { - ClientFormDao.ClientFormModel clientForm = new TestClientForm(); + ClientFormContract.Model clientForm = new TestClientForm(); - assertFalse(FormRollbackDialogUtil.selectForm(0, "0.0.3" + RuntimeEnvironment.application.getString(R.string.current_corrupted_form) - , RuntimeEnvironment.application, new ArrayList(), clientForm, Mockito.mock(RollbackDialogCallback.class))); + assertFalse(FormRollbackDialogUtil.selectForm(Mockito.mock(ClientFormContract.Dao.class) , 0, "0.0.3" + RuntimeEnvironment.application.getString(R.string.current_corrupted_form) + , RuntimeEnvironment.application, new ArrayList(), clientForm, Mockito.mock(RollbackDialogCallback.class))); } @Test public void selectFormShouldReturnFalseWhenItemIndexDoesNotExist() { - ClientForm clientForm = new ClientForm(); + ClientFormContract.Model clientForm = new TestClientForm(); - assertFalse(FormRollbackDialogUtil.selectForm(2, "0.0.3" - , RuntimeEnvironment.application, new ArrayList(), clientForm, Mockito.mock(RollbackDialogCallback.class))); + assertFalse(FormRollbackDialogUtil.selectForm(Mockito.mock(ClientFormContract.Dao.class), 2, "0.0.3" + , RuntimeEnvironment.application, new ArrayList(), clientForm, Mockito.mock(RollbackDialogCallback.class))); } @Test public void selectFormShouldReturnTrueWhenAConfigurableFormIsSelected() { - ClientForm highClientFormVersion = new ClientForm(); + ClientFormContract.Model highClientFormVersion = new TestClientForm(); highClientFormVersion.setVersion("0.0.3"); - ClientForm clientForm = new ClientForm(); + ClientFormContract.Model clientForm = new TestClientForm(); clientForm.setVersion("0.0.2"); - ArrayList clientFormsList = new ArrayList(); + ArrayList clientFormsList = new ArrayList(); clientFormsList.add(clientForm); - ClientFormRepository clientFormRepository = Mockito.mock(ClientFormRepository.class); - ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "clientFormRepository", clientFormRepository); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); RollbackDialogCallback rollbackDialogCallback = Mockito.mock(RollbackDialogCallback.class); - assertTrue(FormRollbackDialogUtil.selectForm(0, "0.0.2" + assertTrue(FormRollbackDialogUtil.selectForm(clientFormRepository, 0, "0.0.2" , RuntimeEnvironment.application, clientFormsList, highClientFormVersion, rollbackDialogCallback)); Mockito.verify(rollbackDialogCallback).onFormSelected(clientForm); - ArgumentCaptor updateClientFormArgumentCaptor = ArgumentCaptor.forClass(ClientForm.class); + ArgumentCaptor updateClientFormArgumentCaptor = ArgumentCaptor.forClass(ClientFormContract.Model.class); Mockito.verify(clientFormRepository, Mockito.times(2)).addOrUpdate(updateClientFormArgumentCaptor.capture()); - ClientForm updatedClientForm1 = updateClientFormArgumentCaptor.getAllValues().get(0); + ClientFormContract.Model updatedClientForm1 = updateClientFormArgumentCaptor.getAllValues().get(0); assertEquals("0.0.2", updatedClientForm1.getVersion()); assertTrue(updatedClientForm1.isActive()); - ClientForm updatedClientForm2 = updateClientFormArgumentCaptor.getAllValues().get(1); + ClientFormContract.Model updatedClientForm2 = updateClientFormArgumentCaptor.getAllValues().get(1); assertEquals("0.0.3", updatedClientForm2.getVersion()); assertFalse(updatedClientForm2.isActive()); - ArgumentCaptor clientFormArgumentCaptor = ArgumentCaptor.forClass(ClientForm.class); + ArgumentCaptor clientFormArgumentCaptor = ArgumentCaptor.forClass(ClientFormContract.Model.class); Mockito.verify(rollbackDialogCallback).onFormSelected(clientFormArgumentCaptor.capture()); - ClientForm selectedClientForm = clientFormArgumentCaptor.getValue(); + ClientFormContract.Model selectedClientForm = clientFormArgumentCaptor.getValue(); assertEquals("0.0.2", selectedClientForm.getVersion()); } @Test public void selectFormShouldReturnTrueWhenBaseFormIsSelected() { - ClientForm highClientFormVersion = new ClientForm(); + ClientFormContract.Model highClientFormVersion = new TestClientForm(); highClientFormVersion.setVersion("0.0.3"); - ClientForm clientForm = new ClientForm(); + ClientFormContract.Model clientForm = new TestClientForm(); clientForm.setVersion("0.0.2"); - ArrayList clientFormsList = new ArrayList(); + ArrayList clientFormsList = new ArrayList(); clientFormsList.add(highClientFormVersion); clientFormsList.add(clientForm); - ClientFormRepository clientFormRepository = Mockito.mock(ClientFormRepository.class); - ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "clientFormRepository", clientFormRepository); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); RollbackDialogCallback rollbackDialogCallback = Mockito.mock(RollbackDialogCallback.class); - assertTrue(FormRollbackDialogUtil.selectForm(0, "base version" + assertTrue(FormRollbackDialogUtil.selectForm(clientFormRepository,0, "base version" , RuntimeEnvironment.application, clientFormsList, highClientFormVersion, rollbackDialogCallback)); - ArgumentCaptor clientFormArgumentCaptor = ArgumentCaptor.forClass(ClientForm.class); + ArgumentCaptor clientFormArgumentCaptor = ArgumentCaptor.forClass(ClientFormContract.Model.class); Mockito.verify(rollbackDialogCallback).onFormSelected(clientFormArgumentCaptor.capture()); - ClientFormDao.ClientFormModel selectedClientForm = clientFormArgumentCaptor.getValue(); + ClientFormContract.Model selectedClientForm = clientFormArgumentCaptor.getValue(); assertEquals(JsonFormConstants.CLIENT_FORM_ASSET_VERSION, selectedClientForm.getVersion()); } - - class TestClientForm implements ClientFormDao.ClientFormModel { - private int id; - - private String version; - - private String identifier; - - private String module; - - private String json; - - private String jurisdiction; - - private String label; - - private boolean active; - - private boolean isNew; - - private Date createdAt; - - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - public String getIdentifier() { - return identifier; - } - - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - - public String getModule() { - return module; - } - - public void setModule(String module) { - this.module = module; - } - - public String getJson() { - return json; - } - - public void setJson(String json) { - this.json = json; - } - - public String getJurisdiction() { - return jurisdiction; - } - - public void setJurisdiction(String jurisdiction) { - this.jurisdiction = jurisdiction; - } - - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - - public boolean isActive() { - return active; - } - - public void setActive(boolean active) { - this.active = active; - } - - public boolean isNew() { - return isNew; - } - - public void setNew(boolean aNew) { - isNew = aNew; - } - - public Date getCreatedAt() { - return createdAt; - } - - public void setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - } - } } \ No newline at end of file diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java index 1b9a8a1ca..14438b730 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/FormUtilsTest.java @@ -1,5 +1,6 @@ package com.vijay.jsonwizard.utils; +import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; @@ -19,7 +20,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; import org.robolectric.util.ReflectionHelpers; -import org.smartregister.client.utils.contract.ClientFormDao; +import org.smartregister.client.utils.contract.ClientFormContract; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -334,22 +335,23 @@ public void testGetSpinnerValueOpenMRSAttributesShouldCorrectlyExtractOpenMRSAtt @Test public void getFormJsonFromRepositoryOrAssets() throws Exception { - formUtils = new FormUtils(context_); + formUtils = new FormUtils(); Resources resources = Mockito.mock(Resources.class); Configuration configuration = Mockito.mock(Configuration.class); - ClientFormRepository clientFormRepository = Mockito.mock(ClientFormRepository.class); - ClientForm clientForm = new ClientForm(); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); + ClientFormContract.Model clientForm = new TestClientForm(); clientForm.setJson("{\"form\":\"Sick Child Referral\",\"count\":\"1\",\"encounter_type\":\" \",\"entity_id\":\"\",\"relational_id\":\"\",\"rules_file\":\"rule/general_neat_referral_form_rules.yml\",\"metadata\":{\"start\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"start\",\"openmrs_entity_id\":\"163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"end\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"end\",\"openmrs_entity_id\":\"163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"today\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"encounter\",\"openmrs_entity_id\":\"encounter_date\"},\"deviceid\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"deviceid\",\"openmrs_entity_id\":\"163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"subscriberid\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"subscriberid\",\"openmrs_entity_id\":\"163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"simserial\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"simserial\",\"openmrs_entity_id\":\"163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"phonenumber\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_data_type\":\"phonenumber\",\"openmrs_entity_id\":\"163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"encounter_location\":\"\",\"look_up\":{\"entity_id\":\"\",\"value\":\"\"}},\"steps\":[{\"title\":\"Sick child form\",\"fields\":[{\"name\":\"chw_referral_service\",\"type\":\"invisible\",\"properties\":{\"text\":\"Choose referral service\"},\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"09978\",\"openmrs_entity_parent\":\"\"},\"options\":[],\"required_status\":\"yes:Please specify referral service\"},{\"name\":\"problem\",\"type\":\"multi_choice_checkbox\",\"properties\":{\"text\":\"Pick condition/problem associated with the client.\"},\"meta_data\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163182AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"options\":[{\"name\":\"Fast_breathing_and_difficulty_with_breathing\",\"text\":\"Fast breathing and difficulty with breathing\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"142373AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Umbilical_cord_navel_bleeding\",\"text\":\"Umbilical cord/navel bleeding\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"123844AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Excessive_crying\",\"text\":\"Excessive crying\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"140944AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Convulsions\",\"text\":\"Convulsions\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"113054AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Unable_to_breastfeed_or_swallow\",\"text\":\"Unable to breastfeed or swallow\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"159861AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Neck_stiffness\",\"text\":\"Neck stiffness\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"112721AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Fever\",\"text\":\"Fever\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"140238AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Bloating\",\"text\":\"Bloating\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"147132AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Redness_around_the_umbilical_cord_foul_smelling_discharge_from_the_umbilical_cord\",\"text\":\"Redness around the umbilical cord, foul-smelling discharge from the umbilical cord\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"132407AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Bacterial_conjunctivitis\",\"text\":\"Bacterial conjunctivitis\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"148026AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Severe_anaemia\",\"text\":\"Severe anaemia\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"162044AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Severe_abdominal_pain\",\"text\":\"Severe abdominal pain\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165271AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Pale_or_jaundiced\",\"text\":\"Pale or jaundiced\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"136443AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Cyanosis_blueness_of_lips\",\"text\":\"Cyanosis (blueness of lips)\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"143050AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Skin_rash_pustules\",\"text\":\"Skin rash / pustules\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"512AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Diarrhea\",\"text\":\"Diarrhea\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"142412AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Vomiting\",\"text\":\"Vomiting\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"122983AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Disabilities\",\"text\":\"Disabilities\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"162558AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Premature_baby\",\"text\":\"Premature baby\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"159908AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Care_of_HIV_exposed_infant\",\"text\":\"Care of HIV-exposed infant\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"164818AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Immunisation\",\"text\":\"Immunisation\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"1914AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Other_symptom\",\"text\":\"Other symptom\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}}],\"required_status\":\"yes:Please specify client's problems\"},{\"name\":\"problem_other\",\"type\":\"text_input_edit_text\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"160632AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"163182AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"properties\":{\"hint\":\"Other symptoms\",\"type\":\"name\"},\"required_status\":\"true:Please specify other symptoms\",\"subjects\":\"problem:map\"},{\"name\":\"service_before_referral\",\"meta_data\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"164378AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"type\":\"multi_choice_checkbox\",\"properties\":{\"text\":\"Pre-referral management given.\"},\"options\":[{\"name\":\"ORS\",\"text\":\"ORS\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"351AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Panadol\",\"text\":\"Panadol\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"70116AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"Other_treatment\",\"text\":\"Other treatment\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}},{\"name\":\"None\",\"text\":\"None\",\"is_exclusive\":true,\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"164369AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"\"}}],\"required_status\":\"Pre-referral management field is required\"},{\"name\":\"service_before_referral_other\",\"type\":\"text_input_edit_text\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"160632AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_entity_parent\":\"164378AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"properties\":{\"hint\":\"Other Treatment\",\"type\":\"name\"},\"required_status\":\"true:Please specify other treatment given\",\"subjects\":\"service_before_referral:map\"},{\"name\":\"chw_referral_hf\",\"type\":\"spinner\",\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"chw_referral_hf\",\"openmrs_entity_parent\":\"\"},\"properties\":{\"text\":\"Choose referral facility\",\"searchable\":\"Choose referral facility\"},\"options\":[],\"required_status\":\"yes:Please specify referral facility\"},{\"name\":\"referral_appointment_date\",\"type\":\"datetime_picker\",\"properties\":{\"hint\":\"Please select the appointment date\",\"type\":\"date_picker\",\"display_format\":\"dd/MM/yyyy\"},\"meta_data\":{\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"referral_appointment_date\",\"openmrs_entity_parent\":\"\"},\"required_status\":\"true:Please specify the appointment date\"},{\"name\":\"referral_date\",\"meta_data\":{\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"163181AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"type\":\"hidden\"},{\"name\":\"referral_time\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"referral_time\",\"type\":\"hidden\"},{\"name\":\"referral_type\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"referral_type\",\"type\":\"hidden\"},{\"name\":\"referral_status\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"referral_status\",\"type\":\"hidden\"}]}]}"); configuration.locale = new Locale("en"); - Mockito.when( context_.getResources()).thenReturn(resources); - Mockito.when( resources.getConfiguration()).thenReturn(configuration); - Mockito.when( CoreLibrary.getInstance().context().getClientFormRepository()).thenReturn(clientFormRepository); - Mockito.when( clientFormRepository.getActiveClientFormByIdentifier("sick_child_referral_form")).thenReturn(clientForm); + Context context = Mockito.spy(RuntimeEnvironment.application); + + Mockito.when(context.getResources()).thenReturn(resources); + Mockito.when(resources.getConfiguration()).thenReturn(configuration); + Mockito.when(clientFormRepository.getActiveClientFormByIdentifier("sick_child_referral_form")).thenReturn(clientForm); - JSONObject form = formUtils.getFormJsonFromRepositoryOrAssets("sick_child_referral_form"); + JSONObject form = formUtils.getFormJsonFromRepositoryOrAssets(context, clientFormRepository, "sick_child_referral_form"); Mockito.verify(clientFormRepository).getActiveClientFormByIdentifier("sick_child_referral_form"); Assert.assertNotNull(form); @@ -365,29 +367,30 @@ public void extractFormNameWithoutExtensionShouldReturnNameWithoutExtension() { @Test public void getRulesFromRepositoryShouldCallRepositoryQueryingClientForm() { String rulesFileIdentifier = "registration_calculation.yml"; - ClientForm clientForm = new ClientForm(); + Context context = Mockito.spy(RuntimeEnvironment.application); + ClientFormContract.Model clientForm = new TestClientForm(); + clientForm.setJson(""); - ClientFormRepository clientFormRepository = Mockito.mock(ClientFormRepository.class); - Mockito.doReturn(clientFormRepository).when(context).getClientFormRepository(); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); Mockito.doReturn(clientForm).when(clientFormRepository).getActiveClientFormByIdentifier(Mockito.eq(rulesFileIdentifier)); ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application); - Assert.assertNotNull(formUtils.getRulesFromRepository(rulesFileIdentifier)); + Assert.assertNotNull(formUtils.getRulesFromRepository(context, clientFormRepository, rulesFileIdentifier)); Mockito.verify(clientFormRepository).getActiveClientFormByIdentifier(Mockito.eq(rulesFileIdentifier)); } @Test public void getSubFormFromRepository() throws JSONException { + Context context = Mockito.spy(RuntimeEnvironment.application); String subFormIdentifier = "some_tests"; - ClientFormDao.ClientFormModel clientForm = new ClientForm(); + ClientFormContract.Model clientForm = new TestClientForm(); clientForm.setJson("{}"); - ClientFormDao clientFormRepository = Mockito.mock(ClientFormDao.class); - Mockito.doReturn(clientFormRepository).when(context).getClientFormRepository(); + ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class); Mockito.doReturn(clientForm).when(clientFormRepository).getActiveClientFormByIdentifier(Mockito.eq("json.form/sub_form/" + subFormIdentifier)); ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application); - JSONObject jsonObject = formUtils.getSubFormJsonFromRepository(subFormIdentifier, null, RuntimeEnvironment.application, false); + JSONObject jsonObject = formUtils.getSubFormJsonFromRepository(context, clientFormRepository, subFormIdentifier, null, false); Mockito.verify(clientFormRepository).getActiveClientFormByIdentifier(Mockito.eq(subFormIdentifier)); Mockito.verify(clientFormRepository).getActiveClientFormByIdentifier(Mockito.eq(subFormIdentifier + ".json")); @@ -398,7 +401,9 @@ public void getSubFormFromRepository() throws JSONException { @Test public void injectFormStatusShouldAddClientFormDetailsToJsonObject() throws JSONException { - ClientFormDao.ClientFormModel clientForm = new ClientForm(); + //TODO: Fix the below line + //ClientFormContract.Model clientForm = new TestClientForm(); + TestClientForm clientForm = new TestClientForm(); clientForm.setId(3); clientForm.setNew(true); clientForm.setVersion("0.0.1"); diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/TestClientForm.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/TestClientForm.java new file mode 100644 index 000000000..ba786b7c2 --- /dev/null +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/TestClientForm.java @@ -0,0 +1,111 @@ +package com.vijay.jsonwizard.utils; + +import org.smartregister.client.utils.contract.ClientFormContract; + +import java.util.Date; + +/** + * Created by Ephraim Kigamba - nek.eam@gmail.com on 01-07-2020. + */ +public class TestClientForm implements ClientFormContract.Model { + private int id; + + private String version; + + private String identifier; + + private String module; + + private String json; + + private String jurisdiction; + + private String label; + + private boolean active; + + private boolean isNew; + + private Date createdAt; + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public String getModule() { + return module; + } + + public void setModule(String module) { + this.module = module; + } + + public String getJson() { + return json; + } + + public void setJson(String json) { + this.json = json; + } + + public String getJurisdiction() { + return jurisdiction; + } + + public void setJurisdiction(String jurisdiction) { + this.jurisdiction = jurisdiction; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + public boolean isNew() { + return isNew; + } + + public void setNew(boolean aNew) { + isNew = aNew; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } +}