2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.fd.honeycomb.v3po.translate.v3po.vpp;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.fail;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 import static org.mockito.MockitoAnnotations.initMocks;
27 import io.fd.honeycomb.v3po.translate.Context;
28 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
29 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.CompletionStage;
32 import java.util.concurrent.ExecutionException;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder;
39 import org.openvpp.jvpp.dto.BridgeDomainAddDel;
40 import org.openvpp.jvpp.dto.BridgeDomainAddDelReply;
41 import org.openvpp.jvpp.future.FutureJVpp;
43 public class BridgeDomainCustomizerTest {
45 private static final byte ADD_OR_UPDATE_BD = (byte) 1;
46 private static final byte ZERO = 0;
49 private FutureJVpp api;
54 private BridgeDomainCustomizer customizer;
55 private NamingContext namingContext;
58 public void setUp() throws Exception {
60 // TODO create base class for tests using vppApi
61 namingContext = new NamingContext("generatedBDName");
62 customizer = new BridgeDomainCustomizer(api, namingContext);
65 private BridgeDomain generateBridgeDomain(final String bdName) {
66 final byte arpTerm = 0;
68 final byte forward = 0;
71 return generateBridgeDomain(bdName, arpTerm, flood, forward, learn, uuf);
74 private BridgeDomain generateBridgeDomain(final String bdName, final int arpTerm, final int flood,
75 final int forward, final int learn, final int uuf) {
76 return new BridgeDomainBuilder()
78 .setArpTermination(BridgeDomainTestUtils.intToBoolean(arpTerm))
79 .setFlood(BridgeDomainTestUtils.intToBoolean(flood))
80 .setForward(BridgeDomainTestUtils.intToBoolean(forward))
81 .setLearn(BridgeDomainTestUtils.intToBoolean(learn))
82 .setUnknownUnicastFlood(BridgeDomainTestUtils.intToBoolean(uuf))
86 private void verifyBridgeDomainAddOrUpdateWasInvoked(final BridgeDomain bd, final int bdId) {
87 final byte arpTerm = BridgeDomainTestUtils.booleanToByte(bd.isArpTermination());
88 final byte flood = BridgeDomainTestUtils.booleanToByte(bd.isFlood());
89 final byte forward = BridgeDomainTestUtils.booleanToByte(bd.isForward());
90 final byte learn = BridgeDomainTestUtils.booleanToByte(bd.isLearn());
91 final byte uuf = BridgeDomainTestUtils.booleanToByte(bd.isUnknownUnicastFlood());
93 // TODO adding equals methods for jvpp DTOs would make ArgumentCaptor usage obsolete
94 ArgumentCaptor<BridgeDomainAddDel> argumentCaptor = ArgumentCaptor.forClass(BridgeDomainAddDel.class);
95 verify(api).bridgeDomainAddDel(argumentCaptor.capture());
96 final BridgeDomainAddDel actual = argumentCaptor.getValue();
97 assertEquals(arpTerm, actual.arpTerm);
98 assertEquals(flood, actual.flood);
99 assertEquals(forward, actual.forward);
100 assertEquals(learn, actual.learn);
101 assertEquals(uuf, actual.uuFlood);
102 assertEquals(ADD_OR_UPDATE_BD, actual.isAdd);
103 assertEquals(bdId, actual.bdId);
106 private void verifyBridgeDomainDeleteWasInvoked(final int bdId) {
107 ArgumentCaptor<BridgeDomainAddDel> argumentCaptor = ArgumentCaptor.forClass(BridgeDomainAddDel.class);
108 verify(api).bridgeDomainAddDel(argumentCaptor.capture());
109 final BridgeDomainAddDel actual = argumentCaptor.getValue();
110 assertEquals(bdId, actual.bdId);
111 assertEquals(ZERO, actual.arpTerm);
112 assertEquals(ZERO, actual.flood);
113 assertEquals(ZERO, actual.forward);
114 assertEquals(ZERO, actual.learn);
115 assertEquals(ZERO, actual.uuFlood);
116 assertEquals(ZERO, actual.isAdd);
119 private void whenBridgeDomainAddDelThen(final int retval) throws ExecutionException, InterruptedException {
120 final CompletionStage<BridgeDomainAddDelReply> replyCS = mock(CompletionStage.class);
121 final CompletableFuture<BridgeDomainAddDelReply> replyFuture = mock(CompletableFuture.class);
122 when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
123 final BridgeDomainAddDelReply reply = new BridgeDomainAddDelReply();
124 reply.retval = retval;
125 when(replyFuture.get()).thenReturn(reply);
126 when(api.bridgeDomainAddDel(any(BridgeDomainAddDel.class))).thenReturn(replyCS);
129 private void whenBridgeDomainAddDelThenSuccess() throws ExecutionException, InterruptedException {
130 whenBridgeDomainAddDelThen(0);
133 private void whenBridgeDomainAddDelThenFailure() throws ExecutionException, InterruptedException {
134 whenBridgeDomainAddDelThen(-1);
138 public void testAddBridgeDomain() throws Exception {
140 final String bdName = "bd1";
141 final BridgeDomain bd = generateBridgeDomain(bdName);
143 whenBridgeDomainAddDelThenSuccess();
145 customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
147 verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
151 public void testAddBridgeDomainFailed() throws Exception {
153 final String bdName = "bd1";
154 final BridgeDomain bd = generateBridgeDomain(bdName);
156 whenBridgeDomainAddDelThenFailure();
159 customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
160 } catch (WriteFailedException.CreateFailedException e) {
161 verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
164 fail("WriteFailedException.CreateFailedException was expected");
168 public void testDeleteBridgeDomain() throws Exception {
170 final String bdName = "bd1";
171 final BridgeDomain bd = generateBridgeDomain(bdName);
172 namingContext.addName(bdId, bdName);
174 whenBridgeDomainAddDelThenSuccess();
176 customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
178 verifyBridgeDomainDeleteWasInvoked(bdId);
182 public void testDeleteUnknownBridgeDomain() throws Exception {
183 final String bdName = "bd1";
184 final BridgeDomain bd = generateBridgeDomain("bd1");
187 customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
188 } catch (IllegalArgumentException e) {
189 verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
192 fail("IllegalArgumentException was expected");
196 public void testDeleteBridgeDomainFailed() throws Exception {
198 final String bdName = "bd1";
199 final BridgeDomain bd = generateBridgeDomain(bdName);
200 namingContext.addName(bdId, bdName);
202 whenBridgeDomainAddDelThenFailure();
205 customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
206 } catch (WriteFailedException.DeleteFailedException e) {
207 verifyBridgeDomainDeleteWasInvoked(bdId);
211 fail("WriteFailedException.DeleteFailedException was expected");
215 public void testUpdateBridgeDomain() throws Exception {
217 final String bdName = "bd1";
218 namingContext.addName(bdId, bdName);
220 final byte arpTermBefore = 1;
221 final byte floodBefore = 1;
222 final byte forwardBefore = 0;
223 final byte learnBefore = 1;
224 final byte uufBefore = 0;
226 final BridgeDomain dataBefore =
227 generateBridgeDomain(bdName, arpTermBefore, floodBefore, forwardBefore, learnBefore, uufBefore);
228 final BridgeDomain dataAfter =
229 generateBridgeDomain(bdName, arpTermBefore ^ 1, floodBefore ^ 1, forwardBefore ^ 1, learnBefore ^ 1,
232 whenBridgeDomainAddDelThenSuccess();
234 customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), dataBefore, dataAfter, ctx);
236 verifyBridgeDomainAddOrUpdateWasInvoked(dataAfter, bdId);
240 public void testUpdateUnknownBridgeDomain() throws Exception {
241 final String bdName = "bd1";
242 final BridgeDomain bdBefore = generateBridgeDomain(bdName, 0, 1, 0 ,1, 0);
243 final BridgeDomain bdAfter = generateBridgeDomain(bdName, 1, 1, 0 ,1, 0);
246 customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bdBefore, bdAfter, ctx);
247 } catch (IllegalArgumentException e) {
248 verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
251 fail("IllegalArgumentException was expected");
255 public void testUpdateBridgeDomainFailed() throws Exception {
257 final String bdName = "bd1";
258 final BridgeDomain bdBefore = generateBridgeDomain(bdName, 0, 1, 0 ,1, 0);
259 final BridgeDomain bdAfter = generateBridgeDomain(bdName, 1, 1, 0 ,1, 0);
260 namingContext.addName(bdId, bdName);
262 whenBridgeDomainAddDelThenFailure();
265 customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bdBefore, bdAfter, ctx);
266 } catch (WriteFailedException.UpdateFailedException e) {
267 verifyBridgeDomainAddOrUpdateWasInvoked(bdAfter, bdId);
270 fail("IllegalStateException was expected");