Skip to content

Commit 62b7c4e

Browse files
Merge pull request #376 from OpenSRP/feature/tt-3
Testing Tuesday 3
2 parents b0743f8 + d807000 commit 62b7c4e

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,6 @@ public String getRadioButtonText(JSONObject item, String value) {
10081008
}
10091009

10101010
public String getSpecifyText(JSONArray jsonArray) {
1011-
FormUtils formUtils = new FormUtils();
10121011
StringBuilder specifyText = new StringBuilder();
10131012
for (int i = 0; i < jsonArray.length(); i++) {
10141013
try {
@@ -1017,7 +1016,7 @@ public String getSpecifyText(JSONArray jsonArray) {
10171016
String type = jsonObject.optString(JsonFormConstants.TYPE, null);
10181017
JSONArray itemArray = jsonObject.getJSONArray(JsonFormConstants.VALUES);
10191018
for (int j = 0; j < itemArray.length(); j++) {
1020-
String s = formUtils.getValueFromSecondaryValues(type, itemArray.getString(j));
1019+
String s = getValueFromSecondaryValues(type, itemArray.getString(j));
10211020
if (!TextUtils.isEmpty(s)) {
10221021
specifyText.append(s).append(",").append(" ");
10231022
}
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
11
package com.vijay.jsonwizard.utils;
22

3+
import com.vijay.jsonwizard.BaseTest;
4+
import com.vijay.jsonwizard.constants.JsonFormConstants;
5+
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
import org.json.JSONObject;
9+
import org.junit.Assert;
10+
import org.junit.Before;
311
import org.junit.Test;
12+
import org.mockito.MockitoAnnotations;
413

514
import java.text.SimpleDateFormat;
615
import java.util.Calendar;
716

817
import static junit.framework.TestCase.assertEquals;
918

10-
public class FormUtilsTest {
19+
public class FormUtilsTest extends BaseTest {
20+
private FormUtils formUtils;
21+
private JSONObject jsonFormObject;
22+
private String jsonForm = "{\"count\":\"4\",\"encounter_type\":\"Test\",\"entity_id\":\"\",\"relational_id\":\"\",\"validate_on_submit\":true,\"show_errors_on_submit\":true,\"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\":\"\"}},\"step1\":{\"title\":\"Test\",\"next\":\"step2\",\"fields\":[{\"key\":\"delivery_complications\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"161641AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_data_type\":\"select one\",\"type\":\"check_box\",\"label\":\"Any delivery complications?\",\"label_text_size\":\"18sp\",\"label_text_color\":\"#FF9800\",\"hint\":\"Any delivery complications?\",\"read_only\":true,\"editable\":true,\"exclusive\":[\"none\"],\"options\":[{\"key\":\"none\",\"text\":\"None\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"severe_bleeding\",\"text\":\"Severe bleeding/Hemorrhage\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"text_color\":\"#000000\"},{\"key\":\"placenta_previa\",\"text\":\"Placenta previa\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"text_size\":\"15sp\"},{\"key\":\"cord_prolapse\",\"text\":\"Cord prolapse\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"text_size\":\"10sp\"},{\"key\":\"prolonged_obstructed_labour\",\"text\":\"Prolonged/obstructed labour\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"abnormal_presentation\",\"text\":\"Abnormal presentation\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"text_color\":\"#FF9800\"},{\"key\":\"perineal_tear\",\"text\":\"Perineal tear (2, 3 or 4th degree)\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"Other\",\"text\":\"Other\",\"value\":false,\"openmrs_choice_id\":\"160034AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}],\"v_required\":{\"value\":\"false\"}},{\"key\":\"first_name\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"person\",\"openmrs_entity_id\":\"first_name\",\"type\":\"edit_text\",\"hidden\":true,\"hint\":\"First name\",\"edit_type\":\"name\",\"v_required\":{\"value\":\"true\",\"err\":\"Please enter the first name\"},\"v_regex\":{\"value\":\"[A-Za-z\\\\s\\\\.\\\\-]*\",\"err\":\"Please enter a valid name\"},\"calculation\":{\"rules-engine\":{\"ex-rules\":{\"rules-file\":\"sample-calculation-rules.yml\"}}}}]},\"step2\":{\"title\":\"Test 2\",\"next\":\"step3\",\"fields\":[{\"key\":\"fetal_heartbeat\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"native_radio\",\"label\":\"Which medications is she still taking ? Which medications is she still taking ?\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"extra_rel\":true,\"has_extra_rel\":\"yes\",\"options\":[{\"key\":\"yes\",\"text\":\"Yes\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"specify_info\":\"specify fetal heart rate (bpm)...\",\"specify_widget\":\"normal_edit_text\",\"specify_info_color\":\"#8C8C8C\",\"secondary_suffix\":\"bpm\",\"content_form\":\"fetal_heartbeat_sub_form\"},{\"key\":\"no\",\"text\":\"No\",\"value\":false,\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"specify_info\":\"specify fetal heart rate (bpm)...\",\"specify_widget\":\"check_box\",\"specify_info_color\":\"#8C8C8C\",\"content_form\":\"fetal_heartbeat_sub_form\"}],\"v_required\":{\"value\":true,\"err\":\"Please specify if fetal heartbeat is present.\"}},{\"key\":\"fetal_heart_beat_rate_value\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"normal_edit_text\",\"edit_text_style\":\"bordered\",\"edit_type\":\"number\"}]},\"step3\":{\"title\":\"Maternal Exam\",\"next\":\"step4\",\"fields\":[{\"key\":\"spacer\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"spacer\",\"type\":\"spacer\",\"spacer_height\":\"20dp\"},{\"key\":\"gravida_label\",\"type\":\"label\",\"label_text_style\":\"bold\",\"text\":\"No. of pregnancies (including this pregnancy)\",\"text_color\":\"#000000\",\"v_required\":{\"value\":true}}]}}";
23+
24+
@Before
25+
public void setUp() throws JSONException {
26+
MockitoAnnotations.initMocks(this);
27+
formUtils = new FormUtils();
28+
jsonFormObject = new JSONObject(jsonForm);
29+
}
1130

1231
@Test
13-
public void testGetDate(){
32+
public void testGetDate() {
1433
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
1534
Calendar calendar = Calendar.getInstance();
1635
calendar.add(Calendar.WEEK_OF_MONTH, -1);
17-
1836
Calendar res = FormUtils.getDate("today-1w");
19-
2037
assertEquals(sdf.format(calendar.getTime()), sdf.format(res.getTime()));
2138
}
39+
40+
@Test
41+
public void testGetFieldFromForm() throws JSONException {
42+
JSONObject field = FormUtils.getFieldFromForm(jsonFormObject, "gravida_label");
43+
Assert.assertNotNull(field);
44+
Assert.assertEquals("label", field.getString(JsonFormConstants.TYPE));
45+
}
46+
47+
@Test
48+
public void testGetSpecifyText() throws JSONException {
49+
String secondaryValues = "[{\"key\":\"respiratory_exam_abnormal\",\"type\":\"check_box\",\"values\":[\"rapid_breathing:Rapid breathing:true\"]},{\"key\":\"respiratory_exam_radio_button\",\"type\":\"native_radio\",\"values\":[\"1:Not done\"]},{\"key\":\"respiratory_exam_abnormal_other\",\"type\":\"edit_text\",\"values\":[\"other:Respiratory exam answer\"]}]";
50+
JSONArray jsonArray = new JSONArray(secondaryValues);
51+
String specifyText = formUtils.getSpecifyText(jsonArray);
52+
Assert.assertNotNull(specifyText);
53+
Assert.assertEquals("Rapid breathing, Not done, Respiratory exam answer", specifyText);
54+
}
55+
56+
@Test
57+
public void testCreateExpansionPanelValues() throws JSONException {
58+
String selectedValues = "[{\"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}]},{\"key\":\"Place_Birth\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"1572AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"openmrs_data_type\":\"select one\",\"type\":\"spinner\",\"hint\":\"Place of birth *\",\"label_info_title\":\"Place of birth spinner\",\"label_info_text\":\"here is some text on this dialog\",\"values\":[\"Health facility\",\"Home\"],\"openmrs_choice_ids\":{\"Health facility\":\"1588AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"Home\":\"1536AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},\"value\":\"Home\"},{\"key\":\"respiratory_exam_abnormal_other\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"edit_text\",\"hint\":\"Specify\",\"relevance\":{\"step1:respiratory_exam_abnormal\":{\"ex-checkbox\":[{\"or\":[\"other\"]}]}},\"value\":\"Reasons\"},{\"key\":\"respiratory_exam_radio_button\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165300AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"type\":\"native_radio\",\"label\":\"Respiratory exam\",\"label_text_style\":\"bold\",\"text_color\":\"#000000\",\"extra_rel\":true,\"has_extra_rel\":\"3\",\"options\":[{\"key\":\"1\",\"text\":\"Not done\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165269AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"value\":true},{\"key\":\"2\",\"text\":\"Normal\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165230AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"},{\"key\":\"3\",\"text\":\"Abnormal\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"concept\",\"openmrs_entity_id\":\"165231AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"}]}]";
59+
JSONArray fields = formUtils.createExpansionPanelValues(new JSONArray(selectedValues));
60+
Assert.assertNotNull(fields);
61+
Assert.assertEquals(3, fields.length());
62+
}
2263
}

sample/src/main/assets/json.form/sub_form/child_enrollment_sub_form.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"openmrs_entity_parent": "",
1717
"openmrs_entity": "TACHYPNEA",
1818
"openmrs_entity_id": "125061",
19-
"value": false
19+
"value": true
2020
},
2121
{
2222
"key": "slow_breathing",
@@ -53,7 +53,8 @@
5353
"openmrs_choice_ids": {
5454
"Health facility": "1588AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
5555
"Home": "1536AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
56-
}
56+
},
57+
"value": "Home"
5758
},
5859
{
5960
"key": "respiratory_exam_abnormal_other",
@@ -76,7 +77,7 @@
7677
}
7778
]
7879
}
79-
}
80+
},"value": "Reasons"
8081
},
8182
{
8283
"key": "respiratory_exam_radio_button",
@@ -95,7 +96,8 @@
9596
"text": "Not done",
9697
"openmrs_entity_parent": "",
9798
"openmrs_entity": "concept",
98-
"openmrs_entity_id": "165269AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
99+
"openmrs_entity_id": "165269AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
100+
"value": true
99101
},
100102
{
101103
"key": "2",

0 commit comments

Comments
 (0)