Integrate MRI projects for Neon
[netconf.git] / netconf / netconf-impl / src / test / java / org / opendaylight / netconf / impl / NetconfServerSessionListenerTest.java
index af514d195c643344bec4149b944f4d8f94480f0f..316bfd3eb84a0a30f80a4132235f202157f43f41 100644 (file)
@@ -8,8 +8,8 @@
 
 package org.opendaylight.netconf.impl;
 
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.argThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
@@ -18,19 +18,19 @@ import static org.mockito.Mockito.verify;
 import io.netty.channel.embedded.EmbeddedChannel;
 import org.custommonkey.xmlunit.Diff;
 import org.custommonkey.xmlunit.XMLUnit;
-import org.hamcrest.CustomMatcher;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.mockito.ArgumentMatcher;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.NetconfTerminationReason;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.netconf.api.monitoring.SessionEvent;
 import org.opendaylight.netconf.api.monitoring.SessionListener;
+import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
 import org.opendaylight.netconf.notifications.NetconfNotification;
 import org.w3c.dom.Document;
@@ -91,15 +91,15 @@ public class NetconfServerSessionListenerTest {
 
     @Test
     public void testOnMessage() throws Exception {
-        final Document reply = XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\" " +
-                "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><example/></rpc-reply>");
+        final Document reply = XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\" "
+                "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><example/></rpc-reply>");
         doReturn(reply).when(router).onNetconfMessage(any(), any());
-        final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"101\" " +
-                "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><example/></rpc>"));
+        final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"101\" "
+                "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><example/></rpc>"));
         listener.onMessage(session, msg);
         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_SUCCESS)));
         channel.runPendingTasks();
-        final NetconfMessage sentMsg = (NetconfMessage) channel.readOutbound();
+        final NetconfMessage sentMsg = channel.readOutbound();
         final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
         Assert.assertTrue(diff.toString(), diff.similar());
     }
@@ -108,8 +108,8 @@ public class NetconfServerSessionListenerTest {
     public void testOnMessageRuntimeFail() throws Exception {
         doThrow(new RuntimeException("runtime fail")).when(router).onNetconfMessage(any(), any());
         final Document reply =
-                XmlUtil.readXmlToDocument("<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
-                        "<example/></rpc>");
+                XmlUtil.readXmlToDocument("<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
+                        "<example/></rpc>");
         final NetconfMessage msg = new NetconfMessage(reply);
         try {
             listener.onMessage(session, msg);
@@ -119,28 +119,29 @@ public class NetconfServerSessionListenerTest {
         }
     }
 
+    @SuppressWarnings("checkstyle:RegexpSinglelineJava")
     @Test
     public void testOnMessageDocumentedFail() throws Exception {
         final Document reply =
-                XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                        "<rpc-error>\n" +
-                        "<error-type>PROTOCOL</error-type>\n" +
-                        "<error-tag>unknown-element</error-tag>\n" +
-                        "<error-severity>ERROR</error-severity>\n" +
-                        "<error-message>Unknown tag bad-rpc in message:\n" +
-                        "&lt;bad-rpc/&gt;\n" +
-                        "</error-message>\n" +
-                        "<error-info>\n" +
-                        "<bad-element>bad-rpc</bad-element>\n" +
-                        "</error-info>\n" +
-                        "</rpc-error>\n" +
-                        "</rpc-reply>");
+                XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+                        + "<rpc-error>\n"
+                        + "<error-type>protocol</error-type>\n"
+                        + "<error-tag>unknown-element</error-tag>\n"
+                        + "<error-severity>error</error-severity>\n"
+                        + "<error-message>Unknown tag bad-rpc in message:\n"
+                        + "&lt;bad-rpc/&gt;\n"
+                        + "</error-message>\n"
+                        + "<error-info>\n"
+                        + "<bad-element>bad-rpc</bad-element>\n"
+                        + "</error-info>\n"
+                        + "</rpc-error>\n"
+                        "</rpc-reply>");
         final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<bad-rpc/>"));
         listener.onMessage(session, msg);
         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.OUT_RPC_ERROR)));
         channel.runPendingTasks();
-        final NetconfMessage sentMsg = (NetconfMessage) channel.readOutbound();
+        final NetconfMessage sentMsg = channel.readOutbound();
         System.out.println(XmlUtil.toString(sentMsg.getDocument()));
         System.out.println(XmlUtil.toString(reply));
         final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
@@ -153,17 +154,7 @@ public class NetconfServerSessionListenerTest {
         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.NOTIFICATION)));
     }
 
-    private CustomMatcher<SessionEvent> sessionEventIs(final SessionEvent.Type type) {
-        return new CustomMatcher<SessionEvent>(type.name()) {
-            @Override
-            public boolean matches(final Object item) {
-                if (!(item instanceof SessionEvent)) {
-                    return false;
-                }
-                final SessionEvent e = (SessionEvent) item;
-                return e.getType().equals(type) && e.getSession().equals(session);
-            }
-        };
+    private ArgumentMatcher<SessionEvent> sessionEventIs(final SessionEvent.Type type) {
+        return event -> event.getType().equals(type) && event.getSession().equals(session);
     }
-
-}
\ No newline at end of file
+}