Bump MRI upstreams
[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.doReturn;
12 import static org.mockito.Mockito.verify;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
16
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSystem;
19 import akka.actor.Status.Failure;
20 import akka.testkit.TestProbe;
21 import akka.util.Timeout;
22 import java.util.Optional;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.common.api.ReadFailedException;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadOperations;
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.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37
38 /**
39  * Adapter for read transaction tests.
40  *
41  * @author Thomas Pantelis
42  */
43 public abstract class ReadTransactionActorTestAdapter {
44     static final YangInstanceIdentifier PATH = YangInstanceIdentifier.empty();
45     static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
46     static final Timeout TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
47     static final ContainerNode NODE = Builders.containerBuilder()
48             .withNodeIdentifier(new NodeIdentifier(QName.create("", "cont")))
49             .build();
50
51     private DOMDataTreeReadOperations mockReadTx;
52     private TestProbe probe;
53     private ActorRef actorRef;
54
55     public void init(final DOMDataTreeReadOperations inMockReadTx, final ActorSystem system,
56             final ActorRef inActorRef) {
57         this.mockReadTx = inMockReadTx;
58         this.probe = TestProbe.apply(system);
59         this.actorRef = inActorRef;
60     }
61
62     @Test
63     public void testRead() {
64         doReturn(immediateFluentFuture(Optional.of(NODE))).when(mockReadTx).read(STORE, PATH);
65         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
66
67         verify(mockReadTx).read(STORE, PATH);
68         final NormalizedNodeMessage response = probe.expectMsgClass(NormalizedNodeMessage.class);
69         assertEquals(NODE, response.getNode());
70     }
71
72     @Test
73     public void testReadEmpty() {
74         doReturn(immediateFluentFuture(Optional.empty())).when(mockReadTx).read(STORE, PATH);
75         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
76
77         verify(mockReadTx).read(STORE, PATH);
78         probe.expectMsgClass(EmptyReadResponse.class);
79     }
80
81     @Test
82     public void testReadFailure() {
83         final ReadFailedException cause = new ReadFailedException("fail");
84         doReturn(immediateFailedFluentFuture(cause)).when(mockReadTx).read(STORE, PATH);
85         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
86
87         verify(mockReadTx).read(STORE, PATH);
88         final Failure response = probe.expectMsgClass(Failure.class);
89         assertEquals(cause, response.cause());
90     }
91
92     @Test
93     public void testExists() {
94         doReturn(immediateTrueFluentFuture()).when(mockReadTx).exists(STORE, PATH);
95         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
96
97         verify(mockReadTx).exists(STORE, PATH);
98         probe.expectMsg(Boolean.TRUE);
99     }
100
101     @Test
102     public void testExistsFailure() {
103         final ReadFailedException cause = new ReadFailedException("fail");
104         doReturn(immediateFailedFluentFuture(cause)).when(mockReadTx).exists(STORE, PATH);
105         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
106
107         verify(mockReadTx).exists(STORE, PATH);
108         final Failure response = probe.expectMsgClass(Failure.class);
109         assertEquals(cause, response.cause());
110     }
111 }