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