blob: 67c2f47feccb46e299908afc022056bc57f2d12c [file] [log] [blame]
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
17package org.onosproject.codec.impl;
18
19import static org.hamcrest.MatcherAssert.assertThat;
20import static org.hamcrest.Matchers.is;
21import static org.hamcrest.Matchers.notNullValue;
22import static org.junit.Assert.assertEquals;
23import static org.onosproject.net.DeviceId.deviceId;
24
25import org.onlab.packet.ChassisId;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.SparseAnnotations;
33import org.onosproject.net.provider.ProviderId;
34
35import com.fasterxml.jackson.databind.node.ObjectNode;
36
37/**
38 * JsonCodec test utilities.
39 */
40public abstract class JsonCodecUtils {
41
42 /**
43 * Checks if given Object can be encoded to JSON and back.
44 *
45 * @param context CodecContext
46 * @param codec JsonCodec
47 * @param pojoIn Java Object to encode.
48 * Object is expected to have #equals implemented.
49 */
50 public static <T> void assertJsonEncodable(final CodecContext context,
51 final JsonCodec<T> codec,
52 final T pojoIn) {
53 final ObjectNode json = codec.encode(pojoIn, context);
54
55 assertThat(json, is(notNullValue()));
56
57 final T pojoOut = codec.decode(json, context);
58 assertThat(pojoOut, is(notNullValue()));
59
60 assertEquals(pojoIn, pojoOut);
61 }
62
63 static final ProviderId PID = new ProviderId("of", "foo");
64 static final ProviderId PIDA = new ProviderId("of", "bar", true);
65 static final DeviceId DID1 = deviceId("of:foo");
66 static final DeviceId DID2 = deviceId("of:bar");
67 static final String MFR = "whitebox";
68 static final String HW = "1.1.x";
69 static final String SW1 = "3.8.1";
70 static final String SW2 = "3.9.5";
71 static final String SN = "43311-12345";
72 static final ChassisId CID = new ChassisId();
73 static final PortNumber P1 = PortNumber.portNumber(1);
74 static final PortNumber P2 = PortNumber.portNumber(2);
75 static final PortNumber P3 = PortNumber.portNumber(3);
76 static final SparseAnnotations A1 = DefaultAnnotations.builder()
77 .set("A1", "a1")
78 .set("B1", "b1")
79 .build();
80 static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
81 static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
82
83}