Merge "BUG-2635 Netconf monitoring for md-sal netconf northbound"
[controller.git] / opendaylight / netconf / netconf-impl / src / test / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultCommitTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.impl.mapping.operations;
10
11 import static org.mockito.Mockito.any;
12 import static org.mockito.Mockito.anySetOf;
13 import static org.mockito.Mockito.anyString;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20
21 import java.util.Collections;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
25 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
26 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
27 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
28 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
29 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
30 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
31 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 public class DefaultCommitTest {
38
39     private NetconfOperationChainedExecution operation;
40     private Document requestMessage;
41     private NetconfOperationRouter router;
42     private DefaultCommitNotificationProducer notifier;
43     private NetconfMonitoringService cap;
44     private DefaultCommit commit;
45
46     @Before
47     public void setUp() throws Exception {
48         operation = mock(NetconfOperationChainedExecution.class);
49         doReturn(XmlUtil.newDocument()).when(operation).execute(any(Document.class));
50         router = mock(NetconfOperationRouter.class);
51         doReturn(false).when(operation).isExecutionTermination();
52         notifier = mock(DefaultCommitNotificationProducer.class);
53         doNothing().when(notifier).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
54         cap = mock(NetconfMonitoringService.class);
55         doReturn(new CapabilitiesBuilder().setCapability(Collections.<Uri>emptyList()).build()).when(cap).getCapabilities();
56         Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
57         doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class));
58         commit = new DefaultCommit(notifier, cap, "", router);
59     }
60
61     @Test
62     public void testHandleWithNotification() throws Exception {
63         requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
64         commit.handle(requestMessage, operation);
65         verify(operation, times(1)).execute(requestMessage);
66         verify(notifier, times(1)).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
67     }
68
69     @Test
70     public void testHandleWithoutNotification() throws Exception {
71         requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
72         Element elem = requestMessage.getDocumentElement();
73         elem.setAttribute("notify", "false");
74         commit.handle(requestMessage, operation);
75         verify(operation, times(1)).execute(requestMessage);
76         verify(notifier, never()).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
77     }
78
79     @Test(expected = NetconfDocumentedException.class)
80     public void testHandle() throws Exception {
81         Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/get.xml");
82         doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class));
83         requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
84         commit.handle(requestMessage, operation);
85     }
86 }