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