HONEYCOMB-116: utility for stubbing jvpp methods
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / RewriteCustomizerTest.java
index 1b35dfe..e28a0f6 100644 (file)
 package io.fd.honeycomb.translate.v3po.interfaces;
 
 import static junit.framework.TestCase.assertTrue;
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.verify;
-import static org.mockito.MockitoAnnotations.initMocks;
 
-import com.google.common.base.Optional;
 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
-import io.fd.honeycomb.translate.write.WriteContext;
-import io.fd.honeycomb.translate.MappingContext;
-import io.fd.honeycomb.translate.v3po.test.TestHelperUtils;
 import io.fd.honeycomb.translate.v3po.util.NamingContext;
 import io.fd.honeycomb.translate.v3po.util.TagRewriteOperation;
 import io.fd.honeycomb.translate.write.WriteFailedException;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
+import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
 import org.junit.Assert;
-import org.junit.Before;
 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.Mapping;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
@@ -54,44 +43,36 @@ import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.openvpp.jvpp.VppBaseCallException;
 import org.openvpp.jvpp.VppInvocationException;
-import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewrite;
-import org.openvpp.jvpp.dto.L2InterfaceVlanTagRewriteReply;
-import org.openvpp.jvpp.future.FutureJVpp;
+import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewrite;
+import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply;
 
-public class RewriteCustomizerTest {
-
-    @Mock
-    private FutureJVpp api;
-    @Mock
-    private WriteContext writeContext;
-    @Mock
-    private MappingContext mappingContext;
+public class RewriteCustomizerTest extends WriterCustomizerTest {
 
     private NamingContext namingContext;
     private RewriteCustomizer customizer;
 
-    public static final String VLAN_IF_NAME = "local0.1";
-    public static final int VLAN_IF_ID = 1;
-    public static final int VLAN_IF_INDEX = 11;
+    private static final String IFC_TEST_INSTANCE = "ifc-test-instance";
+    private static final String IF_NAME = "local0";
+    private static final String VLAN_IF_NAME = "local0.1";
+    private static final int VLAN_IF_ID = 1;
+    private static final int VLAN_IF_INDEX = 11;
+    private InstanceIdentifier<Rewrite> VLAN_IID;
 
-    @Before
+    @Override
     public void setUp() throws Exception {
-        initMocks(this);
-        namingContext = new NamingContext("generatedSubInterfaceName", "test-instance");
-        doReturn(mappingContext).when(writeContext).getMappingContext();
+        namingContext = new NamingContext("generatedSubInterfaceName", IFC_TEST_INSTANCE);
         customizer = new RewriteCustomizer(api, namingContext);
-
-        final Optional<Mapping> ifcMapping = ContextTestUtils.getMapping(VLAN_IF_NAME, VLAN_IF_INDEX);
-        doReturn(ifcMapping).when(mappingContext).read(any());
+        VLAN_IID = getVlanTagRewriteId(IF_NAME, VLAN_IF_ID);
+        ContextTestUtils.mockMapping(mappingContext, VLAN_IF_NAME, VLAN_IF_INDEX, IFC_TEST_INSTANCE);
     }
 
-    private InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
-        final Class<ChildOf<? super SubInterface>> child = (Class)Rewrite.class;
+    private static InstanceIdentifier<Rewrite> getVlanTagRewriteId(final String name, final long index) {
+        final Class<ChildOf<? super SubInterface>> child = (Class) Rewrite.class;
         final InstanceIdentifier id =
-                InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
-                        SubinterfaceAugmentation.class).child(SubInterfaces.class)
-                        .child(SubInterface.class, new SubInterfaceKey(index))
-                        .child(child);
+            InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(name)).augmentation(
+                SubinterfaceAugmentation.class).child(SubInterfaces.class)
+                .child(SubInterface.class, new SubInterfaceKey(index))
+                .child(child);
         return id;
     }
 
