Bug 8152: Transaction is already opened
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / WriteOnlyTransactionTest.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.topology.singleton.impl.tx;
10
11 import static junit.framework.TestCase.assertNull;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.timeout;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.MockitoAnnotations.initMocks;
21 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
22
23 import akka.actor.ActorRef;
24 import akka.actor.ActorSystem;
25 import akka.actor.Props;
26 import akka.pattern.Patterns;
27 import akka.testkit.JavaTestKit;
28 import akka.testkit.TestActorRef;
29 import akka.util.Timeout;
30 import com.google.common.collect.Lists;
31 import com.google.common.util.concurrent.CheckedFuture;
32 import com.google.common.util.concurrent.Futures;
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35 import java.net.UnknownHostException;
36 import java.util.List;
37 import java.util.concurrent.TimeUnit;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.mockito.Mock;
44 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
45 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
46 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
47 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
48 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
50 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
51 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMDataBroker;
52 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
53 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
54 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
55 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
60 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
61 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
62 import scala.concurrent.Await;
63 import scala.concurrent.Future;
64 import scala.concurrent.duration.Duration;
65
66 public class WriteOnlyTransactionTest {
67     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
68     private static final int TIMEOUT_SEC = 5;
69     private static ActorSystem system;
70
71     @Rule
72     public final ExpectedException exception = ExpectedException.none();
73
74     private ActorRef masterRef;
75     private ProxyDOMDataBroker slaveDataBroker;
76     private List<SourceIdentifier> sourceIdentifiers;
77     @Mock
78     private DOMDataBroker deviceDataBroker;
79     @Mock
80     private DOMDataWriteTransaction writeTx;
81     @Mock
82     private DOMRpcService domRpcService;
83
84     @Before
85     public void setup() throws UnknownHostException {
86         initMocks(this);
87
88         system = ActorSystem.create();
89
90         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
91                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
92
93         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
94         doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
95         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
96                 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT);
97
98         masterRef = TestActorRef.create(system, props, "master_read");
99
100         sourceIdentifiers = Lists.newArrayList();
101
102         writeTx = mock(DOMDataWriteTransaction.class);
103         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
104
105         doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
106         doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
107
108         // Create slave data broker for testing proxy
109         slaveDataBroker =
110                 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
111     }
112
113     @After
114     public void teardown() {
115         JavaTestKit.shutdownActorSystem(system, null, true);
116         system = null;
117     }
118
119     @Test
120     public void testPut() throws Exception {
121         /* Initialize data on master */
122
123         initializeDataTest();
124
125         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
126         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
127         final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
128                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
129                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
130
131         // Test of invoking put on master through slave proxy
132
133         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
134
135         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
136         wTx.put(storeType, instanceIdentifier, testNode);
137
138         verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
139
140         wTx.cancel();
141
142     }
143
144     @Test
145     public void testMerge() throws Exception {
146
147         /* Initialize data on master */
148
149         initializeDataTest();
150
151         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
152         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
153         final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
154                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
155                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
156         // Test of invoking merge on master through slave proxy
157
158         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
159         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
160         wTx.merge(storeType, instanceIdentifier, testNode);
161
162         verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
163
164         wTx.cancel();
165
166     }
167
168     @Test
169     public void testDelete() throws Exception {
170
171         /* Initialize data on master */
172
173         initializeDataTest();
174
175         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
176         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
177         // Test of invoking delete on master through slave proxy
178
179         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
180         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
181         wTx.delete(storeType, instanceIdentifier);
182         wTx.cancel();
183
184         verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier);
185
186     }
187
188     @Test
189     public void testSubmit() throws Exception {
190
191         /* Initialize data on master */
192
193         initializeDataTest();
194
195         // Without Tx
196
197         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
198         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
199         doReturn(resultSubmit).when(writeTx).submit();
200
201         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
202
203         final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
204
205         assertNull(result);
206     }
207
208     @Test
209     public void testSubmitWithOperation() throws Exception {
210
211         /* Initialize data on master */
212
213         initializeDataTest();
214         // With Tx
215         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
216         doNothing().when(writeTx).delete(any(), any());
217         wTx.delete(LogicalDatastoreType.CONFIGURATION,
218                 YangInstanceIdentifier.EMPTY);
219
220         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
221         doReturn(resultSubmitTx).when(writeTx).submit();
222
223         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
224
225         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
226
227         assertNull(resultTx);
228     }
229
230     @Test
231     public void testSubmitFail() throws Exception {
232
233         /* Initialize data on master */
234
235         initializeDataTest();
236         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
237         wTx.delete(LogicalDatastoreType.CONFIGURATION,
238                 YangInstanceIdentifier.EMPTY);
239
240         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
241         final CheckedFuture<Void,TransactionCommitFailedException> resultThrowable =
242                 Futures.immediateFailedCheckedFuture(throwable);
243
244         doReturn(resultThrowable).when(writeTx).submit();
245
246         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
247                 wTx.submit();
248
249         exception.expect(TransactionCommitFailedException.class);
250         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
251     }
252
253     @Test
254     public void testCancel() throws Exception {
255
256         /* Initialize data on master */
257
258         initializeDataTest();
259
260         // Without Tx
261         doReturn(true).when(writeTx).cancel();
262         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
263         final Boolean resultFalseNoTx = wTx.cancel();
264         assertEquals(true, resultFalseNoTx);
265     }
266
267     @Test
268     public void testCancelWithOperation() throws Exception {
269
270         /* Initialize data on master */
271
272         initializeDataTest();
273
274         // With Tx, readWriteTx test
275
276         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
277         doNothing().when(writeTx).delete(any(), any());
278         wTx.delete(LogicalDatastoreType.CONFIGURATION,
279                 YangInstanceIdentifier.EMPTY);
280
281         doReturn(true).when(writeTx).cancel();
282         final Boolean resultTrue = wTx.cancel();
283         assertEquals(true, resultTrue);
284
285         final Boolean resultFalse = wTx.cancel();
286         assertEquals(false, resultFalse);
287
288     }
289
290     private void initializeDataTest() throws Exception {
291         final Future<Object> initialDataToActor =
292                 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
293                                 domRpcService), TIMEOUT);
294
295         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
296
297         assertTrue(success instanceof MasterActorDataInitialized);
298     }
299
300 }