blob: fd05aac246f5ae5a6c91488221c80e22bff30e0c [file] [log] [blame]
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07001package org.onlab.onos.store.serializers;
2
3import static org.onlab.onos.net.DeviceId.deviceId;
4import static org.onlab.onos.net.PortNumber.portNumber;
5
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07006import java.nio.ByteBuffer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07007import org.junit.After;
8import org.junit.Before;
9import org.junit.BeforeClass;
10import org.junit.Test;
11import org.onlab.onos.cluster.NodeId;
12import org.onlab.onos.net.ConnectPoint;
13import org.onlab.onos.net.DefaultDevice;
14import org.onlab.onos.net.DefaultLink;
15import org.onlab.onos.net.DefaultPort;
16import org.onlab.onos.net.Device;
17import org.onlab.onos.net.DeviceId;
18import org.onlab.onos.net.Link;
19import org.onlab.onos.net.LinkKey;
20import org.onlab.onos.net.PortNumber;
21import org.onlab.onos.net.provider.ProviderId;
22import org.onlab.packet.IpPrefix;
23import org.onlab.util.KryoPool;
24
25import com.google.common.collect.ImmutableMap;
26import com.google.common.collect.ImmutableSet;
27import com.google.common.testing.EqualsTester;
28
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070029public 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 HIGUCHI03fec1f2014-10-03 09:13:50 -070049 .register(KryoPoolUtil.API)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070050 .register(ImmutableMap.class, new ImmutableMapSerializer())
51 .register(ImmutableSet.class, new ImmutableSetSerializer())
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070052
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070053
Ayaka Koshibe84411362014-10-01 09:33:42 -070054
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070055 .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}