Integrate netconf-mapping-api into netconf-server
[netconf.git] / protocol / netconf-server / src / test / java / org / opendaylight / netconf / server / osgi / NetconfOperationRouterImplTest.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.osgi;
9
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;
18
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
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.server.api.operations.HandlingPriority;
33 import org.opendaylight.netconf.server.api.operations.NetconfOperation;
34 import org.opendaylight.netconf.server.api.operations.NetconfOperationChainedExecution;
35 import org.opendaylight.netconf.server.api.operations.NetconfOperationService;
36 import org.opendaylight.yangtools.yang.common.ErrorTag;
37 import org.w3c.dom.Document;
38 import org.xml.sax.SAXException;
39
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class NetconfOperationRouterImplTest {
42     private static final String TEST_RPC =
43         "<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><test/></rpc>\n";
44     private static final String MAX_PRIORITY_REPLY = "<high/>";
45     private static final String DEFAULT_PRIORITY_REPLY = "<default/>";
46
47     private static Document TEST_RPC_DOC;
48
49     @Mock
50     private NetconfOperationService operationService;
51     @Mock
52     private NetconfOperationService operationService2;
53     @Mock
54     private NetconfOperation maxPrioMock;
55     @Mock
56     private NetconfOperation defaultPrioMock;
57
58     private NetconfOperationRouterImpl operationRouter;
59     private NetconfOperationRouterImpl emptyOperationRouter;
60
61     @BeforeClass
62     public static void suiteSetUp() throws IOException, SAXException {
63         TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
64     }
65
66     @Before
67     public void setUp() throws Exception {
68         doReturn(HandlingPriority.HANDLE_WITH_MAX_PRIORITY).when(maxPrioMock).canHandle(any(Document.class));
69         doReturn(XmlUtil.readXmlToDocument(MAX_PRIORITY_REPLY)).when(maxPrioMock).handle(any(Document.class),
70                 any(NetconfOperationChainedExecution.class));
71
72         doReturn(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY).when(defaultPrioMock).canHandle(any(Document.class));
73         doReturn(XmlUtil.readXmlToDocument(DEFAULT_PRIORITY_REPLY)).when(defaultPrioMock).handle(any(Document.class),
74                 any(NetconfOperationChainedExecution.class));
75
76         final Set<NetconfOperation> operations = new HashSet<>();
77         operations.add(maxPrioMock);
78         operations.add(defaultPrioMock);
79         doReturn(operations).when(operationService).getNetconfOperations();
80         doNothing().when(operationService).close();
81
82         operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
83         doReturn(Collections.emptySet()).when(operationService2).getNetconfOperations();
84         emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
85     }
86
87     @Test
88     public void testOnNetconfMessage() throws Exception {
89         final ArgumentCaptor<NetconfOperationChainedExecution> highPriorityChainEx =
90                 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
91         final ArgumentCaptor<NetconfOperationChainedExecution> defaultPriorityChainEx =
92                 ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
93
94         final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
95
96         //max priority message is first in chain
97         verify(maxPrioMock).handle(any(Document.class), highPriorityChainEx.capture());
98         final NetconfOperationChainedExecution chainedExecution = highPriorityChainEx.getValue();
99         assertFalse(chainedExecution.isExecutionTermination());
100
101         //execute next in chain
102         final Document execute = chainedExecution.execute(XmlUtil.newDocument());
103         assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
104
105         //default priority message is second and last
106         verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
107         assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
108
109         assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
110     }
111
112     @Test
113     public void testOnNetconfMessageFail() throws Exception {
114         final DocumentedException ex =  assertThrows(DocumentedException.class,
115             () -> emptyOperationRouter.onNetconfMessage(TEST_RPC_DOC, null));
116         assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
117     }
118
119     @Test
120     public void testClose() throws Exception {
121         operationRouter.close();
122         verify(operationService).close();
123     }
124 }