Move netconf{-client,impl,util} to protocol/
[netconf.git] / protocol / netconf-impl / src / test / java / org / opendaylight / netconf / impl / 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.impl.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.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;
39
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class NetconfOperationRouterImplTest {
42
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/>";
47
48     private static Document TEST_RPC_DOC;
49
50     @Mock
51     private NetconfOperationService operationService;
52     @Mock
53     private NetconfOperationService operationService2;
54     @Mock
55     private NetconfOperation maxPrioMock;
56     @Mock
57     private NetconfOperation defaultPrioMock;
58
59     private NetconfOperationRouterImpl operationRouter;
60     private NetconfOperationRouterImpl emptyOperationRouter;
61
62     @BeforeClass
63     public static void suiteSetUp() throws IOException, SAXException {
64         TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
65     }
66
67     @Before
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));
72
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));
76
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();
82
83         operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
84         doReturn(Collections.emptySet()).when(operationService2).getNetconfOperations();
85         emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
86     }
87
88     @Test
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);
94
95         final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
96
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());
101
102         //execute next in chain
103         final Document execute = chainedExecution.execute(XmlUtil.newDocument());
104         assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
105
106         //default priority message is second and last
107         verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
108         assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
109
110         assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
111     }
112
113     @Test
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());
118     }
119
120     @Test
121     public void testClose() throws Exception {
122         operationRouter.close();
123         verify(operationService).close();
124     }
125
126 }