View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * JsonUtil Test Class
43   *
44   */
45  public class JsonUtilTest extends LuteceTestCase
46  {
47      /**
48       * Test of code and message error responses
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       * Test of code only error responses
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      * Test of replaceAccent method, of class fr.paris.lutece.util.string.StringUtil.
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      * Test of replaceAccent method, of class fr.paris.lutece.util.string.StringUtil.
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 }