From a7147d16c31d9028c6b5dc557264433de0f11c91 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Fri, 23 Sep 2016 16:39:09 +0200 Subject: 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 --- .../interfacesstate/InterfaceCustomizerTest.java | 24 ++-- .../InterfaceDataTranslatorTest.java | 55 ++++++++++ .../v3po/interfacesstate/InterfaceUtilsTest.java | 55 ---------- .../ip/Ipv4AddressCustomizerTest.java | 121 ++++++--------------- 4 files changed, 102 insertions(+), 153 deletions(-) create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceDataTranslatorTest.java delete mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceUtilsTest.java (limited to 'v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate') 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 { + ListReaderCustomizerTest 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 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 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 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 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 expectedIds = Arrays.asList(new InterfaceKey(IFACE0_NAME), new InterfaceKey( - IFACE1_NAME)); + IFACE1_NAME)); final List actualIds = getCustomizer().getAllIds(id, ctx); verifySwInterfaceDumpWasInvoked(0, "", 1); @@ -200,7 +200,7 @@ public class InterfaceCustomizerTest extends @Test public void testGetAllIdsWithDisabled() throws Exception { final InstanceIdentifier 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/InterfaceDataTranslatorTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceDataTranslatorTest.java new file mode 100644 index 000000000..56567f282 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceDataTranslatorTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.v3po.interfacesstate; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Tap; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser; +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 InterfaceDataTranslatorTest implements InterfaceDataTranslator { + + @Test + public void testVppPhysAddrToYang() throws Exception { + 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 { + vppPhysAddrToYang(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testVppPhysAddrToYangInvalidByteArrayLength() throws Exception { + vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5}); + } + + @Test + public void testGetInterfaceType() { + 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/InterfaceUtilsTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceUtilsTest.java deleted file mode 100644 index c74e1ccfb..000000000 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/interfacesstate/InterfaceUtilsTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.fd.honeycomb.translate.v3po.interfacesstate; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Tap; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser; -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 { - - @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})); - } - - @Test(expected = NullPointerException.class) - public void testVppPhysAddrToYangFailNullArgument() throws Exception { - InterfaceUtils.vppPhysAddrToYang(null); - } - - @Test(expected = IllegalArgumentException.class) - public void testVppPhysAddrToYangInvalidByteArrayLength() throws Exception { - InterfaceUtils.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")); - } -} \ 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 { +public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest 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; @@ -89,64 +91,45 @@ public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest 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 future = new CompletableFuture<>(); + future.complete(reply); + CompletableFuture 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
id = getId("192.168.2.1", IFACE_NAME); final InstanceIdentifier
id2 = getId("192.168.2.2", IFACE_2_NAME); final List 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 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 id = getId("192.168.2.1", IFACE_NAME); - - List 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 id = getId("192.168.2.1", IFACE_NAME); List 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())); -- cgit 1.2.3-korg