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.controller.config.util.xml.DocumentedException;
27 import org.opendaylight.controller.config.util.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\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><test/></rpc>\n";
38 private static final String MAX_PRIORITY_REPLY = "<high/>";
39 private static final String DEFAULT_PRIORITY_REPLY = "<default/>";
41 private static Document TEST_RPC_DOC;
44 private NetconfOperationService operationService;
46 private NetconfOperationService operationService2;
48 private NetconfOperation maxPrioMock;
50 private NetconfOperation defaultPrioMock;
52 private NetconfOperationRouterImpl operationRouter;
53 private NetconfOperationRouterImpl emptyOperationRouter;
56 public static void suiteSetUp() throws IOException, SAXException {
57 TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
61 public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
64 doReturn(HandlingPriority.HANDLE_WITH_MAX_PRIORITY).when(maxPrioMock).canHandle(any(Document.class));
65 doReturn(XmlUtil.readXmlToDocument(MAX_PRIORITY_REPLY)).when(maxPrioMock).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
67 doReturn(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY).when(defaultPrioMock).canHandle(any(Document.class));
68 doReturn(XmlUtil.readXmlToDocument(DEFAULT_PRIORITY_REPLY)).when(defaultPrioMock).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
70 Set<NetconfOperation> operations = new HashSet<>();
71 operations.add(maxPrioMock);
72 operations.add(defaultPrioMock);
73 doReturn(operations).when(operationService).getNetconfOperations();
74 doNothing().when(operationService).close();
76 operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
77 doReturn(Collections.EMPTY_SET).when(operationService2).getNetconfOperations();
78 emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
82 public void testOnNetconfMessage() throws Exception {
83 ArgumentCaptor<NetconfOperationChainedExecution> highPriorityChainEx = ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
84 ArgumentCaptor<NetconfOperationChainedExecution> defaultPriorityChainEx = ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
86 final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
88 //max priority message is first in chain
89 verify(maxPrioMock).handle(any(Document.class), highPriorityChainEx.capture());
90 final NetconfOperationChainedExecution chainedExecution = highPriorityChainEx.getValue();
91 Assert.assertFalse(chainedExecution.isExecutionTermination());
93 //execute next in chain
94 final Document execute = chainedExecution.execute(XmlUtil.newDocument());
95 Assert.assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
97 //default priority message is second and last
98 verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
99 Assert.assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
101 Assert.assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
105 public void testOnNetconfMessageFail() throws Exception {
107 emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null);
108 Assert.fail("Exception expected");
109 } catch (DocumentedException e) {
110 Assert.assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.operation_not_supported);
115 public void testClose() throws Exception {
116 operationRouter.close();
117 verify(operationService).close();