HONEYCOMB-238: provide interface mode as a part of ietf-acl configuration
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / VxlanGpeCustomizerTest.java
index bec33cb..e60cdaf 100644 (file)
 
 package io.fd.honeycomb.translate.v3po.interfaces;
 
-import static java.util.Collections.singletonList;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-import static org.mockito.MockitoAnnotations.initMocks;
 
-import com.google.common.base.Optional;
 import com.google.common.net.InetAddresses;
-import io.fd.honeycomb.translate.ModificationCache;
-import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
-import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
-import io.fd.honeycomb.translate.v3po.util.NamingContext;
-import io.fd.honeycomb.translate.write.WriteContext;
+import io.fd.honeycomb.translate.v3po.DisabledInterfacesManager;
+import io.fd.honeycomb.translate.vpp.util.NamingContext;
 import io.fd.honeycomb.translate.write.WriteFailedException;
-import io.fd.honeycomb.translate.MappingContext;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.CompletionStage;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import org.junit.Before;
+import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
-import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
-import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
-import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
@@ -63,75 +46,53 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpe;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VxlanGpeBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.openvpp.jvpp.VppBaseCallException;
-import org.openvpp.jvpp.dto.VxlanAddDelTunnelReply;
-import org.openvpp.jvpp.dto.VxlanGpeAddDelTunnel;
-import org.openvpp.jvpp.dto.VxlanGpeAddDelTunnelReply;
-import org.openvpp.jvpp.future.FutureJVpp;
+import io.fd.vpp.jvpp.VppBaseCallException;
+import io.fd.vpp.jvpp.core.dto.VxlanGpeAddDelTunnel;
+import io.fd.vpp.jvpp.core.dto.VxlanGpeAddDelTunnelReply;
 
-public class VxlanGpeCustomizerTest {
+public class VxlanGpeCustomizerTest extends WriterCustomizerTest {
 
     private static final byte ADD_VXLAN_GPE = 1;
     private static final byte DEL_VXLAN_GPE = 0;
 
     @Mock
-    private FutureJVpp api;
-    @Mock
-    private WriteContext writeContext;
-    @Mock
-    private MappingContext mappingContext;
+    private DisabledInterfacesManager interfaceDisableContext;
 
     private VxlanGpeCustomizer customizer;
     private String ifaceName;
     private InstanceIdentifier<VxlanGpe> id;
 
-    @Before
+    @Override
     public void setUp() throws Exception {
-        initMocks(this);
         InterfaceTypeTestUtils.setupWriteContext(writeContext,
             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanGpeTunnel.class);
-        // TODO create base class for tests using vppApi
-        NamingContext namingContext = new NamingContext("generateInterfaceNAme", "test-instance");
-        final ModificationCache toBeReturned = new ModificationCache();
-        doReturn(toBeReturned).when(writeContext).getModificationCache();
-        doReturn(mappingContext).when(writeContext).getMappingContext();
-
-        customizer = new VxlanGpeCustomizer(api, namingContext);
+        NamingContext namingContext = new NamingContext("generateInterfaceName", "test-instance");
+        customizer = new VxlanGpeCustomizer(api, namingContext, interfaceDisableContext);
 
-        ifaceName = "eth0";
+        ifaceName = "elth0";
         id = InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(ifaceName))
-                        .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
+            .augmentation(VppInterfaceAugmentation.class).child(VxlanGpe.class);
     }
 
-    private void whenVxlanGpeAddDelTunnelThenSuccess()
-        throws ExecutionException, InterruptedException, VppBaseCallException, TimeoutException {
-        final CompletionStage<VxlanGpeAddDelTunnelReply> replyCS = mock(CompletionStage.class);
-        final CompletableFuture<VxlanGpeAddDelTunnelReply> replyFuture = mock(CompletableFuture.class);
-        when(replyCS.toCompletableFuture()).thenReturn(replyFuture);
-        final VxlanGpeAddDelTunnelReply reply = new VxlanGpeAddDelTunnelReply();
-        when(replyFuture.get(anyLong(), eq(TimeUnit.SECONDS))).thenReturn(reply);
-        when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class))).thenReturn(replyCS);
+    private void whenVxlanGpeAddDelTunnelThenSuccess() {
+        when(api.vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class)))
+            .thenReturn(future(new VxlanGpeAddDelTunnelReply()));
     }
 
