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
+ }
0 commit comments