blob: 379249dc0202ba15c6ee2dcbec45ef86d4dc86ea [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070017
Thomas Vachuskad16ce182014-10-29 17:25:29 -070018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.testing.EqualsTester;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080022
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070023import org.junit.After;
24import org.junit.Before;
25import org.junit.BeforeClass;
26import org.junit.Test;
Marc De Leenheerbb382352015-04-23 18:20:34 -070027import org.onlab.util.Frequency;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.cluster.NodeId;
29import org.onosproject.cluster.RoleInfo;
30import org.onosproject.core.DefaultGroupId;
31import org.onosproject.mastership.MastershipTerm;
32import org.onosproject.net.Annotations;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DefaultAnnotations;
35import org.onosproject.net.DefaultDevice;
36import org.onosproject.net.DefaultLink;
37import org.onosproject.net.DefaultPort;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.HostLocation;
41import org.onosproject.net.Link;
42import org.onosproject.net.Link.Type;
43import org.onosproject.net.LinkKey;
Marc De Leenheerbb382352015-04-23 18:20:34 -070044import org.onosproject.net.OchPort;
45import org.onosproject.net.OduCltPort;
46import org.onosproject.net.OmsPort;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.PortNumber;
48import org.onosproject.net.SparseAnnotations;
Sho SHIMIZU166e61e2015-01-21 18:07:12 -080049import org.onosproject.net.flow.DefaultFlowRule;
50import org.onosproject.net.flow.DefaultTrafficSelector;
51import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.flow.FlowId;
Sho SHIMIZU166e61e2015-01-21 18:07:12 -080053import org.onosproject.net.flow.FlowRule;
54import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080055import org.onosproject.net.intent.IntentId;
56import org.onosproject.net.provider.ProviderId;
57import org.onosproject.net.resource.Bandwidth;
58import org.onosproject.net.resource.BandwidthResourceAllocation;
59import org.onosproject.net.resource.DefaultLinkResourceAllocations;
60import org.onosproject.net.resource.DefaultLinkResourceRequest;
61import org.onosproject.net.resource.Lambda;
62import org.onosproject.net.resource.LambdaResourceAllocation;
63import org.onosproject.net.resource.LinkResourceRequest;
64import org.onosproject.net.resource.ResourceAllocation;
65import org.onosproject.net.intent.constraint.AnnotationConstraint;
66import org.onosproject.net.intent.constraint.BandwidthConstraint;
67import org.onosproject.net.intent.constraint.LambdaConstraint;
68import org.onosproject.net.intent.constraint.LatencyConstraint;
69import org.onosproject.net.intent.constraint.LinkTypeConstraint;
70import org.onosproject.net.intent.constraint.ObstacleConstraint;
71import org.onosproject.net.intent.constraint.WaypointConstraint;
alshabib7911a052014-10-16 17:49:37 -070072import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070073import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080074import org.onlab.packet.Ip4Address;
75import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070076import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080077import org.onlab.packet.Ip4Prefix;
78import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070079import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070080import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070081
Thomas Vachuskad16ce182014-10-29 17:25:29 -070082import java.nio.ByteBuffer;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080083import java.util.Arrays;
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -080084import java.util.Collections;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080085import java.util.HashMap;
86import java.util.Map;
87import java.util.Set;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080088import java.time.Duration;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070089
90import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080091import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080092import static org.onosproject.net.DeviceId.deviceId;
93import static org.onosproject.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070094
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070095public class KryoSerializerTest {
96
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070097 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070098 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070099 private static final DeviceId DID1 = deviceId("of:foo");
100 private static final DeviceId DID2 = deviceId("of:bar");
101 private static final PortNumber P1 = portNumber(1);
102 private static final PortNumber P2 = portNumber(2);
103 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
104 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
105 private static final String MFR = "whitebox";
106 private static final String HW = "1.1.x";
107 private static final String SW1 = "3.8.1";
108 private static final String SW2 = "3.9.5";
109 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -0700110 private static final ChassisId CID = new ChassisId();
111 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
112 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700113 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
114 .set("A1", "a1")
115 .set("B1", "b1")
116 .build();
117 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
118 .remove("A1")
119 .set("B3", "b3")
120 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700121
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700122 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700123
124 @BeforeClass
125 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700126 }
127
128 @Before
129 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700130 serializer = new KryoSerializer() {
131
132 @Override
133 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700134 serializerPool = KryoNamespace.newBuilder()
135 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800136 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
137 .build();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700138 }
139 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700140 }
141
142 @After
143 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700144 }
145
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800146 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700147 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700148 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700149 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700150 T copy = serializer.decode(buffer);
151
152 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700153
154 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700155 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700156 .testEquals();
157 }
158
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800159 private <T> void testSerializable(T original) {
160 byte[] bs = serializer.encode(original);
161 T copy = serializer.decode(bs);
162 assertNotNull(copy);
163 }
164
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700165
166 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700167 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800168 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700169 }
170
171 @Test
172 public void testDefaultLink() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800173 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
174 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700175 }
176
177 @Test
178 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800179 testSerializedEquals(new DefaultPort(DEV1, P1, true));
180 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700181 }
182
183 @Test
Marc De Leenheerbb382352015-04-23 18:20:34 -0700184 public void testOmsPort() {
185 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
186 Frequency.ofGHz(100)));
187 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
188 Frequency.ofGHz(100), A1_2));
189 }
190
191 @Test
192 public void testOchPort() {
193 testSerializedEquals(new OchPort(DEV1, P1, true, OchPort.SignalType.ODU0, false, OchPort.GridType.DWDM,
194 OchPort.ChannelSpacing.CHL_100GHZ, -8, 4));
195 testSerializedEquals(new OchPort(DEV1, P1, true, OchPort.SignalType.ODU0, false, OchPort.GridType.DWDM,
196 OchPort.ChannelSpacing.CHL_100GHZ, -8, 4, A1_2));
197 }
198
199 @Test
200 public void testOduCltPort() {
201 testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE));
202 testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE, A1_2));
203 }
204
205 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700206 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800207 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700208 }
209
210 @Test
211 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800212 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
213 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
214 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700215 }
216
217 @Test
218 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800219 testSerializedEquals(ImmutableSet.of(DID1, DID2));
220 testSerializedEquals(ImmutableSet.of(DID1));
221 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700222 }
223
224 @Test
225 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800226 testSerializedEquals(ImmutableList.of(DID1, DID2));
227 testSerializedEquals(ImmutableList.of(DID1));
228 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700229 }
230
231 @Test
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800232 public void testFlowRuleBatchEntry() {
233 final FlowRule rule1 =
Brian O'Connor6b528132015-03-10 16:39:52 -0700234 new DefaultFlowRule(DID1, DefaultTrafficSelector.emptySelector(),
235 DefaultTrafficTreatment.emptyTreatment(), 0, 0, 0, true);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800236 final FlowRuleBatchEntry entry1 =
237 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
238 final FlowRuleBatchEntry entry2 =
Sho SHIMIZU7446da92015-01-22 16:27:06 -0800239 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800240
Sho SHIMIZU7446da92015-01-22 16:27:06 -0800241 testSerializedEquals(entry1);
242 testSerializedEquals(entry2);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800243 }
244
245 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700246 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800247 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700248 }
249
250 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800251 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800252 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800253 }
254
255 @Test
256 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800257 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800258 }
259
260 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700261 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800262 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700263 }
264
265 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800266 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800267 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800268 }
269
270 @Test
271 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800272 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800273 }
274
275 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700276 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800277 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700278 }
279
280 @Test
281 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800282 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700283 }
284
285 @Test
286 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800287 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700288 }
289
290 @Test
291 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800292 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700293 }
294
295 @Test
296 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800297 testSerializedEquals(PID);
298 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700299 }
300
301 @Test
302 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800303 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
304 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700305 }
306
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700307 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700308 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800309 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700310 }
311
312 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700313 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800314 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700315 }
316
317 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700318 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800319 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700320 asList(new NodeId("stby1"), new NodeId("stby2"))));
321 }
322
323 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800324 public void testDefaultLinkResourceRequest() {
325 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
326 .addLambdaRequest()
327 .addBandwidthRequest(32.195)
328 .build()
329 );
330 }
331
332 @Test
333 public void testDefaultLinkResourceAllocations() {
334 LinkResourceRequest request = DefaultLinkResourceRequest
335 .builder(IntentId.valueOf(2501), ImmutableList.of())
336 .addLambdaRequest()
337 .addBandwidthRequest(32.195)
338 .build();
339 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
340 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800341 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.bps(10.0)),
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800342 new LambdaResourceAllocation(Lambda.valueOf(1))));
343 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
344 }
345
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800346 @Test
347 public void testLambdaConstraint() {
348 testSerializable(new LambdaConstraint(Lambda.valueOf(1)));
349 }
350
351 @Test
352 public void testBandwidthConstraint() {
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800353 testSerializable(new BandwidthConstraint(Bandwidth.bps(1000.0)));
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800354 }
355
356 @Test
357 public void testLinkTypeConstraint() {
358 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
359 }
360
361 @Test
362 public void testLatencyConstraint() {
363 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
364 }
365
366 @Test
367 public void testWaypointConstraint() {
368 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
369 }
370
371 @Test
372 public void testObstacleConstraint() {
373 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
374 }
375
376 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800377 public void testArraysAsList() {
378 testSerializedEquals(Arrays.asList(1, 2, 3));
379 }
380
381 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800382 public void testAnnotationConstraint() {
383 testSerializable(new AnnotationConstraint("distance", 100.0));
384 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800385
386 @Test
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800387 public void testDefaultGroupId() {
388 testSerializedEquals(new DefaultGroupId(99));
389 }
390
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -0800391 @Test
392 public void testEmptySet() {
393 testSerializedEquals(Collections.emptySet());
394 }
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800395
396 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700397 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700398 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700399 final byte[] a1Bytes = serializer.encode(A1);
400 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700401 assertAnnotationsEquals(copiedA1, A1);
402
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700403 final byte[] a12Bytes = serializer.encode(A1_2);
404 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700405 assertAnnotationsEquals(copiedA12, A1_2);
406 }
407
408 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700409 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700410 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700411 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700412 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700413 }
414 assertEquals(expected.keys(), actual.keys());
415 for (String key : expected.keys()) {
416 assertEquals(expected.value(key), actual.value(key));
417 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700418 }
419
420}