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.impl.osgi;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.verify;
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.HashSet;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.netconf.api.DocumentedException;
31 import org.opendaylight.netconf.api.xml.XmlUtil;
32 import org.opendaylight.netconf.mapping.api.HandlingPriority;
33 import org.opendaylight.netconf.mapping.api.NetconfOperation;
34 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
35 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
36 import org.opendaylight.yangtools.yang.common.ErrorTag;
37 import org.w3c.dom.Document;
38 import org.xml.sax.SAXException;
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class NetconfOperationRouterImplTest {
43 private static final String TEST_RPC = "<rpc message-id=\"101\" "
44 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><test/></rpc>\n";
45 private static final String MAX_PRIORITY_REPLY = "<high/>";
46 private static final String DEFAULT_PRIORITY_REPLY = "<default/>";
48 private static Document TEST_RPC_DOC;
51 private NetconfOperationService operationService;
53 private NetconfOperationService operationService2;
55 private NetconfOperation maxPrioMock;
57 private NetconfOperation defaultPrioMock;
59 private NetconfOperationRouterImpl operationRouter;
60 private NetconfOperationRouterImpl emptyOperationRouter;
63 public static void suiteSetUp() throws IOException, SAXException {
64 TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
68 public void setUp() throws Exception {
69 doReturn(HandlingPriority.HANDLE_WITH_MAX_PRIORITY).when(maxPrioMock).canHandle(any(Document.class));
70 doReturn(XmlUtil.readXmlToDocument(MAX_PRIORITY_REPLY)).when(maxPrioMock).handle(any(Document.class),
71 any(NetconfOperationChainedExecution.class));
73 doReturn(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY).when(defaultPrioMock).canHandle(any(Document.class));
74 doReturn(XmlUtil.readXmlToDocument(DEFAULT_PRIORITY_REPLY)).when(defaultPrioMock).handle(any(Document.class),
75 any(NetconfOperationChainedExecution.class));
77 final Set<NetconfOperation> operations = new HashSet<>();
78 operations.add(maxPrioMock);
79 operations.add(defaultPrioMock);
80 doReturn(operations).when(operationService).getNetconfOperations();
81 doNothing().when(operationService).close();
83 operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
84 doReturn(Collections.emptySet()).when(operationService2).getNetconfOperations();
85 emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
89 public void testOnNetconfMessage() throws Exception {
90 final ArgumentCaptor<NetconfOperationChainedExecution> highPriorityChainEx =
91 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
92 final ArgumentCaptor<NetconfOperationChainedExecution> defaultPriorityChainEx =
93 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
95 final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
97 //max priority message is first in chain
98 verify(maxPrioMock).handle(any(Document.class), highPriorityChainEx.capture());
99 final NetconfOperationChainedExecution chainedExecution = highPriorityChainEx.getValue();
100 assertFalse(chainedExecution.isExecutionTermination());
102 //execute next in chain
103 final Document execute = chainedExecution.execute(XmlUtil.newDocument());
104 assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
106 //default priority message is second and last
107 verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
108 assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
110 assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
114 public void testOnNetconfMessageFail() throws Exception {
115 final DocumentedException ex = assertThrows(DocumentedException.class,
116 () -> emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null));
117 assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
121 public void testClose() throws Exception {
122 operationRouter.close();
123 verify(operationService).close();