Remove CloseableUtil
[netconf.git] / protocol / netconf-server / src / test / java / org / opendaylight / netconf / server / NetconfServerSessionListenerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.server;
9
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;
18
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.monitoring.NetconfMonitoringService;
32 import org.opendaylight.netconf.api.monitoring.SessionEvent;
33 import org.opendaylight.netconf.api.monitoring.SessionListener;
34 import org.opendaylight.netconf.api.xml.XmlUtil;
35 import org.opendaylight.netconf.notifications.NetconfNotification;
36 import org.opendaylight.netconf.server.osgi.NetconfOperationRouter;
37 import org.w3c.dom.Document;
38
39 @RunWith(MockitoJUnitRunner.StrictStubs.class)
40 public class NetconfServerSessionListenerTest {
41     @Mock
42     private NetconfOperationRouter router;
43     @Mock
44     private NetconfMonitoringService monitoring;
45     @Mock
46     private AutoCloseable closeable;
47     @Mock
48     private SessionListener monitoringListener;
49     private NetconfServerSession session;
50     private EmbeddedChannel channel;
51     private NetconfServerSessionListener listener;
52
53     @BeforeClass
54     public static void classSetUp() throws Exception {
55         XMLUnit.setIgnoreWhitespace(true);
56     }
57
58     @Before
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);
67     }
68
69     @Test
70     public void testOnSessionUp() throws Exception {
71         listener.onSessionUp(session);
72         verify(monitoringListener).onSessionUp(session);
73     }
74
75     @Test
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();
82     }
83
84     @Test
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();
90     }
91
92     @Test
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());
105     }
106
107     @Test
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)));
117     }
118
119     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
120     @Test
121     public void testOnMessageDocumentedFail() throws Exception {
122         final Document reply =
123                 XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
124                         + "<rpc-error>\n"
125                         + "<error-type>protocol</error-type>\n"
126                         + "<error-tag>unknown-element</error-tag>\n"
127                         + "<error-severity>error</error-severity>\n"
128                         + "<error-message>Unknown tag bad-rpc in message:\n"
129                         + "&lt;bad-rpc/&gt;\n"
130                         + "</error-message>\n"
131                         + "<error-info>\n"
132                         + "<bad-element>bad-rpc</bad-element>\n"
133                         + "</error-info>\n"
134                         + "</rpc-error>\n"
135                         + "</rpc-reply>");
136         final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<bad-rpc/>"));
137         listener.onMessage(session, msg);
138         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
139         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.OUT_RPC_ERROR)));
140         channel.runPendingTasks();
141         final NetconfMessage sentMsg = channel.readOutbound();
142         System.out.println(XmlUtil.toString(sentMsg.getDocument()));
143         System.out.println(XmlUtil.toString(reply));
144         final Diff diff = XMLUnit.compareXML(reply, sentMsg.getDocument());
145         assertTrue(diff.toString(), diff.similar());
146     }
147
148     @Test
149     public void testOnNotification() throws Exception {
150         listener.onNotification(session, new NetconfNotification(XmlUtil.readXmlToDocument("<notification/>")));
151         verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.NOTIFICATION)));
152     }
153
154     private ArgumentMatcher<SessionEvent> sessionEventIs(final SessionEvent.Type type) {
155         return event -> event.getType().equals(type) && event.getSession().equals(session);
156     }
157 }