Migrate yang-data-codec-gson to JUnit5
[yangtools.git] / codec / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / Bug8083Test.java
index 4f6ceea2ec30182f57591cd3119bd107b133ec41..b393af197429701bd8ac370f1a75668c56ad98b5 100644 (file)
@@ -5,12 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.yang.data.codec.gson;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -23,9 +22,9 @@ import com.google.gson.stream.JsonWriter;
 import java.io.IOException;
 import java.io.StringReader;
 import java.net.URISyntaxException;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 import org.mockito.ArgumentCaptor;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -38,14 +37,12 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
-public class Bug8083Test {
+class Bug8083Test {
     private static final QNameModule FOOMOD = QNameModule.create(XMLNamespace.of("http://example.com/foomod"));
     private static final QNameModule BARMOD = QNameModule.create(XMLNamespace.of("http://example.com/barmod"));
 
@@ -132,8 +129,8 @@ public class Bug8083Test {
 
     private static EffectiveModelContext FULL_SCHEMA_CONTEXT;
 
-    @BeforeClass
-    public static void init() {
+    @BeforeAll
+    static void init() {
         FULL_SCHEMA_CONTEXT = YangParserTestUtils.parseYang("""
             module example-barmod {
               namespace "http://example.com/barmod";
@@ -166,27 +163,27 @@ public class Bug8083Test {
             }""", FOOBAR_YANG, ZAB_YANG);
     }
 
-    @AfterClass
-    public static void cleanup() {
+    @AfterAll
+    static void cleanup() {
         FULL_SCHEMA_CONTEXT = null;
     }
 
     @Test
-    public void testInstanceIdentifierSerializeNew() throws IOException {
+    void testInstanceIdentifierSerializeNew() throws IOException {
         assertEquals("/example-foomod:top/foo-list[name='key-value']/example-barmod:bar-container/bar-leaf",
             writeInstanceIdentifier(JSONCodecFactorySupplier.RFC7951));
     }
 
     @Test
-    public void testInstanceIdentifierSerializeOld() throws IOException {
+    void testInstanceIdentifierSerializeOld() throws IOException {
         assertEquals("/example-foomod:top/example-foomod:foo-list[example-foomod:name='key-value']"
                 + "/example-barmod:bar-container/example-barmod:bar-leaf",
             writeInstanceIdentifier(JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02));
     }
 
     @Test
-    public void testRFC7951InstanceIdentifierPath() throws IOException, URISyntaxException {
-        final String inputJson = loadTextFile("/bug8083/json/foo.json");
+    void testRFC7951InstanceIdentifierPath() throws IOException, URISyntaxException {
+        final var inputJson = loadTextFile("/bug8083/json/foo.json");
 
         // deserialization
         final var result = new NormalizationResultHolder();
@@ -196,17 +193,15 @@ public class Bug8083Test {
         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
         final var transformedInput = result.getResult().data();
 
-        assertTrue(transformedInput instanceof ContainerNode);
-        final var container = (ContainerNode) transformedInput;
-        final var child = container.childByArg(new NodeIdentifier(FOO_QNAME));
-        assertTrue(child instanceof LeafNode);
-        assertEquals(TEST_IID, child.body());
+        final var container = assertInstanceOf(ContainerNode.class, transformedInput);
+        assertEquals(TEST_IID,
+            assertInstanceOf(LeafNode.class, container.childByArg(new NodeIdentifier(FOO_QNAME))).body());
     }
 
     @Test
-    public void testInstanceIdentifierPathWithEmptyListKey() throws IOException, URISyntaxException {
+    void testInstanceIdentifierPathWithEmptyListKey() throws IOException, URISyntaxException {
         final var schemaContext = YangParserTestUtils.parseYang(BAZ_YANG);
-        final String inputJson = loadTextFile("/bug8083/json/baz.json");
+        final var inputJson = loadTextFile("/bug8083/json/baz.json");
 
         // deserialization
         final var result = new NormalizationResultHolder();
@@ -219,9 +214,9 @@ public class Bug8083Test {
     }
 
     @Test
-    public void testInstanceIdentifierPathWithIdentityrefListKey() throws IOException, URISyntaxException {
+    void testInstanceIdentifierPathWithIdentityrefListKey() throws IOException, URISyntaxException {
         final var schemaContext = YangParserTestUtils.parseYang(ZAB_YANG);
-        final String inputJson = loadTextFile("/bug8083/json/zab.json");
+        final var inputJson = loadTextFile("/bug8083/json/zab.json");
 
         // deserialization
         final var result = new NormalizationResultHolder();
@@ -234,9 +229,9 @@ public class Bug8083Test {
     }
 
     @Test
-    public void testInstanceIdentifierPathWithInstanceIdentifierListKey() throws IOException, URISyntaxException {
-        final EffectiveModelContext schemaContext = YangParserTestUtils.parseYang(FOOBAR_YANG);
-        final String inputJson = loadTextFile("/bug8083/json/foobar.json");
+    void testInstanceIdentifierPathWithInstanceIdentifierListKey() throws IOException, URISyntaxException {
+        final var schemaContext = YangParserTestUtils.parseYang(FOOBAR_YANG);
+        final var inputJson = loadTextFile("/bug8083/json/foobar.json");
 
         // deserialization
         final var result = new NormalizationResultHolder();
@@ -249,19 +244,15 @@ public class Bug8083Test {
     }
 
     private static JSONCodec<YangInstanceIdentifier> getCodec(final JSONCodecFactorySupplier supplier) {
-        final DataSchemaNode top = FULL_SCHEMA_CONTEXT.getDataChildByName(TOP_QNAME);
-        assertTrue(top instanceof ContainerSchemaNode);
-        final DataSchemaNode foo = ((ContainerSchemaNode) top).getDataChildByName(FOO_QNAME);
-        assertTrue(foo instanceof LeafSchemaNode);
-        final TypeDefinition<? extends TypeDefinition<?>> type = ((LeafSchemaNode) foo).getType();
-        assertTrue(type instanceof InstanceIdentifierTypeDefinition);
-        return supplier.createSimple(FULL_SCHEMA_CONTEXT)
-                .instanceIdentifierCodec((InstanceIdentifierTypeDefinition) type);
+        final var top = assertInstanceOf(ContainerSchemaNode.class, FULL_SCHEMA_CONTEXT.dataChildByName(TOP_QNAME));
+        final var foo = assertInstanceOf(LeafSchemaNode.class, top.dataChildByName(FOO_QNAME));
+        final var type = assertInstanceOf(InstanceIdentifierTypeDefinition.class, foo.getType());
+        return supplier.createSimple(FULL_SCHEMA_CONTEXT).instanceIdentifierCodec(type);
     }
 
     private static String writeInstanceIdentifier(final JSONCodecFactorySupplier supplier) throws IOException {
-        final JsonWriter writer = mock(JsonWriter.class);
-        ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
+        final var writer = mock(JsonWriter.class);
+        final var captor = ArgumentCaptor.forClass(String.class);
         doReturn(writer).when(writer).value(captor.capture());
 
         getCodec(supplier).writeValue(writer, TEST_IID);