Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.serializers; |
| 2 | |
| 3 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 4 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 5 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 6 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 7 | import org.junit.After; |
| 8 | import org.junit.Before; |
| 9 | import org.junit.BeforeClass; |
| 10 | import org.junit.Test; |
| 11 | import org.onlab.onos.cluster.NodeId; |
| 12 | import org.onlab.onos.net.ConnectPoint; |
| 13 | import org.onlab.onos.net.DefaultDevice; |
| 14 | import org.onlab.onos.net.DefaultLink; |
| 15 | import org.onlab.onos.net.DefaultPort; |
| 16 | import org.onlab.onos.net.Device; |
| 17 | import org.onlab.onos.net.DeviceId; |
| 18 | import org.onlab.onos.net.Link; |
| 19 | import org.onlab.onos.net.LinkKey; |
| 20 | import org.onlab.onos.net.PortNumber; |
| 21 | import org.onlab.onos.net.provider.ProviderId; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.util.KryoPool; |
| 24 | |
| 25 | import com.google.common.collect.ImmutableMap; |
| 26 | import com.google.common.collect.ImmutableSet; |
| 27 | import com.google.common.testing.EqualsTester; |
| 28 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 29 | public class KryoSerializerTests { |
| 30 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 31 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 32 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 33 | private static final PortNumber P1 = portNumber(1); |
| 34 | private static final PortNumber P2 = portNumber(2); |
| 35 | private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1); |
| 36 | private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2); |
| 37 | private static final String MFR = "whitebox"; |
| 38 | private static final String HW = "1.1.x"; |
| 39 | private static final String SW1 = "3.8.1"; |
| 40 | private static final String SW2 = "3.9.5"; |
| 41 | private static final String SN = "43311-12345"; |
| 42 | private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN); |
| 43 | |
| 44 | private static KryoPool kryos; |
| 45 | |
| 46 | @BeforeClass |
| 47 | public static void setUpBeforeClass() throws Exception { |
| 48 | kryos = KryoPool.newBuilder() |
Yuta HIGUCHI | 03fec1f | 2014-10-03 09:13:50 -0700 | [diff] [blame^] | 49 | .register(KryoPoolUtil.API) |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 50 | .register(ImmutableMap.class, new ImmutableMapSerializer()) |
| 51 | .register(ImmutableSet.class, new ImmutableSetSerializer()) |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 52 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 53 | |
Ayaka Koshibe | 8441136 | 2014-10-01 09:33:42 -0700 | [diff] [blame] | 54 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 55 | .build(); |
| 56 | } |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() throws Exception { |
| 60 | } |
| 61 | |
| 62 | @After |
| 63 | public void tearDown() throws Exception { |
| 64 | // removing Kryo instance to use fresh Kryo on each tests |
| 65 | kryos.getKryo(); |
| 66 | } |
| 67 | |
| 68 | private static <T> void testSerialized(T original) { |
| 69 | ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); |
| 70 | kryos.serialize(original, buffer); |
| 71 | buffer.flip(); |
| 72 | T copy = kryos.deserialize(buffer); |
| 73 | |
| 74 | new EqualsTester() |
| 75 | .addEqualityGroup(original, copy) |
| 76 | .testEquals(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | @Test |
| 81 | public final void test() { |
| 82 | testSerialized(new ConnectPoint(DID1, P1)); |
| 83 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); |
| 84 | testSerialized(new DefaultPort(DEV1, P1, true)); |
| 85 | testSerialized(DID1); |
| 86 | testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); |
| 87 | testSerialized(ImmutableMap.of(DID1, DEV1)); |
| 88 | testSerialized(ImmutableMap.of()); |
| 89 | testSerialized(ImmutableSet.of(DID1, DID2)); |
| 90 | testSerialized(ImmutableSet.of(DID1)); |
| 91 | testSerialized(ImmutableSet.of()); |
| 92 | testSerialized(IpPrefix.valueOf("192.168.0.1/24")); |
| 93 | testSerialized(new LinkKey(CP1, CP2)); |
| 94 | testSerialized(new NodeId("SomeNodeIdentifier")); |
| 95 | testSerialized(P1); |
| 96 | testSerialized(PID); |
| 97 | } |
| 98 | |
| 99 | } |