99import java .util .List ;
1010import java .util .Map ;
1111
12+
1213import org .json .JSONArray ;
14+ import org .json .JSONException ;
1315import org .json .JSONObject ;
1416import org .junit .Before ;
1517import org .junit .Test ;
@@ -29,73 +31,72 @@ public void setup() {
2931
3032 @ Test
3133 public void createsJSONObjectForProperty_ReturnsJSONObject () throws Exception {
32- JSONObject results = jsonProducer .createJSONObjectForProperty ("description" , new Task (null ,
34+ JSONObject results = jsonProducer .createJSONObjectForProperty (asList ( "description" ) , new Task (null ,
3335 null , null ));
3436
3537 assertThat (results , instanceOf (JSONObject .class ));
3638 }
3739
40+
3841 @ Test
3942 public void createsJSONObjectForProperty_GivenTask_ReturnsJSONObject_WithPropertyNameAsKeyAndPropertyValueAsValue ()
4043 throws Exception {
4144 Task task = new Task (null , null , "My first task" );
4245
43- JSONObject results = jsonProducer .createJSONObjectForProperty ("description" , task );
46+ JSONObject results = jsonProducer .createJSONObjectForProperty (asList ( "description" ) , task );
4447
4548 assertThat (results .getString ("description" ), is ("My first task" ));
4649 }
4750
4851
4952
5053 @ Test
51- public void createsJSONObjectForProperty_GivenNote_ReturnsJSONObject_WithPropertyNameAsKeyAndPropertyValueAsValue ()
52- throws Exception {
53- Note note = new Note ("My first note" , null );
54-
55- JSONObject results = jsonProducer .createJSONObjectForProperty ("text" , note );
56-
57- assertThat (results .getString ("text" ), is ("My first note" ));
58- }
59-
60- @ Test
61- public void createsJSONObjectForProperty_GivenNote_ReturnsJSONObject_WithDeepRelation ()
54+ public void createsJSONObjectForProperty_GivenNote_ReturnsJSONObject_WithDeepRelation_usingOGNL ()
6255 throws Exception {
63- Note note = new Note ("My first note" , new Owner ("Pawan" , "pawanspace @gmail.com" ));
56+ Note note = new Note ("My first note" , new Owner ("Pawan" , "pawan @gmail.com" ));
6457
65- JSONObject results = jsonProducer .createJSONObjectForProperty ("writer.name" , note );
58+ JSONObject results = jsonProducer .createJSONObjectForProperty (asList ( "writer.name" ) , note );
6659
6760 assertThat (results .getString ("writer.name" ), is ("Pawan" ));
6861 }
6962
7063
64+
7165 @ Test
66+ @ SuppressWarnings ("unchecked" )
7267 public void createsListOfJSONArrayReturns_MultipleValuesMappedInArray_ForMultipleObjects () throws Exception {
7368 Map <Task , List <String >> testData = createMapForTasksTestData ();
7469
75- List < JSONArray > results = jsonProducer .createJSONArrayListForMultipleObjectsProperties (testData );
70+ JSONArray results = jsonProducer .createJSONArrayForMultipleObjects (testData );
7671
77- assertThat (results .size (), is (3 ));
72+ assertThat (results .length (), is (3 ));
73+ JSONObject jsonObject = getTask2 (results );
74+
75+ assertThat (jsonObject .getString ("description" ), is ("Task 2" ));
76+ assertThat (jsonObject .get ("notes" ), instanceOf (List .class ));
77+ assertThat (((List <Note >)jsonObject .get ("notes" )).get (0 ).getText (), is ("My first note" ));
78+ assertThat (jsonObject .getString ("assignee.name" ), is ("Varun" ));
79+ assertThat (
jsonObject .
getString (
"assignee.email" ),
is (
"[email protected] " ));
80+
7881 }
79-
80- @ Test
81- public void createsJSONArrayReturns_MultiplePropertiesForOneObjectInJSONArray () throws Exception {
82- Owner writer =
new Owner (
"Pawan" ,
"[email protected] " );
83- Note note = new Note ("My first note" , writer );
84- Task task = new Task (writer , asList (note ), "Task 1" );
8582
86- JSONArray array = jsonProducer .createJSONArrayForProperties (asList ("description" , "notes" , "assignee.name" , "assignee.email" ), task );
87-
88- assertThat (array .getJSONObject (0 ).getString ("description" ), is ("Task 1" ));
89- assertThat (array .getJSONObject (1 ).get ("notes" ), instanceOf (List .class ));
90- assertThat (array .getJSONObject (2 ).getString ("assignee.name" ), is ("Pawan" ));
91- assertThat (
array .
getJSONObject (
3 ).
getString (
"assignee.email" ),
is (
"[email protected] " ));
83+ private JSONObject getTask2 (JSONArray results ) throws JSONException {
84+ JSONObject jsonObject = null ;
85+
86+ for (int i = 0 ; i < results .length (); i ++) {
87+ JSONObject obj = (JSONObject )results .get (i );
88+ if (obj .getString ("description" ).equals ("Task 2" )){
89+ jsonObject = obj ;
90+ }
91+ }
92+ return jsonObject ;
9293 }
9394
9495
9596 private Map <Task , List <String >> createMapForTasksTestData (){
9697 Map <Task , List <String >> testData = new HashMap <Task , List <String >>();
97- Owner writer1 = new Owner ("Pawan" , "pawanspace @gmail.com" );
98- Owner writer2 = new Owner ("Varun" , "varunspace @gmail.com" );
98+ Owner writer1 = new Owner ("Pawan" , "pawan @gmail.com" );
99+ Owner writer2 = new Owner ("Varun" , "varun @gmail.com" );
99100
100101 Note note1 = new Note ("My first note" , writer1 );
101102 Note note2 = new Note ("My second note" , writer2 );
@@ -105,7 +106,7 @@ private Map<Task, List<String>> createMapForTasksTestData(){
105106 Task task3 = new Task (writer1 , null , "Task 3" );
106107
107108 testData .put (task1 , asList ("description" , "notes" ));
108- testData .put (task2 , asList ("description" , "notes" , "assignee.name" ));
109+ testData .put (task2 , asList ("description" , "notes" , "assignee.name" , "assignee.email" ));
109110 testData .put (task3 , asList ("description" , "assignee.name" ));
110111
111112
0 commit comments