Cleanup use of deprecated constructs
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / 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.sal.connect.netconf.sal.tx;
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.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TARGET_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
19
20 import java.net.InetSocketAddress;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
29 import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
30 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
31 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
32 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.copy.config.input.target.ConfigTarget;
34 import org.opendaylight.yangtools.rcf8528.data.util.EmptyMountPointContext;
35 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
36 import org.opendaylight.yangtools.yang.common.Empty;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class WriteCandidateRunningTxTest extends AbstractTestModelTest {
46     @Mock
47     private DOMRpcService rpc;
48     private NetconfBaseOps netconfOps;
49     private RemoteDeviceId id;
50
51     @Before
52     public void setUp() {
53         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpc).invokeRpc(any(), any());
54         netconfOps = new NetconfBaseOps(rpc, new EmptyMountPointContext(SCHEMA_CONTEXT));
55         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
56     }
57
58     @Test
59     public void testSubmit() throws Exception {
60         final WriteCandidateRunningTx tx = new WriteCandidateRunningTx(id, netconfOps, true);
61         //check, if lock is called
62         final ContainerNode candidateLock =
63                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME);
64         final ContainerNode runningLock =
65                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME);
66         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), runningLock);
67         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), candidateLock);
68         tx.put(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId(), TxTestUtils.getContainerNode());
69         tx.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode());
70         //check, if both edits are called
71         verify(rpc, times(2)).invokeRpc(
72                 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any());
73         tx.commit().get();
74         //check, if unlock is called
75         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME),
76                 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
77         final ContainerNode candidateUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
78                 NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME);
79         final ContainerNode runningUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
80                 NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME);
81         verify(rpc).invokeRpc(SchemaPath.create(true, NETCONF_UNLOCK_QNAME), candidateUnlock);
82         verify(rpc).invokeRpc(SchemaPath.create(true, NETCONF_UNLOCK_QNAME), runningUnlock);
83     }
84
85     private static ContainerNode getLockContent(final QName op, final QName datastore) {
86         final LeafNode<Object> datastoreLeaf = Builders.leafBuilder().withNodeIdentifier(toId(datastore))
87                 .withValue(Empty.getInstance()).build();
88         final ChoiceNode choice = Builders.choiceBuilder()
89                 .withNodeIdentifier(toId(ConfigTarget.QNAME))
90                 .withChild(datastoreLeaf)
91                 .build();
92         final ContainerNode target = Builders.containerBuilder()
93                 .withNodeIdentifier(toId(NETCONF_TARGET_QNAME))
94                 .withChild(choice).build();
95         return Builders.containerBuilder()
96                 .withNodeIdentifier(toId(op))
97                 .withChild(target)
98                 .build();
99     }
100
101 }