Move V3poUtils for vpp-translate-utils
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / v3po / translate / v3po / interfaces / VhostUserCustomizerTest.java
index 076df67..a6fb643 100644 (file)
 
 package io.fd.honeycomb.v3po.translate.v3po.interfaces;
 
+import static io.fd.honeycomb.v3po.translate.v3po.ContextTestUtils.getMapping;
+import static io.fd.honeycomb.v3po.translate.v3po.ContextTestUtils.getMappingIid;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 import static org.mockito.MockitoAnnotations.initMocks;
 
-import io.fd.honeycomb.v3po.translate.Context;
+import io.fd.honeycomb.v3po.translate.MappingContext;
+import io.fd.honeycomb.v3po.translate.ModificationCache;
 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
-import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
+import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
+import io.fd.honeycomb.v3po.translate.write.WriteContext;
 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
@@ -62,9 +67,10 @@ public class VhostUserCustomizerTest {
     @Mock
     private FutureJVpp api;
     @Mock
-    private Context ctx;
+    private WriteContext writeContext;
+    @Mock
+    private MappingContext mappingContext;
 
-    private NamingContext namingContext;
     private VhostUserCustomizer customizer;
     private static final int IFACE_ID = 1;
     private static final String IFACE_NAME = "eth0";
@@ -75,7 +81,13 @@ public class VhostUserCustomizerTest {
     @Before
     public void setUp() throws Exception {
         initMocks(this);
-        namingContext = new NamingContext("generatedInterfaceName");
+        InterfaceTypeTestUtils.setupWriteContext(writeContext,
+            org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser.class);
+        final NamingContext namingContext = new NamingContext("generatedInterfaceName", "test-instance");
+        final ModificationCache toBeReturned = new ModificationCache();
+        doReturn(toBeReturned).when(writeContext).getModificationCache();
+        doReturn(mappingContext).when(writeContext).getMappingContext();
+
         // TODO create base class for tests using vppApi
         customizer = new VhostUserCustomizer(api, namingContext);
     }
@@ -140,7 +152,7 @@ public class VhostUserCustomizerTest {
         final CreateVhostUserIf actual = argumentCaptor.getValue();
         assertEquals(0, actual.customDevInstance);
 
-        assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
+        assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
         assertEquals(0, actual.renumber);
         assertEquals(0, actual.useCustomMac);
         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
@@ -154,7 +166,7 @@ public class VhostUserCustomizerTest {
         final ModifyVhostUserIf actual = argumentCaptor.getValue();
         assertEquals(0, actual.customDevInstance);
 
-        assertEquals(V3poUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
+        assertEquals(TranslateUtils.booleanToByte(VhostUserRole.Server.equals(vhostUser.getRole())), actual.isServer);
         assertEquals(0, actual.renumber);
         assertEquals(swIfIndex, actual.swIfIndex);
         assertArrayEquals(vhostUser.getSocket().getBytes(), actual.sockFilename);
@@ -182,9 +194,9 @@ public class VhostUserCustomizerTest {
 
         whenCreateVhostUserIfThenSuccess();
 
-        customizer.writeCurrentAttributes(ID, vhostUser, ctx);
+        customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
         verifyCreateVhostUserIfWasInvoked(vhostUser);
-        assertTrue(namingContext.containsIndex(IFACE_NAME));
+        verify(mappingContext).put(eq(getMappingIid(IFACE_NAME, "test-instance")), eq(getMapping(IFACE_NAME, 0).get()));
     }
 
     @Test
@@ -194,11 +206,11 @@ public class VhostUserCustomizerTest {
         whenVxlanAddDelTunnelThenFailure();
 
         try {
-            customizer.writeCurrentAttributes(ID, vhostUser, ctx);
+            customizer.writeCurrentAttributes(ID, vhostUser, writeContext);
         } catch (WriteFailedException.CreateFailedException e) {
             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
             verifyCreateVhostUserIfWasInvoked(vhostUser);
-            assertFalse(namingContext.containsIndex(IFACE_NAME));
+            verifyZeroInteractions(mappingContext);
             return;
         }
         fail("WriteFailedException.CreateFailedException was expected");
@@ -208,32 +220,24 @@ public class VhostUserCustomizerTest {
     public void testUpdateCurrentAttributes() throws Exception {
         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
-        namingContext.addName(IFACE_ID, IFACE_NAME);
+        doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
 
         whenModifyVhostUserIfThenSuccess();
 
-        customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, ctx);
+        customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
         verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
     }
 
-    @Test
-    public void testUpdateCurrentAttributesNoUpdate() throws Exception {
-        final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Server, "socketName");
-        final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName");
-        customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, ctx);
-        verify(api, never()).modifyVhostUserIf(any(ModifyVhostUserIf.class));
-    }
-
     @Test
     public void testUpdateCurrentAttributesFailed() throws Exception {
         final VhostUser vhostUserBefore = generateVhostUser(VhostUserRole.Client, "socketName0");
         final VhostUser vhostUserAfter = generateVhostUser(VhostUserRole.Server, "socketName1");
-        namingContext.addName(IFACE_ID, IFACE_NAME);
+        doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
 
         whenModifyVhostUserIfThenFailure();
 
         try {
-            customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, ctx);
+            customizer.updateCurrentAttributes(ID, vhostUserBefore, vhostUserAfter, writeContext);
         } catch (WriteFailedException.UpdateFailedException e) {
             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
             verifyModifyVhostUserIfWasInvoked(vhostUserAfter, IFACE_ID);
@@ -245,28 +249,30 @@ public class VhostUserCustomizerTest {
     @Test
     public void testDeleteCurrentAttributes() throws Exception {
         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
-        namingContext.addName(IFACE_ID, IFACE_NAME);
+        doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
 
         whenDeleteVhostUserIfThenSuccess();
 
-        customizer.deleteCurrentAttributes(ID, vhostUser, ctx);
+        customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
         verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
-        assertFalse(namingContext.containsIndex(IFACE_NAME));
+        verify(mappingContext).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
     }
 
     @Test
     public void testDeleteCurrentAttributesFailed() throws Exception {
         final VhostUser vhostUser = generateVhostUser(VhostUserRole.Client, "socketName");
-        namingContext.addName(IFACE_ID, IFACE_NAME);
+        doReturn(getMapping(IFACE_NAME, IFACE_ID)).when(mappingContext).read(getMappingIid(IFACE_NAME, "test-instance"));
 
         whenDeleteVhostUserIfThenFailure();
 
         try {
-            customizer.deleteCurrentAttributes(ID, vhostUser, ctx);
+            customizer.deleteCurrentAttributes(ID, vhostUser, writeContext);
         } catch (WriteFailedException.DeleteFailedException e) {
             assertEquals(VppApiInvocationException.class, e.getCause().getClass());
             verifyDeleteVhostUserIfWasInvoked(IFACE_ID);
-            assertTrue(namingContext.containsIndex(IFACE_NAME));
+            // Delete from context not invoked if delete from VPP failed
+            verify(mappingContext, times(0)).delete(eq(getMappingIid(IFACE_NAME, "test-instance")));
+            verify(mappingContext).read(eq(getMappingIid(IFACE_NAME, "test-instance")));
             return;
         }
         fail("WriteFailedException.DeleteFailedException was expected");