Skip to content

Commit 283639d

Browse files
Merge branch 'master' into feature/tt-4
2 parents 3be8794 + ee8ab0a commit 283639d

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.vijay.jsonwizard.rules;
2+
3+
import android.content.Context;
4+
5+
import org.jeasy.rules.api.Rule;
6+
import org.jeasy.rules.api.Rules;
7+
import org.json.JSONArray;
8+
import org.json.JSONException;
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.mockito.Mockito;
14+
import org.mockito.junit.MockitoJUnitRunner;
15+
import org.powermock.reflect.internal.WhiteboxImpl;
16+
17+
import java.util.ArrayList;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import timber.log.Timber;
22+
23+
@RunWith(MockitoJUnitRunner.class)
24+
public class RulesEngineFactoryTest {
25+
26+
private RulesEngineFactory rulesEngineFactory;
27+
28+
@Before
29+
public void setUp() {
30+
rulesEngineFactory = new RulesEngineFactory();
31+
}
32+
33+
@Test
34+
public void testGetDynamicRulesFromJsonArrayShouldReturnNonEmptyRulesList() throws Exception {
35+
String expected = "[" +
36+
"{\"key\":\"c29afdf9843e4c909a793dafd70e045b\"}," +
37+
"{" +
38+
"\"condition\":\"step1_diagnostic_test_c29afdf9843e4c909a793dafd70e045b == 'Pregnancy Test'\"," +
39+
"\"name\":\"step1_diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," +
40+
"\"description\":\"diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," +
41+
"\"priority\":1," +
42+
"\"actions\":\"isRelevant = true\"" +
43+
"}" +
44+
"]";
45+
try {
46+
JSONArray jsonArray = new JSONArray(expected);
47+
rulesEngineFactory = new RulesEngineFactory(Mockito.mock(Context.class), new HashMap<String, String>());
48+
Map<String, Rules> ruleMap = new HashMap<>();
49+
WhiteboxImpl.setInternalState(rulesEngineFactory, "ruleMap", ruleMap);
50+
Rules result = WhiteboxImpl.invokeMethod(rulesEngineFactory, "getDynamicRulesFromJsonArray", jsonArray);
51+
Rule ruleObject = result.iterator().next();
52+
Assert.assertEquals("step1_diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b", ruleObject.getName());
53+
Assert.assertEquals("diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b", ruleObject.getDescription());
54+
Assert.assertEquals(1, ruleObject.getPriority());
55+
} catch (JSONException e) {
56+
Timber.e(e);
57+
}
58+
}
59+
60+
@Test
61+
public void testGetDynamicRulesFromJsonArrayShouldReturnNullIfKeyElementIsMissing() throws Exception {
62+
String expected = "[" +
63+
"{" +
64+
"\"condition\":\"step1_diagnostic_test_c29afdf9843e4c909a793dafd70e045b == 'Pregnancy Test'\"," +
65+
"\"name\":\"step1_diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," +
66+
"\"description\":\"diagnostic_test_result_spinner_c29afdf9843e4c909a793dafd70e045b\"," +
67+
"\"priority\":1," +
68+
"\"actions\":\"isRelevant = true\"" +
69+
"}" +
70+
"]";
71+
try {
72+
JSONArray jsonArray = new JSONArray(expected);
73+
rulesEngineFactory = new RulesEngineFactory();
74+
Map<String, Rules> ruleMap = new HashMap<>();
75+
WhiteboxImpl.setInternalState(rulesEngineFactory, "ruleMap", ruleMap);
76+
Rules result = WhiteboxImpl.invokeMethod(rulesEngineFactory, "getDynamicRulesFromJsonArray", jsonArray);
77+
Assert.assertNull(result);
78+
} catch (JSONException e) {
79+
Timber.e(e);
80+
}
81+
}
82+
83+
@Test
84+
public void testGetValueShouldReturnBooleanTrue() {
85+
Assert.assertTrue((Boolean) rulesEngineFactory.getValue("true"));
86+
}
87+
88+
@Test
89+
public void testGetValueShouldReturnInteger() {
90+
Assert.assertEquals(1, rulesEngineFactory.getValue("1"));
91+
}
92+
93+
@Test
94+
public void testGetValueShouldReturnFloat() {
95+
Assert.assertEquals(1.00f, rulesEngineFactory.getValue("1.000"));
96+
}
97+
98+
@Test
99+
public void testGetValueShouldReturnValuePassed() {
100+
Assert.assertEquals("kilo", rulesEngineFactory.getValue("kilo"));
101+
}
102+
103+
@Test
104+
public void testGetValueShouldReturnStringArrayList() {
105+
rulesEngineFactory = new RulesEngineFactory(Mockito.mock(Context.class), new HashMap<String, String>());
106+
ArrayList<String> strings = new ArrayList<>();
107+
strings.add("kg");
108+
strings.add("mg");
109+
Assert.assertEquals(strings, rulesEngineFactory.getValue("[kg,mg]"));
110+
}
111+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.vijay.jsonwizard.utils;
2+
3+
import com.vijay.jsonwizard.constants.JsonFormConstants;
4+
import com.vijay.jsonwizard.domain.MultiSelectItem;
5+
6+
import org.json.JSONObject;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mockito.junit.MockitoJUnitRunner;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
@RunWith(MockitoJUnitRunner.class)
16+
public class MultiSelectListUtilsTest {
17+
18+
@Test
19+
public void testToJsonShouldReturnEmptyJsonArray() {
20+
List<MultiSelectItem> multiSelectItems = new ArrayList<>();
21+
Assert.assertTrue((MultiSelectListUtils.toJson(multiSelectItems).length() == 0));
22+
}
23+
24+
@Test
25+
public void testToJsonShouldReturnANonEmptyJsonArray() {
26+
List<MultiSelectItem> multiSelectItems = new ArrayList<>();
27+
MultiSelectItem multiSelectItem = new MultiSelectItem();
28+
multiSelectItem.setKey("key");
29+
multiSelectItem.setText("value");
30+
multiSelectItem.setOpenmrsEntityId("1233AAAAA");
31+
multiSelectItem.setOpenmrsEntity("concept");
32+
multiSelectItem.setOpenmrsEntityParent("2331AAA");
33+
multiSelectItem.setValue(new JSONObject().toString());
34+
multiSelectItems.add(multiSelectItem);
35+
Assert.assertTrue((MultiSelectListUtils.toJson(multiSelectItems).length() > 0));
36+
JSONObject jsonObjectResult = MultiSelectListUtils.toJson(multiSelectItems).optJSONObject(0);
37+
Assert.assertEquals("key", jsonObjectResult.optString(JsonFormConstants.KEY));
38+
Assert.assertEquals("value", jsonObjectResult.optString(JsonFormConstants.MultiSelectUtils.TEXT));
39+
Assert.assertEquals("concept", jsonObjectResult.optString(JsonFormConstants.OPENMRS_ENTITY));
40+
Assert.assertEquals("1233AAAAA", jsonObjectResult.optString(JsonFormConstants.OPENMRS_ENTITY_ID));
41+
Assert.assertEquals("2331AAA", jsonObjectResult.optString(JsonFormConstants.OPENMRS_ENTITY_PARENT));
42+
}
43+
}

0 commit comments

Comments
 (0)