2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.impl.mapping.operations;
11 import com.google.common.collect.Sets;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
16 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
17 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
18 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
20 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
25 import static org.mockito.Mockito.*;
27 public class DefaultCommitTest {
29 private NetconfOperationChainedExecution operation;
30 private Document requestMessage;
31 private NetconfOperationRouter router;
32 private DefaultCommitNotificationProducer notifier;
33 private CapabilityProvider cap;
34 private DefaultCommit commit;
37 public void setUp() throws Exception {
38 operation = mock(NetconfOperationChainedExecution.class);
39 doReturn(XmlUtil.newDocument()).when(operation).execute(any(Document.class));
40 router = mock(NetconfOperationRouter.class);
41 doReturn(false).when(operation).isExecutionTermination();
42 notifier = mock(DefaultCommitNotificationProducer.class);
43 doNothing().when(notifier).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
44 cap = mock(CapabilityProvider.class);
45 doReturn(Sets.newHashSet()).when(cap).getCapabilities();
46 Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
47 doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class));
48 commit = new DefaultCommit(notifier, cap, "", router);
52 public void testHandleWithNotification() throws Exception {
53 requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
54 commit.handle(requestMessage, operation);
55 verify(operation, times(1)).execute(requestMessage);
56 verify(notifier, times(1)).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
60 public void testHandleWithoutNotification() throws Exception {
61 requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
62 Element elem = requestMessage.getDocumentElement();
63 elem.setAttribute("notify", "false");
64 commit.handle(requestMessage, operation);
65 verify(operation, times(1)).execute(requestMessage);
66 verify(notifier, never()).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
69 @Test(expected = NetconfDocumentedException.class)
70 public void testHandle() throws Exception {
71 Document rpcData = XmlFileLoader.xmlFileToDocument("netconfMessages/get.xml");
72 doReturn(rpcData).when(router).onNetconfMessage(any(Document.class), any(NetconfServerSession.class));
73 requestMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/commit.xml");
74 commit.handle(requestMessage, operation);