Change handling of netconf cluster transactions
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / actors / WriteTransactionActorTest.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.verify;
12 import static org.mockito.Mockito.when;
13
14 import akka.actor.ActorSystem;
15 import akka.pattern.Patterns;
16 import akka.testkit.JavaTestKit;
17 import akka.testkit.TestActorRef;
18 import akka.testkit.TestProbe;
19 import akka.util.Timeout;
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.Futures;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.After;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
32 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
34 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
35 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
36 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
37 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitReply;
38 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
45 import scala.concurrent.Await;
46 import scala.concurrent.Future;
47
48 public class WriteTransactionActorTest {
49     private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
50     private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
51     private static final Timeout TIMEOUT = Timeout.apply(5, TimeUnit.SECONDS);
52
53     @Mock
54     private DOMDataWriteTransaction deviceWriteTx;
55     private TestProbe probe;
56     private ActorSystem system;
57     private TestActorRef<WriteTransactionActor> actorRef;
58     private NormalizedNode<?, ?> node;
59
60     @Before
61     public void setUp() throws Exception {
62         MockitoAnnotations.initMocks(this);
63         system = ActorSystem.apply();
64         probe = TestProbe.apply(system);
65         node = Builders.containerBuilder()
66                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("cont")))
67                 .build();
68         actorRef = TestActorRef.create(system, WriteTransactionActor.props(deviceWriteTx), "testA");
69     }
70
71     @After
72     public void tearDown() throws Exception {
73         JavaTestKit.shutdownActorSystem(system, null, true);
74     }
75
76     @Test
77     public void testPut() throws Exception {
78         final NormalizedNodeMessage normalizedNodeMessage = new NormalizedNodeMessage(PATH, node);
79         actorRef.tell(new PutRequest(STORE, normalizedNodeMessage), probe.ref());
80         verify(deviceWriteTx).put(STORE, PATH, node);
81     }
82
83     @Test
84     public void testMerge() throws Exception {
85         final NormalizedNodeMessage normalizedNodeMessage = new NormalizedNodeMessage(PATH, node);
86         actorRef.tell(new MergeRequest(STORE, normalizedNodeMessage), probe.ref());
87         verify(deviceWriteTx).merge(STORE, PATH, node);
88     }
89
90     @Test
91     public void testDelete() throws Exception {
92         actorRef.tell(new DeleteRequest(STORE, PATH), probe.ref());
93         verify(deviceWriteTx).delete(STORE, PATH);
94     }
95
96     @Test
97     public void testCancel() throws Exception {
98         when(deviceWriteTx.cancel()).thenReturn(true);
99         final Future<Object> cancelFuture = Patterns.ask(actorRef, new CancelRequest(), TIMEOUT);
100         final Object result = Await.result(cancelFuture, TIMEOUT.duration());
101         Preconditions.checkState(result instanceof Boolean);
102         verify(deviceWriteTx).cancel();
103         Assert.assertTrue((Boolean) result);
104     }
105
106     @Test
107     public void testSubmit() throws Exception {
108         when(deviceWriteTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
109         final Future<Object> submitFuture = Patterns.ask(actorRef, new SubmitRequest(), TIMEOUT);
110         final Object result = Await.result(submitFuture, TIMEOUT.duration());
111         Assert.assertTrue(result instanceof SubmitReply);
112         verify(deviceWriteTx).submit();
113     }
114
115     @Test
116     public void testSubmitFail() throws Exception {
117         final RpcError rpcError =
118                 RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "fail", "fail");
119         final TransactionCommitFailedException cause = new TransactionCommitFailedException("fail", rpcError);
120         when(deviceWriteTx.submit()).thenReturn(Futures.immediateFailedCheckedFuture(cause));
121         final Future<Object> submitFuture = Patterns.ask(actorRef, new SubmitRequest(), TIMEOUT);
122         final Object result = Await.result(submitFuture, TIMEOUT.duration());
123         Assert.assertEquals(cause, result);
124         verify(deviceWriteTx).submit();
125     }
126
127 }