2 * Copyright (c) 2016 Cisco Systems, Inc. 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.pipeline.tx;
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;
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;
42 public class ProxyWriteOnlyTransactionTest {
43 private static final YangInstanceIdentifier path = YangInstanceIdentifier.create();
44 private ArgumentCaptor<NormalizedNodeMessage> nodeMessageArgumentCaptor;
47 private ProxyNetconfDeviceDataBroker mockedDelegate;
50 private ActorSystem mockedActorSystem;
53 private NormalizedNode<?, ?> normalizedNode;
55 private ProxyWriteOnlyTransaction tx;
59 MockitoAnnotations.initMocks(this);
61 when(mockedActorSystem.dispatcher()).thenReturn(ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
63 nodeMessageArgumentCaptor = ArgumentCaptor.forClass(NormalizedNodeMessage.class);
64 tx = new ProxyWriteOnlyTransaction(mockedActorSystem, mockedDelegate);
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());
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());
86 public void testCancel() {
87 when(mockedDelegate.cancel()).thenReturn(true);
88 assertTrue(tx.cancel());
89 verify(mockedDelegate).cancel();
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));
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);
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());
115 submitFuture.checkedGet();
116 fail("Exception expected");
117 } catch(Exception e) {
118 assertTrue(e instanceof TransactionCommitFailedException);
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);
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());
140 fail("Exception expected");
141 } catch(Exception e) {
142 assertTrue(e.getCause() instanceof TransactionCommitFailedException);