2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
15 package org.onap.msb.sdk.discovery.util;
17 import com.fasterxml.jackson.core.type.TypeReference;
18 import java.util.List;
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;
27 public class JacksonJsonUtilTest {
29 public void testBeanToJson() {
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());
40 public void testJsonToBean() {
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());
52 public void testJsonToBean_fail() {
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);