2 * Copyright (c) 2017 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.
17 package io.fd.hc2vpp.lisp.gpe.translate.write;
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
25 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.vpp.jvpp.core.dto.GpeAddDelIface;
28 import io.fd.vpp.jvpp.core.dto.GpeAddDelIfaceReply;
29 import io.fd.vpp.jvpp.core.dto.GpeAddDelNativeFwdRpathReply;
30 import java.util.List;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.NativeForwardPathsTables;
35 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable;
36 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableBuilder;
37 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 public class NativeForwardPathsTableCustomizerTest extends WriterCustomizerTest {
42 private static final long TABLE_ID = 1L;
43 private NativeForwardPathsTableCustomizer customizer;
44 private InstanceIdentifier<NativeForwardPathsTable> validId;
47 private ArgumentCaptor<GpeAddDelIface> requestCaptor;
50 protected void setUpTest() throws Exception {
51 customizer = new NativeForwardPathsTableCustomizer(api);
52 validId = InstanceIdentifier.create(NativeForwardPathsTables.class)
53 .child(NativeForwardPathsTable.class, new NativeForwardPathsTableKey(TABLE_ID));
54 when(api.gpeAddDelIface(any())).thenReturn(future(new GpeAddDelIfaceReply()));
58 public void testWriteValid() throws WriteFailedException {
59 when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
60 customizer.writeCurrentAttributes(validId, validTable(), writeContext);
61 verify(api, times(1)).gpeAddDelIface(requestCaptor.capture());
63 final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
65 assertEquals(desiredRequest(1, 1), requests.get(0));
69 public void testDeleteValid() throws WriteFailedException {
70 when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
71 customizer.deleteCurrentAttributes(validId, validTable(), writeContext);
72 verify(api, times(1)).gpeAddDelIface(requestCaptor.capture());
74 final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
76 assertEquals(desiredRequest(0, 1), requests.get(0));
80 public void testUpdateValid() throws WriteFailedException {
81 when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
82 final NativeForwardPathsTable before = validTableBefore();
83 final NativeForwardPathsTable after = validTable();
84 // emulates what default update would do
85 customizer.deleteCurrentAttributes(validId, before, writeContext);
86 customizer.writeCurrentAttributes(validId, after, writeContext);
87 verify(api, times(2)).gpeAddDelIface(requestCaptor.capture());
89 final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
91 // removes one from old data
92 assertEquals(desiredRequest(0, 1), requests.get(0));
95 assertEquals(desiredRequest(1, 1), requests.get(1));
99 private GpeAddDelIface desiredRequest(final int isAdd, final int tableId) {
100 GpeAddDelIface request = new GpeAddDelIface();
103 request.dpTable = tableId;
104 request.vni = request.dpTable;
105 request.isAdd = (byte) isAdd;
109 private NativeForwardPathsTable validTableBefore() {
110 return new NativeForwardPathsTableBuilder()
111 .setTableId(TABLE_ID)
115 private NativeForwardPathsTable validTable() {
116 return new NativeForwardPathsTableBuilder()
117 .setTableId(TABLE_ID)