sanitize Boolean autoboxing NPE 57/3857/1
authorMichal Rehak <mirehak@cisco.com>
Thu, 19 Dec 2013 19:48:41 +0000 (20:48 +0100)
committerMichal Rehak <mirehak@cisco.com>
Thu, 19 Dec 2013 20:39:08 +0000 (21:39 +0100)
added mockito dependency
added smoke test for NPE in flow operations

Change-Id: I285685de135596cfee29cbe15862f0945bf1ed72
Signed-off-by: Michal Rehak <mirehak@cisco.com>
openflowplugin/pom.xml
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/FlowConvertor.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/ModelDrivenSwitchImplTest.java [new file with mode: 0644]
pom.xml

index e1447e7cb8634593fd85ce8ddc2dff4985e5f9de..ae1ee310a385893550cf1c4b78667fd02f358df8 100644 (file)
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+          <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <scope>test</scope>
+        </dependency>
+        
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
index 404f5abcf7f68fba56b59e621f61ae58cc791ef8..d667d52f0ff8004220280d2a10fd61dff2111163 100644 (file)
@@ -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<RpcResult<BarrierOutput>> 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<RpcResult<BarrierOutput>> 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<RpcResult<BarrierOutput>> barrierOFLib = messageService.barrier(barrierInput.build(), cookie);
        }
         
index b0cbfb0a8d5482433a77a2871cfe212b4f1ca4cf..db543037cb767c2600f39bc071995dfc3bc76fd2 100644 (file)
@@ -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 (file)
index 0000000..fabeb85
--- /dev/null
@@ -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<RpcError> errorSet = Collections.emptySet();
+        RpcResult<UpdateFlowOutput> 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<RpcError> errorSet = Collections.emptySet();
+        RpcResult<UpdateFlowOutput> 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<RpcError> errorSet = Collections.emptySet();
+        RpcResult<UpdateFlowOutput> 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 ed511a839f5c7ecd4658dd4b1bce6473baa80b9e..a0a978baafd8caf03434e9816e123e5a2024e770 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
           <version>4.8.1</version>
           <scope>test</scope>
         </dependency>
+        <dependency>
+          <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.9.5</version>
+            <scope>test</scope>
+        </dependency>
       </dependencies>
     </dependencyManagement>