2 * Copyright 2016 ZTE, Inc. and others.
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.
16 package org.onap.msb.apiroute.wrapper.util;
18 import java.util.List;
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.onap.msb.apiroute.api.PublishFullAddress;
23 import org.onap.msb.apiroute.api.RouteServer;
24 import org.onap.msb.apiroute.wrapper.util.JacksonJsonUtil;
26 import com.fasterxml.jackson.core.type.TypeReference;
29 public class JacksonJsonUtilTest {
31 public void testBeanToJson(){
33 RouteServer server=new RouteServer("127.0.0.1","80");
34 String json=JacksonJsonUtil.beanToJson(server);
35 Assert.assertEquals("{\"ip\":\"127.0.0.1\",\"port\":\"80\",\"weight\":0}",json);
38 Assert.fail("Exception" + e.getMessage());
43 public void testJsonToBean(){
45 String json="{\"ip\":\"127.0.0.1\",\"port\":\"80\",\"weight\":0}";
46 RouteServer server=(RouteServer) JacksonJsonUtil.jsonToBean(json, RouteServer.class);
47 Assert.assertEquals("127.0.0.1",server.getIp());
48 Assert.assertEquals("80",server.getPort());
51 Assert.fail("Exception" + e.getMessage());
57 // public void testJsonToBean_Fail(){
59 // String json="{\"ip\":\"127.0.0.1\",\"port\":\"80\",\"weight\":0";
60 // RouteServer server=(RouteServer) JacksonJsonUtil.jsonToBean(json, RouteServer.class);
62 // catch(Exception e){
63 // Assert.assertEquals("class org.onap.msb.apiroute.api.RouteServer JsonTobean faild",e.getMessage());
68 public void testJsonToListBean(){
70 String resultJson="[{\"domain\": \"wudith.openpalette.zte.com.cn\",\"port\": \"80\",\"publish_url\": \"/api/wudith/v1\",\"visualRange\": \"0\",\"publish_protocol\": \"http\"},"
71 + "{\"ip\": \"10.74.165.246\",\"port\": \"80\",\"publish_url\": \"/api/wudith/v1\",\"visualRange\": \"0\",\"publish_protocol\": \"http\"}]";
72 List<PublishFullAddress> publishFullAddressList =
73 JacksonJsonUtil.jsonToListBean(resultJson, new TypeReference<List<PublishFullAddress>>() {});
74 Assert.assertEquals(2,publishFullAddressList.size());
75 Assert.assertEquals("80",publishFullAddressList.get(0).getPort());
78 Assert.fail("Exception" + e.getMessage());
83 public void testJsonToListBean_Fail(){
85 String resultJson="[\"domain\": \"wudith.openpalette.zte.com.cn\",\"port\": \"80\",\"publish_url\": \"/api/wudith/v1\",\"visualRange\": \"0\",\"publish_protocol\": \"http\"},"
86 + "{\"ip\": \"10.74.165.246\",\"port\": \"80\",\"publish_url\": \"/api/wudith/v1\",\"visualRange\": \"0\",\"publish_protocol\": \"http\"}]";
87 List<PublishFullAddress> publishFullAddressList =
88 JacksonJsonUtil.jsonToListBean(resultJson, new TypeReference<List<PublishFullAddress>>() {});
91 Assert.assertTrue(e instanceof Exception);