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.TransactionStatus;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
31 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
32 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
34 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 public class ProxyWriteTransactionTest {
42 private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
43 private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
45 private ActorSystem system;
46 private TestProbe masterActor;
47 private ContainerNode node;
48 private ProxyWriteTransaction tx;
51 public void setUp() throws Exception {
52 system = ActorSystem.apply();
53 masterActor = new TestProbe(system);
54 final RemoteDeviceId id = new RemoteDeviceId("dev1", InetSocketAddress.createUnresolved("localhost", 17830));
55 node = Builders.containerBuilder()
56 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "cont")))
58 tx = new ProxyWriteTransaction(masterActor.ref(), id, system, Timeout.apply(5, TimeUnit.SECONDS));
62 public void tearDown() throws Exception {
63 JavaTestKit.shutdownActorSystem(system, null, true);
67 public void testCancel() throws Exception {
68 final Future<Boolean> submit = Executors.newSingleThreadExecutor().submit(() -> tx.cancel());
69 masterActor.expectMsgClass(CancelRequest.class);
70 masterActor.reply(true);
71 Assert.assertTrue(submit.get());
75 public void testCancelSubmitted() throws Exception {
76 final ListenableFuture<Void> submitFuture = tx.submit();
77 masterActor.expectMsgClass(SubmitRequest.class);
78 masterActor.reply(new SubmitReply());
80 final Future<Boolean> submit = Executors.newSingleThreadExecutor().submit(() -> tx.cancel());
81 masterActor.expectNoMsg();
82 Assert.assertFalse(submit.get());
86 public void testSubmit() throws Exception {
87 final ListenableFuture<Void> submitFuture = tx.submit();
88 masterActor.expectMsgClass(SubmitRequest.class);
89 masterActor.reply(new SubmitReply());
94 public void testDoubleSubmit() throws Exception {
95 final ListenableFuture<Void> submitFuture = tx.submit();
96 masterActor.expectMsgClass(SubmitRequest.class);
97 masterActor.reply(new SubmitReply());
100 tx.submit().checkedGet();
101 Assert.fail("Should throw IllegalStateException");
102 } catch (final IllegalStateException e) {
103 masterActor.expectNoMsg();
108 public void testCommit() throws Exception {
109 final ListenableFuture<RpcResult<TransactionStatus>> submitFuture = tx.commit();
110 masterActor.expectMsgClass(SubmitRequest.class);
111 masterActor.reply(new SubmitReply());
112 Assert.assertEquals(TransactionStatus.SUBMITED, submitFuture.get().getResult());
116 public void testDelete() throws Exception {
117 tx.delete(STORE, PATH);
118 masterActor.expectMsgClass(DeleteRequest.class);
122 public void testDeleteClosed() throws Exception {
125 tx.delete(STORE, PATH);
126 Assert.fail("Should throw IllegalStateException");
127 } catch (final IllegalStateException e) {
128 masterActor.expectNoMsg();
133 public void testPut() throws Exception {
134 tx.put(STORE, PATH, node);
135 masterActor.expectMsgClass(PutRequest.class);
139 public void testPutClosed() throws Exception {
142 tx.put(STORE, PATH, node);
143 Assert.fail("Should throw IllegalStateException");
144 } catch (final IllegalStateException e) {
145 masterActor.expectNoMsg();
150 public void testMerge() throws Exception {
151 tx.merge(STORE, PATH, node);
152 masterActor.expectMsgClass(MergeRequest.class);
156 public void testMergeClosed() throws Exception {
159 tx.merge(STORE, PATH, node);
160 Assert.fail("Should throw IllegalStateException");
161 } catch (final IllegalStateException e) {
162 masterActor.expectNoMsg();
167 public void testGetIdentifier() throws Exception {
168 Assert.assertEquals(tx, tx.getIdentifier());
171 private void submit() throws TransactionCommitFailedException {
172 final CheckedFuture<Void, TransactionCommitFailedException> submit = tx.submit();
173 masterActor.expectMsgClass(SubmitRequest.class);
174 masterActor.reply(new SubmitReply());