2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.baseservice.roa.util.clientsdk;
19 import org.openo.baseservice.roa.util.clientsdk.demo.JsonTestClass;
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;
31 import net.sf.json.JSONObject;
33 import junit.framework.Assert;
41 * @version SDNO 0.5 13-Jun-2016
43 public class TestJsonUtil {
48 * @throws java.lang.Exception
52 public static void setUpBeforeClass() throws Exception {
58 * @throws java.lang.Exception
62 public static void tearDownAfterClass() throws Exception {
68 * @throws java.lang.Exception
72 public void setUp() throws Exception {
78 * @throws java.lang.Exception
82 public void tearDown() throws Exception {
92 public void testUnMarshalStringClassOfT() throws Exception {
93 final String name = "myname";
95 final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}";
97 final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, JsonTestClass.class);
99 Assert.assertNotNull(jsonObj);
100 Assert.assertEquals(name, jsonObj.getName());
101 Assert.assertEquals(id, jsonObj.getId());
112 public void testUnMarshalStringTypeReferenceOfT() throws Exception {
113 final String name = "myname";
115 final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}";
117 final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, new TypeReference<JsonTestClass>() {});
119 Assert.assertNotNull(jsonObj);
120 Assert.assertEquals(name, jsonObj.getName());
121 Assert.assertEquals(id, jsonObj.getId());
131 public void testMarshal() throws Exception {
132 final JsonTestClass jsonObj = new JsonTestClass();
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());
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);
165 * @throws JsonParseException
166 * @throws JsonMappingException
171 public void testGetMapper() throws JsonParseException, JsonMappingException, Exception {
172 final String name = "myname";
174 final ObjectMapper mapper = JsonUtil.getMapper();
175 Assert.assertNotNull(mapper);
176 final JSONObject json = new JSONObject();
177 json.put("name", name);
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());