Migrate mdsal-common-api to JUnit5
[mdsal.git] / common / mdsal-common-api / src / test / java / org / opendaylight / mdsal / common / api / LogicalDatastoreTypeTest.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.mdsal.common.api;
9
10 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.util.stream.Stream;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.params.ParameterizedTest;
23 import org.junit.jupiter.params.provider.MethodSource;
24
25 class LogicalDatastoreTypeTest {
26     @ParameterizedTest
27     @MethodSource
28     void serialization(final LogicalDatastoreType type, final byte[] expected) throws Exception {
29         final var bout = new ByteArrayOutputStream();
30         try (var out = new DataOutputStream(bout)) {
31             type.writeTo(out);
32         }
33         assertArrayEquals(expected, bout.toByteArray());
34
35         try (var in = new DataInputStream(new ByteArrayInputStream(expected))) {
36             assertSame(type, LogicalDatastoreType.readFrom(in));
37         }
38     }
39
40     static Stream<Object[]> serialization() {
41         return Stream.of(
42             new Object[] { LogicalDatastoreType.OPERATIONAL, new byte[] { 1 }},
43             new Object[] { LogicalDatastoreType.CONFIGURATION, new byte[] { 2 }});
44     }
45
46     @Test
47     void invalidSerialization() throws Exception {
48         try (var in = new DataInputStream(new ByteArrayInputStream(new byte[] { 0 }))) {
49             final var ex = assertThrows(IOException.class, () -> LogicalDatastoreType.readFrom(in));
50             assertEquals("Unknown type 0", ex.getMessage());
51         }
52     }
53
54 }