Skip to content

Commit

Permalink
Fix more tests for form config work
Browse files Browse the repository at this point in the history
- Fix FormConfigurationJsonFormActivityTest
- Fix FormRollbackDialogUtilTest
- Fix FormUtilsTest
  • Loading branch information
ekigamba committed Jul 1, 2020
1 parent 6d7cf6e commit bd6d90f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,16 @@ public void setUp() throws Exception {
public void getRulesShouldReturnCallFormUtils() throws Exception {
FormUtils formUtils = Mockito.mock(FormUtils.class);

ReflectionHelpers.setStaticField(FormUtils.class, "instance", formUtils);
ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application);
String rulesFileIdentifier = "registration_calculation.yml";

ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class);

NativeFormLibrary.getInstance().setClientFormDao(clientFormRepository);
ReflectionHelpers.setField(formConfigurationJsonFormActivity, "formUtils", formUtils);

Mockito.doReturn(new BufferedReader(new StringReader(""))).when(formUtils).getRulesFromRepository(
Mockito.eq(RuntimeEnvironment.application),
Mockito.eq(clientFormRepository),
rulesFileIdentifier);
Mockito.eq(rulesFileIdentifier));


formConfigurationJsonFormActivity.getRules(RuntimeEnvironment.application, rulesFileIdentifier);
Expand All @@ -70,12 +68,12 @@ public void getRulesShouldReturnCallFormUtils() throws Exception {
public void getSubFormShouldCallFormUtils() throws Exception {
FormUtils formUtils = Mockito.mock(FormUtils.class);

ReflectionHelpers.setStaticField(FormUtils.class, "instance", formUtils);
ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application);
String subFormIdentifier = "tuberculosis_test";

JSONObject jsonObject = new JSONObject();
ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class);
NativeFormLibrary.getInstance().setClientFormDao(clientFormRepository);
ReflectionHelpers.setField(formConfigurationJsonFormActivity, "formUtils", formUtils);

Mockito.doReturn(jsonObject).when(formUtils).getSubFormJsonFromRepository(RuntimeEnvironment.application, clientFormRepository, subFormIdentifier, null, false);

Expand All @@ -89,8 +87,9 @@ public void getSubFormShouldCallHandleFormErrorWhenFormReturnedIsCorrupted() thr
FormConfigurationJsonFormActivity spiedActivity = Mockito.spy(formConfigurationJsonFormActivity);

ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class);
ReflectionHelpers.setStaticField(FormUtils.class, "instance", formUtils);
String subFormIdentifier = "tuberculosis_test.json";
NativeFormLibrary.getInstance().setClientFormDao(clientFormRepository);
ReflectionHelpers.setField(spiedActivity, "formUtils", formUtils);

Mockito.doAnswer(new Answer() {
@Override
Expand All @@ -111,20 +110,20 @@ public void handleFormErrorShouldCallFormUtilsHandleError() {
//formConfigurationJsonFormActivity.contex
FormConfigurationJsonFormActivity spiedActivity = Mockito.spy(formConfigurationJsonFormActivity);
ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class);
NativeFormLibrary.getInstance().setClientFormDao(clientFormRepository);
ReflectionHelpers.setField(spiedActivity, "formUtils", formUtils);

ReflectionHelpers.setStaticField(FormUtils.class, "instance", formUtils);
ReflectionHelpers.setField(formUtils, "mContext", RuntimeEnvironment.application);
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
OnFormFetchedCallback<String> onFormFetchedCallback = invocation.getArgument(2);
OnFormFetchedCallback<String> onFormFetchedCallback = invocation.getArgument(4);
onFormFetchedCallback.onFormFetched("");
return null;
}
}).when(formUtils).handleJsonFormOrRulesError(RuntimeEnvironment.application, clientFormRepository, Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class));
}).when(formUtils).handleJsonFormOrRulesError(Mockito.eq(spiedActivity), Mockito.eq(clientFormRepository), Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class));

spiedActivity.handleFormError(false, formIdentifier);
Mockito.verify(formUtils).handleJsonFormOrRulesError(RuntimeEnvironment.application, clientFormRepository, Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class));
Mockito.verify(formUtils).handleJsonFormOrRulesError(Mockito.eq(spiedActivity), Mockito.eq(clientFormRepository), Mockito.eq(false), Mockito.eq(formIdentifier), Mockito.any(OnFormFetchedCallback.class));
Mockito.verify(spiedActivity).finish();
}

Expand Down Expand Up @@ -188,7 +187,9 @@ public void onCreateShouldCallShowFormVersionUpdateDialog() throws JSONException
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());
Bundle bundle = new Bundle();
bundle.putString(JsonFormBaseActivity.JSON_STATE, "{}");
spiedActivity.onCreate(bundle);

Mockito.verify(spiedActivity).showFormVersionUpdateDialog(jsonObject, getString(R.string.form_update_title), getString(R.string.form_update_message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void selectFormShouldReturnTrueWhenBaseFormIsSelected() {
clientFormsList.add(clientForm);

ClientFormContract.Dao clientFormRepository = Mockito.mock(ClientFormContract.Dao.class);
Mockito.doReturn(new TestClientForm()).when(clientFormRepository).createNewClientFormModel();

RollbackDialogCallback rollbackDialogCallback = Mockito.mock(RollbackDialogCallback.class);
assertTrue(FormRollbackDialogUtil.selectForm(clientFormRepository,0, "base version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ public void getRulesFromRepositoryShouldCallRepositoryQueryingClientForm() {
clientForm.setJson("");
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(context, clientFormRepository, rulesFileIdentifier));

Expand All @@ -388,7 +387,6 @@ public void getSubFormFromRepository() throws JSONException {
clientForm.setJson("{}");
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(context, clientFormRepository, subFormIdentifier, null, false);

Expand Down

0 comments on commit bd6d90f

Please sign in to comment.