From e7a0775b21c2ea6b7bb8efb63b5384df26e27fbb Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Fri, 23 Sep 2016 07:21:11 +0200 Subject: HONEYCOMB-116: utility for stubbing jvpp methods - introduces FutureProducer (inspired by https://gerrit.fd.io/r/#/c/2650/) - updates unit tests for v3po and lisp Change-Id: I56488bb1dcd6fcaf6821a58f99b528677e095662 Signed-off-by: Marek Gradzki --- .../ArpTerminationTableEntryCustomizerTest.java | 12 +--- .../v3po/vpp/BridgeDomainCustomizerTest.java | 83 ++++++++++++---------- .../translate/v3po/vpp/BridgeDomainTestUtils.java | 51 ------------- .../v3po/vpp/L2FibEntryCustomizerTest.java | 13 +--- 4 files changed, 52 insertions(+), 107 deletions(-) delete mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainTestUtils.java (limited to 'v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp') diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/ArpTerminationTableEntryCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/ArpTerminationTableEntryCustomizerTest.java index efd485f10..4a38101af 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/ArpTerminationTableEntryCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/ArpTerminationTableEntryCustomizerTest.java @@ -26,11 +26,9 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import io.fd.honeycomb.translate.v3po.test.ContextTestUtils; -import io.fd.honeycomb.translate.v3po.test.TestHelperUtils; -import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest; import io.fd.honeycomb.translate.v3po.util.NamingContext; import io.fd.honeycomb.translate.write.WriteFailedException; -import java.util.concurrent.CompletableFuture; +import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; @@ -89,15 +87,11 @@ public class ArpTerminationTableEntryCustomizerTest extends WriterCustomizerTest } private void whenBdIpMacAddDelThenSuccess() { - final CompletableFuture replyFuture = new CompletableFuture<>(); - final BdIpMacAddDelReply reply = new BdIpMacAddDelReply(); - replyFuture.complete(reply); - doReturn(replyFuture).when(api).bdIpMacAddDel(any(BdIpMacAddDel.class)); + doReturn(future(new BdIpMacAddDelReply())).when(api).bdIpMacAddDel(any(BdIpMacAddDel.class)); } private void whenBdIpMacAddDelThenFailure() { - doReturn(TestHelperUtils.createFutureException()).when(api) - .bdIpMacAddDel(any(BdIpMacAddDel.class)); + doReturn(failedFuture()).when(api).bdIpMacAddDel(any(BdIpMacAddDel.class)); } private BdIpMacAddDel generateBdIpMacAddDelRequest(final byte[] ipAddress, final byte[] macAddress, diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainCustomizerTest.java index d08192c87..6029f3faf 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainCustomizerTest.java @@ -16,25 +16,26 @@ package io.fd.honeycomb.translate.v3po.vpp; +import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import io.fd.honeycomb.translate.v3po.test.ContextTestUtils; -import io.fd.honeycomb.translate.v3po.test.TestHelperUtils; -import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest; import io.fd.honeycomb.translate.v3po.util.NamingContext; import io.fd.honeycomb.translate.write.WriteFailedException; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.ExecutionException; +import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest; +import javax.annotation.Nullable; import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; import org.openvpp.jvpp.VppInvocationException; import org.openvpp.jvpp.core.dto.BridgeDomainAddDel; import org.openvpp.jvpp.core.dto.BridgeDomainAddDelReply; @@ -50,6 +51,22 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { customizer = new BridgeDomainCustomizer(api, new NamingContext("generatedBDName", BD_CTX_NAME)); } + @Nullable + private static Boolean intToBoolean(final int value) { + if (value == 0) { + return Boolean.FALSE; + } + if (value == 1) { + return Boolean.TRUE; + } + return null; + } + + private static KeyedInstanceIdentifier bdIdentifierForName( + final String bdName) { + return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(bdName)); + } + private BridgeDomain generateBridgeDomain(final String bdName) { final byte arpTerm = 0; final byte flood = 1; @@ -63,22 +80,22 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { final int forward, final int learn, final int uuf) { return new BridgeDomainBuilder() .setName(bdName) - .setArpTermination(BridgeDomainTestUtils.intToBoolean(arpTerm)) - .setFlood(BridgeDomainTestUtils.intToBoolean(flood)) - .setForward(BridgeDomainTestUtils.intToBoolean(forward)) - .setLearn(BridgeDomainTestUtils.intToBoolean(learn)) - .setUnknownUnicastFlood(BridgeDomainTestUtils.intToBoolean(uuf)) + .setArpTermination(intToBoolean(arpTerm)) + .setFlood(intToBoolean(flood)) + .setForward(intToBoolean(forward)) + .setLearn(intToBoolean(learn)) + .setUnknownUnicastFlood(intToBoolean(uuf)) .build(); } private void verifyBridgeDomainAddOrUpdateWasInvoked(final BridgeDomain bd, final int bdId) throws VppInvocationException { final BridgeDomainAddDel expected = new BridgeDomainAddDel(); - expected.arpTerm = BridgeDomainTestUtils.booleanToByte(bd.isArpTermination()); - expected.flood = BridgeDomainTestUtils.booleanToByte(bd.isFlood()); - expected.forward = BridgeDomainTestUtils.booleanToByte(bd.isForward()); - expected.learn = BridgeDomainTestUtils.booleanToByte(bd.isLearn()); - expected.uuFlood = BridgeDomainTestUtils.booleanToByte(bd.isUnknownUnicastFlood()); + expected.arpTerm = booleanToByte(bd.isArpTermination()); + expected.flood = booleanToByte(bd.isFlood()); + expected.forward = booleanToByte(bd.isForward()); + expected.learn = booleanToByte(bd.isLearn()); + expected.uuFlood = booleanToByte(bd.isUnknownUnicastFlood()); expected.isAdd = ADD_OR_UPDATE_BD; expected.bdId = bdId; verify(api).bridgeDomainAddDel(expected); @@ -90,20 +107,12 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { verify(api).bridgeDomainAddDel(expected); } - private void whenBridgeDomainAddDelThenSuccess() - throws ExecutionException, InterruptedException, VppInvocationException { - final CompletionStage replyCS = mock(CompletionStage.class); - final CompletableFuture replyFuture = mock(CompletableFuture.class); - when(replyCS.toCompletableFuture()).thenReturn(replyFuture); - final BridgeDomainAddDelReply reply = new BridgeDomainAddDelReply(); - when(replyFuture.get()).thenReturn(reply); - when(api.bridgeDomainAddDel(any(BridgeDomainAddDel.class))).thenReturn(replyCS); + private void whenBridgeDomainAddDelThenSuccess() { + when(api.bridgeDomainAddDel(any(BridgeDomainAddDel.class))).thenReturn(future(new BridgeDomainAddDelReply())); } - private void whenBridgeDomainAddDelThenFailure() - throws ExecutionException, InterruptedException, VppInvocationException { - doReturn(TestHelperUtils.createFutureException()).when(api) - .bridgeDomainAddDel(any(BridgeDomainAddDel.class)); + private void whenBridgeDomainAddDelThenFailure() { + doReturn(failedFuture()).when(api).bridgeDomainAddDel(any(BridgeDomainAddDel.class)); } @Test @@ -115,7 +124,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { whenBridgeDomainAddDelThenSuccess(); - customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, writeContext); + customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext); verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId); verify(mappingContext).put( @@ -131,7 +140,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { whenBridgeDomainAddDelThenSuccess(); - customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, writeContext); + customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext); verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId); verify(mappingContext).put( @@ -148,7 +157,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { whenBridgeDomainAddDelThenFailure(); try { - customizer.writeCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, writeContext); + customizer.writeCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext); } catch (WriteFailedException.CreateFailedException e) { verifyBridgeDomainAddOrUpdateWasInvoked(bd, bdId); return; @@ -165,7 +174,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { whenBridgeDomainAddDelThenSuccess(); - customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, writeContext); + customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext); verifyBridgeDomainDeleteWasInvoked(bdId); } @@ -177,7 +186,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { ContextTestUtils.mockEmptyMapping(mappingContext, bdName, BD_CTX_NAME); try { - customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, writeContext); + customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext); } catch (IllegalArgumentException e) { verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class)); return; @@ -195,7 +204,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { whenBridgeDomainAddDelThenFailure(); try { - customizer.deleteCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bd, writeContext); + customizer.deleteCurrentAttributes(bdIdentifierForName(bdName), bd, writeContext); } catch (WriteFailedException.DeleteFailedException e) { verifyBridgeDomainDeleteWasInvoked(bdId); return; @@ -225,7 +234,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { whenBridgeDomainAddDelThenSuccess(); customizer - .updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), dataBefore, dataAfter, + .updateCurrentAttributes(bdIdentifierForName(bdName), dataBefore, dataAfter, writeContext); verifyBridgeDomainAddOrUpdateWasInvoked(dataAfter, bdId); } @@ -239,7 +248,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { try { customizer - .updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bdBefore, bdAfter, + .updateCurrentAttributes(bdIdentifierForName(bdName), bdBefore, bdAfter, writeContext); } catch (IllegalArgumentException e) { verify(api, never()).bridgeDomainAddDel(any(BridgeDomainAddDel.class)); @@ -260,7 +269,7 @@ public class BridgeDomainCustomizerTest extends WriterCustomizerTest { try { customizer - .updateCurrentAttributes(BridgeDomainTestUtils.bdIdentifierForName(bdName), bdBefore, bdAfter, + .updateCurrentAttributes(bdIdentifierForName(bdName), bdBefore, bdAfter, writeContext); } catch (WriteFailedException.UpdateFailedException e) { verifyBridgeDomainAddOrUpdateWasInvoked(bdAfter, bdId); diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainTestUtils.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainTestUtils.java deleted file mode 100644 index 1c49a9c62..000000000 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/BridgeDomainTestUtils.java +++ /dev/null @@ -1,51 +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.vpp; - -import javax.annotation.Nullable; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; - -final class BridgeDomainTestUtils { - - private BridgeDomainTestUtils() { - throw new UnsupportedOperationException("Utility class cannot be instantiated."); - } - - public static byte booleanToByte(@Nullable final Boolean value) { - return value != null && value ? (byte) 1 : (byte) 0; - } - - @Nullable - public static Boolean intToBoolean(final int value) { - if (value == 0) { - return Boolean.FALSE; - } - if (value == 1) { - return Boolean.TRUE; - } - return null; - } - - public static KeyedInstanceIdentifier bdIdentifierForName( - final String bdName) { - return InstanceIdentifier.create(BridgeDomains.class).child(BridgeDomain.class, new BridgeDomainKey(bdName)); - } -} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/L2FibEntryCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/L2FibEntryCustomizerTest.java index 1f15788f0..728ba9306 100644 --- a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/L2FibEntryCustomizerTest.java +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vpp/L2FibEntryCustomizerTest.java @@ -25,11 +25,9 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import io.fd.honeycomb.translate.v3po.test.ContextTestUtils; -import io.fd.honeycomb.translate.v3po.test.TestHelperUtils; -import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest; import io.fd.honeycomb.translate.v3po.util.NamingContext; import io.fd.honeycomb.translate.write.WriteFailedException; -import java.util.concurrent.CompletableFuture; +import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress; @@ -46,7 +44,6 @@ import org.openvpp.jvpp.VppBaseCallException; import org.openvpp.jvpp.VppInvocationException; import org.openvpp.jvpp.core.dto.L2FibAddDel; import org.openvpp.jvpp.core.dto.L2FibAddDelReply; -import org.openvpp.jvpp.core.dto.L2InterfaceVlanTagRewriteReply; public class L2FibEntryCustomizerTest extends WriterCustomizerTest { private static final String BD_CTX_NAME = "bd-test-instance"; @@ -76,15 +73,11 @@ public class L2FibEntryCustomizerTest extends WriterCustomizerTest { } private void whenL2FibAddDelThenSuccess() { - final CompletableFuture replyFuture = new CompletableFuture<>(); - final L2FibAddDelReply reply = new L2FibAddDelReply(); - replyFuture.complete(reply); - doReturn(replyFuture).when(api).l2FibAddDel(any(L2FibAddDel.class)); + doReturn(future(new L2FibAddDelReply())).when(api).l2FibAddDel(any(L2FibAddDel.class)); } private void whenL2FibAddDelThenFailure() { - doReturn(TestHelperUtils.createFutureException()).when(api) - .l2FibAddDel(any(L2FibAddDel.class)); + doReturn(failedFuture()).when(api).l2FibAddDel(any(L2FibAddDel.class)); } private L2FibAddDel generateL2FibAddDelRequest(final long mac, final byte isAdd) { -- cgit 1.2.3-korg