Clean up test interactions and migrate to JUnit5.
Change-Id: I4a62a1a980552205712b10552bb64229ead95b83
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
*/
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 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.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.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.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"));
private static DatabindContext DATABIND;
+ @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"));
}
}
- static final void assertRangeViolation(final ThrowingRunnable runnable) {
- final var ex = assertThrows(RestconfDocumentedException.class, runnable);
+ static final void assertRangeViolation(final Executable executable) {
+ final var ex = assertThrows(RestconfDocumentedException.class, executable);
final var errors = ex.getErrors();
assertEquals(1, errors.size());
*/
package org.opendaylight.restconf.nb.rfc8040.databind;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Map;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-public class JsonResourceBodyTest extends AbstractResourceBodyTest {
- public JsonResourceBodyTest() {
+class JsonResourceBodyTest extends AbstractResourceBodyTest {
+ JsonResourceBodyTest() {
super(JsonResourceBody::new);
}
@Test
- public void moduleDataTest() throws Exception {
+ void moduleDataTest() throws Exception {
final var entryId = NodeIdentifierWithPredicates.of(LST11,
Map.of(KEYVALUE111, "value1", KEYVALUE112, "value2"));
}
@Test
- public void moduleSubContainerDataPutTest() throws Exception {
+ void moduleSubContainerDataPutTest() throws Exception {
assertEquals(Builders.containerBuilder()
.withNodeIdentifier(CONT1_NID)
.withChild(ImmutableNodes.leafNode(LF11, YangInstanceIdentifier.of(CONT_NID, CONT1_NID,
}
@Test
- public void testRangeViolation() throws Exception {
+ void testRangeViolation() throws Exception {
assertRangeViolation(() -> parse("netconf786:foo", """
{
"netconf786:foo": {
*/
package org.opendaylight.restconf.nb.rfc8040.databind;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
import java.util.Map;
-import org.junit.Test;
+import java.util.Optional;
+import org.junit.jupiter.api.Test;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.common.ErrorType;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-public class XmlResourceBodyTest extends AbstractResourceBodyTest {
+class XmlResourceBodyTest extends AbstractResourceBodyTest {
private static final QName TOP_LEVEL_LIST = QName.create("foo", "2017-08-09", "top-level-list");
- public XmlResourceBodyTest() {
+ XmlResourceBodyTest() {
super(XmlResourceBody::new);
}
+ private void mockMount() {
+ doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
+ doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
+ .getService(DOMSchemaService.class);
+ }
+
/**
* Test PUT operation when message root element is not the same as the last element in request URI.
* PUT operation message should always start with schema node from URI otherwise exception should be
* thrown.
*/
@Test
- public void wrongRootElementTest() {
+ void wrongRootElementTest() {
+ mockMount();
+
assertThrowsException("",
"Incorrect message root element (instance:identifier:module)cont1, should be "
+ "(urn:ietf:params:xml:ns:yang:ietf-restconf)data");
}
@Test
- public void testRangeViolation() throws Exception {
+ void testRangeViolation() throws Exception {
assertRangeViolation(() -> parse("netconf786:foo", """
<foo xmlns="netconf786"><bar>100</bar></foo>"""));
}
@Test
- public void putXmlTest() throws Exception {
+ void putXmlTest() throws Exception {
final var keyName = QName.create(TOP_LEVEL_LIST, "key-leaf");
assertEquals(Builders.mapEntryBuilder()
.withNodeIdentifier(NodeIdentifierWithPredicates.of(TOP_LEVEL_LIST, keyName, "key-value"))
}
@Test
- public void moduleDataTest() throws Exception {
+ void moduleDataTest() throws Exception {
testModuleData("instance-identifier-module:cont");
}
@Test
- public void moduleDataMountPointTest() throws Exception {
+ void moduleDataMountPointTest() throws Exception {
+ mockMount();
+
testModuleData("instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont");
}
}
@Test
- public void moduleSubContainerDataPutTest() throws Exception {
+ void moduleSubContainerDataPutTest() throws Exception {
testModuleSubContainerDataPut("instance-identifier-module:cont/cont1");
}
@Test
- public void moduleSubContainerDataPutMountPointTest() throws Exception {
+ void moduleSubContainerDataPutMountPointTest() throws Exception {
+ mockMount();
+
testModuleSubContainerDataPut(
"instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1");
}