Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Jul 1, 2020
1 parent 8b37ba3 commit 6d7cf6e
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -36,7 +38,6 @@ public class FormConfigurationJsonFormActivityTest extends BaseActivityTest {

@Before
public void setUp() throws Exception {
//formConfigurationJsonFormActivity = new FormConfigurationJsonFormActivity();
formConfigurationJsonFormActivity = Robolectric.buildActivity(FormConfigurationJsonFormActivity.class)
.get();
}
Expand All @@ -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
Expand All @@ -65,28 +75,29 @@ 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
public void getSubFormShouldCallHandleFormErrorWhenFormReturnedIsCorrupted() throws Exception {
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() {
@Override
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));
Expand All @@ -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);
Expand All @@ -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();
}

Expand All @@ -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");
Expand All @@ -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();

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@


import com.vijay.jsonwizard.BaseTest;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.interfaces.RollbackDialogCallback;

import org.junit.Test;
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;

/**
Expand All @@ -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>(), 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<ClientFormContract.Model>(), 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>(), clientForm, Mockito.mock(RollbackDialogCallback.class)));
assertFalse(FormRollbackDialogUtil.selectForm(Mockito.mock(ClientFormContract.Dao.class), 2, "0.0.3"
, RuntimeEnvironment.application, new ArrayList<ClientFormContract.Model>(), 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<ClientForm> clientFormsList = new ArrayList<ClientForm>();
ArrayList<ClientFormContract.Model> clientFormsList = new ArrayList<ClientFormContract.Model>();
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<ClientForm> updateClientFormArgumentCaptor = ArgumentCaptor.forClass(ClientForm.class);
ArgumentCaptor<ClientFormContract.Model> 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<ClientForm> clientFormArgumentCaptor = ArgumentCaptor.forClass(ClientForm.class);
ArgumentCaptor<ClientFormContract.Model> 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<ClientForm> clientFormsList = new ArrayList<ClientForm>();
ArrayList<ClientFormContract.Model> clientFormsList = new ArrayList<ClientFormContract.Model>();
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<ClientFormDao.ClientFormModel> clientFormArgumentCaptor = ArgumentCaptor.forClass(ClientForm.class);
ArgumentCaptor<ClientFormContract.Model> 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;
}
}
}
Loading

0 comments on commit 6d7cf6e

Please sign in to comment.