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