Merge "Netconf-cli compilable and included in project"
[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 com.google.common.collect.Sets;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
25 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
26 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
27 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
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.w3c.dom.Document;
33 import org.w3c.dom.Element;
34
35 public class DefaultCommitTest {
36
37     private NetconfOperationChainedExecution operation;
38     private Document requestMessage;
39     private NetconfOperationRouter router;
40     private DefaultCommitNotificationProducer notifier;
41     private CapabilityProvider cap;
42     private DefaultCommit commit;
43
44     @Before
45     public void setUp() throws Exception {
46         operation = mock(NetconfOperationChainedExecution.class);
47         doReturn(XmlUtil.newDocument()).when(operation).execute(any(Document.class));
48         router = mock(NetconfOperationRouter.class);
49         doReturn(false).when(operation).isExecutionTermination();
50         notifier = mock(DefaultCommitNotificationProducer.class);
51         doNothing().when(notifier).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
52         cap = mock(CapabilityProvider.class);
53         doReturn(Sets.newHashSet()).when(cap).getCapabilities();
54         Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
55         doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class));
56         commit = new DefaultCommit(notifier, cap, "", router);
57     }
58
59     @Test
60     public void testHandleWithNotification() throws Exception {
61         requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
62         commit.handle(requestMessage, operation);
63         verify(operation, times(1)).execute(requestMessage);
64         verify(notifier, times(1)).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
65     }
66
67     @Test
68     public void testHandleWithoutNotification() throws Exception {
69         requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
70         Element elem = requestMessage.getDocumentElement();
71         elem.setAttribute("notify", "false");
72         commit.handle(requestMessage, operation);
73         verify(operation, times(1)).execute(requestMessage);
74         verify(notifier, never()).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
75     }
76
77     @Test(expected = NetconfDocumentedException.class)
78     public void testHandle() throws Exception {
79         Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/get.xml");
80         doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class));
81         requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
82         commit.handle(requestMessage, operation);
83     }
84 }