50% Code Coverage-MSB Java SDK
[msb/java-sdk.git] / src / test / java / org / onap / msb / sdk / discovery / util / JacksonJsonUtilTest.java
1 /**
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14
15 package org.onap.msb.sdk.discovery.util;
16
17 import com.fasterxml.jackson.core.type.TypeReference;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.onap.msb.sdk.discovery.common.RouteException;
24 import org.onap.msb.sdk.discovery.entity.NodeAddress;
25
26
27 public class JacksonJsonUtilTest {
28     @Test
29     public void testBeanToJson() {
30         try {
31             NodeAddress address = new NodeAddress("127.0.0.1", "80");
32             String json = JacksonJsonUtil.beanToJson(address);
33             Assert.assertEquals("{\"ip\":\"127.0.0.1\",\"port\":\"80\"}", json);
34         } catch (Exception e) {
35             Assert.fail("Exception" + e.getMessage());
36         }
37     }
38
39     @Test
40     public void testJsonToBean() {
41         try {
42             String json = "{\"ip\":\"127.0.0.1\",\"port\":\"80\"}";
43             NodeAddress address = (NodeAddress) JacksonJsonUtil.jsonToBean(json, NodeAddress.class);
44             Assert.assertEquals("127.0.0.1", address.getIp());
45             Assert.assertEquals("80", address.getPort());
46         } catch (Exception e) {
47             Assert.fail("Exception" + e.getMessage());
48         }
49     }
50     
51     @Test
52     public void testJsonToBean_fail() {
53         try {
54             String json = "{\"ip\":\"127.0.0.1,\"port\":\"80\"}";
55             NodeAddress address = (NodeAddress) JacksonJsonUtil.jsonToBean(json, NodeAddress.class);
56             Assert.assertEquals("127.0.0.1", address.getIp());
57         } catch (Exception e) {
58                 Assert.assertTrue(e instanceof RouteException);
59         }
60     }
61 }