f4ff5eca0db6c49c8c5f70dbe974cb7f723ad79f
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / commands / AbstractRequestSuccessTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.access.commands;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertEquals;
12
13 import org.apache.commons.lang.SerializationUtils;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.access.ABIVersion;
17 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
20 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.MemberName;
22 import org.opendaylight.controller.cluster.access.concepts.RequestSuccess;
23
24 public abstract class AbstractRequestSuccessTest<T extends RequestSuccess<?, T>> {
25     private static final FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
26             MemberName.forName("test"), FrontendType.forName("one"));
27     protected static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
28     protected static final LocalHistoryIdentifier HISTORY_IDENTIFIER = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
29
30     private final @NonNull T object;
31     private final int expectedSize;
32     private final int legacySize;
33
34     protected AbstractRequestSuccessTest(final T object, final int expectedSize, final int legacySize) {
35         this.object = requireNonNull(object);
36         this.expectedSize = expectedSize;
37         this.legacySize = legacySize;
38     }
39
40     @Test
41     public void serializationTest() {
42         final var bytes = SerializationUtils.serialize(object);
43         assertEquals(legacySize, bytes.length);
44         assertEquals(expectedSize, SerializationUtils.serialize(object.toVersion(ABIVersion.CHLORINE_SR2)).length);
45
46         @SuppressWarnings("unchecked")
47         final var deserialize = (T) SerializationUtils.deserialize(bytes);
48
49         assertEquals(object.getTarget(), deserialize.getTarget());
50         assertEquals(object.getVersion(), deserialize.getVersion());
51         assertEquals(object.getSequence(), deserialize.getSequence());
52         doAdditionalAssertions(deserialize);
53     }
54
55     protected void doAdditionalAssertions(final T deserialize) {
56         // No-op by default
57     }
58 }