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;
@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";
@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);
}
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);
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);
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
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");
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);
@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");