Bug 6714 - Use singleton service in clustered netconf topology
[netconf.git] / netconf / netconf-topology / src / test / java / org / opendaylight / netconf / topology / pipeline / tx / ProxyWriteOnlyTransactionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.pipeline.tx;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import akka.actor.ActorSystem;
22 import akka.dispatch.ExecutionContexts;
23 import akka.dispatch.Futures;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import com.google.common.util.concurrent.MoreExecutors;
27 import java.util.concurrent.ExecutionException;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
36 import org.opendaylight.netconf.topology.pipeline.ProxyNetconfDeviceDataBroker;
37 import org.opendaylight.netconf.topology.util.messages.NormalizedNodeMessage;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41
42 public class ProxyWriteOnlyTransactionTest {
43     private static final YangInstanceIdentifier path = YangInstanceIdentifier.create();
44     private ArgumentCaptor<NormalizedNodeMessage> nodeMessageArgumentCaptor;
45
46     @Mock
47     private ProxyNetconfDeviceDataBroker mockedDelegate;
48
49     @Mock
50     private ActorSystem mockedActorSystem;
51
52     @Mock
53     private NormalizedNode<?, ?> normalizedNode;
54
55     private ProxyWriteOnlyTransaction tx;
56
57     @Before
58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60
61         when(mockedActorSystem.dispatcher()).thenReturn(ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
62
63         nodeMessageArgumentCaptor = ArgumentCaptor.forClass(NormalizedNodeMessage.class);
64         tx = new ProxyWriteOnlyTransaction(mockedActorSystem, mockedDelegate);
65     }
66
67     @Test
68     public void testPut() {
69         doNothing().when(mockedDelegate).put(any(LogicalDatastoreType.class), any(NormalizedNodeMessage.class));
70         tx.put(LogicalDatastoreType.OPERATIONAL, path, normalizedNode);
71         verify(mockedDelegate).put(eq(LogicalDatastoreType.OPERATIONAL), nodeMessageArgumentCaptor.capture());
72         assertEquals(path, nodeMessageArgumentCaptor.getValue().getIdentifier());
73         assertEquals(normalizedNode, nodeMessageArgumentCaptor.getValue().getNode());
74     }
75
76     @Test
77     public void testMerge() {
78         doNothing().when(mockedDelegate).merge(any(LogicalDatastoreType.class), any(NormalizedNodeMessage.class));
79         tx.merge(LogicalDatastoreType.CONFIGURATION, path, normalizedNode);
80         verify(mockedDelegate).merge(eq(LogicalDatastoreType.CONFIGURATION), nodeMessageArgumentCaptor.capture());
81         assertEquals(path, nodeMessageArgumentCaptor.getValue().getIdentifier());
82         assertEquals(normalizedNode, nodeMessageArgumentCaptor.getValue().getNode());
83     }
84
85     @Test
86     public void testCancel() {
87         when(mockedDelegate.cancel()).thenReturn(true);
88         assertTrue(tx.cancel());
89         verify(mockedDelegate).cancel();
90     }
91
92     @Test
93     public void testDelete() {
94         doNothing().when(mockedDelegate).delete(any(LogicalDatastoreType.class), any(YangInstanceIdentifier.class));
95         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
96         verify(mockedDelegate).delete(eq(LogicalDatastoreType.OPERATIONAL), eq(path));
97     }
98
99     @Test
100     public void testSuccessfulSubmit() throws Exception {
101         when(mockedDelegate.submit()).thenReturn(Futures.<Void>successful(null));
102         CheckedFuture submitFuture = tx.submit();
103         verify(mockedDelegate).submit();
104         assertTrue(submitFuture.isDone());
105         assertEquals(submitFuture.checkedGet(), null);
106     }
107
108     @Test
109     public void testFailedSubmit() {
110         when(mockedDelegate.submit()).thenReturn(Futures.<Void>failed(new TransactionCommitFailedException("fail")));
111         CheckedFuture submitFuture = tx.submit();
112         verify(mockedDelegate).submit();
113         assertTrue(submitFuture.isDone());
114         try {
115             submitFuture.checkedGet();
116             fail("Exception expected");
117         } catch(Exception e) {
118             assertTrue(e instanceof TransactionCommitFailedException);
119         }
120     }
121
122     @Test
123     public void testSuccessfulCommit() throws ExecutionException, InterruptedException {
124         RpcResult<TransactionStatus> rpcResult = mock(RpcResult.class);
125         when(mockedDelegate.commit()).thenReturn(Futures.successful(rpcResult));
126         ListenableFuture<RpcResult<TransactionStatus>> submitFuture = tx.commit();
127         verify(mockedDelegate).commit();
128         assertTrue(submitFuture.isDone());
129         assertEquals(submitFuture.get(), rpcResult);
130     }
131
132     @Test
133     public void testFailedCommit() {
134         when(mockedDelegate.commit()).thenReturn(Futures.<RpcResult<TransactionStatus>>failed(new TransactionCommitFailedException("faile")));
135         ListenableFuture<RpcResult<TransactionStatus>> submitFuture = tx.commit();
136         verify(mockedDelegate).commit();
137         assertTrue(submitFuture.isDone());
138         try {
139             submitFuture.get();
140             fail("Exception expected");
141         } catch(Exception e) {
142             assertTrue(e.getCause() instanceof TransactionCommitFailedException);
143         }
144     }
145 }