2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.pipeline.tx;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import akka.actor.ActorSystem;
21 import akka.dispatch.ExecutionContexts;
22 import akka.dispatch.Futures;
23 import com.google.common.base.Optional;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.MoreExecutors;
26 import java.net.InetSocketAddress;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
34 import org.opendaylight.netconf.topology.pipeline.ProxyNetconfDeviceDataBroker;
35 import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 public class ProxyReadOnlyTransactionTest {
40 private static final RemoteDeviceId REMOTE_DEVICE_ID = new RemoteDeviceId("testing-device", new InetSocketAddress(9999));
41 private static final YangInstanceIdentifier path = YangInstanceIdentifier.create();
44 private ProxyNetconfDeviceDataBroker mockedProxyDataBroker;
47 private ActorSystem mockedActorSystem;
50 private NormalizedNodeMessage mockedNodeMessage;
53 private NormalizedNode mockedNode;
55 private ProxyReadOnlyTransaction proxyReadOnlyTx;
59 MockitoAnnotations.initMocks(this);
61 when(mockedActorSystem.dispatcher()).thenReturn(ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
62 when(mockedNodeMessage.getNode()).thenReturn(mockedNode);
64 proxyReadOnlyTx = new ProxyReadOnlyTransaction(mockedActorSystem, REMOTE_DEVICE_ID, mockedProxyDataBroker);
68 public void testSuccessfulRead() throws ReadFailedException {
69 when(mockedProxyDataBroker.read(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class)))
70 .thenReturn(Futures.successful(Optional.of(mockedNodeMessage)));
71 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readResultFuture = proxyReadOnlyTx.read(LogicalDatastoreType.CONFIGURATION, path);
72 verify(mockedProxyDataBroker).read(eq(LogicalDatastoreType.CONFIGURATION), eq(path));
73 assertTrue(readResultFuture.isDone());
74 assertEquals(readResultFuture.checkedGet().get(), mockedNode);
78 public void testFailedRead() {
79 when(mockedProxyDataBroker.read(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class)))
80 .thenReturn(Futures.<Optional<NormalizedNodeMessage>>failed(new ReadFailedException("Test read failed!")));
81 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readResultFuture = proxyReadOnlyTx.read(LogicalDatastoreType.CONFIGURATION, path);
82 verify(mockedProxyDataBroker).read(eq(LogicalDatastoreType.CONFIGURATION), eq(path));
83 assertTrue(readResultFuture.isDone());
85 readResultFuture.checkedGet();
86 fail("Exception expected");
87 } catch(Exception e) {
88 assertTrue(e instanceof ReadFailedException);
93 public void testDataOnPathDoesNotExistPathRead() throws ReadFailedException {
94 when(mockedProxyDataBroker.read(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class)))
95 .thenReturn(Futures.successful(Optional.absent()));
96 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readResultFuture = proxyReadOnlyTx.read(LogicalDatastoreType.CONFIGURATION, path);
97 verify(mockedProxyDataBroker).read(eq(LogicalDatastoreType.CONFIGURATION), eq(path));
98 assertTrue(readResultFuture.isDone());
99 assertTrue(!readResultFuture.checkedGet().isPresent());
103 public void testFailedExists() {
104 when(mockedProxyDataBroker.exists(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class)))
105 .thenReturn(Futures.<Boolean>failed(new ReadFailedException("Test read failed!")));
106 CheckedFuture existsFuture = proxyReadOnlyTx.exists(LogicalDatastoreType.OPERATIONAL, path);
107 verify(mockedProxyDataBroker).exists(eq(LogicalDatastoreType.OPERATIONAL), eq(path));
108 assertTrue(existsFuture.isDone());
110 existsFuture.checkedGet();
111 fail("Exception expected");
112 } catch(Exception e) {
113 assertTrue(e instanceof ReadFailedException);
118 public void testExists() throws Exception {
119 when(mockedProxyDataBroker.exists(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class)))
120 .thenReturn(Futures.successful(true));
121 CheckedFuture<Boolean, ReadFailedException> existsFuture = proxyReadOnlyTx.exists(LogicalDatastoreType.OPERATIONAL, path);
122 verify(mockedProxyDataBroker).exists(eq(LogicalDatastoreType.OPERATIONAL), eq(path));
123 assertTrue(existsFuture.isDone());
124 assertTrue(existsFuture.checkedGet());