@@ -114,70 +95,48 @@ public class RewriteCustomizerTest {
     /**
      * Positive response
      */
-    private void whenL2InterfaceVlanTagRewriteThenSuccess()
-            throws ExecutionException, InterruptedException, VppInvocationException {
-        final CompletableFuture<L2InterfaceVlanTagRewriteReply> replyFuture = new CompletableFuture<>();
-        final L2InterfaceVlanTagRewriteReply reply = new L2InterfaceVlanTagRewriteReply();
-        replyFuture.complete(reply);
-        doReturn(replyFuture).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
+    private void whenL2InterfaceVlanTagRewriteThenSuccess() {
+        doReturn(future(new L2InterfaceVlanTagRewriteReply())).when(api)
+            .l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
     }
 
     /**
      * Failure response send
      */
-    private void whenL2InterfaceVlanTagRewriteThenFailure()
-            throws ExecutionException, InterruptedException, VppInvocationException {
-        doReturn(TestHelperUtils.<L2InterfaceVlanTagRewriteReply>createFutureException()).when(api)
-                .l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
-    }
-
-    private void verifyL2InterfaceVlanTagRewriteWasInvoked(final L2InterfaceVlanTagRewrite expected)
-            throws VppInvocationException {
-        ArgumentCaptor<L2InterfaceVlanTagRewrite> argumentCaptor =
-                ArgumentCaptor.forClass(L2InterfaceVlanTagRewrite.class);
-        verify(api).l2InterfaceVlanTagRewrite(argumentCaptor.capture());
-        final L2InterfaceVlanTagRewrite actual = argumentCaptor.getValue();
-        assertEquals(expected.swIfIndex, actual.swIfIndex);
-        assertEquals(expected.vtrOp, actual.vtrOp);
-        assertEquals(expected.pushDot1Q, actual.pushDot1Q);
-        assertEquals(expected.tag1, actual.tag1);
-        assertEquals(expected.tag2, actual.tag2);
+    private void whenL2InterfaceVlanTagRewriteThenFailure() {
+        doReturn(failedFuture()).when(api).l2InterfaceVlanTagRewrite(any(L2InterfaceVlanTagRewrite.class));
     }
 
     private void verifyL2InterfaceVlanTagRewriteDeleteWasInvoked() throws VppInvocationException {
         final L2InterfaceVlanTagRewrite request = new L2InterfaceVlanTagRewrite();
         request.swIfIndex = VLAN_IF_INDEX;
-        verifyL2InterfaceVlanTagRewriteWasInvoked(request);
+        verify(api).l2InterfaceVlanTagRewrite(request);
     }
 
     @Test
     public void testCreate() throws Exception {
         final TagRewriteOperation op = TagRewriteOperation.pop_2;
         final Rewrite vlanTagRewrite = generateRewrite(op);
-        final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
 
         whenL2InterfaceVlanTagRewriteThenSuccess();
 
-        customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
+        customizer.writeCurrentAttributes(VLAN_IID, vlanTagRewrite, writeContext);
 
-        verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
+        verify(api).l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
     }
 
     @Test
     public void testCreateFailed() throws Exception {
         final TagRewriteOperation op = TagRewriteOperation.pop_2;
         final Rewrite vlanTagRewrite = generateRewrite(op);
-        final String subIfaceName = "local0.11";
-        final int subifIndex = 1;
-        final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(subIfaceName, subifIndex);
 
         whenL2InterfaceVlanTagRewriteThenFailure();
 
         try {
-            customizer.writeCurrentAttributes(id, vlanTagRewrite, writeContext);
+            customizer.writeCurrentAttributes(VLAN_IID, vlanTagRewrite, writeContext);
         } catch (WriteFailedException.CreateFailedException e) {
             assertTrue(e.getCause() instanceof VppBaseCallException);
-            verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
+            verify(api).l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, op));
             return;
         }
         fail("WriteFailedException.CreateFailedException was expected");
@@ -187,30 +146,28 @@ public class RewriteCustomizerTest {
     public void testUpdate() throws Exception {
         final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
         final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
-        final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
 
         whenL2InterfaceVlanTagRewriteThenSuccess();
 
-        customizer.updateCurrentAttributes(id, before, after, writeContext);
+        customizer.updateCurrentAttributes(VLAN_IID, before, after, writeContext);
 
-        verifyL2InterfaceVlanTagRewriteWasInvoked(
-                generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
+        verify(api)
+            .l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
     }
 
     @Test
     public void testUpdateFailed() throws Exception {
         final Rewrite before = generateRewrite(TagRewriteOperation.pop_2);
         final Rewrite after = generateRewrite(TagRewriteOperation.pop_1);
-        final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
 
         whenL2InterfaceVlanTagRewriteThenFailure();
 
         try {
-            customizer.updateCurrentAttributes(id, before, after, writeContext);
+            customizer.updateCurrentAttributes(VLAN_IID, before, after, writeContext);
         } catch (WriteFailedException.UpdateFailedException e) {
             assertTrue(e.getCause() instanceof VppBaseCallException);
-            verifyL2InterfaceVlanTagRewriteWasInvoked(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX,
-                    TagRewriteOperation.pop_1));
+            verify(api)
+                .l2InterfaceVlanTagRewrite(generateL2InterfaceVlanTagRewrite(VLAN_IF_INDEX, TagRewriteOperation.pop_1));
             return;
         }
         fail("WriteFailedException.UpdateFailedException was expected");
@@ -218,23 +175,19 @@ public class RewriteCustomizerTest {
 
     @Test
     public void testDelete() throws Exception {
-        final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
-
         whenL2InterfaceVlanTagRewriteThenSuccess();
 
-        customizer.deleteCurrentAttributes(id, null, writeContext);
+        customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
 
         verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();
     }
 
     @Test
     public void testDeleteFailed() throws Exception {
-        final InstanceIdentifier<Rewrite> id = getVlanTagRewriteId(VLAN_IF_NAME, VLAN_IF_ID);
-
         whenL2InterfaceVlanTagRewriteThenFailure();
 
         try {
-            customizer.deleteCurrentAttributes(id, null, writeContext);
+            customizer.deleteCurrentAttributes(VLAN_IID, null, writeContext);
         } catch (WriteFailedException.DeleteFailedException e) {
             Assert.assertTrue(e.getCause() instanceof VppBaseCallException);
             verifyL2InterfaceVlanTagRewriteDeleteWasInvoked();