HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.codec.impl; |
| 18 | |
| 19 | import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | import static org.hamcrest.Matchers.is; |
| 21 | import static org.hamcrest.Matchers.notNullValue; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.onosproject.net.DeviceId.deviceId; |
| 24 | |
| 25 | import org.onlab.packet.ChassisId; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.SparseAnnotations; |
| 33 | import org.onosproject.net.provider.ProviderId; |
| 34 | |
| 35 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 36 | |
| 37 | /** |
| 38 | * JsonCodec test utilities. |
| 39 | */ |
| 40 | public 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 | } |