Bug 1254 - increase UT coverage 18/9918/2
authorMartin Bobak <mbobak@cisco.com>
Wed, 13 Aug 2014 05:56:07 +0000 (07:56 +0200)
committerMartin Bobak <mbobak@cisco.com>
Fri, 15 Aug 2014 05:52:24 +0000 (07:52 +0200)
- fixed NPE in OpenflowPortUtils#getPortLogicalName when OpenFlowVersion was
  UNSUPPORTED
- added unit test for fix

Change-Id: I22e54e0d7e9b9c6a07534d03399634021de39235
Signed-off-by: Martin Bobak <mbobak@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/OpenflowPortsUtil.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/PortTranslatorUtilTest.java

index ca02d0ad7bee3ca0f6ed0b71749819474b9e13c5..6ef7d54b478793cb544156dff9cffaa9f4da8ab6 100644 (file)
@@ -66,6 +66,9 @@ public class OpenflowPortsUtil {
     }
 
     public static String getPortLogicalName(OpenflowVersion ofVersion, Long portNumber) {
+       if (ofVersion.equals(OpenflowVersion.UNSUPPORTED)){
+           return null;
+       }
        return versionPortMap.get(ofVersion).inverse().get(portNumber);
     }
 
index 7530210bd247e88ec332997869221c454ba8d584..7950399030822d80837d2cc4d88d4e09a79ea959 100644 (file)
@@ -1,17 +1,41 @@
 package org.opendaylight.openflowplugin.openflow.md.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+import java.math.BigInteger;
