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