Convert ProxyDOMDataBroker tx creation to async
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / actors / ReadTransactionActorTestAdapter.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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 package org.opendaylight.netconf.topology.singleton.impl.actors;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
13
14 import akka.actor.ActorRef;
15 import akka.actor.ActorSystem;
16 import akka.actor.Status.Failure;
17 import akka.testkit.TestProbe;
18 import akka.util.Timeout;
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.Futures;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
26 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
34
35 /**
36  * Adapter for read transaction tests.
37  *
38  * @author Thomas Pantelis
39  */
40 public abstract class ReadTransactionActorTestAdapter {
41     static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
42     static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
43     static final Timeout TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
44     static final NormalizedNode<?, ?> NODE = Builders.containerBuilder()
45             .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "cont"))).build();
46
47     private DOMDataReadTransaction mockReadTx;
48     private TestProbe probe;
49     private ActorRef actorRef;
50
51     public void init(DOMDataReadTransaction inMockReadTx, ActorSystem system, ActorRef inActorRef) {
52         this.mockReadTx = inMockReadTx;
53         this.probe = TestProbe.apply(system);
54         this.actorRef = inActorRef;
55     }
56
57     @Test
58     public void testRead() {
59         when(mockReadTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.of(NODE)));
60         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
61
62         verify(mockReadTx).read(STORE, PATH);
63         final NormalizedNodeMessage response = probe.expectMsgClass(NormalizedNodeMessage.class);
64         assertEquals(NODE, response.getNode());
65     }
66
67     @Test
68     public void testReadEmpty() {
69         when(mockReadTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
70         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
71
72         verify(mockReadTx).read(STORE, PATH);
73         probe.expectMsgClass(EmptyReadResponse.class);
74     }
75
76     @Test
77     public void testReadFailure() {
78         final ReadFailedException cause = new ReadFailedException("fail");
79         when(mockReadTx.read(STORE, PATH)).thenReturn(Futures.immediateFailedCheckedFuture(cause));
80         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
81
82         verify(mockReadTx).read(STORE, PATH);
83         final Failure response = probe.expectMsgClass(Failure.class);
84         assertEquals(cause, response.cause());
85     }
86
87     @Test
88     public void testExists() {
89         when(mockReadTx.exists(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(true));
90         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
91
92         verify(mockReadTx).exists(STORE, PATH);
93         probe.expectMsg(true);
94     }
95
96     @Test
97     public void testExistsFailure() {
98         final ReadFailedException cause = new ReadFailedException("fail");
99         when(mockReadTx.exists(STORE, PATH)).thenReturn(Futures.immediateFailedCheckedFuture(cause));
100         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
101
102         verify(mockReadTx).exists(STORE, PATH);
103         final Failure response = probe.expectMsgClass(Failure.class);
104         assertEquals(cause, response.cause());
105     }
106 }