Comment out equality check in AbstractCompositeWriter.updateCurrent
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / v3po / translate / v3po / vpp / BridgeDomainCustomizerTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.fd.honeycomb.v3po.translate.v3po.vpp;
17
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;
26
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;
42
43 public class BridgeDomainCustomizerTest {
44
45     private static final byte ADD_OR_UPDATE_BD = (byte) 1;
46     private static final byte ZERO = 0;
47
48     @Mock
49     private FutureJVpp api;
50
51     @Mock
52     private Context ctx;
53
54     private BridgeDomainCustomizer customizer;
55     private NamingContext namingContext;
56
57     @Before
58     public void setUp() throws Exception {
59         initMocks(this);
60         // TODO create base class for tests using vppApi
61         namingContext = new NamingContext("generatedBDName");
62         customizer = new BridgeDomainCustomizer(api, namingContext);
63     }
64
65     private BridgeDomain generateBridgeDomain(final String bdName) {
66         final byte arpTerm = 0;
67         final byte flood = 1;
68         final byte forward = 0;
69         final byte learn = 1;
70         final byte uuf = 0;
71         return generateBridgeDomain(bdName, arpTerm, flood, forward, learn, uuf);
72     }
73
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()
77                 .setName(bdName)
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))
83                 .build();
84     }
85
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());
92
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);
104     }
105
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);
117     }
118
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);
127     }
128
129     private void whenBridgeDomainAddDelThenSuccess() throws ExecutionException, InterruptedException {
130         whenBridgeDomainAddDelThen(0);
131     }
132
133     private void whenBridgeDomainAddDelThenFailure() throws ExecutionException, InterruptedException {
134         whenBridgeDomainAddDelThen(-1);
135     }
136
137     @Test
138     public void testAddBridgeDomain() throws Exception {
139         final int bdId = 1;
140         final String bdName = "bd1";
141         final BridgeDomain bd = generateBridgeDomain(bdName);
142
143         whenBridgeDomainAddDelThenSuccess();
144
145         customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
146
147         verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
148     }
149
150     @Test
151     public void testAddBridgeDomainFailed() throws Exception {
152         final int bdId = 1;
153         final String bdName = "bd1";
154         final BridgeDomain bd = generateBridgeDomain(bdName);
155
156         whenBridgeDomainAddDelThenFailure();
157
158         try {
159             customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
160         } catch (WriteFailedException.CreateFailedException e) {
161             verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId);
162             return;
163         }
164         fail("WriteFailedException.CreateFailedException  was expected");
165     }
166
167     @Test
168     public void testDeleteBridgeDomain() throws Exception {
169         final int bdId = 1;
170         final String bdName = "bd1";
171         final BridgeDomain bd = generateBridgeDomain(bdName);
172         namingContext.addName(bdId, bdName);
173
174         whenBridgeDomainAddDelThenSuccess();
175
176         customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
177
178         verifyBridgeDomainDeleteWasInvoked(bdId);
179     }
180
181     @Test
182     public void testDeleteUnknownBridgeDomain() throws Exception {
183         final String bdName = "bd1";
184         final BridgeDomain bd = generateBridgeDomain("bd1");
185
186         try {
187             customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
188         } catch (IllegalArgumentException e) {
189             verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
190             return;
191         }
192         fail("IllegalArgumentException was expected");
193     }
194
195     @Test
196     public void testDeleteBridgeDomainFailed() throws Exception {
197         final int bdId = 1;
198         final String bdName = "bd1";
199         final BridgeDomain bd = generateBridgeDomain(bdName);
200         namingContext.addName(bdId, bdName);
201
202         whenBridgeDomainAddDelThenFailure();
203
204         try {
205             customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, ctx);
206         } catch (WriteFailedException.DeleteFailedException e) {
207             verifyBridgeDomainDeleteWasInvoked(bdId);
208             return;
209         }
210
211         fail("WriteFailedException.DeleteFailedException was expected");
212     }
213
214     @Test
215     public void testUpdateBridgeDomain() throws Exception {
216         final int bdId = 1;
217         final String bdName = "bd1";
218         namingContext.addName(bdId, bdName);
219
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;
225
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,
230                         uufBefore ^ 1);
231
232         whenBridgeDomainAddDelThenSuccess();
233
234         customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), dataBefore, dataAfter, ctx);
235
236         verifyBridgeDomainAddOrUpdateWasInvoked(dataAfter, bdId);
237     }
238
239     @Test
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);
244
245         try {
246             customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bdBefore, bdAfter, ctx);
247         } catch (IllegalArgumentException e) {
248             verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class));
249             return;
250         }
251         fail("IllegalArgumentException was expected");
252     }
253
254     @Test
255     public void testUpdateBridgeDomainFailed() throws Exception {
256         final int bdId = 1;
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);
261
262         whenBridgeDomainAddDelThenFailure();
263
264         try {
265             customizer.updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bdBefore, bdAfter, ctx);
266         } catch (WriteFailedException.UpdateFailedException  e) {
267             verifyBridgeDomainAddOrUpdateWasInvoked(bdAfter, bdId);
268             return;
269         }
270         fail("IllegalStateException was expected");
271     }
272
273 }