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.
17 package io.fd.hc2vpp.acl.util.iface.macip;
19 import static io.fd.hc2vpp.acl.write.request.MacIpInterfaceAssignmentRequest.addNew;
20 import static io.fd.hc2vpp.acl.write.request.MacIpInterfaceAssignmentRequest.deleteExisting;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.fail;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
30 import io.fd.hc2vpp.acl.AclIIds;
31 import io.fd.hc2vpp.acl.util.AclContextManager;
32 import io.fd.hc2vpp.acl.write.request.MacIpInterfaceAssignmentRequest;
33 import io.fd.hc2vpp.common.test.util.FutureProducer;
34 import io.fd.hc2vpp.common.test.util.NamingContextHelper;
35 import io.fd.hc2vpp.common.translate.util.NamingContext;
36 import io.fd.honeycomb.translate.MappingContext;
37 import io.fd.honeycomb.translate.write.WriteFailedException;
38 import io.fd.vpp.jvpp.acl.dto.MacipAclInterfaceAddDel;
39 import io.fd.vpp.jvpp.acl.dto.MacipAclInterfaceAddDelReply;
40 import io.fd.vpp.jvpp.acl.future.FutureJVppAclFacade;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Captor;
45 import org.mockito.Mock;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev181001.acls.attachment.points.Interface;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev181001.acls.attachment.points.InterfaceKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 public class MacIpInterfaceAssignmentRequestTest implements NamingContextHelper,FutureProducer {
52 private static final String INTERFACE_NAME = "iface";
53 private static final int INTERFACE_INDEX = 4;
54 private static final String ACL_NAME = "mac-ip-acl";
55 private static final int ACL_INDEX = 5;
58 private ArgumentCaptor<MacipAclInterfaceAddDel> requestCaptor;
61 private FutureJVppAclFacade api;
64 private MappingContext mappingContext;
66 private InstanceIdentifier<Interface> validIdentifier;
67 private NamingContext interfaceContext;
69 private AclContextManager macIpAclContext;
72 public void setUp() throws Exception {
75 validIdentifier = AclIIds.ACLS_AP.child(Interface.class, new InterfaceKey(INTERFACE_NAME));
77 interfaceContext = new NamingContext("iface", "interface-context");
79 defineMapping(mappingContext, INTERFACE_NAME, INTERFACE_INDEX, "interface-context");
80 when(macIpAclContext.getAclIndex(ACL_NAME, mappingContext)).thenReturn(ACL_INDEX);
81 when(api.macipAclInterfaceAddDel(any())).thenReturn(future(new MacipAclInterfaceAddDelReply()));
85 public void testAddNew() throws Exception {
86 addNew(mappingContext)
88 .identifier(validIdentifier)
89 .interfaceContext(interfaceContext)
90 .macIpAclContext(macIpAclContext)
92 verify(api, times(1)).macipAclInterfaceAddDel(requestCaptor.capture());
94 final MacipAclInterfaceAddDel request = requestCaptor.getValue();
96 assertNotNull(request);
97 assertEquals(1, request.isAdd);
98 assertEquals(INTERFACE_INDEX, request.swIfIndex);
99 assertEquals(ACL_INDEX, request.aclIndex);
103 public void testDeleteExisting() throws Exception {
104 deleteExisting(mappingContext)
106 .identifier(validIdentifier)
107 .interfaceContext(interfaceContext)
108 .macIpAclContext(macIpAclContext)
110 verify(api, times(1)).macipAclInterfaceAddDel(requestCaptor.capture());
112 final MacipAclInterfaceAddDel request = requestCaptor.getValue();
114 assertNotNull(request);
115 assertEquals(0, request.isAdd);
116 assertEquals(INTERFACE_INDEX, request.swIfIndex);
117 assertEquals(ACL_INDEX, request.aclIndex);
121 public void testInvalidCases() throws Exception {
122 verifyFailsWithNullPointer(addNew(mappingContext));
123 verifyFailsWithNullPointer(addNew(mappingContext).aclName(ACL_NAME));
124 verifyFailsWithNullPointer(addNew(mappingContext).aclName(ACL_NAME).interfaceContext(interfaceContext));
125 verifyFailsWithNullPointer(addNew(mappingContext).aclName(ACL_NAME).interfaceContext(interfaceContext)
126 .macIpAclContext(macIpAclContext));
127 verifyFailsWithNullPointer(addNew(mappingContext).aclName(ACL_NAME).interfaceContext(interfaceContext)
128 .identifier(validIdentifier));
131 private void verifyFailsWithNullPointer(final MacIpInterfaceAssignmentRequest request) throws WriteFailedException {
133 request.execute(api);
134 } catch (NullPointerException e) {
137 fail("Test should have thrown null pointer");