diff options
author | Jan Srnicek <jsrnicek@cisco.com> | 2016-09-23 16:39:09 +0200 |
---|---|---|
committer | Jan Srnicek <jsrnicek@cisco.com> | 2016-09-23 16:41:57 +0200 |
commit | a7147d16c31d9028c6b5dc557264433de0f11c91 (patch) | |
tree | 15816600ab3dfa5eea706ef1b1910b2e13ae66d7 /v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate | |
parent | e7a0775b21c2ea6b7bb8efb63b5384df26e27fbb (diff) |
HONEYCOMB-145 - Utility Class Refactoring
problematic mockito-all changed to mockito-core( https://github.com/mockito/mockito/issues/324)
Translate Utils Splitted to multiple Trait Interfaces
Ipv4Translator - Logic for translation of ipv4-based data
Ipv6Translator - Logic for translation of ipv6-based data
MacTranslator - Logic for translation of mac-based data
AddressTranslator - Aggregation trait for Ipv4/Ipv6/Mac
JvppReplyConsumer - Logic for extracting replies from jvpp calls
ByteDataTranslator - any byte-based conversions
Plus some existing utility classes changed to traits
Change-Id: I342b625954223966802e65dca0fabf8456c89345
Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
Diffstat (limited to 'v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate')
-rw-r--r-- | v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceCustomizerTest.java | 24 | ||||
-rw-r--r-- | v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceDataTranslatorTest.java (renamed from v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceUtilsTest.java) | 22 | ||||
-rw-r--r-- | v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java | 121 |
3 files changed, 58 insertions, 109 deletions
diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceCustomizerTest.java index acc407bd3..5c6f88abd 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceCustomizerTest.java @@ -49,7 +49,7 @@ import org.openvpp.jvpp.core.dto.SwInterfaceDetailsReplyDump; import org.openvpp.jvpp.core.dto.SwInterfaceDump; public class InterfaceCustomizerTest extends - ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> { + ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> implements InterfaceDataTranslator { private static final String IFC_CTX_NAME = "ifc-test-instance"; private static final String IFACE0_NAME = "eth0"; @@ -96,23 +96,23 @@ public class InterfaceCustomizerTest extends private void verifySwInterfaceDumpWasInvoked(final int nameFilterValid, final String ifaceName, final int dumpIfcsInvocationCount) - throws VppInvocationException { + throws VppInvocationException { final SwInterfaceDump expected = new SwInterfaceDump(); - expected.nameFilterValid = (byte)nameFilterValid; + expected.nameFilterValid = (byte) nameFilterValid; expected.nameFilter = ifaceName.getBytes(); verify(api, times(dumpIfcsInvocationCount)).swInterfaceDump(expected); } - private static void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) { + private void assertIfacesAreEqual(final Interface iface, final SwInterfaceDetails details) { assertEquals(iface.getName(), new String(details.interfaceName)); - Assert.assertEquals(InterfaceUtils.yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex); - assertEquals(iface.getPhysAddress().getValue(), InterfaceUtils.vppPhysAddrToYang(details.l2Address)); + Assert.assertEquals(yangIfIndexToVpp(iface.getIfIndex().intValue()), details.swIfIndex); + assertEquals(iface.getPhysAddress().getValue(), vppPhysAddrToYang(details.l2Address)); } @Test public void testReadCurrentAttributes() throws Exception { final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(IFACE0_NAME)); + .child(Interface.class, new InterfaceKey(IFACE0_NAME)); final InterfaceBuilder builder = getCustomizer().getBuilder(id); final SwInterfaceDetails iface = new SwInterfaceDetails(); @@ -134,7 +134,7 @@ public class InterfaceCustomizerTest extends public void testReadCurrentAttributesFailed() throws Exception { final String ifaceName = IFACE0_NAME; final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(ifaceName)); + .child(Interface.class, new InterfaceKey(ifaceName)); final InterfaceBuilder builder = getCustomizer().getBuilder(id); whenSwInterfaceDumpThenReturn(Collections.emptyList()); @@ -152,7 +152,7 @@ public class InterfaceCustomizerTest extends @Test public void testReadSubInterface() throws Exception { final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME)); + .child(Interface.class, new InterfaceKey(SUB_IFACE_NAME)); final InterfaceBuilder builder = mock(InterfaceBuilder.class); final SwInterfaceDetails iface = new SwInterfaceDetails(); @@ -172,7 +172,7 @@ public class InterfaceCustomizerTest extends @Test public void testGetAllIds() throws Exception { final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class); + .child(Interface.class); final SwInterfaceDetails swIf0 = new SwInterfaceDetails(); swIf0.swIfIndex = 0; @@ -188,7 +188,7 @@ public class InterfaceCustomizerTest extends whenSwInterfaceDumpThenReturn(Arrays.asList(swIf0, swIf1, swSubIf1)); final List<InterfaceKey> expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey( - IFACE1_NAME)); + IFACE1_NAME)); final List<InterfaceKey> actualIds = getCustomizer().getAllIds(id, ctx); verifySwInterfaceDumpWasInvoked(0, "", 1); @@ -200,7 +200,7 @@ public class InterfaceCustomizerTest extends @Test public void testGetAllIdsWithDisabled() throws Exception { final InstanceIdentifier<Interface> id = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class); + .child(Interface.class); doReturn(true).when(interfaceDisableContext).isInterfaceDisabled(1, mappingContext); diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceUtilsTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceDataTranslatorTest.java index c74e1ccfb..56567f282 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceUtilsTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceDataTranslatorTest.java @@ -25,31 +25,31 @@ 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.VxlanGpeTunnel; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanTunnel; -public class InterfaceUtilsTest { +public class InterfaceDataTranslatorTest implements InterfaceDataTranslator { @Test public void testVppPhysAddrToYang() throws Exception { - assertEquals("01:02:03:04:05:06", InterfaceUtils.vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5, 6})); - assertEquals("0a:0b:0c:0d:0e:0f", InterfaceUtils.vppPhysAddrToYang(new byte[]{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0})); + assertEquals("01:02:03:04:05:06", vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5, 6})); + assertEquals("0a:0b:0c:0d:0e:0f", vppPhysAddrToYang(new byte[]{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0})); } @Test(expected = NullPointerException.class) public void testVppPhysAddrToYangFailNullArgument() throws Exception { - InterfaceUtils.vppPhysAddrToYang(null); + vppPhysAddrToYang(null); } @Test(expected = IllegalArgumentException.class) public void testVppPhysAddrToYangInvalidByteArrayLength() throws Exception { - InterfaceUtils.vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5}); + vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5}); } @Test public void testGetInterfaceType() { - assertEquals(Tap.class, InterfaceUtils.getInterfaceType("tap0")); - assertEquals(VxlanTunnel.class, InterfaceUtils.getInterfaceType("vxlan0")); - assertEquals(VxlanGpeTunnel.class, InterfaceUtils.getInterfaceType("vxlan_gpe0")); - assertEquals(VhostUser.class, InterfaceUtils.getInterfaceType("VirtualEthernet0/0/0")); - assertEquals(EthernetCsmacd.class, InterfaceUtils.getInterfaceType("eth0.0")); - assertEquals(EthernetCsmacd.class, InterfaceUtils.getInterfaceType("local0")); + assertEquals(Tap.class, getInterfaceType("tap0")); + assertEquals(VxlanTunnel.class, getInterfaceType("vxlan0")); + assertEquals(VxlanGpeTunnel.class, getInterfaceType("vxlan_gpe0")); + assertEquals(VhostUser.class, getInterfaceType("VirtualEthernet0/0/0")); + assertEquals(EthernetCsmacd.class, getInterfaceType("eth0.0")); + assertEquals(EthernetCsmacd.class, getInterfaceType("local0")); } }
\ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java index be0ef14df..d351d2402 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java @@ -16,12 +16,11 @@ package io.fd.honeycomb.translate.v3po.interfacesstate.ip; -import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseBytes; -import static org.hamcrest.CoreMatchers.is; + +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableList; @@ -29,13 +28,14 @@ import io.fd.honeycomb.translate.ModificationCache; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; import io.fd.honeycomb.translate.v3po.test.ContextTestUtils; +import io.fd.honeycomb.translate.v3po.util.Ipv4Translator; import io.fd.honeycomb.translate.v3po.util.NamingContext; -import io.fd.honeycomb.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest; import java.util.Arrays; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; -import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; @@ -54,13 +54,15 @@ import org.openvpp.jvpp.core.dto.IpAddressDetails; import org.openvpp.jvpp.core.dto.IpAddressDetailsReplyDump; import org.openvpp.jvpp.core.dto.IpAddressDump; -public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> { +public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> implements + Ipv4Translator { private static final String IFACE_NAME = "eth0"; private static final String IFACE_2_NAME = "eth1"; private static final int IFACE_ID = 1; private static final int IFACE_2_ID = 2; private static final String IFC_CTX_NAME = "ifc-test-instance"; + public static final String CACHE_KEY = Ipv4AddressCustomizer.class.getName(); private NamingContext interfacesContext; @@ -90,63 +92,44 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, } @Test - public void testReadCurrentAttributesFromCache() throws ReadFailedException { - ModificationCache cache = new ModificationCache(); - - IpAddressDetails detail1 = new IpAddressDetails(); - IpAddressDetails detail2 = new IpAddressDetails(); - IpAddressDetails detail3 = new IpAddressDetails(); - - detail1.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); - detail2.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); - detail3.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")))); - - IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); - reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); - - cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply); - when(ctx.getModificationCache()).thenReturn(cache); - - final AddressBuilder builder = new AddressBuilder(); - final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME); - - getCustomizer().readCurrentAttributes(id, builder, ctx); - - assertEquals("192.168.2.1", builder.getIp().getValue()); - } - - @Test public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException { - ModificationCache cache = new ModificationCache(); + //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work) + ModificationCache cache = mock(ModificationCache.class); IpAddressDetails detail1 = new IpAddressDetails(); IpAddressDetails detail2 = new IpAddressDetails(); detail1.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); detail2.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); reply.ipAddressDetails = ImmutableList.of(detail1); IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump(); reply2.ipAddressDetails = ImmutableList.of(detail2); - when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2)); + CompletableFuture<IpAddressDetailsReplyDump> future = new CompletableFuture<>(); + future.complete(reply); + CompletableFuture<IpAddressDetailsReplyDump> future2 = new CompletableFuture<>(); + future2.complete(reply2); + + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2) + .thenReturn(future).thenReturn(future2); + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2)) + .thenReturn(future(reply)).thenReturn(future(reply2)); when(ctx.getModificationCache()).thenReturn(cache); + final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME); final InstanceIdentifier<Address> id2 = getId("192.168.2.2", IFACE_2_NAME); final List<AddressKey> ifc1Ids = getCustomizer().getAllIds(id, ctx); assertThat(ifc1Ids.size(), is(1)); - assertThat(ifc1Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1")))); + assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1")))); final List<AddressKey> ifc2Ids = getCustomizer().getAllIds(id2, ctx); assertThat(ifc2Ids.size(), is(1)); - assertThat(ifc2Ids, CoreMatchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2")))); + assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2")))); AddressBuilder builder = new AddressBuilder(); getCustomizer().readCurrentAttributes(id, builder, ctx); @@ -157,7 +140,7 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, } @Test - public void testReadCurrentAttributesFromOperationalData() throws ReadFailedException { + public void testReadCurrentAttributesSuccessfull() throws ReadFailedException { ModificationCache cache = new ModificationCache(); IpAddressDetails detail1 = new IpAddressDetails(); @@ -165,11 +148,11 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, IpAddressDetails detail3 = new IpAddressDetails(); detail1.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); detail2.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); detail3.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")))); IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); @@ -185,41 +168,7 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, } @Test - public void testGetAllIdsFromCache() throws ReadFailedException { - ModificationCache cache = new ModificationCache(); - - IpAddressDetails detail1 = new IpAddressDetails(); - IpAddressDetails detail2 = new IpAddressDetails(); - IpAddressDetails detail3 = new IpAddressDetails(); - - detail1.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); - detail2.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); - detail3.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")))); - - IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); - reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); - - cache.put(Ipv4ReadUtils.CACHE_KEY + IFACE_NAME, reply); - when(ctx.getModificationCache()).thenReturn(cache); - - final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME); - - List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream() - .map(key -> key.getIp()) - .collect(Collectors.toList()); - - verify(api, times(0)).ipAddressDump(Mockito.any(IpAddressDump.class)); - assertEquals(3, ids.size()); - assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue())); - assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue())); - assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue())); - } - - @Test - public void testGetAllIdsFromOperationalData() throws ReadFailedException { + public void testGetAllIdsFromSuccessfull() throws ReadFailedException { ModificationCache cache = new ModificationCache(); IpAddressDetails detail1 = new IpAddressDetails(); @@ -227,11 +176,11 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, IpAddressDetails detail3 = new IpAddressDetails(); detail1.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1")))); detail2.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2")))); detail3.ip = reverseBytes( - TranslateUtils.ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")))); + ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3")))); IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); @@ -241,8 +190,8 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, final InstanceIdentifier<Address> id = getId("192.168.2.1", IFACE_NAME); List<Ipv4AddressNoZone> ids = getCustomizer().getAllIds(id, ctx).stream() - .map(key -> key.getIp()) - .collect(Collectors.toList()); + .map(key -> key.getIp()) + .collect(Collectors.toList()); assertEquals(3, ids.size()); assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue())); |