Integrate MRI projects for Neon
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / tx / 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
9 package org.opendaylight.netconf.sal.connect.netconf.sal.tx;
10
11 import static org.junit.Assert.fail;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.ArgumentMatchers.isNull;
15 import static org.mockito.Mockito.atMost;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.inOrder;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.never;
20 import static org.mockito.Mockito.verify;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_FILTER_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
25
26 import com.google.common.util.concurrent.CheckedFuture;
27 import com.google.common.util.concurrent.Futures;
28 import java.net.InetSocketAddress;
29 import java.util.concurrent.ExecutionException;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.InOrder;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
39 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
40 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
41 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
42 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
43 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
45 import org.opendaylight.yangtools.yang.common.RpcError;
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.schema.MapNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
53
54 public class NetconfDeviceWriteOnlyTxTest {
55
56     private final RemoteDeviceId id = new RemoteDeviceId("test-mount", new InetSocketAddress(99));
57
58     @Mock
59     private DOMRpcService rpc;
60     private YangInstanceIdentifier yangIId;
61
62     @Before
63     public void setUp() throws Exception {
64         MockitoAnnotations.initMocks(this);
65
66         final CheckedFuture<DefaultDOMRpcResult, Exception> successFuture =
67                 Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null));
68
69         doReturn(successFuture)
70                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
71                 .doReturn(successFuture)
72                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
73
74         yangIId = YangInstanceIdentifier.builder().node(NetconfState.QNAME).build();
75     }
76
77     @Test
78     public void testIgnoreNonVisibleData() {
79         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
80                 false);
81         final MapNode emptyList = ImmutableNodes.mapNodeBuilder(NETCONF_FILTER_QNAME).build();
82         tx.merge(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
83                 .create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
84         tx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier
85                 .create(new YangInstanceIdentifier.NodeIdentifier(NETCONF_FILTER_QNAME)), emptyList);
86
87         verify(rpc, atMost(1)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
88     }
89
90     @Test
91     public void testDiscardChanges() throws InterruptedException {
92         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
93                 .when(rpc).invokeRpc(any(SchemaPath.class), isNull());
94
95         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
96                 false);
97         try {
98             tx.commit().get();
99         } catch (final ExecutionException e) {
100             // verify discard changes was sent
101             final InOrder inOrder = inOrder(rpc);
102             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME),
103                     NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
104             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME),
105                     NetconfMessageTransformUtil.COMMIT_RPC_CONTENT);
106             inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME)),
107                     isNull());
108             inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME),
109                     NetconfBaseOps.getUnLockContent(NETCONF_CANDIDATE_QNAME));
110             return;
111         }
112
113         fail("Submit should fail");
114     }
115
116     @Test
117     public void testFailedCommit() throws Exception {
118         final CheckedFuture<DefaultDOMRpcResult, Exception> rpcErrorFuture = Futures.immediateCheckedFuture(
119                 new DefaultDOMRpcResult(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "a", "m")));
120
121         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
122         .doReturn(rpcErrorFuture).when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
123
124         final WriteCandidateTx tx = new WriteCandidateTx(id, new NetconfBaseOps(rpc, mock(SchemaContext.class)),
125                 false);
126
127         try {
128             tx.commit().get();
129             fail("Submit should fail");
130         } catch (final ExecutionException e) {
131             // Intended
132         }
133     }
134
135     @Test
136     public void testDiscardChangesNotSentWithoutCandidate() {
137         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
138                 .doReturn(Futures.immediateFailedCheckedFuture(new IllegalStateException("Failed tx")))
139                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
140
141         final WriteRunningTx tx = new WriteRunningTx(
142                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext()), false);
143
144         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
145         tx.commit();
146         // verify discard changes was sent
147         final InOrder inOrder = inOrder(rpc);
148         inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_LOCK_QNAME),
149                 NetconfBaseOps.getLockContent(NETCONF_RUNNING_QNAME));
150         inOrder.verify(rpc).invokeRpc(eq(toPath(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME)),
151                 any(NormalizedNode.class));
152         inOrder.verify(rpc).invokeRpc(toPath(NetconfMessageTransformUtil.NETCONF_UNLOCK_QNAME),
153                 NetconfBaseOps.getUnLockContent(NETCONF_RUNNING_QNAME));
154     }
155
156     @Test
157     public void testListenerSuccess() throws Exception {
158         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
159                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
160         final WriteCandidateTx tx = new WriteCandidateTx(
161                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
162         final TxListener listener = mock(TxListener.class);
163         tx.addListener(listener);
164         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
165         tx.commit();
166         verify(listener).onTransactionSubmitted(tx);
167         verify(listener).onTransactionSuccessful(tx);
168         verify(listener, never()).onTransactionFailed(eq(tx), any());
169         verify(listener, never()).onTransactionCancelled(tx);
170     }
171
172     @Test
173     public void testListenerCancellation() throws Exception {
174         doReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult((NormalizedNode<?, ?>) null)))
175                 .when(rpc).invokeRpc(any(SchemaPath.class), isNull());
176         final WriteCandidateTx tx = new WriteCandidateTx(
177                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
178         final TxListener listener = mock(TxListener.class);
179         tx.addListener(listener);
180         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
181         tx.cancel();
182         verify(listener).onTransactionCancelled(tx);
183         verify(listener, never()).onTransactionSubmitted(tx);
184         verify(listener, never()).onTransactionSuccessful(tx);
185         verify(listener, never()).onTransactionFailed(eq(tx), any());
186     }
187
188     @Test
189     public void testListenerFailure() throws Exception {
190         final IllegalStateException cause = new IllegalStateException("Failed tx");
191         doReturn(Futures.immediateFailedCheckedFuture(cause))
192                 .when(rpc).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
193         final WriteCandidateTx tx = new WriteCandidateTx(
194                 id, new NetconfBaseOps(rpc, BaseSchema.BASE_NETCONF_CTX.getSchemaContext()), false);
195         final TxListener listener = mock(TxListener.class);
196         tx.addListener(listener);
197         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
198         tx.commit();
199         final ArgumentCaptor<Exception> excCaptor = ArgumentCaptor.forClass(Exception.class);
200         verify(listener).onTransactionSubmitted(tx);
201         verify(listener).onTransactionFailed(eq(tx), excCaptor.capture());
202         Assert.assertEquals(cause, excCaptor.getValue().getCause().getCause());
203         verify(listener, never()).onTransactionSuccessful(tx);
204         verify(listener, never()).onTransactionCancelled(tx);
205     }
206 }