1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.json;
35
36 import com.fasterxml.jackson.databind.JsonNode;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39 import fr.paris.lutece.test.LuteceTestCase;
40
41
42
43
44
45 public class JsonUtilTest extends LuteceTestCase
46 {
47
48
49
50 public void testErrorCodeMessageResponse( )
51 {
52 System.out.println( "errorMessageResponse" );
53 AbstractJsonResponse response = new ErrorJsonResponse( "codeValue", "messageValue" );
54 String strJson = JsonUtil.buildJsonResponse( response );
55 System.out.println( strJson );
56
57 String strRefJson = "{\"errorCode\":\"codeValue\",\"message\":\"messageValue\",\"status\":\"ERROR\"}";
58 ObjectMapper objectMapper = new ObjectMapper( );
59 JsonNode objectNodeRef;
60 JsonNode objectNodeJson;
61 try
62 {
63 objectNodeRef = objectMapper.readTree( strRefJson );
64 objectNodeJson = objectMapper.readTree( strJson );
65 }
66 catch( Exception e )
67 {
68 throw new AssertionError( "Should not happen" );
69 }
70
71 assertEquals( objectNodeRef, objectNodeJson );
72 }
73
74
75
76
77 public void testErrorCodeResponse( )
78 {
79 System.out.println( "errorResponse" );
80 AbstractJsonResponse response = new ErrorJsonResponse( "codeValue" );
81 String strJson = JsonUtil.buildJsonResponse( response );
82 System.out.println( strJson );
83
84 String strRefJson = "{\"errorCode\":\"codeValue\",\"message\":null,\"status\":\"ERROR\"}";
85 ObjectMapper objectMapper = new ObjectMapper( );
86 JsonNode objectNodeRef;
87 JsonNode objectNodeJson;
88 try
89 {
90 objectNodeRef = objectMapper.readTree( strRefJson );
91 objectNodeJson = objectMapper.readTree( strJson );
92 }
93 catch( Exception e )
94 {
95 throw new AssertionError( "Should not happen" );
96 }
97
98 assertEquals( objectNodeRef, objectNodeJson );
99 }
100
101
102
103
104 public void testResponseTRUE( )
105 {
106 System.out.println( "response" );
107
108 AbstractJsonResponse response = new JsonResponse( Boolean.TRUE );
109 String strJson = JsonUtil.buildJsonResponse( response );
110 System.out.println( strJson );
111
112 String strRefJson = "{\"result\":true,\"status\":\"OK\"}";
113 ObjectMapper objectMapper = new ObjectMapper( );
114 JsonNode objectNodeRef;
115 JsonNode objectNodeJson;
116 try
117 {
118 objectNodeRef = objectMapper.readTree( strRefJson );
119 objectNodeJson = objectMapper.readTree( strJson );
120 }
121 catch( Exception e )
122 {
123 throw new AssertionError( "Should not happen" );
124 }
125
126 assertEquals( objectNodeRef, objectNodeJson );
127
128 }
129
130
131
132
133 public void testResponseBEAN( )
134 {
135 System.out.println( "response" );
136
137 AbstractJsonResponse response = new JsonResponse( new Object( )
138 {
139 @SuppressWarnings( "unused" )
140 public String beanstringfield = "Foo";
141 @SuppressWarnings( "unused" )
142 public int beanintfield = 37;
143 } );
144 String strJson = JsonUtil.buildJsonResponse( response );
145 System.out.println( strJson );
146
147 String strRefJson = "{\"result\":{\"beanstringfield\":\"Foo\", \"beanintfield\":37},\"status\":\"OK\"}";
148 ObjectMapper objectMapper = new ObjectMapper( );
149 JsonNode objectNodeRef;
150 JsonNode objectNodeJson;
151 try
152 {
153 objectNodeRef = objectMapper.readTree( strRefJson );
154 objectNodeJson = objectMapper.readTree( strJson );
155 }
156 catch( Exception e )
157 {
158 throw new AssertionError( "Should not happen" );
159 }
160
161 assertEquals( objectNodeRef, objectNodeJson );
162
163 }
164
165 }