fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 2.2.1 / 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.mockito.Matchers.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
14
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
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;
34
35 public class NetconfOperationRouterImplTest {
36
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/>";
41
42     private static Document TEST_RPC_DOC;
43
44     @Mock
45     private NetconfOperationService operationService;
46     @Mock
47     private NetconfOperationService operationService2;
48     @Mock
49     private NetconfOperation maxPrioMock;
50     @Mock
51     private NetconfOperation defaultPrioMock;
52
53     private NetconfOperationRouterImpl operationRouter;
54     private NetconfOperationRouterImpl emptyOperationRouter;
55
56     @BeforeClass
57     public static void suiteSetUp() throws IOException, SAXException {
58         TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
59     }
60
61     @Before
62     public void setUp() throws Exception {
63         MockitoAnnotations.initMocks(this);
64
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));
68
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));
72
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();
78
79         operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
80         doReturn(Collections.EMPTY_SET).when(operationService2).getNetconfOperations();
81         emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
82     }
83
84     @Test
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);
90
91         final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
92
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());
97
98         //execute next in chain
99         final Document execute = chainedExecution.execute(XmlUtil.newDocument());
100         Assert.assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
101
102         //default priority message is second and last
103         verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
104         Assert.assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
105
106         Assert.assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
107     }
108
109     @Test
110     public void testOnNetconfMessageFail() throws Exception {
111         try {
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);
116         }
117     }
118
119     @Test
120     public void testClose() throws Exception {
121         operationRouter.close();
122         verify(operationService).close();
123     }
124
125 }