d41e6955f80fe55fff8aa7dd2404044dfce46880
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / actors / ReadTransactionActorTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.actors;
10
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import akka.actor.ActorSystem;
15 import akka.testkit.JavaTestKit;
16 import akka.testkit.TestActorRef;
17 import akka.testkit.TestProbe;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.Futures;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
28 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
31 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
36
37 public class ReadTransactionActorTest {
38
39     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
40     private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
41
42     @Mock
43     private DOMDataReadOnlyTransaction deviceReadTx;
44     private TestProbe probe;
45     private ActorSystem system;
46     private TestActorRef<ReadTransactionActor> actorRef;
47
48     @Before
49     public void setUp() throws Exception {
50         MockitoAnnotations.initMocks(this);
51         system = ActorSystem.apply();
52         probe = TestProbe.apply(system);
53         actorRef = TestActorRef.create(system, ReadTransactionActor.props(deviceReadTx), "testA");
54     }
55
56     @After
57     public void tearDown() throws Exception {
58         JavaTestKit.shutdownActorSystem(system, null, true);
59     }
60
61     @Test
62     public void testRead() throws Exception {
63         final ContainerNode node = Builders.containerBuilder()
64                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "cont")))
65                 .build();
66         when(deviceReadTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.of(node)));
67         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
68         verify(deviceReadTx).read(STORE, PATH);
69         probe.expectMsgClass(NormalizedNodeMessage.class);
70     }
71
72     @Test
73     public void testReadEmpty() throws Exception {
74         when(deviceReadTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
75         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
76         verify(deviceReadTx).read(STORE, PATH);
77         probe.expectMsgClass(EmptyReadResponse.class);
78     }
79
80     @Test
81     public void testReadFailure() throws Exception {
82         final ReadFailedException cause = new ReadFailedException("fail");
83         when(deviceReadTx.read(STORE, PATH)).thenReturn(Futures.immediateFailedCheckedFuture(cause));
84         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
85         verify(deviceReadTx).read(STORE, PATH);
86         probe.expectMsg(cause);
87     }
88
89     @Test
90     public void testExists() throws Exception {
91         when(deviceReadTx.exists(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(true));
92         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
93         verify(deviceReadTx).exists(STORE, PATH);
94         probe.expectMsg(true);
95     }
96
97     @Test
98     public void testExistsFailure() throws Exception {
99         final ReadFailedException cause = new ReadFailedException("fail");
100         when(deviceReadTx.exists(STORE, PATH)).thenReturn(Futures.immediateFailedCheckedFuture(cause));
101         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
102         verify(deviceReadTx).exists(STORE, PATH);
103         probe.expectMsg(cause);
104     }
105 }