Merge "Fixup Augmentable and Identifiable methods changing"
[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.net.InetAddresses;
31 import com.google.common.util.concurrent.CheckedFuture;
32 import com.google.common.util.concurrent.Futures;
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.DOMMountPointService;
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     @Mock
80     private DOMMountPointService mountPointService;
81     private ActorRef masterRef;
82     private ProxyDOMDataBroker slaveDataBroker;
83     private List<SourceIdentifier> sourceIdentifiers;
84     private NormalizedNode<?, ?> testNode;
85     private YangInstanceIdentifier instanceIdentifier;
86     private LogicalDatastoreType storeType;
87
88     @Before
89     public void setup() throws Exception {
90         initMocks(this);
91
92         system = ActorSystem.create();
93
94         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
95                 new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9999));
96
97         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
98         doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout();
99         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
100                 DEFAULT_SCHEMA_REPOSITORY, TIMEOUT, mountPointService);
101
102         masterRef = TestActorRef.create(system, props, "master_read");
103
104         sourceIdentifiers = Lists.newArrayList();
105
106         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
107
108         doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
109         doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
110         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
111         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
112         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
113
114         // Create slave data broker for testing proxy
115         slaveDataBroker =
116                 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
117         initializeDataTest();
118         testNode = ImmutableContainerNodeBuilder.create()
119                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "TestQname")))
120                 .withChild(ImmutableNodes.leafNode(QName.create("", "NodeQname"), "foo")).build();
121         instanceIdentifier = YangInstanceIdentifier.EMPTY;
122         storeType = LogicalDatastoreType.CONFIGURATION;
123     }
124
125     @After
126     public void teardown() {
127         JavaTestKit.shutdownActorSystem(system, null, true);
128         system = null;
129     }
130
131     @Test
132     public void testPut() throws Exception {
133         // Test of invoking put on master through slave proxy
134         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
135         wTx.put(storeType, instanceIdentifier, testNode);
136
137         verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
138
139         wTx.cancel();
140     }
141
142     @Test
143     public void testMerge() throws Exception {
144         // Test of invoking merge on master through slave proxy
145         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
146         wTx.merge(storeType, instanceIdentifier, testNode);
147
148         verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
149
150         wTx.cancel();
151     }
152
153     @Test
154     public void testDelete() throws Exception {
155         // Test of invoking delete on master through slave proxy
156         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
157         wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
158         wTx.cancel();
159
160         verify(writeTx, timeout(2000)).delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
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 =
181                 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 }