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.mockito.Matchers.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.HashSet;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.netconf.api.DocumentedException;
27 import org.opendaylight.netconf.api.xml.XmlUtil;
28 import org.opendaylight.netconf.mapping.api.HandlingPriority;
29 import org.opendaylight.netconf.mapping.api.NetconfOperation;
30 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
31 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
32 import org.w3c.dom.Document;
33 import org.xml.sax.SAXException;
35 public class NetconfOperationRouterImplTest {
37 private static final String TEST_RPC = "<rpc message-id=\"101\" "
38 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><test/></rpc>\n";
39 private static final String MAX_PRIORITY_REPLY = "<high/>";
40 private static final String DEFAULT_PRIORITY_REPLY = "<default/>";
42 private static Document TEST_RPC_DOC;
45 private NetconfOperationService operationService;
47 private NetconfOperationService operationService2;
49 private NetconfOperation maxPrioMock;
51 private NetconfOperation defaultPrioMock;
53 private NetconfOperationRouterImpl operationRouter;
54 private NetconfOperationRouterImpl emptyOperationRouter;
57 public static void suiteSetUp() throws IOException, SAXException {
58 TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
62 public void setUp() throws Exception {
63 MockitoAnnotations.initMocks(this);
65 doReturn(HandlingPriority.HANDLE_WITH_MAX_PRIORITY).when(maxPrioMock).canHandle(any(Document.class));
66 doReturn(XmlUtil.readXmlToDocument(MAX_PRIORITY_REPLY)).when(maxPrioMock).handle(any(Document.class),
67 any(NetconfOperationChainedExecution.class));
69 doReturn(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY).when(defaultPrioMock).canHandle(any(Document.class));
70 doReturn(XmlUtil.readXmlToDocument(DEFAULT_PRIORITY_REPLY)).when(defaultPrioMock).handle(any(Document.class),
71 any(NetconfOperationChainedExecution.class));
73 final Set<NetconfOperation> operations = new HashSet<>();
74 operations.add(maxPrioMock);
75 operations.add(defaultPrioMock);
76 doReturn(operations).when(operationService).getNetconfOperations();
77 doNothing().when(operationService).close();
79 operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
80 doReturn(Collections.EMPTY_SET).when(operationService2).getNetconfOperations();
81 emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
85 public void testOnNetconfMessage() throws Exception {
86 final ArgumentCaptor<NetconfOperationChainedExecution> highPriorityChainEx =
87 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
88 final ArgumentCaptor<NetconfOperationChainedExecution> defaultPriorityChainEx =
89 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
91 final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
93 //max priority message is first in chain
94 verify(maxPrioMock).handle(any(Document.class), highPriorityChainEx.capture());
95 final NetconfOperationChainedExecution chainedExecution = highPriorityChainEx.getValue();
96 Assert.assertFalse(chainedExecution.isExecutionTermination());
98 //execute next in chain
99 final Document execute = chainedExecution.execute(XmlUtil.newDocument());
100 Assert.assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
102 //default priority message is second and last
103 verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
104 Assert.assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
106 Assert.assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
110 public void testOnNetconfMessageFail() throws Exception {
112 emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null);
113 Assert.fail("Exception expected");
114 } catch (final DocumentedException e) {
115 Assert.assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.OPERATION_NOT_SUPPORTED);
120 public void testClose() throws Exception {
121 operationRouter.close();
122 verify(operationService).close();