b51375b14558803e1c873c358b01b04407e87f5a
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / ProxyWriteTransactionTest.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.tx;
10
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;
40
41 public class ProxyWriteTransactionTest {
42     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
43     private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
44
45     private ActorSystem system;
46     private TestProbe masterActor;
47     private ContainerNode node;
48     private ProxyWriteTransaction tx;
49
50     @Before
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")))
57                 .build();
58         tx = new ProxyWriteTransaction(masterActor.ref(), id, system, Timeout.apply(5, TimeUnit.SECONDS));
59     }
60
61     @After
62     public void tearDown() throws Exception {
63         JavaTestKit.shutdownActorSystem(system, null, true);
64     }
65
66     @Test
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());
72     }
73
74     @Test
75     public void testCancelSubmitted() throws Exception {
76         final ListenableFuture<Void> submitFuture = tx.submit();
77         masterActor.expectMsgClass(SubmitRequest.class);
78         masterActor.reply(new SubmitReply());
79         submitFuture.get();
80         final Future<Boolean> submit = Executors.newSingleThreadExecutor().submit(() -> tx.cancel());
81         masterActor.expectNoMsg();
82         Assert.assertFalse(submit.get());
83     }
84
85     @Test
86     public void testSubmit() throws Exception {
87         final ListenableFuture<Void> submitFuture = tx.submit();
88         masterActor.expectMsgClass(SubmitRequest.class);
89         masterActor.reply(new SubmitReply());
90         submitFuture.get();
91     }
92
93     @Test
94     public void testDoubleSubmit() throws Exception {
95         final ListenableFuture<Void> submitFuture = tx.submit();
96         masterActor.expectMsgClass(SubmitRequest.class);
97         masterActor.reply(new SubmitReply());
98         submitFuture.get();
99         try {
100             tx.submit().checkedGet();
101             Assert.fail("Should throw IllegalStateException");
102         } catch (final IllegalStateException e) {
103             masterActor.expectNoMsg();
104         }
105     }
106
107     @Test
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());
113     }
114
115     @Test
116     public void testDelete() throws Exception {
117         tx.delete(STORE, PATH);
118         masterActor.expectMsgClass(DeleteRequest.class);
119     }
120
121     @Test
122     public void testDeleteClosed() throws Exception {
123         submit();
124         try {
125             tx.delete(STORE, PATH);
126             Assert.fail("Should throw IllegalStateException");
127         } catch (final IllegalStateException e) {
128             masterActor.expectNoMsg();
129         }
130     }
131
132     @Test
133     public void testPut() throws Exception {
134         tx.put(STORE, PATH, node);
135         masterActor.expectMsgClass(PutRequest.class);
136     }
137
138     @Test
139     public void testPutClosed() throws Exception {
140         submit();
141         try {
142             tx.put(STORE, PATH, node);
143             Assert.fail("Should throw IllegalStateException");
144         } catch (final IllegalStateException e) {
145             masterActor.expectNoMsg();
146         }
147     }
148
149     @Test
150     public void testMerge() throws Exception {
151         tx.merge(STORE, PATH, node);
152         masterActor.expectMsgClass(MergeRequest.class);
153     }
154
155     @Test
156     public void testMergeClosed() throws Exception {
157         submit();
158         try {
159             tx.merge(STORE, PATH, node);
160             Assert.fail("Should throw IllegalStateException");
161         } catch (final IllegalStateException e) {
162             masterActor.expectNoMsg();
163         }
164     }
165
166     @Test
167     public void testGetIdentifier() throws Exception {
168         Assert.assertEquals(tx, tx.getIdentifier());
169     }
170
171     private void submit() throws TransactionCommitFailedException {
172         final CheckedFuture<Void, TransactionCommitFailedException> submit = tx.submit();
173         masterActor.expectMsgClass(SubmitRequest.class);
174         masterActor.reply(new SubmitReply());
175         submit.checkedGet();
176     }
177
178 }