2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
11 import akka.actor.ActorSystem;
12 import akka.testkit.JavaTestKit;
13 import akka.testkit.TestProbe;
14 import akka.util.Timeout;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
31 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
32 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 public class ProxyWriteTransactionTest {
40 private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
41 private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
43 private ActorSystem system;
44 private TestProbe masterActor;
45 private ContainerNode node;
46 private ProxyWriteTransaction tx;
49 public void setUp() throws Exception {
50 system = ActorSystem.apply();
51 masterActor = new TestProbe(system);
52 final RemoteDeviceId id = new RemoteDeviceId("dev1", InetSocketAddress.createUnresolved("localhost", 17830));
53 node = Builders.containerBuilder()
54 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "cont")))
56 tx = new ProxyWriteTransaction(masterActor.ref(), id, system, Timeout.apply(5, TimeUnit.SECONDS));
60 public void tearDown() throws Exception {
61 JavaTestKit.shutdownActorSystem(system, null, true);
65 public void testCancel() throws Exception {
66 final Future<Boolean> submit = Executors.newSingleThreadExecutor().submit(() -> tx.cancel());
67 masterActor.expectMsgClass(CancelRequest.class);
68 masterActor.reply(true);
69 Assert.assertTrue(submit.get());
73 public void testCancelSubmitted() throws Exception {
74 final ListenableFuture<Void> submitFuture = tx.submit();
75 masterActor.expectMsgClass(SubmitRequest.class);
76 masterActor.reply(new SubmitReply());
78 final Future<Boolean> submit = Executors.newSingleThreadExecutor().submit(() -> tx.cancel());
79 masterActor.expectNoMsg();
80 Assert.assertFalse(submit.get());
84 public void testSubmit() throws Exception {
85 final ListenableFuture<Void> submitFuture = tx.submit();
86 masterActor.expectMsgClass(SubmitRequest.class);
87 masterActor.reply(new SubmitReply());
92 public void testDoubleSubmit() throws Exception {
93 final ListenableFuture<Void> submitFuture = tx.submit();
94 masterActor.expectMsgClass(SubmitRequest.class);
95 masterActor.reply(new SubmitReply());
98 tx.submit().checkedGet();
99 Assert.fail("Should throw IllegalStateException");
100 } catch (final IllegalStateException e) {
101 masterActor.expectNoMsg();
106 public void testDelete() throws Exception {
107 tx.delete(STORE, PATH);
108 masterActor.expectMsgClass(DeleteRequest.class);
112 public void testDeleteClosed() throws Exception {
115 tx.delete(STORE, PATH);
116 Assert.fail("Should throw IllegalStateException");
117 } catch (final IllegalStateException e) {
118 masterActor.expectNoMsg();
123 public void testPut() throws Exception {
124 tx.put(STORE, PATH, node);
125 masterActor.expectMsgClass(PutRequest.class);
129 public void testPutClosed() throws Exception {
132 tx.put(STORE, PATH, node);
133 Assert.fail("Should throw IllegalStateException");
134 } catch (final IllegalStateException e) {
135 masterActor.expectNoMsg();
140 public void testMerge() throws Exception {
141 tx.merge(STORE, PATH, node);
142 masterActor.expectMsgClass(MergeRequest.class);
146 public void testMergeClosed() throws Exception {
149 tx.merge(STORE, PATH, node);
150 Assert.fail("Should throw IllegalStateException");
151 } catch (final IllegalStateException e) {
152 masterActor.expectNoMsg();
157 public void testGetIdentifier() throws Exception {
158 Assert.assertEquals(tx, tx.getIdentifier());
161 private void submit() throws TransactionCommitFailedException {
162 final CheckedFuture<Void, TransactionCommitFailedException> submit = tx.submit();
163 masterActor.expectMsgClass(SubmitRequest.class);
164 masterActor.reply(new SubmitReply());