f1eb5ff1c16e88d729bcfa841be334d0b9eb6749
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / spi / WriteCandidateRunningTxTest.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.client.mdsal.spi;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
16 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_TARGET_NODEID;
17 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
18 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.toId;
19
20 import com.google.common.util.concurrent.Futures;
21 import java.net.InetSocketAddress;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
29 import org.opendaylight.netconf.client.mdsal.AbstractTestModelTest;
30 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
31 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
32 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
33 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
35 import org.opendaylight.yangtools.rfc8528.data.util.EmptyMountPointContext;
36 import org.opendaylight.yangtools.yang.common.Empty;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class WriteCandidateRunningTxTest extends AbstractTestModelTest {
45     @Mock
46     private Rpcs.Normalized rpc;
47     private NetconfBaseOps netconfOps;
48     private RemoteDeviceId id;
49
50     @Before
51     public void setUp() {
52         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(rpc).invokeNetconf(any(), any());
53         netconfOps = new NetconfBaseOps(rpc, new EmptyMountPointContext(SCHEMA_CONTEXT));
54         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
55     }
56
57     @Test
58     public void testSubmit() throws Exception {
59         final WriteCandidateRunningTx tx = new WriteCandidateRunningTx(id, netconfOps, true);
60         //check, if lock is called
61         final ContainerNode candidateLock =
62                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID);
63         final ContainerNode runningLock =
64                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID);
65         verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME, runningLock);
66         verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME, candidateLock);
67         tx.put(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId(), TxTestUtils.getContainerNode());
68         tx.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode());
69         //check, if both edits are called
70         verify(rpc, times(2)).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), any());
71         tx.commit().get();
72         //check, if unlock is called
73         verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME,
74                 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
75         final ContainerNode candidateUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
76                 NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID);
77         final ContainerNode runningUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
78                 NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID);
79         verify(rpc).invokeNetconf(NETCONF_UNLOCK_QNAME, candidateUnlock);
80         verify(rpc).invokeNetconf(NETCONF_UNLOCK_QNAME, runningUnlock);
81     }
82
83     private static ContainerNode getLockContent(final QName op, final NodeIdentifier datastore) {
84         return Builders.containerBuilder()
85             .withNodeIdentifier(toId(op))
86             .withChild(Builders.containerBuilder()
87                 .withNodeIdentifier(NETCONF_TARGET_NODEID)
88                 .withChild(Builders.choiceBuilder()
89                     .withNodeIdentifier(toId(ConfigTarget.QNAME))
90                     .withChild(ImmutableNodes.leafNode(datastore, Empty.value()))
91                     .build())
92                 .build())
93             .build();
94     }
95
96 }