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