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