Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.serializers; |
| 2 | |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 4 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 5 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 6 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 7 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 8 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 9 | import org.junit.After; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.BeforeClass; |
| 12 | import org.junit.Test; |
| 13 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 14 | import org.onlab.onos.mastership.MastershipTerm; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.Annotations; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.ConnectPoint; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 17 | import org.onlab.onos.net.DefaultAnnotations; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.DefaultDevice; |
| 19 | import org.onlab.onos.net.DefaultLink; |
| 20 | import org.onlab.onos.net.DefaultPort; |
| 21 | import org.onlab.onos.net.Device; |
| 22 | import org.onlab.onos.net.DeviceId; |
| 23 | import org.onlab.onos.net.Link; |
| 24 | import org.onlab.onos.net.LinkKey; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.MastershipRole; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.PortNumber; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.SparseAnnotations; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.provider.ProviderId; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
| 31 | import org.onlab.util.KryoPool; |
| 32 | |
| 33 | import com.google.common.collect.ImmutableMap; |
| 34 | import com.google.common.collect.ImmutableSet; |
| 35 | import com.google.common.testing.EqualsTester; |
| 36 | |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 37 | public class KryoSerializerTest { |
| 38 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 39 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 40 | private static final ProviderId PIDA = new ProviderId("of", "foo", true); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 41 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 42 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 43 | private static final PortNumber P1 = portNumber(1); |
| 44 | private static final PortNumber P2 = portNumber(2); |
| 45 | private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1); |
| 46 | private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2); |
| 47 | private static final String MFR = "whitebox"; |
| 48 | private static final String HW = "1.1.x"; |
| 49 | private static final String SW1 = "3.8.1"; |
| 50 | private static final String SW2 = "3.9.5"; |
| 51 | private static final String SN = "43311-12345"; |
| 52 | private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 53 | private static final SparseAnnotations A1 = DefaultAnnotations.builder() |
| 54 | .set("A1", "a1") |
| 55 | .set("B1", "b1") |
| 56 | .build(); |
| 57 | private static final SparseAnnotations A1_2 = DefaultAnnotations.builder() |
| 58 | .remove("A1") |
| 59 | .set("B3", "b3") |
| 60 | .build(); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 61 | |
| 62 | private static KryoPool kryos; |
| 63 | |
| 64 | @BeforeClass |
| 65 | public static void setUpBeforeClass() throws Exception { |
| 66 | kryos = KryoPool.newBuilder() |
Yuta HIGUCHI | 03fec1f | 2014-10-03 09:13:50 -0700 | [diff] [blame] | 67 | .register(KryoPoolUtil.API) |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 68 | .register(ImmutableMap.class, new ImmutableMapSerializer()) |
| 69 | .register(ImmutableSet.class, new ImmutableSetSerializer()) |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 70 | .build(); |
| 71 | } |
| 72 | |
| 73 | @Before |
| 74 | public void setUp() throws Exception { |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() throws Exception { |
| 79 | // removing Kryo instance to use fresh Kryo on each tests |
| 80 | kryos.getKryo(); |
| 81 | } |
| 82 | |
| 83 | private static <T> void testSerialized(T original) { |
| 84 | ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); |
| 85 | kryos.serialize(original, buffer); |
| 86 | buffer.flip(); |
| 87 | T copy = kryos.deserialize(buffer); |
| 88 | |
| 89 | new EqualsTester() |
| 90 | .addEqualityGroup(original, copy) |
| 91 | .testEquals(); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | @Test |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 96 | public final void testSerialization() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 97 | testSerialized(new ConnectPoint(DID1, P1)); |
| 98 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); |
| 99 | testSerialized(new DefaultPort(DEV1, P1, true)); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 100 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1)); |
| 101 | testSerialized(new DefaultPort(DEV1, P1, true, A1_2)); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 102 | testSerialized(DID1); |
| 103 | testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); |
| 104 | testSerialized(ImmutableMap.of(DID1, DEV1)); |
| 105 | testSerialized(ImmutableMap.of()); |
| 106 | testSerialized(ImmutableSet.of(DID1, DID2)); |
| 107 | testSerialized(ImmutableSet.of(DID1)); |
| 108 | testSerialized(ImmutableSet.of()); |
| 109 | testSerialized(IpPrefix.valueOf("192.168.0.1/24")); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 110 | testSerialized(IpAddress.valueOf("192.168.0.1")); |
Yuta HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 111 | testSerialized(LinkKey.linkKey(CP1, CP2)); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 112 | testSerialized(new NodeId("SomeNodeIdentifier")); |
| 113 | testSerialized(P1); |
| 114 | testSerialized(PID); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 115 | testSerialized(PIDA); |
| 116 | testSerialized(new NodeId("bar")); |
| 117 | testSerialized(MastershipTerm.of(new NodeId("foo"), 2)); |
| 118 | for (MastershipRole role : MastershipRole.values()) { |
| 119 | testSerialized(role); |
| 120 | } |
| 121 | } |
| 122 | |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 123 | @Test |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 124 | public final void testAnnotations() { |
| 125 | // Annotations does not have equals defined, manually test equality |
| 126 | final byte[] a1Bytes = kryos.serialize(A1); |
| 127 | SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes); |
| 128 | assertAnnotationsEquals(copiedA1, A1); |
| 129 | |
| 130 | final byte[] a12Bytes = kryos.serialize(A1_2); |
| 131 | SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes); |
| 132 | assertAnnotationsEquals(copiedA12, A1_2); |
| 133 | } |
| 134 | |
| 135 | // code clone |
| 136 | public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 137 | SparseAnnotations expected = DefaultAnnotations.builder().build(); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 138 | for (SparseAnnotations a : annotations) { |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 139 | expected = DefaultAnnotations.union(expected, a); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 140 | } |
| 141 | assertEquals(expected.keys(), actual.keys()); |
| 142 | for (String key : expected.keys()) { |
| 143 | assertEquals(expected.value(key), actual.value(key)); |
| 144 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } |