Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / netconf-topology / src / test / java / org / opendaylight / netconf / topology / pipeline / tx / ProxyReadOnlyTransactionTest.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.pipeline.tx;
10
11
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;
19
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;
38
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();
42
43     @Mock
44     private ProxyNetconfDeviceDataBroker mockedProxyDataBroker;
45
46     @Mock
47     private ActorSystem mockedActorSystem;
48
49     @Mock
50     private NormalizedNodeMessage mockedNodeMessage;
51
52     @Mock
53     private NormalizedNode mockedNode;
54
55     private ProxyReadOnlyTransaction proxyReadOnlyTx;
56
57     @Before
58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60
61         when(mockedActorSystem.dispatcher()).thenReturn(ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
62         when(mockedNodeMessage.getNode()).thenReturn(mockedNode);
63
64         proxyReadOnlyTx = new ProxyReadOnlyTransaction(mockedActorSystem, REMOTE_DEVICE_ID, mockedProxyDataBroker);
65     }
66
67     @Test
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);
75     }
76
77     @Test
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());
84         try {
85             readResultFuture.checkedGet();
86             fail("Exception expected");
87         } catch(Exception e) {
88             assertTrue(e instanceof ReadFailedException);
89         }
90     }
91
92     @Test
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());
100     }
101
102     @Test
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());
109         try {
110             existsFuture.checkedGet();
111             fail("Exception expected");
112         } catch(Exception e) {
113             assertTrue(e instanceof ReadFailedException);
114         }
115     }
116
117     @Test
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());
125     }
126 }