11package com .mastercard .developer .json ;
22
33import org .junit .Assert ;
4+ import org .junit .Ignore ;
45import org .junit .Rule ;
56import org .junit .Test ;
67import org .junit .rules .ExpectedException ;
78
8- public class JsonEngineTest {
9+ import java .util .Collections ;
10+
11+ @ Ignore
12+ public abstract class BaseJsonEngineTest {
13+
14+ protected static JsonEngine instanceUnderTest ;
915
1016 @ Rule
1117 public ExpectedException expectedException = ExpectedException .none ();
@@ -20,10 +26,10 @@ public void testGetParentJsonPath_Nominal() {
2026 String jsonPath4 = "obj1" ;
2127
2228 // WHEN
23- String parentJsonPath1 = JsonEngine .getParentJsonPath (jsonPath1 );
24- String parentJsonPath2 = JsonEngine .getParentJsonPath (jsonPath2 );
25- String parentJsonPath3 = JsonEngine .getParentJsonPath (jsonPath3 );
26- String parentJsonPath4 = JsonEngine .getParentJsonPath (jsonPath4 );
29+ String parentJsonPath1 = instanceUnderTest .getParentJsonPath (jsonPath1 );
30+ String parentJsonPath2 = instanceUnderTest .getParentJsonPath (jsonPath2 );
31+ String parentJsonPath3 = instanceUnderTest .getParentJsonPath (jsonPath3 );
32+ String parentJsonPath4 = instanceUnderTest .getParentJsonPath (jsonPath4 );
2733
2834 // THEN
2935 Assert .assertEquals ("$['obj1']['obj2']" , parentJsonPath1 );
@@ -42,16 +48,16 @@ public void testGetJsonElementKey_Nominal() {
4248 String jsonPath4 = "obj2" ;
4349
4450 // WHEN
45- String jsonElementKey1 = JsonEngine .getJsonElementKey (jsonPath1 );
46- String jsonElementKey2 = JsonEngine .getJsonElementKey (jsonPath2 );
47- String jsonElementKey3 = JsonEngine .getJsonElementKey (jsonPath3 );
48- String jsonElementKey4 = JsonEngine .getJsonElementKey (jsonPath4 );
51+ String jsonElementKey1 = instanceUnderTest .getJsonElementKey (jsonPath1 );
52+ String jsonElementKey2 = instanceUnderTest .getJsonElementKey (jsonPath2 );
53+ String jsonElementKey3 = instanceUnderTest .getJsonElementKey (jsonPath3 );
54+ String jsonElementKey4 = instanceUnderTest .getJsonElementKey (jsonPath4 );
4955
5056 // THEN
5157 Assert .assertEquals ("obj2" , jsonElementKey1 );
5258 Assert .assertEquals ("obj2" , jsonElementKey2 );
5359 Assert .assertEquals ("obj2" , jsonElementKey3 );
54- Assert .assertEquals ("obj2" , jsonElementKey3 );
60+ Assert .assertEquals ("obj2" , jsonElementKey4 );
5561 }
5662
5763 @ Test
@@ -65,7 +71,7 @@ public void testGetParentJsonPath_ShouldThrowIllegalArgumentException_WhenJsonPa
6571 expectedException .expectMessage ("json can not be null or empty" );
6672
6773 // WHEN
68- JsonEngine .getParentJsonPath (jsonPath );
74+ instanceUnderTest .getParentJsonPath (jsonPath );
6975 }
7076
7177 @ Test
@@ -79,7 +85,7 @@ public void testGetJsonElementKey_ShouldThrowIllegalArgumentException_WhenJsonPa
7985 expectedException .expectMessage ("json can not be null or empty" );
8086
8187 // WHEN
82- JsonEngine .getJsonElementKey (jsonPath );
88+ instanceUnderTest .getJsonElementKey (jsonPath );
8389 }
8490
8591 @ Test
@@ -93,7 +99,7 @@ public void testGetParentJsonPath_ShouldThrowIllegalStateException_WhenNoParent(
9399 expectedException .expectMessage ("Unable to find parent for '$'" );
94100
95101 // WHEN
96- JsonEngine .getParentJsonPath (jsonPath );
102+ instanceUnderTest .getParentJsonPath (jsonPath );
97103 }
98104
99105 @ Test
@@ -107,6 +113,29 @@ public void testGetJsonElementKey_ShouldThrowIllegalStateException_WhenNoKey() {
107113 expectedException .expectMessage ("Unable to find object key for '$'" );
108114
109115 // WHEN
110- JsonEngine .getJsonElementKey (jsonPath );
116+ instanceUnderTest .getJsonElementKey (jsonPath );
117+ }
118+
119+ @ Test
120+ public void testToJsonString_ShouldThrowIllegalStateException_WhenNullObject () {
121+ expectedException .expect (IllegalStateException .class );
122+ expectedException .expectMessage ("Can't get a JSON string from a null object!" );
123+ instanceUnderTest .toJsonString (null );
124+ }
125+
126+ @ Test
127+ public void testIsNullOrEmptyJson_Nominal () {
128+ Assert .assertTrue (instanceUnderTest .isNullOrEmptyJson (null ));
129+ Assert .assertTrue (instanceUnderTest .isNullOrEmptyJson (instanceUnderTest .parse ("{}" )));
130+ Assert .assertFalse (instanceUnderTest .isNullOrEmptyJson (instanceUnderTest .parse ("string" )));
131+ Assert .assertFalse (instanceUnderTest .isNullOrEmptyJson (instanceUnderTest .parse ("true" )));
132+ Assert .assertFalse (instanceUnderTest .isNullOrEmptyJson (instanceUnderTest .parse ("false" )));
133+ Assert .assertFalse (instanceUnderTest .isNullOrEmptyJson (instanceUnderTest .parse ("123" )));
134+ Assert .assertFalse (instanceUnderTest .isNullOrEmptyJson (instanceUnderTest .parse ("{\" data\" :123}" )));
135+ }
136+
137+ @ Test
138+ public void testGetPropertyKeys_ShouldReturnEmptyList_WhenNullObject () {
139+ Assert .assertEquals (Collections .emptyList (), instanceUnderTest .getPropertyKeys (null ));
111140 }
112141}
0 commit comments