Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / test / java / org / onap / msb / apiroute / wrapper / util / JacksonJsonUtilTest.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
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 package org.onap.msb.apiroute.wrapper.util;
17
18 import java.util.List;
19
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;
25
26 import com.fasterxml.jackson.core.type.TypeReference;
27
28
29 public class JacksonJsonUtilTest {
30     @Test
31     public void testBeanToJson(){
32         try{
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);
36         }
37         catch(Exception e){
38             Assert.fail("Exception" + e.getMessage());
39         }
40     }
41     
42     @Test
43     public void testJsonToBean(){
44         try{
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());
49         }
50         catch(Exception e){
51             Assert.fail("Exception" + e.getMessage());
52         }
53     }
54     
55     
56 //    @Test
57 //    public void testJsonToBean_Fail(){
58 //        try{
59 //            String json="{\"ip\":\"127.0.0.1\",\"port\":\"80\",\"weight\":0";
60 //            RouteServer server=(RouteServer) JacksonJsonUtil.jsonToBean(json, RouteServer.class);            
61 //        }
62 //        catch(Exception e){
63 //          Assert.assertEquals("class org.onap.msb.apiroute.api.RouteServer JsonTobean faild",e.getMessage());
64 //        }
65 //    }
66     
67     @Test
68     public void testJsonToListBean(){
69       try{
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());
76       }
77       catch(Exception e){
78           Assert.fail("Exception" + e.getMessage());
79       }
80     }
81     
82     @Test
83     public void testJsonToListBean_Fail(){
84       try{
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>>() {});
89       }
90       catch(Exception e){
91         Assert.assertTrue(e instanceof Exception);
92       }
93     }
94 }