Skip to content

Commit 3be8794

Browse files
committed
✔️ Adding tests
1 parent 62b7c4e commit 3be8794

File tree

3 files changed

+123
-11
lines changed

3 files changed

+123
-11
lines changed

android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormUtils.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,14 +1289,16 @@ public void updateValueToJSONArray(JSONObject jsonObject, String valueString) {
12891289
public String addFormDetails(String formString) {
12901290
String form = "";
12911291
try {
1292-
JSONObject jsonForm = new JSONObject(formString);
1293-
String formVersion = jsonForm.optString(JsonFormConstants.FORM_VERSION, "");
1294-
JSONObject formData = new JSONObject();
1295-
formData.put(JsonFormConstants.Properties.APP_VERSION_NAME, BuildConfig.VERSION_NAME);
1296-
formData.put(JsonFormConstants.Properties.APP_FORM_VERSION, formVersion);
1297-
jsonForm.put(JsonFormConstants.Properties.DETAILS, formData);
1298-
1299-
form = String.valueOf(jsonForm);
1292+
if (StringUtils.isNoneBlank(formString)) {
1293+
JSONObject jsonForm = new JSONObject(formString);
1294+
String formVersion = jsonForm.optString(JsonFormConstants.FORM_VERSION, "");
1295+
JSONObject formData = new JSONObject();
1296+
formData.put(JsonFormConstants.Properties.APP_VERSION_NAME, BuildConfig.VERSION_NAME);
1297+
formData.put(JsonFormConstants.Properties.APP_FORM_VERSION, formVersion);
1298+
jsonForm.put(JsonFormConstants.Properties.DETAILS, formData);
1299+
1300+
form = String.valueOf(jsonForm);
1301+
}
13001302
} catch (JSONException e) {
13011303
Timber.e(e, "%s --> addFormDetails", this.getClass().getCanonicalName());
13021304
}

android-json-form-wizard/src/test/java/com/vijay/jsonwizard/activities/JsonFormActivityTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
import com.vijay.jsonwizard.R;
66
import com.vijay.jsonwizard.constants.JsonFormConstants;
77

8+
import org.jeasy.rules.api.Facts;
9+
import org.json.JSONObject;
810
import org.junit.Assert;
911
import org.junit.Before;
1012
import org.junit.Test;
1113
import org.mockito.MockitoAnnotations;
14+
import org.powermock.reflect.Whitebox;
1215
import org.robolectric.Robolectric;
1316
import org.robolectric.RuntimeEnvironment;
1417
import org.robolectric.android.controller.ActivityController;
1518

1619
public class JsonFormActivityTest extends BaseActivityTest {
17-
1820
private JsonFormActivity activity;
1921
private ActivityController<JsonFormActivity> controller;
2022

@@ -47,4 +49,33 @@ public void testSetConfirmationMessageUpdatesConfirmationMessageCorrectly() {
4749
activity.setConfirmCloseMessage(DUMMY_TEST_STRING);
4850
Assert.assertEquals(DUMMY_TEST_STRING, activity.getConfirmCloseMessage());
4951
}
52+
53+
@Test
54+
public void testGetValueFromAddressCoreForEditTexts() throws Exception {
55+
JSONObject jsonObject = new JSONObject("{\"key\":\"pregest_weight\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"person\",\"openmrs_entity_id\":\"5090\",\"type\":\"normal_edit_text\",\"edit_text_style\":\"bordered\",\"edit_type\":\"number\",\"relevance\":{\"step1:pregest_weight_unknown\":{\"ex-checkbox\":[{\"not\":[\"pregest_weight_unknown\"]}]}},\"v_numeric\":{\"value\":\"true\",\"err\":\"\"},\"v_min\":{\"value\":\"30\",\"err\":\"Weight must be equal or greater than 30\"},\"v_max\":{\"value\":\"180\",\"err\":\"Weight must be equal or less than 180\"},\"v_required\":{\"value\":\"true\",\"err\":\"Pre-gestational weight is required\"},\"step\":\"step1\",\"is-rule-check\":true}");
56+
Facts facts = Whitebox.invokeMethod(activity, "getValueFromAddressCore", jsonObject);
57+
Assert.assertNotNull(facts);
58+
Assert.assertTrue(facts.asMap().containsKey("step1_pregest_weight"));
59+
Assert.assertEquals(0, facts.asMap().get("step1_pregest_weight"));
60+
}
61+
62+
@Test
63+
public void testGetValueFromAddressCoreForRadioButtons() throws Exception {
64+
JSONObject jsonObject = new JSONObject("{\"key\":\"blood_type_test_status\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165383AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"label\":\"Blood type test\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"type\":\"extended_radio_button\",\"options\":[{\"key\":\"done_today\",\"text\":\"Done today\",\"type\":\"done_today\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165383AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"done_earlier\",\"text\":\"Done earlier\",\"type\":\"done_earlier\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165385AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"ordered\",\"text\":\"Ordered\",\"type\":\"ordered\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165384AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"not_done\",\"text\":\"Not done\",\"type\":\"not_done\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"1118AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}],\"v_required\":{\"value\":true,\"err\":\"Blood type status is required\"},\"index\":\"0\",\"step\":\"step1\",\"is-rule-check\":true,\"value\":\"done_today\"}");
65+
Facts facts = Whitebox.invokeMethod(activity, "getValueFromAddressCore", jsonObject);
66+
Assert.assertNotNull(facts);
67+
Assert.assertTrue(facts.asMap().containsKey("step1_blood_type_test_status"));
68+
Assert.assertEquals("done_today", facts.asMap().get("step1_blood_type_test_status"));
69+
}
70+
71+
@Test
72+
public void testGetValueFromAddressCoreForCheckBoxes() throws Exception{
73+
JSONObject jsonObject = new JSONObject("{\"key\":\"respiratory_exam_abnormal\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"check_box\",\"label\":\"Abnormal\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"options\":[{\"key\":\"rapid_breathing\",\"text\":\"Rapid breathing\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"TACHYPNEA\",\"openmrs_entity_id\":\"125061\",\"value\":true},{\"key\":\"slow_breathing\",\"text\":\"Slow breathing\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"value\":false},{\"key\":\"other\",\"text\":\"Other (specify)\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"value\":false}],\"value\":[\"rapid_breathing\"],\"is-rule-check\":false}");
74+
Facts facts = Whitebox.invokeMethod(activity,"getValueFromAddressCore", jsonObject);
75+
Assert.assertNotNull(facts);
76+
Assert.assertTrue(facts.asMap().containsKey("slow_breathing"));
77+
Assert.assertEquals("false",facts.asMap().get("slow_breathing"));
78+
Assert.assertTrue(facts.asMap().containsKey("rapid_breathing"));
79+
Assert.assertEquals("true",facts.asMap().get("rapid_breathing"));
80+
}
5081
}

0 commit comments

Comments
 (0)