From f22a71b353463817693fa69c50456d02f20ece78 Mon Sep 17 00:00:00 2001 From: Michal Rehak Date: Thu, 19 Dec 2013 20:48:41 +0100 Subject: [PATCH] sanitize Boolean autoboxing NPE added mockito dependency added smoke test for NPE in flow operations Change-Id: I285685de135596cfee29cbe15862f0945bf1ed72 Signed-off-by: Michal Rehak --- openflowplugin/pom.xml | 6 + .../md/core/sal/ModelDrivenSwitchImpl.java | 7 +- .../md/core/sal/convertor/FlowConvertor.java | 6 +- .../core/sal/ModelDrivenSwitchImplTest.java | 134 ++++++++++++++++++ pom.xml | 6 + 5 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImplTest.java diff --git a/openflowplugin/pom.xml b/openflowplugin/pom.xml index e1447e7cb8..ae1ee310a3 100644 --- a/openflowplugin/pom.xml +++ b/openflowplugin/pom.xml @@ -98,6 +98,12 @@ junit test + + org.mockito + mockito-all + test + + org.slf4j slf4j-log4j12 diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImpl.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImpl.java index 404f5abcf7..d667d52f0f 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImpl.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImpl.java @@ -200,6 +200,7 @@ import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; +import com.google.common.base.Objects; import com.google.common.util.concurrent.Futures; /** @@ -233,7 +234,7 @@ public class ModelDrivenSwitchImpl extends AbstractModelDrivenSwitch { // the request can be routed through any connection to the switch SwitchConnectionDistinguisher cookie = null ; - if (input.isBarrier()) { + if (Objects.firstNonNull(input.isBarrier(), Boolean.FALSE)) { Future> barrierOFLib = messageService.barrier(barrierInput.build(), cookie); } @@ -341,7 +342,7 @@ public class ModelDrivenSwitchImpl extends AbstractModelDrivenSwitch { // the request can be routed through any connection to the switch SwitchConnectionDistinguisher cookie = null ; - if (input.isBarrier()) { + if (Objects.firstNonNull(input.isBarrier(), Boolean.FALSE)) { Future> barrierOFLib = messageService.barrier(barrierInput.build(), cookie); } @@ -553,7 +554,7 @@ public class ModelDrivenSwitchImpl extends AbstractModelDrivenSwitch { // the request can be routed through any connection to the switch SwitchConnectionDistinguisher cookie = null ; - if (input.getUpdatedFlow().isBarrier()) { + if (Objects.firstNonNull(input.getUpdatedFlow().isBarrier(), Boolean.FALSE)) { Future> barrierOFLib = messageService.barrier(barrierInput.build(), cookie); } diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/FlowConvertor.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/FlowConvertor.java index b0cbfb0a8d..db543037cb 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/FlowConvertor.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/FlowConvertor.java @@ -54,6 +54,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Objects; + /** * Utility class for converting a MD-SAL Flow into the OF flow mod */ @@ -112,13 +114,13 @@ public class FlowConvertor { if (flow instanceof AddFlowInput) { flowMod.setCommand(FlowModCommand.OFPFCADD); } else if (flow instanceof RemoveFlowInput) { - if (flow.isStrict() != null && flow.isStrict()) { + if (Objects.firstNonNull(flow.isStrict(), Boolean.FALSE)) { flowMod.setCommand(FlowModCommand.OFPFCDELETESTRICT); } else { flowMod.setCommand(FlowModCommand.OFPFCDELETE); } } else if (flow instanceof UpdatedFlow) { - if (flow.isStrict() != null && flow.isStrict()) { + if (Objects.firstNonNull(flow.isStrict(), Boolean.FALSE)) { flowMod.setCommand(FlowModCommand.OFPFCMODIFYSTRICT); } else { flowMod.setCommand(FlowModCommand.OFPFCMODIFY); diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImplTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImplTest.java new file mode 100644 index 0000000000..fabeb85e54 --- /dev/null +++ b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImplTest.java @@ -0,0 +1,134 @@ +/** + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.openflowplugin.openflow.md.core.sal; + +import java.math.BigInteger; +import java.util.Collections; +import java.util.Set; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.opendaylight.controller.sal.common.util.Futures; +import org.opendaylight.controller.sal.common.util.Rpcs; +import org.opendaylight.openflowplugin.openflow.md.OFConstants; +import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor; +import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher; +import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService; +import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.opendaylight.yangtools.yang.common.RpcError; +import org.opendaylight.yangtools.yang.common.RpcResult; + +/** + * simple NPE smoke test + */ +@RunWith(MockitoJUnitRunner.class) +public class ModelDrivenSwitchImplTest { + + private ModelDrivenSwitchImpl mdSwitchOF10; + private ModelDrivenSwitchImpl mdSwitchOF13; + + @Mock + private SessionContext context; + @Mock + private ConnectionConductor conductor; + @Mock + private IMessageDispatchService messageDispatchService; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + Mockito.when(context.getPrimaryConductor()).thenReturn(conductor); + Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService); + Mockito.when(conductor.getVersion()) + .thenReturn(OFConstants.OFP_VERSION_1_0) + .thenReturn(OFConstants.OFP_VERSION_1_3); + mdSwitchOF10 = new ModelDrivenSwitchImpl(null, null, context); + mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context); + } + + + /** + * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#addFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput)}. + */ + @Test + public void testAddFlow() { + UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder(); + updateFlowOutput.setTransactionId(new TransactionId(new BigInteger("42"))); + Set errorSet = Collections.emptySet(); + RpcResult result = Rpcs.getRpcResult(true, + updateFlowOutput.build(), errorSet); + Mockito.when(messageDispatchService.flowMod(Matchers.any(FlowModInput.class), + Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result)); + + AddFlowInputBuilder input = new AddFlowInputBuilder(); + input.setMatch(new MatchBuilder().build()); + + mdSwitchOF10.addFlow(input.build()); + mdSwitchOF13.addFlow(input.build()); + } + + /** + * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#removeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput)}. + */ + @Test + public void testRemoveFlow() { + UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder(); + updateFlowOutput.setTransactionId(new TransactionId(new BigInteger("42"))); + Set errorSet = Collections.emptySet(); + RpcResult result = Rpcs.getRpcResult(true, + updateFlowOutput.build(), errorSet); + Mockito.when(messageDispatchService.flowMod(Matchers.any(FlowModInput.class), + Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result)); + + RemoveFlowInputBuilder input = new RemoveFlowInputBuilder(); + input.setMatch(new MatchBuilder().build()); + + mdSwitchOF10.removeFlow(input.build()); + mdSwitchOF13.removeFlow(input.build()); + } + + /** + * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#updateFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput)}. + */ + @Test + public void testUpdateFlow() { + UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder(); + updateFlowOutput.setTransactionId(new TransactionId(new BigInteger("42"))); + Set errorSet = Collections.emptySet(); + RpcResult result = Rpcs.getRpcResult(true, + updateFlowOutput.build(), errorSet); + Mockito.when(messageDispatchService.flowMod(Matchers.any(FlowModInput.class), + Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result)); + + UpdateFlowInputBuilder input = new UpdateFlowInputBuilder(); + UpdatedFlowBuilder updatedFlow = new UpdatedFlowBuilder(); + updatedFlow.setMatch(new MatchBuilder().build()); + input.setUpdatedFlow(updatedFlow.build()); + + mdSwitchOF10.updateFlow(input.build()); + mdSwitchOF13.updateFlow(input.build()); + } + +} diff --git a/pom.xml b/pom.xml index ed511a839f..a0a978baaf 100755 --- a/pom.xml +++ b/pom.xml @@ -149,6 +149,12 @@ 4.8.1 test + + org.mockito + mockito-all + 1.9.5 + test + -- 2.36.6