2 * Copyright (c) 2018 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netconf.topology.singleton.impl.actors;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
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;
36 * Adapter for read transaction tests.
38 * @author Thomas Pantelis
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();
47 private DOMDataReadTransaction mockReadTx;
48 private TestProbe probe;
49 private ActorRef actorRef;
51 public void init(DOMDataReadTransaction inMockReadTx, ActorSystem system, ActorRef inActorRef) {
52 this.mockReadTx = inMockReadTx;
53 this.probe = TestProbe.apply(system);
54 this.actorRef = inActorRef;
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());
62 verify(mockReadTx).read(STORE, PATH);
63 final NormalizedNodeMessage response = probe.expectMsgClass(NormalizedNodeMessage.class);
64 assertEquals(NODE, response.getNode());
68 public void testReadEmpty() {
69 when(mockReadTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
70 actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
72 verify(mockReadTx).read(STORE, PATH);
73 probe.expectMsgClass(EmptyReadResponse.class);
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());
82 verify(mockReadTx).read(STORE, PATH);
83 final Failure response = probe.expectMsgClass(Failure.class);
84 assertEquals(cause, response.cause());
88 public void testExists() {
89 when(mockReadTx.exists(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(true));
90 actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
92 verify(mockReadTx).exists(STORE, PATH);
93 probe.expectMsg(true);
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());
102 verify(mockReadTx).exists(STORE, PATH);
103 final Failure response = probe.expectMsgClass(Failure.class);
104 assertEquals(cause, response.cause());