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