Migrate restconf/restconf-nb tests to JUnit5
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractResourceBodyTest.java
index 1568e8e18bf3312be9f7f2db78e4f993faa45356..65f0ede627919ef81de7811306e3ee6eac010492 100644 (file)
@@ -7,37 +7,40 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.databind;
 
-import static java.util.Objects.requireNonNull;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
+import com.google.common.collect.ImmutableMap;
 import java.io.IOException;
 import java.io.InputStream;
 import java.text.ParseException;
-import java.util.Optional;
 import java.util.function.Function;
 import org.eclipse.jdt.annotation.NonNull;
-import org.junit.BeforeClass;
-import org.junit.function.ThrowingRunnable;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.function.Executable;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
-import org.opendaylight.mdsal.dom.api.DOMSchemaService;
-import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
 import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
+import org.opendaylight.restconf.common.errors.RestconfError;
+import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
+import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy.StrategyAndPath;
+import org.opendaylight.restconf.server.api.DatabindContext;
+import org.opendaylight.restconf.server.api.ResourceBody;
+import org.opendaylight.restconf.server.api.ServerException;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
+@ExtendWith(MockitoExtension.class)
 abstract class AbstractResourceBodyTest extends AbstractBodyTest {
     static final NodeIdentifier CONT_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
     static final NodeIdentifier CONT1_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont1"));
@@ -52,42 +55,31 @@ abstract class AbstractResourceBodyTest extends AbstractBodyTest {
     static final QName LF11 = QName.create("augment:module:leaf:list", "2014-01-27", "lf11");
     static final QName LFLST11 = QName.create(LF11, "lflst11");
 
-    static EffectiveModelContext MODEL_CONTEXT;
+    private static DatabindContext DATABIND;
+
+    @Mock
+    DOMDataBroker dataBroker;
+    @Mock
+    DOMMountPointService mountPointService;
+    @Mock
+    DOMMountPoint mountPoint;
 
     private final Function<InputStream, ResourceBody> bodyConstructor;
-    private final DOMMountPointService mountPointService;
-    private final DOMMountPoint mountPoint;
 
     AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
-        this.bodyConstructor = requireNonNull(bodyConstructor);
-
-        mountPointService = mock(DOMMountPointService.class);
-        mountPoint = mock(DOMMountPoint.class);
-        doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
-        doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
-            .getService(DOMSchemaService.class);
-
+        assertNotNull(bodyConstructor);
+        this.bodyConstructor = bodyConstructor;
     }
 
-    @BeforeClass
-    public static final void initModelContext() throws Exception {
+    @BeforeAll
+    static final void initModelContext() throws Exception {
         final var testFiles = loadFiles("/instanceidentifier/yang");
         testFiles.addAll(loadFiles("/modules"));
         testFiles.addAll(loadFiles("/foo-xml-test/yang"));
-        MODEL_CONTEXT = YangParserTestUtils.parseYangFiles(testFiles);
-    }
-
-    // FIXME: migrate callers to use string literals
-    @Deprecated
-    final @NonNull NormalizedNode parseResource(final String uriPath, final String resourceName) throws IOException {
-        return parse(uriPath, AbstractResourceBodyTest.class.getResourceAsStream(resourceName));
+        DATABIND = DatabindContext.ofModel(YangParserTestUtils.parseYangFiles(testFiles));
     }
 
     final @NonNull NormalizedNode parse(final String uriPath, final String patchBody) throws IOException {
-        return parse(uriPath, stringInputStream(patchBody));
-    }
-
-    private @NonNull NormalizedNode parse(final String uriPath, final InputStream patchBody) throws IOException {
         final ApiPath apiPath;
         try {
             apiPath = ApiPath.parse(uriPath);
@@ -95,18 +87,29 @@ abstract class AbstractResourceBodyTest extends AbstractBodyTest {
             throw new AssertionError(e);
         }
 
-        try (var body = bodyConstructor.apply(patchBody)) {
-            final var context = InstanceIdentifierContext.ofApiPath(apiPath, MODEL_CONTEXT, mountPointService);
-            return body.toNormalizedNode(context.getInstanceIdentifier(), context.inference(), context.getSchemaNode());
+        final var strategy = new MdsalRestconfStrategy(DATABIND, dataBroker, ImmutableMap.of(), null, null, null,
+            mountPointService);
+        final StrategyAndPath stratAndPath;
+        try {
+            stratAndPath = strategy.resolveStrategyPath(apiPath);
+        } catch (ServerException e) {
+            throw new AssertionError(e);
+        }
+
+        try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
+            return body.toNormalizedNode(stratAndPath.path());
         }
     }
 
-    static final void assertRangeViolation(final ThrowingRunnable runnable) {
-        final var ex = assertThrows(RestconfDocumentedException.class, runnable);
+    static final RestconfError assertError(final Executable executable) {
+        final var ex = assertThrows(RestconfDocumentedException.class, executable);
         final var errors = ex.getErrors();
         assertEquals(1, errors.size());
+        return errors.get(0);
+    }
 
-        final var error = errors.get(0);
+    static final void assertRangeViolation(final Executable executable) {
+        final var error = assertError(executable);
         assertEquals(ErrorType.APPLICATION, error.getErrorType());
         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
         assertEquals("bar error app tag", error.getErrorAppTag());