Bump upstreams
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / spi / NetconfDeviceWriteOnlyTxTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.junit.Assert.assertThrows;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.atMost;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.inOrder;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.never;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
20 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_CANDIDATE_NODEID;
21 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
22 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.NETCONF_RUNNING_NODEID;
23
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import java.net.InetSocketAddress;
27 import java.util.concurrent.ExecutionException;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.InOrder;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
37 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
38 import org.opendaylight.netconf.client.mdsal.AbstractBaseSchemasTest;
39 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
40 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
41 import org.opendaylight.netconf.client.mdsal.impl.NetconfBaseOps;
42 import org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
44 import org.opendaylight.yangtools.yang.common.ErrorTag;
45 import org.opendaylight.yangtools.yang.common.ErrorType;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
52 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
53
54 @RunWith(MockitoJUnitRunner.StrictStubs.class)
55 public class NetconfDeviceWriteOnlyTxTest extends AbstractBaseSchemasTest {
56     private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
57
58     @Mock
59     private Rpcs.Normalized rpc;
60     private YangInstanceIdentifier yangIId;
61
62     @Before
63     public void setUp() {
64         final ListenableFuture<DefaultDOMRpcResult> successFuture =
65                 Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null));
66
67         doReturn(successFuture)
68                 .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
69                 .doReturn(successFuture)
70                 .when(rpc).invokeNetconf(any(), any());
71
72         yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
73     }
74
75     @Test
76     public void testIgnoreNonVisibleData() {
77         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)),
78                 false);
79         final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
80         tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
81                 .create(new NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
82         tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
83                 .create(new NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
84
85         verify(rpc, atMost(1)).invokeNetconf(any(), any());
86     }
87
88     @Test
89     public void testDiscardChanges() {
90         final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
91             .commit();
92         assertThrows(ExecutionException.class, () -> Futures.getDone(future));
93
94         // verify discard changes was sent
95         final InOrder inOrder = inOrder(rpc);
96         inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
97             NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_NODEID));
98         inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME,
99             NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
100         inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME,
101             DISCARD_CHANGES_RPC_CONTENT);
102         inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
103             NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_NODEID));
104     }
105
106     @Test
107     public void testFailedCommit() {
108         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
109             .doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(RpcResultBuilder.newError(ErrorType.APPLICATION,
110                 new ErrorTag("a"), "m")))).when(rpc).invokeNetconf(any(), any());
111
112         final var future = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(MountPointContext.class)), false)
113             .commit();
114
115         assertThrows(ExecutionException.class, () -> Futures.getDone(future));
116     }
117
118     @Test
119     public void testDiscardChangesNotSentWithoutCandidate() {
120         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
121             .doReturn(Futures.immediateFailedFuture(new IllegalStateException("Failed tx")))
122             .when(rpc).invokeNetconf(any(), any());
123
124         final WriteRunningTx tx = new WriteRunningTx(id,
125             new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchemaWithNotifications().getMountPointContext()), false);
126
127         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
128         tx.commit();
129         // verify discard changes was sent
130         final InOrder inOrder = inOrder(rpc);
131         inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME,
132                 NetconfBaseOps.getLockContent(NETCONF_RUNNING_NODEID));
133         inOrder.verify(rpc).invokeNetconf(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), any());
134         inOrder.verify(rpc).invokeNetconf(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME,
135                 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_NODEID));
136     }
137
138     @Test
139     public void testListenerSuccess() throws Exception {
140         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult((ContainerNode) null)))
141                 .when(rpc).invokeNetconf(any(), any());
142         final WriteCandidateTx tx = new WriteCandidateTx(
143                 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
144         final TxListener listener = mock(TxListener.class);
145         tx.addListener(listener);
146         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
147         tx.commit();
148         verify(listener).onTransactionSubmitted(tx);
149         verify(listener).onTransactionSuccessful(tx);
150         verify(listener, never()).onTransactionFailed(eq(tx), any());
151         verify(listener, never()).onTransactionCancelled(tx);
152     }
153
154     @Test
155     public void testListenerCancellation() throws Exception {
156         final WriteCandidateTx tx = new WriteCandidateTx(
157                 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
158         final TxListener listener = mock(TxListener.class);
159         tx.addListener(listener);
160         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
161         tx.cancel();
162         verify(listener).onTransactionCancelled(tx);
163         verify(listener, never()).onTransactionSubmitted(tx);
164         verify(listener, never()).onTransactionSuccessful(tx);
165         verify(listener, never()).onTransactionFailed(eq(tx), any());
166     }
167
168     @Test
169     public void testListenerFailure() throws Exception {
170         final IllegalStateException cause = new IllegalStateException("Failed tx");
171         doReturn(Futures.immediateFailedFuture(cause)).when(rpc).invokeNetconf(any(), any());
172         final WriteCandidateTx tx = new WriteCandidateTx(
173                 id, new NetconfBaseOps(rpc, BASE_SCHEMAS.getBaseSchema().getMountPointContext()), false);
174         final TxListener listener = mock(TxListener.class);
175         tx.addListener(listener);
176         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
177         tx.commit();
178         final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
179         verify(listener).onTransactionSubmitted(tx);
180         verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
181         Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
182         verify(listener, never()).onTransactionSuccessful(tx);
183         verify(listener, never()).onTransactionCancelled(tx);
184     }
185 }