+import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
 
 /**
  * Created by Martin Bobak mbobak@cisco.com on 7/29/14.
  */
 public class PortTranslatorUtilTest {
 
-
+    private static final String MAC_ADDRESS = "00:01:02:03:04:05";
+    private static final String NAME = "PortTranslatorTest";
+    private final Boolean[] pfBls = {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false};
+    private final boolean[] pfV10Bls = {false, false, false, false, false, false, false, false, false, false, false, false};
+    private final boolean[] portCfgBools = {false, false, false, false};
+    private final boolean[] portCfgV10bools = {false, false, false, false, false, false, false};
+    private final boolean[] portStateBools = {false, false, false, false};
+    private final Long currentSpeed = Long.decode("4294967295");
+    private static final Long maxSpeed = Long.decode("4294967295");
+
+    @Before
+    public void setupEnvironment(){
+        OpenflowPortsUtil.init();
+    }
 
     /**
      * Test  method for {@link org.opendaylight.openflowplugin.openflow.md.util.PortTranslatorUtil#translatePortFeatures(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures)}
@@ -19,41 +43,20 @@ public class PortTranslatorUtilTest {
     @Test
     public void testTranslatePortFeatures() {
 
-        Boolean[] bls = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
-
-        for (int i = 0; i < 16; i++) {
-            bls[i] = true;
-            final PortFeatures apf = new PortFeatures(bls[0],bls[1],bls[2],bls[3],bls[4],bls[5],bls[6],bls[7],bls[8],
-                    bls[9],bls[10],bls[11],bls[12],bls[13],bls[14],bls[15]);
 
+        for (int i = 0; i < pfBls.length; i++) {
+            pfBls[i] = true;
+            final PortFeatures apf = getPortFeatures();
             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf = PortTranslatorUtil.translatePortFeatures(apf);
             assertEqualsPortFeatures(apf, npf);
-            bls[i] = false;
+            pfBls[i] = false;
         }
 
     }
 
-    private void assertEqualsPortFeatures(PortFeatures apf, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
-        assertEquals(apf.is_100gbFd(), npf.isHundredGbFd());
-        assertEquals(apf.is_100mbFd(), npf.isHundredMbFd());
-        assertEquals(apf.is_100mbHd(), npf.isHundredMbHd());
-
-        assertEquals(apf.is_10gbFd(), npf.isTenGbFd());
-        assertEquals(apf.is_10mbFd(), npf.isTenMbFd());
-        assertEquals(apf.is_10mbHd(), npf.isTenMbHd());
-
-        assertEquals(apf.is_1gbFd(), npf.isOneGbFd());
-        assertEquals(apf.is_1gbHd(), npf.isOneGbHd());
-        assertEquals(apf.is_1tbFd(), npf.isOneTbFd());
-
-        assertEquals(apf.is_40gbFd(), npf.isFortyGbFd());
-
-        assertEquals(apf.isAutoneg(), npf.isAutoeng());
-        assertEquals(apf.isCopper(), npf.isCopper());
-        assertEquals(apf.isFiber(), npf.isFiber());
-        assertEquals(apf.isOther(), npf.isOther());
-        assertEquals(apf.isPause(), npf.isPause());
-        assertEquals(apf.isPauseAsym(), npf.isPauseAsym());
+    private PortFeatures getPortFeatures() {
+        return new PortFeatures(pfBls[0], pfBls[1], pfBls[2], pfBls[3], pfBls[4], pfBls[5], pfBls[6], pfBls[7], pfBls[8],
+                pfBls[9], pfBls[10], pfBls[11], pfBls[12], pfBls[13], pfBls[14], pfBls[15]);
     }
 
     /**
@@ -62,22 +65,87 @@ public class PortTranslatorUtilTest {
     @Test
     public void testTranslatePortFeaturesV10() {
 
-        boolean[] bls = {false,false,false,false,false,false,false,false,false,false,false,false};
-
-        for (int i = 0; i <12; i++) {
 
-            bls[i] = true;
+        for (int i = 0; i < pfV10Bls.length; i++) {
 
-            final PortFeaturesV10 apfV10 = new PortFeaturesV10(bls[0],bls[1],bls[2],bls[3],bls[4],bls[5],bls[6],
-                    bls[7],bls[8],bls[9],bls[10],bls[11]);
+            pfV10Bls[i] = true;
+            final PortFeaturesV10 apfV10 = getPortFeaturesV10();
             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf = PortTranslatorUtil.translatePortFeatures(apfV10);
             assertEqualsPortFeaturesV10(apfV10, npf);
-            bls[i] = false;
+            pfV10Bls[i] = false;
 
         }
 
     }
 
+    private PortFeaturesV10 getPortFeaturesV10() {
+        return new PortFeaturesV10(pfV10Bls[0], pfV10Bls[1], pfV10Bls[2], pfV10Bls[3], pfV10Bls[4], pfV10Bls[5], pfV10Bls[6],
+                pfV10Bls[7], pfV10Bls[8], pfV10Bls[9], pfV10Bls[10], pfV10Bls[11]);
+    }
+
+    /**
+     * Test  method for {@link org.opendaylight.openflowplugin.openflow.md.util.PortTranslatorUtil#translatePort(Short, java.math.BigInteger, Long, org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping)} ()}
+     */
+    @Test
+    public void testTranslatePort() {
+
+        Short version = OpenflowVersion.OF10.getVersion();
+
+        BigInteger dataPathId = BigInteger.ONE;
+        Long portNumber = Long.MAX_VALUE;
+        PortGrouping portGrouping = mockPortGrouping();
+
+        NodeConnectorUpdated nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
+        assertNotNull(nodeConnectorUpdated);
+        version = OpenflowVersion.OF13.getVersion();
+        nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
+        assertNotNull(nodeConnectorUpdated);
+
+        version = OpenflowVersion.UNSUPPORTED.getVersion();
+        nodeConnectorUpdated = PortTranslatorUtil.translatePort(version, dataPathId, portNumber, portGrouping);
+        assertNotNull(nodeConnectorUpdated);
+
+
+    }
+
+    private PortGrouping mockPortGrouping() {
+        PortGrouping portGrouping = mock(PortGrouping.class);
+        when(portGrouping.getAdvertisedFeatures()).thenReturn(getPortFeatures());
+        when(portGrouping.getAdvertisedFeaturesV10()).thenReturn(getPortFeaturesV10());
+        when(portGrouping.getConfig()).thenReturn(getPortConfig());
+        when(portGrouping.getConfigV10()).thenReturn(getPortConfigV10());
+        when(portGrouping.getCurrentFeatures()).thenReturn(getPortFeatures());
+        when(portGrouping.getCurrentFeaturesV10()).thenReturn(getPortFeaturesV10());
+        when(portGrouping.getCurrSpeed()).thenReturn(currentSpeed);
+        when(portGrouping.getHwAddr()).thenReturn(getMacAddress());
+        when(portGrouping.getName()).thenReturn(NAME);
+        when(portGrouping.getMaxSpeed()).thenReturn(maxSpeed);
+        when(portGrouping.getPeerFeatures()).thenReturn(getPortFeatures());
+        when(portGrouping.getPeerFeaturesV10()).thenReturn(getPortFeaturesV10());
+        when(portGrouping.getPortNo()).thenReturn(Long.MAX_VALUE);
+        when(portGrouping.getState()).thenReturn(getPortState());
+        when(portGrouping.getSupportedFeatures()).thenReturn(getPortFeatures());
+        when(portGrouping.getSupportedFeaturesV10()).thenReturn(getPortFeaturesV10());
+        return portGrouping;
+    }
+
+    private PortState getPortState() {
+        PortState portState = new PortState(portStateBools[0], portStateBools[1], portStateBools[2]);
+        return portState;
+    }
+
+    private MacAddress getMacAddress() {
+        return new MacAddress(MAC_ADDRESS);
+    }
+
+    private PortConfigV10 getPortConfigV10() {
+        return new PortConfigV10(portCfgV10bools[0], portCfgV10bools[1], portCfgV10bools[2], portCfgV10bools[3], portCfgV10bools[4], portCfgV10bools[5], portCfgV10bools[6]);
+    }
+
+    private PortConfig getPortConfig() {
+        return new PortConfig(portCfgBools[0], portCfgBools[1], portCfgBools[2], portCfgBools[3]);
+    }
+
     private void assertEqualsPortFeaturesV10(PortFeaturesV10 apfV10, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
         assertEquals(apfV10.is_100mbFd(), npf.isHundredMbFd());
         assertEquals(apfV10.is_100mbHd(), npf.isHundredMbHd());
@@ -96,4 +164,27 @@ public class PortTranslatorUtilTest {
         assertEquals(apfV10.isPauseAsym(), npf.isPauseAsym());
     }
 
+    private void assertEqualsPortFeatures(PortFeatures apf, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures npf) {
+        assertEquals(apf.is_100gbFd(), npf.isHundredGbFd());
+        assertEquals(apf.is_100mbFd(), npf.isHundredMbFd());
+        assertEquals(apf.is_100mbHd(), npf.isHundredMbHd());
+
+        assertEquals(apf.is_10gbFd(), npf.isTenGbFd());
+        assertEquals(apf.is_10mbFd(), npf.isTenMbFd());
+        assertEquals(apf.is_10mbHd(), npf.isTenMbHd());
+
+        assertEquals(apf.is_1gbFd(), npf.isOneGbFd());
+        assertEquals(apf.is_1gbHd(), npf.isOneGbHd());
+        assertEquals(apf.is_1tbFd(), npf.isOneTbFd());
+
+        assertEquals(apf.is_40gbFd(), npf.isFortyGbFd());
+
+        assertEquals(apf.isAutoneg(), npf.isAutoeng());
+        assertEquals(apf.isCopper(), npf.isCopper());
+        assertEquals(apf.isFiber(), npf.isFiber());
+        assertEquals(apf.isOther(), npf.isOther());
+        assertEquals(apf.isPause(), npf.isPause());
+        assertEquals(apf.isPauseAsym(), npf.isPauseAsym());
+    }
+
 }