Modernize AbstractRequestTest
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / ABIVersionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.controller.cluster.access.ABIVersion.BORON;
14 import static org.opendaylight.controller.cluster.access.ABIVersion.TEST_FUTURE_VERSION;
15 import static org.opendaylight.controller.cluster.access.ABIVersion.TEST_PAST_VERSION;
16
17 import com.google.common.io.ByteArrayDataOutput;
18 import com.google.common.io.ByteStreams;
19 import java.io.IOException;
20 import org.junit.Test;
21
22 public class ABIVersionTest {
23     @Test
24     public void testInvalidVersions() {
25         assertTrue(TEST_PAST_VERSION.compareTo(TEST_FUTURE_VERSION) < 0);
26         assertTrue(TEST_PAST_VERSION.compareTo(BORON) < 0);
27         assertTrue(TEST_FUTURE_VERSION.compareTo(BORON) > 0);
28     }
29
30     @Test
31     public void testBoronVersion() throws Exception {
32         assertEquals((short)5, BORON.shortValue());
33         assertEquals(BORON, ABIVersion.valueOf(BORON.shortValue()));
34         assertEquals(BORON, ABIVersion.readFrom(ByteStreams.newDataInput(writeVersion(BORON))));
35     }
36
37     @Test
38     public void testInvalidPastVersion() {
39         assertThrows(PastVersionException.class, () -> ABIVersion.valueOf(TEST_PAST_VERSION.shortValue()));
40     }
41
42     @Test
43     public void testInvalidFutureVersion() {
44         assertThrows(FutureVersionException.class, () -> ABIVersion.valueOf(TEST_FUTURE_VERSION.shortValue()));
45     }
46
47     private static byte[] writeVersion(final ABIVersion version) {
48         final ByteArrayDataOutput bado = ByteStreams.newDataOutput();
49         bado.writeShort(version.shortValue());
50         return bado.toByteArray();
51     }
52
53     @Test
54     public void testBadRead() {
55         final var in = ByteStreams.newDataInput(writeVersion(TEST_PAST_VERSION));
56         assertThrows(IOException.class, () -> ABIVersion.readFrom(in));
57     }
58 }