Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / concepts / AbstractEnvelopeTest.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.concepts;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12
13 import org.apache.commons.lang3.SerializationUtils;
14 import org.junit.Before;
15 import org.junit.Test;
16
17 public abstract class AbstractEnvelopeTest<E extends Envelope<?>> {
18     protected record EnvelopeDetails<E extends Envelope<?>>(E envelope, int expectedSize) {
19         // Nothing else
20     }
21
22     private static final FrontendIdentifier FRONTEND =
23             new FrontendIdentifier(MemberName.forName("test"), FrontendIdentifierTest.ONE_FRONTEND_TYPE);
24     private static final ClientIdentifier CLIENT = new ClientIdentifier(FRONTEND, 0);
25     private static final LocalHistoryIdentifier HISTORY = new LocalHistoryIdentifier(CLIENT, 0);
26     protected static final TransactionIdentifier OBJECT = new TransactionIdentifier(HISTORY, 0);
27
28     private E envelope;
29     private int expectedSize;
30
31     @Before
32     public void setUp() throws Exception {
33         final var details = createEnvelope();
34         envelope = requireNonNull(details.envelope);
35         expectedSize = details.expectedSize;
36     }
37
38     @Test
39     public void testProxySerializationDeserialization() {
40         final byte[] serializedBytes = SerializationUtils.serialize(envelope);
41         assertEquals(expectedSize, serializedBytes.length);
42         @SuppressWarnings("unchecked")
43         final E deserialize = (E) SerializationUtils.deserialize(serializedBytes);
44         checkDeserialized(deserialize);
45     }
46
47     private void checkDeserialized(final E deserializedEnvelope) {
48         assertEquals(envelope.getSessionId(), deserializedEnvelope.getSessionId());
49         assertEquals(envelope.getTxSequence(), deserializedEnvelope.getTxSequence());
50         final var expectedMessage = envelope.getMessage();
51         final var actualMessage = deserializedEnvelope.getMessage();
52         assertEquals(expectedMessage.getSequence(), actualMessage.getSequence());
53         assertEquals(expectedMessage.getTarget(), actualMessage.getTarget());
54         assertEquals(expectedMessage.getVersion(), actualMessage.getVersion());
55         assertEquals(expectedMessage.getClass(), actualMessage.getClass());
56         doAdditionalAssertions(envelope, deserializedEnvelope);
57     }
58
59     protected abstract EnvelopeDetails<E> createEnvelope();
60
61     protected abstract void doAdditionalAssertions(E envelope, E resolvedObject);
62 }