Merge "Bug 2806 - Immediate and infinite reconnect attempts during negotiation" into...
[netconf.git] / opendaylight / netconf / 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.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;
34
35 public class NetconfOperationRouterImplTest {
36
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/>";
40
41     private static Document TEST_RPC_DOC;
42
43     @Mock
44     private NetconfOperationService operationService;
45     @Mock
46     private NetconfOperationService operationService2;
47     @Mock
48     private NetconfOperation maxPrioMock;
49     @Mock
50     private NetconfOperation defaultPrioMock;
51
52     private NetconfOperationRouterImpl operationRouter;
53     private NetconfOperationRouterImpl emptyOperationRouter;
54
55     @BeforeClass
56     public static void suiteSetUp() throws IOException, SAXException {
57         TEST_RPC_DOC = XmlUtil.readXmlToDocument(TEST_RPC);
58     }
59
60     @Before
61     public void setUp() throws Exception {
62         MockitoAnnotations.initMocks(this);
63
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));
66
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));
69
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();
75
76         operationRouter = new NetconfOperationRouterImpl(operationService, null, "session-1");
77         doReturn(Collections.EMPTY_SET).when(operationService2).getNetconfOperations();
78         emptyOperationRouter = new NetconfOperationRouterImpl(operationService2, null, "session-1");
79     }
80
81     @Test
82     public void testOnNetconfMessage() throws Exception {
83         ArgumentCaptor<NetconfOperationChainedExecution> highPriorityChainEx = ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
84         ArgumentCaptor<NetconfOperationChainedExecution> defaultPriorityChainEx = ArgumentCaptor.forClass(NetconfOperationChainedExecution.class);
85
86         final Document document = operationRouter.onNetconfMessage(TEST_RPC_DOC, null);
87
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());
92
93         //execute next in chain
94         final Document execute = chainedExecution.execute(XmlUtil.newDocument());
95         Assert.assertEquals(DEFAULT_PRIORITY_REPLY, XmlUtil.toString(execute).trim());
96
97         //default priority message is second and last
98         verify(defaultPrioMock).handle(any(Document.class), defaultPriorityChainEx.capture());
99         Assert.assertTrue(defaultPriorityChainEx.getValue().isExecutionTermination());
100
101         Assert.assertEquals(MAX_PRIORITY_REPLY, XmlUtil.toString(document).trim());
102     }
103
104     @Test
105     public void testOnNetconfMessageFail() throws Exception {
106         try{
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);
111         }
112     }
113
114     @Test
115     public void testClose() throws Exception {
116         operationRouter.close();
117         verify(operationService).close();
118     }
119
120 }