CommonLibrary(util/rest-client) code upload.
[vfc/nfvo/wfengine.git] / rest-client / src / test / java / org / openo / baseservice / roa / util / clientsdk / TestJsonUtil.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openo.baseservice.roa.util.clientsdk;
18
19 import org.openo.baseservice.roa.util.clientsdk.demo.JsonTestClass;
20
21 import org.codehaus.jackson.JsonParseException;
22 import org.codehaus.jackson.map.JsonMappingException;
23 import org.codehaus.jackson.map.ObjectMapper;
24 import org.codehaus.jackson.type.TypeReference;
25 import org.junit.After;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30
31 import net.sf.json.JSONObject;
32
33 import junit.framework.Assert;
34
35 /**
36  * <br/>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version SDNO 0.5 13-Jun-2016
42  */
43 public class TestJsonUtil {
44
45     /**
46      * <br/>
47      * 
48      * @throws java.lang.Exception
49      * @since SDNO 0.5
50      */
51     @BeforeClass
52     public static void setUpBeforeClass() throws Exception {
53     }
54
55     /**
56      * <br/>
57      * 
58      * @throws java.lang.Exception
59      * @since SDNO 0.5
60      */
61     @AfterClass
62     public static void tearDownAfterClass() throws Exception {
63     }
64
65     /**
66      * <br/>
67      * 
68      * @throws java.lang.Exception
69      * @since SDNO 0.5
70      */
71     @Before
72     public void setUp() throws Exception {
73     }
74
75     /**
76      * <br/>
77      * 
78      * @throws java.lang.Exception
79      * @since SDNO 0.5
80      */
81     @After
82     public void tearDown() throws Exception {
83     }
84
85     /**
86      * <br/>
87      * 
88      * @throws Exception
89      * @since SDNO 0.5
90      */
91     @Test
92     public void testUnMarshalStringClassOfT() throws Exception {
93         final String name = "myname";
94         final int id = 25;
95         final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}";
96
97         final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, JsonTestClass.class);
98
99         Assert.assertNotNull(jsonObj);
100         Assert.assertEquals(name, jsonObj.getName());
101         Assert.assertEquals(id, jsonObj.getId());
102
103     }
104
105     /**
106      * <br/>
107      * 
108      * @throws Exception
109      * @since SDNO 0.5
110      */
111     @Test
112     public void testUnMarshalStringTypeReferenceOfT() throws Exception {
113         final String name = "myname";
114         final int id = 25;
115         final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}";
116
117         final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, new TypeReference<JsonTestClass>() {});
118
119         Assert.assertNotNull(jsonObj);
120         Assert.assertEquals(name, jsonObj.getName());
121         Assert.assertEquals(id, jsonObj.getId());
122     }
123
124     /**
125      * <br/>
126      * 
127      * @throws Exception
128      * @since SDNO 0.5
129      */
130     @Test
131     public void testMarshal() throws Exception {
132         final JsonTestClass jsonObj = new JsonTestClass();
133         jsonObj.setId(1);
134         jsonObj.setName("somename");
135         final String str = JsonUtil.marshal(jsonObj);
136         final JSONObject json = JSONObject.fromObject(str);
137         Assert.assertNotNull(json);
138         Assert.assertEquals(json.getString("name"), jsonObj.getName());
139         Assert.assertEquals(json.getInt("id"), jsonObj.getId());
140
141     }
142
143     /**
144      * <br/>
145      * 
146      * @throws Exception
147      * @since SDNO 0.5
148      */
149     @Test
150     public void testMarshalJsonObj() throws Exception {
151         final JSONObject jsonObj = new JSONObject();
152         jsonObj.put("id", 10);
153         jsonObj.put("name", "some-name");
154         final String str = JsonUtil.marshal(jsonObj);
155         final JSONObject json = JSONObject.fromObject(str);
156         Assert.assertNotNull(json);
157         Assert.assertEquals(json.getString("name"), "some-name");
158         Assert.assertEquals(json.getInt("id"), 10);
159
160     }
161
162     /**
163      * <br/>
164      * 
165      * @throws JsonParseException
166      * @throws JsonMappingException
167      * @throws Exception
168      * @since SDNO 0.5
169      */
170     @Test
171     public void testGetMapper() throws JsonParseException, JsonMappingException, Exception {
172         final String name = "myname";
173         final int id = 25;
174         final ObjectMapper mapper = JsonUtil.getMapper();
175         Assert.assertNotNull(mapper);
176         final JSONObject json = new JSONObject();
177         json.put("name", name);
178         json.put("id", id);
179         final JsonTestClass jsonObj = mapper.readValue(json.toString(), JsonTestClass.class);
180         Assert.assertNotNull(jsonObj);
181         Assert.assertEquals(name, jsonObj.getName());
182         Assert.assertEquals(id, jsonObj.getId());
183     }
184 }