View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.json;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  import org.mule.util.IOUtils;
11  
12  import org.codehaus.jackson.node.ArrayNode;
13  import org.codehaus.jackson.node.ObjectNode;
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  public class JsonDataTestCase extends AbstractMuleTestCase
24  {
25  
26      @Test
27      public void testReadingArrayData() throws Exception
28      {
29          JsonData jsonData = readJsonData("test-data.json");
30          assertTrue(jsonData.isArray());
31          assertEquals("test from Mule: 6ffca02b-9d52-475e-8b17-946acdb01492", jsonData.getAsString("[0]/text"));
32          assertEquals("test from Mule: 6ffca02b-9d52-475e-8b17-946acdb01492",
33              jsonData.getAsString("[0]/'text'"));
34  
35          assertEquals("Mule Test", jsonData.getAsString("[0]/'user'/name"));
36          assertEquals("Mule Test9", jsonData.getAsString("[9]/user/name"));
37          // test toString() since it was broken for arrays
38          assertNotNull(jsonData.toString());
39          try
40          {
41              assertNull(jsonData.get("[0]/user/XXX"));
42              fail("Property XXX does not exist");
43          }
44          catch (Exception e)
45          {
46              // expected
47          }
48          try
49          {
50              jsonData.get("foo[0]/user");
51              fail("foo is not the root element name");
52          }
53          catch (Exception e)
54          {
55              // expected
56          }
57  
58          try
59          {
60              jsonData.get("[10]/user");
61              fail("Index should be out of bounds");
62          }
63          catch (Exception e)
64          {
65              // expected
66          }
67      }
68  
69      @Test
70      public void testReadingComplexData() throws Exception
71      {
72          JsonData jsonData = readJsonData("filters.json");
73          assertFalse(jsonData.isArray());
74  
75          // assertEquals("/**", jsonData.get("filters[0]/channels"));
76          assertEquals("teh ", jsonData.getAsString("filters[1]/init[1][0]"));
77          assertEquals("the ", jsonData.getAsString("filters[1]/init[1][1]"));
78          // test toString() since it was broken for arrays
79          assertNotNull(jsonData.toString());
80      }
81  
82      @Test
83      public void testReadingWithQuotedString() throws Exception
84      {
85          JsonData jsonData = readJsonData("bitly-response.json");
86          assertEquals("NfeyS",
87              jsonData.getAsString("results/'http://rossmason.blogspot.com/2008/01/about-me.html'/hash"));
88      }
89  
90      @Test
91      public void testReadingArray() throws Exception
92      {
93          JsonData jsonData = readJsonData("flickr-response.json");
94  
95          assertEquals("4136507840", jsonData.getAsString("photos/photo[0]/id"));
96  
97          ArrayNode photos = (ArrayNode) jsonData.get("photos/photo");
98          assertNotNull(photos);
99          assertEquals(10, photos.size());
100 
101         Object o = jsonData.get("photos");
102         assertNotNull(o);
103         assertTrue(o instanceof ObjectNode);
104     }
105 
106     private JsonData readJsonData(String filename) throws Exception
107     {
108         String json = IOUtils.getResourceAsString(filename, getClass());
109         return new JsonData(json);
110     }
111 }