Migrate to assertThrows() 63/99663/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Feb 2022 18:10:29 +0000 (19:10 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Feb 2022 18:10:29 +0000 (19:10 +0100)
ExpectedException.none() is deprecated, use assertThrows() for the same
function.

Change-Id: I6baa6ff28e572d2070adf15017ba21f4a09b0b25
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestGetAugmentedElementWhenEqualNamesTest.java
restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/URITest.java

index 42000ae233d3e72834645893cffb6436cf0a0f16..bfe9c1d185ba27cb63b342d6f67d0f2c1bfddf91 100644 (file)
@@ -7,19 +7,19 @@
  */
 package org.opendaylight.controller.sal.restconf.impl.test;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 
 import java.io.FileNotFoundException;
 import org.junit.BeforeClass;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
 public class RestGetAugmentedElementWhenEqualNamesTest {
@@ -28,9 +28,6 @@ public class RestGetAugmentedElementWhenEqualNamesTest {
 
     private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
 
-    @Rule
-    public ExpectedException exception = ExpectedException.none();
-
     @BeforeClass
     public static void init() throws FileNotFoundException {
         schemaContext = TestUtils.loadSchemaContext("/common/augment/yang");
@@ -40,19 +37,16 @@ public class RestGetAugmentedElementWhenEqualNamesTest {
     public void augmentedNodesInUri() {
         InstanceIdentifierContext<?> iiWithData =
                 controllerContext.toInstanceIdentifier("main:cont/augment-main-a:cont1");
-        assertEquals("ns:augment:main:a", iiWithData.getSchemaNode().getQName().getNamespace().toString());
+        assertEquals(XMLNamespace.of("ns:augment:main:a"), iiWithData.getSchemaNode().getQName().getNamespace());
         iiWithData = controllerContext.toInstanceIdentifier("main:cont/augment-main-b:cont1");
-        assertEquals("ns:augment:main:b", iiWithData.getSchemaNode().getQName().getNamespace().toString());
+        assertEquals(XMLNamespace.of("ns:augment:main:b"), iiWithData.getSchemaNode().getQName().getNamespace());
     }
 
     @Test
     public void nodeWithoutNamespaceHasMoreAugments() {
-        try {
-            controllerContext.toInstanceIdentifier("main:cont/cont1");
-            fail("Expected exception");
-        } catch (final RestconfDocumentedException e) {
-            assertTrue(e.getErrors().get(0).getErrorMessage()
-                    .contains("is added as augment from more than one module"));
-        }
+        final var ex = assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("main:cont/cont1"));
+        assertThat(ex.getErrors().get(0).getErrorMessage(),
+            containsString("is added as augment from more than one module"));
     }
 }
index 1904e7f28efe3cc67b329119429becd9133ad1b8..ab9074a5e628177f79ccab1aceb78410d2eda078 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.controller.sal.restconf.impl.test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -18,9 +19,7 @@ import java.io.FileNotFoundException;
 import java.util.Optional;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
@@ -41,9 +40,6 @@ public class URITest {
     private final ControllerContext controllerContext =
             TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
 
-    @Rule
-    public ExpectedException exception = ExpectedException.none();
-
     @BeforeClass
     public static void init() throws FileNotFoundException, ReactorException {
         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
@@ -84,14 +80,14 @@ public class URITest {
 
     @Test
     public void testToInstanceIdentifierListWithNullKey() {
-        this.exception.expect(RestconfDocumentedException.class);
-        controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes:user/null/boo"));
     }
 
     @Test
     public void testToInstanceIdentifierListWithMissingKey() {
-        this.exception.expect(RestconfDocumentedException.class);
-        controllerContext.toInstanceIdentifier("simple-nodes:user/foo");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes:user/foo"));
     }
 
     @Test
@@ -114,26 +110,26 @@ public class URITest {
 
     @Test
     public void testToInstanceIdentifierChoiceException() {
-        this.exception.expect(RestconfDocumentedException.class);
-        controllerContext.toInstanceIdentifier("simple-nodes:food/snack");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes:food/snack"));
     }
 
     @Test
     public void testToInstanceIdentifierCaseException() {
-        this.exception.expect(RestconfDocumentedException.class);
-        controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes:food/sports-arena"));
     }
 
     @Test
     public void testToInstanceIdentifierChoiceCaseException() {
-        this.exception.expect(RestconfDocumentedException.class);
-        controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes:food/snack/sports-arena"));
     }
 
     @Test
     public void testToInstanceIdentifierWithoutNode() {
-        this.exception.expect(RestconfDocumentedException.class);
-        controllerContext.toInstanceIdentifier("simple-nodes");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes"));
     }
 
     @Test
@@ -159,12 +155,11 @@ public class URITest {
 
     @Test
     public void testMountPointWithoutMountPointSchema() throws FileNotFoundException, ReactorException {
-        this.exception.expect(RestconfDocumentedException.class);
-
-        controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class");
+        assertThrows(RestconfDocumentedException.class,
+            () -> controllerContext.toInstanceIdentifier("simple-nodes:users/yang-ext:mount/test-interface2:class"));
     }
 
-    public void initSchemaService() {
+    private void initSchemaService() {
         doReturn(Optional.of(FixedDOMSchemaService.of(mountSchemaContext))).when(mountInstance)
             .getService(DOMSchemaService.class);
     }