2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.server;
10 import static org.junit.Assert.assertThrows;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.argThat;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.verify;
19 import io.netty.channel.embedded.EmbeddedChannel;
20 import org.custommonkey.xmlunit.Diff;
21 import org.custommonkey.xmlunit.XMLUnit;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentMatcher;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.NetconfTerminationReason;
31 import org.opendaylight.netconf.api.messages.NotificationMessage;
32 import org.opendaylight.netconf.api.xml.XmlUtil;
33 import org.opendaylight.netconf.server.api.monitoring.NetconfMonitoringService;
34 import org.opendaylight.netconf.server.api.monitoring.SessionEvent;
35 import org.opendaylight.netconf.server.api.monitoring.SessionListener;
36 import org.opendaylight.netconf.server.osgi.NetconfOperationRouterImpl;
37 import org.w3c.dom.Document;
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class NetconfServerSessionListenerTest {
42 private NetconfOperationRouterImpl router;
44 private NetconfMonitoringService monitoring;
46 private AutoCloseable closeable;
48 private SessionListener monitoringListener;
49 private NetconfServerSession session;
50 private EmbeddedChannel channel;
51 private NetconfServerSessionListener listener;
54 public static void classSetUp() throws Exception {
55 XMLUnit.setIgnoreWhitespace(true);
59 public void setUp() throws Exception {
60 doReturn(monitoringListener).when(monitoring).getSessionListener();
61 doNothing().when(monitoringListener).onSessionUp(any());
62 doNothing().when(monitoringListener).onSessionDown(any());
63 doNothing().when(monitoringListener).onSessionEvent(any());
64 channel = new EmbeddedChannel();
65 session = new NetconfServerSession(null, channel, 0L, null);
66 listener = new NetconfServerSessionListener(router, monitoring, closeable);
70 public void testOnSessionUp() throws Exception {
71 listener.onSessionUp(session);
72 verify(monitoringListener).onSessionUp(session);
76 public void testOnSessionDown() throws Exception {
77 final Exception cause = new RuntimeException("cause");
78 listener.onSessionDown(session, cause);
79 verify(monitoringListener).onSessionDown(session);
80 verify(closeable).close();
81 verify(router).close();
85 public void testOnSessionTerminated() throws Exception {
86 listener.onSessionTerminated(session, new NetconfTerminationReason("reason"));
87 verify(monitoringListener).onSessionDown(session);
88 verify(closeable).close();
89 verify(router).close();
93 public void testOnMessage() throws Exception {
94 final Document reply = XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\" "
95 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><example/></rpc-reply>");
96 doReturn(reply).when(router).onNetconfMessage(any(), any());
97 final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"101\" "
98 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><example/></rpc>"));
99 listener.onMessage(session, msg);
100 verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_SUCCESS)));
101 channel.runPendingTasks();
102 final NetconfMessage sentMsg = channel.readOutbound();
103 final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
104 assertTrue(diff.toString(), diff.similar());
108 public void testOnMessageRuntimeFail() throws Exception {
109 doThrow(new RuntimeException("runtime fail")).when(router).onNetconfMessage(any(), any());
110 final Document reply =
111 XmlUtil.readXmlToDocument("<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
112 + "<example/></rpc>");
113 final NetconfMessage msg = new NetconfMessage(reply);
114 final IllegalStateException ex = assertThrows(IllegalStateException.class,
115 () -> listener.onMessage(session, msg));
116 verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
120 public void testOnMessageDocumentedFail() throws Exception {
121 final Document reply =
122 XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
124 + "<error-type>protocol</error-type>\n"
125 + "<error-tag>unknown-element</error-tag>\n"
126 + "<error-severity>error</error-severity>\n"
127 + "<error-message>Unknown tag bad-rpc in message:\n"
128 + "<bad-rpc/>\n"
129 + "</error-message>\n"
131 + "<bad-element>bad-rpc</bad-element>\n"
135 final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<bad-rpc/>"));
136 listener.onMessage(session, msg);
137 verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
138 verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.OUT_RPC_ERROR)));
139 channel.runPendingTasks();
140 final NetconfMessage sentMsg = channel.readOutbound();
141 final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
142 assertTrue(diff.toString(), diff.similar());
146 public void testOnNotification() throws Exception {
147 listener.onNotification(session, new NotificationMessage(XmlUtil.readXmlToDocument("<notification/>")));
148 verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.NOTIFICATION)));
151 private ArgumentMatcher<SessionEvent> sessionEventIs(final SessionEvent.Type type) {
152 return event -> event.getType().equals(type) && event.getSession().equals(session);