2ccf8744497b6e590d23556e47f700fad48f33ed
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / actors / ReadWriteTransactionActorTest.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.timeout;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
14
15 import akka.actor.ActorSystem;
16 import akka.pattern.Patterns;
17 import akka.testkit.JavaTestKit;
18 import akka.testkit.TestActorRef;
19 import akka.testkit.TestProbe;
20 import akka.util.Timeout;
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.Futures;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
34 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
35 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
36 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
37 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
38 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
39 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
40 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
41 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
42 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitFailedReply;
43 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
44 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.common.RpcError;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
51 import scala.concurrent.Await;
52 import scala.concurrent.Future;
53 import scala.concurrent.duration.Duration;
54
55 public class ReadWriteTransactionActorTest {
56
57     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
58     private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
59     private static final Timeout TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
60
61     @Mock
62     private DOMDataReadWriteTransaction deviceReadWriteTx;
63     private TestProbe probe;
64     private ActorSystem system;
65     private TestActorRef<WriteTransactionActor> actorRef;
66     private ContainerNode node;
67
68     @Before
69     public void setUp() throws Exception {
70         MockitoAnnotations.initMocks(this);
71         system = ActorSystem.apply();
72         probe = TestProbe.apply(system);
73         node = Builders.containerBuilder()
74                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "cont")))
75                 .build();
76         actorRef = TestActorRef.create(system, ReadWriteTransactionActor.props(deviceReadWriteTx,
77                 Duration.apply(2, TimeUnit.SECONDS)), "testA");
78     }
79
80     @After
81     public void tearDown() throws Exception {
82         JavaTestKit.shutdownActorSystem(system, null, true);
83     }
84
85     @Test
86     public void testRead() throws Exception {
87         when(deviceReadWriteTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.of(node)));
88         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
89         verify(deviceReadWriteTx).read(STORE, PATH);
90         probe.expectMsgClass(NormalizedNodeMessage.class);
91     }
92
93     @Test
94     public void testReadEmpty() throws Exception {
95         when(deviceReadWriteTx.read(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
96         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
97         verify(deviceReadWriteTx).read(STORE, PATH);
98         probe.expectMsgClass(EmptyReadResponse.class);
99     }
100
101     @Test
102     public void testReadFailure() throws Exception {
103         final ReadFailedException cause = new ReadFailedException("fail");
104         when(deviceReadWriteTx.read(STORE, PATH)).thenReturn(Futures.immediateFailedCheckedFuture(cause));
105         actorRef.tell(new ReadRequest(STORE, PATH), probe.ref());
106         verify(deviceReadWriteTx).read(STORE, PATH);
107         probe.expectMsg(cause);
108     }
109
110     @Test
111     public void testExists() throws Exception {
112         when(deviceReadWriteTx.exists(STORE, PATH)).thenReturn(Futures.immediateCheckedFuture(true));
113         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
114         verify(deviceReadWriteTx).exists(STORE, PATH);
115         probe.expectMsg(true);
116     }
117
118     @Test
119     public void testExistsFailure() throws Exception {
120         final ReadFailedException cause = new ReadFailedException("fail");
121         when(deviceReadWriteTx.exists(STORE, PATH)).thenReturn(Futures.immediateFailedCheckedFuture(cause));
122         actorRef.tell(new ExistsRequest(STORE, PATH), probe.ref());
123         verify(deviceReadWriteTx).exists(STORE, PATH);
124         probe.expectMsg(cause);
125     }
126
127     @Test
128     public void testPut() throws Exception {
129         final NormalizedNodeMessage normalizedNodeMessage = new NormalizedNodeMessage(PATH, node);
130         actorRef.tell(new PutRequest(STORE, normalizedNodeMessage), probe.ref());
131         verify(deviceReadWriteTx).put(STORE, PATH, node);
132     }
133
134     @Test
135     public void testMerge() throws Exception {
136         final NormalizedNodeMessage normalizedNodeMessage = new NormalizedNodeMessage(PATH, node);
137         actorRef.tell(new MergeRequest(STORE, normalizedNodeMessage), probe.ref());
138         verify(deviceReadWriteTx).merge(STORE, PATH, node);
139     }
140
141     @Test
142     public void testDelete() throws Exception {
143         actorRef.tell(new DeleteRequest(STORE, PATH), probe.ref());
144         verify(deviceReadWriteTx).delete(STORE, PATH);
145     }
146
147     @Test
148     public void testCancel() throws Exception {
149         when(deviceReadWriteTx.cancel()).thenReturn(true);
150         final Future<Object> cancelFuture = Patterns.ask(actorRef, new CancelRequest(), TIMEOUT);
151         final Object result = Await.result(cancelFuture, TIMEOUT.duration());
152         Assert.assertTrue(result instanceof Boolean);
153         verify(deviceReadWriteTx).cancel();
154         Assert.assertTrue((Boolean) result);
155     }
156
157     @Test
158     public void testSubmit() throws Exception {
159         when(deviceReadWriteTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
160         final Future<Object> submitFuture = Patterns.ask(actorRef, new SubmitRequest(), TIMEOUT);
161         final Object result = Await.result(submitFuture, TIMEOUT.duration());
162         Assert.assertTrue(result instanceof SubmitReply);
163         verify(deviceReadWriteTx).submit();
164     }
165
166     @Test
167     public void testSubmitFail() throws Exception {
168         final RpcError rpcError =
169                 RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "fail", "fail");
170         final TransactionCommitFailedException cause = new TransactionCommitFailedException("fail", rpcError);
171         when(deviceReadWriteTx.submit()).thenReturn(Futures.immediateFailedCheckedFuture(cause));
172         final Future<Object> submitFuture = Patterns.ask(actorRef, new SubmitRequest(), TIMEOUT);
173         final Object result = Await.result(submitFuture, TIMEOUT.duration());
174         Assert.assertTrue(result instanceof SubmitFailedReply);
175         Assert.assertEquals(cause, ((SubmitFailedReply)result).getThrowable());
176         verify(deviceReadWriteTx).submit();
177     }
178
179     @Test
180     public void testIdleTimeout() throws Exception {
181         final TestProbe testProbe = new TestProbe(system);
182         testProbe.watch(actorRef);
183         verify(deviceReadWriteTx, timeout(3000)).cancel();
184         testProbe.expectTerminated(actorRef, TIMEOUT.duration());
185     }
186 }