From ec57781135cf5c0a7ad26904947f80e29cf65590 Mon Sep 17 00:00:00 2001 From: Michal Rehak Date: Thu, 30 Jun 2016 16:25:11 +0200 Subject: [PATCH] ios-xe-renderer: junit coverage for PolicyWriterUtil - continued Change-Id: I4a87fd4b307d8fb2e4b90a9b40e467ff389f60ec Signed-off-by: Michal Rehak --- .../impl/writer/PolicyWriterUtilTest.java | 100 +++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/PolicyWriterUtilTest.java b/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/PolicyWriterUtilTest.java index a0452a8ba..2356d814f 100644 --- a/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/PolicyWriterUtilTest.java +++ b/renderers/ios-xe/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ios_xe_provider/impl/writer/PolicyWriterUtilTest.java @@ -11,6 +11,7 @@ package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer; import com.google.common.base.Optional; import com.google.common.util.concurrent.Futures; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.junit.Assert; import org.junit.Before; @@ -23,11 +24,19 @@ import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.ServiceChainingUtil; import org.opendaylight.yang.gen.v1.urn.ios.rev160308.ClassNameType; import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMap; import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ClassMapBuilder; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChain; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.ServiceChainBuilder; import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.Class; import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.policy.map.ClassBuilder; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.ServicePathBuilder; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.Local; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.LocalBuilder; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfName; +import org.opendaylight.yang.gen.v1.urn.ios.rev160308._native.service.chain.service.function.forwarder.ServiceFfNameBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.powermock.api.mockito.PowerMockito; @@ -40,7 +49,7 @@ import org.slf4j.LoggerFactory; * Test for {@link PolicyWriterUtil}. */ @RunWith(PowerMockRunner.class) -@PrepareForTest({PolicyWriterUtil.class, NetconfTransactionCreator.class}) +@PrepareForTest({PolicyWriterUtil.class, NetconfTransactionCreator.class, ServiceChainingUtil.class}) public class PolicyWriterUtilTest { private static final Logger LOG = LoggerFactory.getLogger(PolicyWriterUtilTest.class); @@ -183,31 +192,120 @@ public class PolicyWriterUtilTest { @Test public void testWriteInterface() throws Exception { + LOG.debug("scenario: fail with no writeOnlyTransaction"); + final String interfaceName = "unit-interface-1"; + Assert.assertFalse(PolicyWriterUtil.writeInterface(POLICY_MAP_NAME, interfaceName, NODE_ID, dataBroker)); + LOG.debug("scenario: succeed - available writeOnlyTransaction, no submit future"); + PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, "netconfWriteOnlyTransaction")).toReturn(wTxOptional); + Assert.assertTrue(PolicyWriterUtil.writeInterface(POLICY_MAP_NAME, interfaceName, NODE_ID, dataBroker)); + + LOG.debug("scenario: succeed - available writeOnlyTransaction, available future"); + Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + Assert.assertTrue(PolicyWriterUtil.writeInterface(POLICY_MAP_NAME, interfaceName, NODE_ID, dataBroker)); } @Test public void testWriteLocal() throws Exception { + LOG.debug("scenario: succeed with null localForwarder"); + Assert.assertTrue(PolicyWriterUtil.writeLocal(null, NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with no writeOnlyTransaction"); + final Local localForwarder = new LocalBuilder().build(); + Assert.assertFalse(PolicyWriterUtil.writeLocal(localForwarder, NODE_ID, dataBroker)); + + LOG.debug("scenario: succeed - available writeOnlyTransaction, no submit future"); + PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, "netconfWriteOnlyTransaction")).toReturn(wTxOptional); + Assert.assertTrue(PolicyWriterUtil.writeLocal(localForwarder, NODE_ID, dataBroker)); + LOG.debug("scenario: succeed - available writeOnlyTransaction, available future"); + Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + Assert.assertTrue(PolicyWriterUtil.writeLocal(localForwarder, NODE_ID, dataBroker)); } @Test public void testRemoveLocal() throws Exception { + LOG.debug("scenario: succeed with no service path present"); + Assert.assertTrue(PolicyWriterUtil.removeLocal(NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with service path present, no writeOnlyTransaction"); + PowerMockito.stub(PowerMockito.method(ServiceChainingUtil.class, "checkServicePathPresence")).toReturn(true); + Assert.assertFalse(PolicyWriterUtil.removeLocal(NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with service path present, available writeOnlyTransaction, no submit future"); + PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, "netconfWriteOnlyTransaction")).toReturn(wTxOptional); + Assert.assertTrue(PolicyWriterUtil.removeLocal(NODE_ID, dataBroker)); + LOG.debug("scenario: fail with service path present, available writeOnlyTransaction, available future"); + Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + Assert.assertTrue(PolicyWriterUtil.removeLocal(NODE_ID, dataBroker)); } @Test public void testWriteRemote() throws Exception { + LOG.debug("scenario: succeed with null List"); + Assert.assertTrue(PolicyWriterUtil.writeRemote(null, NODE_ID, dataBroker)); + + LOG.debug("scenario: succeed with empty List"); + Assert.assertTrue(PolicyWriterUtil.writeRemote(Collections.emptyList(), NODE_ID, dataBroker)); + LOG.debug("scenario: fail with no writeOnlyTransaction"); + final List remotes = Collections.singletonList(new ServiceFfNameBuilder().build()); + Assert.assertFalse(PolicyWriterUtil.writeRemote(remotes, NODE_ID, dataBroker)); + + LOG.debug("scenario: succeed - available writeOnlyTransaction, no submit future"); + PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, "netconfWriteOnlyTransaction")).toReturn(wTxOptional); + Assert.assertTrue(PolicyWriterUtil.writeRemote(remotes, NODE_ID, dataBroker)); + + LOG.debug("scenario: succeed - available writeOnlyTransaction, available future"); + Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + Assert.assertTrue(PolicyWriterUtil.writeRemote(remotes, NODE_ID, dataBroker)); } @Test public void testWriteServicePaths() throws Exception { + LOG.debug("scenario: succeed with empty List"); + Assert.assertTrue(PolicyWriterUtil.writeServicePaths(Collections.emptyList(), NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with no writeOnlyTransaction"); + final List serviceChains = Collections.singletonList(new ServiceChainBuilder() + .setServicePath(Collections.singletonList(new ServicePathBuilder() + .setServicePathId(42L) + .build())) + .build()); + Assert.assertFalse(PolicyWriterUtil.writeServicePaths(serviceChains, NODE_ID, dataBroker)); + + LOG.debug("scenario: succeed - available writeOnlyTransaction, no submit future"); + PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, "netconfWriteOnlyTransaction")).toReturn(wTxOptional); + Assert.assertTrue(PolicyWriterUtil.writeServicePaths(serviceChains, NODE_ID, dataBroker)); + LOG.debug("scenario: succeed - available writeOnlyTransaction, available future"); + Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + Assert.assertTrue(PolicyWriterUtil.writeServicePaths(serviceChains, NODE_ID, dataBroker)); } @Test public void testRemoveServicePaths() throws Exception { + LOG.debug("scenario: succeed with service path present null"); + Assert.assertTrue(PolicyWriterUtil.removeServicePaths(null, NODE_ID, dataBroker)); + LOG.debug("scenario: succeed with no service path present"); + Assert.assertTrue(PolicyWriterUtil.removeServicePaths(Collections.emptyList(), NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with service path present, no writeOnlyTransaction"); + final List serviceChains = Collections.singletonList(new ServiceChainBuilder() + .setServicePath(Collections.singletonList(new ServicePathBuilder() + .setServicePathId(42L) + .build())) + .build()); + Assert.assertFalse(PolicyWriterUtil.removeServicePaths(serviceChains, NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with service path present, available writeOnlyTransaction, no submit future"); + PowerMockito.stub(PowerMockito.method(NetconfTransactionCreator.class, "netconfWriteOnlyTransaction")).toReturn(wTxOptional); + Assert.assertTrue(PolicyWriterUtil.removeServicePaths(serviceChains, NODE_ID, dataBroker)); + + LOG.debug("scenario: fail with service path present, available writeOnlyTransaction, available future"); + Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + Assert.assertTrue(PolicyWriterUtil.removeServicePaths(serviceChains, NODE_ID, dataBroker)); } } \ No newline at end of file -- 2.36.6