Modernize netconf-util tests 03/96303/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 23 May 2021 18:06:45 +0000 (20:06 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 23 May 2021 18:07:47 +0000 (20:07 +0200)
Use assertThrows() and fixup imports a bit.

Change-Id: I2816141af08815afb0dc926e78ace962ba561ded
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/CloseableUtilTest.java
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfUtilTest.java
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/HardcodedNamespaceResolverTest.java
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/xml/XMLNetconfUtilTest.java

index 43754e9ec69653e6f4d37c5d4dd5587e0731af97..9e517abbe3af57fa9bbeb79c986f9ce301701bb3 100644 (file)
@@ -5,38 +5,32 @@
  * 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.netconf.util;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 
-import com.google.common.collect.Lists;
+import java.util.List;
 import org.junit.Test;
 
 public class CloseableUtilTest {
-
-    @SuppressWarnings("checkstyle:IllegalCatch")
     @Test
-    public void testCloseAllFail() throws Exception {
+    public void testCloseAllFail() {
         final AutoCloseable failingCloseable = () -> {
             throw new RuntimeException("testing failing close");
         };
 
-        try {
-            CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable));
-            fail("Exception with suppressed should be thrown");
-        } catch (final RuntimeException e) {
-            assertEquals(1, e.getSuppressed().length);
-        }
+        final RuntimeException ex = assertThrows(RuntimeException.class,
+            () -> CloseableUtil.closeAll(List.of(failingCloseable, failingCloseable)));
+        assertEquals(1, ex.getSuppressed().length);
     }
 
     @Test
     public void testCloseAll() throws Exception {
         final AutoCloseable failingCloseable = mock(AutoCloseable.class);
         doNothing().when(failingCloseable).close();
-        CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable));
+        CloseableUtil.closeAll(List.of(failingCloseable, failingCloseable));
     }
 }
\ No newline at end of file
index e05cb26e4f13481fd1b037a98e0ad9435d20825e..fd291b22ad6ac569e3daa6e52b730283fb595cd2 100644 (file)
@@ -10,12 +10,12 @@ package org.opendaylight.netconf.util;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
 
 import java.util.Collections;
 import javax.xml.transform.dom.DOMResult;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
-import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.runtime.spi.BindingRuntimeHelpers;
@@ -80,6 +80,6 @@ public class NetconfUtilTest {
         final Document actual = (Document) result.getNode();
         final Document expected = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/sessions.xml"));
         final Diff diff = XMLUnit.compareXML(expected, actual);
-        Assert.assertTrue(diff.toString(), diff.similar());
+        assertTrue(diff.toString(), diff.similar());
     }
 }
index c728862068048bfd09b3854132ad3d9874217764..aae32b0e732d29c6f20ce4bd2220f3b9d4f1306b 100644 (file)
@@ -5,30 +5,26 @@
  * 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.netconf.util.xml;
 
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 
 import org.junit.Test;
 
 public class HardcodedNamespaceResolverTest {
-
     @Test
     public void testResolver() throws Exception {
         final HardcodedNamespaceResolver hardcodedNamespaceResolver =
                 new HardcodedNamespaceResolver("prefix", "namespace");
 
         assertEquals("namespace", hardcodedNamespaceResolver.getNamespaceURI("prefix"));
-        try {
-            hardcodedNamespaceResolver.getNamespaceURI("unknown");
-            fail("Unknown namespace lookup should fail");
-        } catch (IllegalStateException e) {
-            assertTrue(e.getMessage().startsWith("Prefix mapping not found for "));
-        }
+        final IllegalStateException ex = assertThrows(IllegalStateException.class,
+            () -> hardcodedNamespaceResolver.getNamespaceURI("unknown"));
+        assertThat(ex.getMessage(), startsWith("Prefix mapping not found for "));
 
         assertNull(hardcodedNamespaceResolver.getPrefix("any"));
         assertNull(hardcodedNamespaceResolver.getPrefixes("any"));
index bf3c573c1faba45bb174189e2be2c94a8b7f2a76..e9e572148e44189a3e4695c636c405e28d03cdd2 100644 (file)
@@ -5,12 +5,12 @@
  * 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.netconf.util.xml;
 
+import static org.hamcrest.CoreMatchers.startsWith;
+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 javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpression;
@@ -19,19 +19,14 @@ import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.w3c.dom.Element;
 
 public class XMLNetconfUtilTest {
-
     @Test
     public void testXPath() throws Exception {
         final XPathExpression correctXPath = XMLNetconfUtil.compileXPath("/top/innerText");
-        try {
-            XMLNetconfUtil.compileXPath("!@(*&$!");
-            fail("Incorrect xpath should fail");
-        } catch (IllegalStateException e) {
-            assertTrue(e.getMessage().startsWith("Error while compiling xpath expression "));
-        }
+        final IllegalStateException ex = assertThrows(IllegalStateException.class,
+            () -> XMLNetconfUtil.compileXPath("!@(*&$!"));
+        assertThat(ex.getMessage(), startsWith("Error while compiling xpath expression "));
         final Object value = XmlUtil.evaluateXPath(correctXPath,
                 XmlUtil.readXmlToDocument("<top><innerText>value</innerText></top>"), XPathConstants.NODE);
         assertEquals("value", ((Element) value).getTextContent());
     }
-
 }