Migrate netconf users of submit() to commit()
[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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_TARGET_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
20
21 import com.google.common.util.concurrent.Futures;
22 import java.net.InetSocketAddress;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
29 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
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.yang.common.Empty;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43
44 public class WriteCandidateRunningTxTest {
45     @Mock
46     private DOMRpcService rpc;
47     private NetconfBaseOps netconfOps;
48     private RemoteDeviceId id;
49
50     @Before
51     public void setUp() throws Exception {
52         MockitoAnnotations.initMocks(this);
53         final SchemaContext schemaContext =
54                 YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
55         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult())).when(rpc).invokeRpc(any(), any());
56         netconfOps = new NetconfBaseOps(rpc, schemaContext);
57         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
58     }
59
60     @Test
61     public void testSubmit() throws Exception {
62         final WriteCandidateRunningTx tx = new WriteCandidateRunningTx(id, netconfOps, true);
63         //check, if lock is called
64         final ContainerNode candidateLock =
65                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME);
66         final ContainerNode runningLock =
67                 getLockContent(NETCONF_LOCK_QNAME, NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME);
68         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), runningLock);
69         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_LOCK_QNAME), candidateLock);
70         tx.put(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getContainerId(), TxTestUtils.getContainerNode());
71         tx.merge(LogicalDatastoreType.CONFIGURATION, TxTestUtils.getLeafId(), TxTestUtils.getLeafNode());
72         //check, if both edits are called
73         verify(rpc, times(2)).invokeRpc(
74                 eq(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)), any());
75         tx.commit().get();
76         //check, if unlock is called
77         verify(rpc).invokeRpc(SchemaPath.create(true, NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME),
78                 NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
79         final ContainerNode candidateUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
80                 NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME);
81         final ContainerNode runningUnlock = getLockContent(NETCONF_UNLOCK_QNAME,
82                 NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME);
83         verify(rpc).invokeRpc(SchemaPath.create(true, NETCONF_UNLOCK_QNAME), candidateUnlock);
84         verify(rpc).invokeRpc(SchemaPath.create(true, NETCONF_UNLOCK_QNAME), runningUnlock);
85     }
86
87     private static ContainerNode getLockContent(final QName op, final QName datastore) {
88         final LeafNode<Object> datastoreLeaf = Builders.leafBuilder().withNodeIdentifier(toId(datastore))
89                 .withValue(Empty.getInstance()).build();
90         final ChoiceNode choice = Builders.choiceBuilder()
91                 .withNodeIdentifier(toId(ConfigTarget.QNAME))
92                 .withChild(datastoreLeaf)
93                 .build();
94         final ContainerNode target = Builders.containerBuilder()
95                 .withNodeIdentifier(toId(NETCONF_TARGET_QNAME))
96                 .withChild(choice).build();
97         return Builders.containerBuilder()
98                 .withNodeIdentifier(toId(op))
99                 .withChild(target)
100                 .build();
101     }
102
103 }