-    /**
-     * Failure response send
-     */
-    private void whenVxlanGpeAddDelTunnelThenFailure()
-            throws ExecutionException, InterruptedException, VppBaseCallException {
-        doReturn(TestHelperUtils.<VxlanAddDelTunnelReply>createFutureException()).when(api)
-                .vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
+    private void whenVxlanGpeAddDelTunnelThenFailure() {
+        doReturn(failedFuture()).when(api).vxlanGpeAddDelTunnel(any(VxlanGpeAddDelTunnel.class));
     }
 
     private VxlanGpeAddDelTunnel verifyVxlanGpeAddDelTunnelWasInvoked(final VxlanGpe vxlanGpe)
-            throws VppBaseCallException {
+        throws VppBaseCallException {
         ArgumentCaptor<VxlanGpeAddDelTunnel> argumentCaptor = ArgumentCaptor.forClass(VxlanGpeAddDelTunnel.class);
         verify(api).vxlanGpeAddDelTunnel(argumentCaptor.capture());
         final VxlanGpeAddDelTunnel actual = argumentCaptor.getValue();
         assertEquals(0, actual.isIpv6);
         assertArrayEquals(InetAddresses.forString(vxlanGpe.getLocal().getIpv4Address().getValue()).getAddress(),
-                actual.local);
+            actual.local);
         assertArrayEquals(InetAddresses.forString(vxlanGpe.getRemote().getIpv4Address().getValue()).getAddress(),
-                actual.remote);
+            actual.remote);
         assertEquals(vxlanGpe.getVni().getValue().intValue(), actual.vni);
         assertEquals(vxlanGpe.getNextProtocol().getIntValue(), actual.protocol);
         assertEquals(vxlanGpe.getEncapVrfId().intValue(), actual.encapVrfId);
@@ -144,7 +105,7 @@ public class VxlanGpeCustomizerTest {
         assertEquals(ADD_VXLAN_GPE, actual.isAdd);
     }
 
-    private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException{
+    private void verifyVxlanGpeDeleteWasInvoked(final VxlanGpe vxlanGpe) throws VppBaseCallException {
         final VxlanGpeAddDelTunnel actual = verifyVxlanGpeAddDelTunnelWasInvoked(vxlanGpe);
         assertEquals(DEL_VXLAN_GPE, actual.isAdd);
     }
@@ -169,32 +130,29 @@ public class VxlanGpeCustomizerTest {
         final VxlanGpe vxlanGpe = generateVxlanGpe();
 
         whenVxlanGpeAddDelTunnelThenSuccess();
-
-        doReturn(Optional.absent())
-            .when(mappingContext).read(ContextTestUtils.getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
+        noMappingDefined(mappingContext, ifaceName, "test-instance");
 
         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
         verifyVxlanGpeAddWasInvoked(vxlanGpe);
-        verify(mappingContext).put(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")), eq(
-                ContextTestUtils.getMapping(ifaceName, 0).get()));
+        verify(mappingContext).put(eq(mappingIid(ifaceName, "test-instance")), eq(
+            mapping(ifaceName, 0).get()));
     }
 
     @Test
     public void testWriteCurrentAttributesMappingAlreadyPresent() throws Exception {
         final VxlanGpe vxlanGpe = generateVxlanGpe();
+        final int ifaceId = 0;
 
         whenVxlanGpeAddDelTunnelThenSuccess();
-        final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(ifaceName, 0);
-
-        doReturn(Optional.of(new MappingsBuilder().setMapping(singletonList(ifcMapping.get())).build()))
-            .when(mappingContext).read(ContextTestUtils.getMappingIid(ifaceName, "test-instance").firstIdentifierOf(Mappings.class));
+        defineMapping(mappingContext, ifaceName, ifaceId, "test-instance");
 
         customizer.writeCurrentAttributes(id, vxlanGpe, writeContext);
         verifyVxlanGpeAddWasInvoked(vxlanGpe);
 
         // Remove the first mapping before putting in the new one
-        verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")));
-        verify(mappingContext).put(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")), eq(ifcMapping.get()));
+        verify(mappingContext).delete(eq(mappingIid(ifaceName, "test-instance")));
+        verify(mappingContext).put(eq(mappingIid(ifaceName, "test-instance")),
+            eq(mapping(ifaceName, ifaceId).get()));
     }
 
     @Test
@@ -210,8 +168,8 @@ public class VxlanGpeCustomizerTest {
             verifyVxlanGpeAddWasInvoked(vxlanGpe);
             // Mapping not stored due to failure
             verify(mappingContext, times(0))
-                    .put(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")), eq(
-                            ContextTestUtils.getMapping(ifaceName, 0).get()));
+                .put(eq(mappingIid(ifaceName, "test-instance")), eq(
+                    mapping(ifaceName, 0).get()));
             return;
         }
         fail("WriteFailedException.CreateFailedException was expected");
@@ -233,12 +191,11 @@ public class VxlanGpeCustomizerTest {
         final VxlanGpe vxlanGpe = generateVxlanGpe();
 
         whenVxlanGpeAddDelTunnelThenSuccess();
-        doReturn(ContextTestUtils.getMapping(ifaceName, 1)).when(mappingContext).read(
-                ContextTestUtils.getMappingIid(ifaceName, "test-instance"));
+        defineMapping(mappingContext, ifaceName, 1, "test-instance");
 
         customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
         verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
-        verify(mappingContext).delete(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")));
+        verify(mappingContext).delete(eq(mappingIid(ifaceName, "test-instance")));
     }
 
     @Test
@@ -246,15 +203,14 @@ public class VxlanGpeCustomizerTest {
         final VxlanGpe vxlanGpe = generateVxlanGpe();
 
         whenVxlanGpeAddDelTunnelThenFailure();
-        doReturn(ContextTestUtils.getMapping(ifaceName, 1)).when(mappingContext).read(
-                ContextTestUtils.getMappingIid(ifaceName, "test-instance"));
+        defineMapping(mappingContext, ifaceName, 1, "test-instance");
 
         try {
             customizer.deleteCurrentAttributes(id, vxlanGpe, writeContext);
         } catch (WriteFailedException.DeleteFailedException e) {
             assertTrue(e.getCause() instanceof VppBaseCallException);
             verifyVxlanGpeDeleteWasInvoked(vxlanGpe);
-            verify(mappingContext, times(0)).delete(eq(ContextTestUtils.getMappingIid(ifaceName, "test-instance")));
+            verify(mappingContext, times(0)).delete(eq(mappingIid(ifaceName, "test-instance")));
             return;
         }
         fail("WriteFailedException.DeleteFailedException was expected");