diff options
author | tian.ming@huawei.com <tian.ming@huawei.com> | 2016-08-25 11:54:47 +0800 |
---|---|---|
committer | tian.ming@huawei.com <tian.ming@huawei.com> | 2016-08-25 12:10:24 +0800 |
commit | 8b49886b96d5f7ebc91dbf1aa158b31ac4a72848 (patch) | |
tree | 101717a1c2af134de01bf4007486e9c3d44a6de9 /CommonLibrary/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java | |
parent | b25d7a854e3cbf73fa2ee3a8260bf1ebb433cf04 (diff) |
CommonLibrary(util/rest-client) code upload.
Change-Id: I46c9c2ef19e43ebc7f61d8c1b5972c362e7f2d2a
Signed-off-by: tian.ming@huawei.com <tian.ming@huawei.com>
Diffstat (limited to 'CommonLibrary/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java')
-rw-r--r-- | CommonLibrary/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/CommonLibrary/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java b/CommonLibrary/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java new file mode 100644 index 0000000..d532b94 --- /dev/null +++ b/CommonLibrary/rest-client/src/test/java/org/openo/baseservice/roa/util/clientsdk/TestJsonUtil.java @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2016, Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.baseservice.roa.util.clientsdk; + +import org.openo.baseservice.roa.util.clientsdk.demo.JsonTestClass; + +import org.codehaus.jackson.JsonParseException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import net.sf.json.JSONObject; + +import junit.framework.Assert; + +/** + * <br/> + * <p> + * </p> + * + * @author + * @version SDNO 0.5 13-Jun-2016 + */ +public class TestJsonUtil { + + /** + * <br/> + * + * @throws java.lang.Exception + * @since SDNO 0.5 + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since SDNO 0.5 + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since SDNO 0.5 + */ + @Before + public void setUp() throws Exception { + } + + /** + * <br/> + * + * @throws java.lang.Exception + * @since SDNO 0.5 + */ + @After + public void tearDown() throws Exception { + } + + /** + * <br/> + * + * @throws Exception + * @since SDNO 0.5 + */ + @Test + public void testUnMarshalStringClassOfT() throws Exception { + final String name = "myname"; + final int id = 25; + final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}"; + + final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, JsonTestClass.class); + + Assert.assertNotNull(jsonObj); + Assert.assertEquals(name, jsonObj.getName()); + Assert.assertEquals(id, jsonObj.getId()); + + } + + /** + * <br/> + * + * @throws Exception + * @since SDNO 0.5 + */ + @Test + public void testUnMarshalStringTypeReferenceOfT() throws Exception { + final String name = "myname"; + final int id = 25; + final String jsonstr = "{\"name\": \"" + name + "\", \"id\": " + id + "}"; + + final JsonTestClass jsonObj = JsonUtil.unMarshal(jsonstr, new TypeReference<JsonTestClass>() {}); + + Assert.assertNotNull(jsonObj); + Assert.assertEquals(name, jsonObj.getName()); + Assert.assertEquals(id, jsonObj.getId()); + } + + /** + * <br/> + * + * @throws Exception + * @since SDNO 0.5 + */ + @Test + public void testMarshal() throws Exception { + final JsonTestClass jsonObj = new JsonTestClass(); + jsonObj.setId(1); + jsonObj.setName("somename"); + final String str = JsonUtil.marshal(jsonObj); + final JSONObject json = JSONObject.fromObject(str); + Assert.assertNotNull(json); + Assert.assertEquals(json.getString("name"), jsonObj.getName()); + Assert.assertEquals(json.getInt("id"), jsonObj.getId()); + + } + + /** + * <br/> + * + * @throws Exception + * @since SDNO 0.5 + */ + @Test + public void testMarshalJsonObj() throws Exception { + final JSONObject jsonObj = new JSONObject(); + jsonObj.put("id", 10); + jsonObj.put("name", "some-name"); + final String str = JsonUtil.marshal(jsonObj); + final JSONObject json = JSONObject.fromObject(str); + Assert.assertNotNull(json); + Assert.assertEquals(json.getString("name"), "some-name"); + Assert.assertEquals(json.getInt("id"), 10); + + } + + /** + * <br/> + * + * @throws JsonParseException + * @throws JsonMappingException + * @throws Exception + * @since SDNO 0.5 + */ + @Test + public void testGetMapper() throws JsonParseException, JsonMappingException, Exception { + final String name = "myname"; + final int id = 25; + final ObjectMapper mapper = JsonUtil.getMapper(); + Assert.assertNotNull(mapper); + final JSONObject json = new JSONObject(); + json.put("name", name); + json.put("id", id); + final JsonTestClass jsonObj = mapper.readValue(json.toString(), JsonTestClass.class); + Assert.assertNotNull(jsonObj); + Assert.assertEquals(name, jsonObj.getName()); + Assert.assertEquals(id, jsonObj.getId()); + } +} |