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.groupbasedpolicy.renderer.vpp.util;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 public class GbpNetconfTransactionTest {
38 private final DataBroker dataBroker = mock(DataBroker.class);
39 private final ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
40 private final ReadOnlyTransaction rTx = mock(ReadOnlyTransaction.class);
41 private final Node node = mock(Node.class);
42 @SuppressWarnings("unchecked")
43 private final InstanceIdentifier<Node> nodeIid = mock(InstanceIdentifier.class);
44 @SuppressWarnings("unchecked")
45 private final CheckedFuture<Void, TransactionCommitFailedException> future = mock(CheckedFuture.class);
46 @SuppressWarnings("unchecked")
47 private final CheckedFuture<Optional<Node>, ReadFailedException> futureNode = mock(CheckedFuture.class);
48 private final ConfigCommand command = mock(ConfigCommand.class);
51 public void writeConfigCommandExceptionTest() {
52 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
53 doThrow(new RuntimeException()).when(command).execute(rwTx);
55 final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
56 verify(dataBroker, times(1)).newReadWriteTransaction();
62 public void writeConfigCommandReattemptTest() {
63 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
64 doThrow(new IllegalStateException()).when(command).execute(rwTx);
66 final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
67 verify(dataBroker, times(6)).newReadWriteTransaction();
72 public void writeConfigCommandTest() throws Exception {
73 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
74 when(rwTx.submit()).thenReturn(future);
75 doNothing().when(command).execute(rwTx);
76 when(future.get()).thenReturn(null);
78 final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
79 verify(dataBroker, times(1)).newReadWriteTransaction();
84 public void writeDataExceptionTest() {
85 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
86 doThrow(new RuntimeException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
88 final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
89 verify(dataBroker, times(1)).newReadWriteTransaction();
94 public void writeDataReattemptTest() {
95 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
96 doThrow(new IllegalStateException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
98 final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
99 verify(dataBroker, times(6)).newReadWriteTransaction();
104 public void writeDataTest() throws Exception {
105 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
106 when(rwTx.submit()).thenReturn(future);
107 doNothing().when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
108 when(future.get()).thenReturn(null);
110 final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
111 verify(dataBroker, times(1)).newReadWriteTransaction();
116 public void readDataExceptionTest() {
117 when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
118 doThrow(new RuntimeException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
120 final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
122 verify(dataBroker, times(1)).newReadOnlyTransaction();
123 assertFalse(result.isPresent());
127 public void readDataReattemptTest() {
128 when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
129 doThrow(new IllegalStateException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
131 final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
133 verify(dataBroker, times(6)).newReadOnlyTransaction();
134 assertFalse(result.isPresent());
138 public void readDataTest() throws Exception {
139 when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
140 when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
141 when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
142 .setKey(new NodeKey(new NodeId("node"))).build()));
144 final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
146 verify(dataBroker, times(1)).newReadOnlyTransaction();
147 assertTrue(result.isPresent());
151 public void deleteConfigCommandExceptionTest() {
152 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
153 doThrow(new RuntimeException()).when(command).execute(rwTx);
155 final boolean result = GbpNetconfTransaction.delete(dataBroker, command, (byte)5);
156 verify(dataBroker, times(1)).newReadWriteTransaction();
161 public void deleteConfigCommandReattemptTest() {
162 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
163 doThrow(new IllegalStateException()).when(command).execute(rwTx);
165 final boolean result = GbpNetconfTransaction.delete(dataBroker, command, (byte)5);
166 verify(dataBroker, times(6)).newReadWriteTransaction();
171 public void deleteConfigCommandTest() throws Exception {
172 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
173 when(rwTx.submit()).thenReturn(future);
174 doNothing().when(command).execute(rwTx);
175 when(future.get()).thenReturn(null);
177 final boolean result = GbpNetconfTransaction.delete(dataBroker, command, (byte)5);
178 verify(dataBroker, times(1)).newReadWriteTransaction();
183 public void deleteDataExceptionTest() {
184 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
185 doThrow(new RuntimeException()).when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
187 final boolean result = GbpNetconfTransaction.delete(dataBroker, nodeIid, (byte)5);
188 verify(dataBroker, times(1)).newReadWriteTransaction();
193 public void deleteDataReattemptTest() {
194 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
195 doThrow(new IllegalStateException()).when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
197 final boolean result = GbpNetconfTransaction.delete(dataBroker, nodeIid, (byte)5);
198 verify(dataBroker, times(6)).newReadWriteTransaction();
203 public void deleteDataTest() throws Exception {
204 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
205 when(rwTx.submit()).thenReturn(future);
206 doNothing().when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
207 when(future.get()).thenReturn(null);
209 final boolean result = GbpNetconfTransaction.delete(dataBroker, nodeIid, (byte)5);
210 verify(dataBroker, times(1)).newReadWriteTransaction();