Adding additional JUnit tests for SwitchManager 92/92/1
authorKalvin Hom <kahom@cisco.com>
Sat, 30 Mar 2013 00:46:50 +0000 (17:46 -0700)
committerKalvin Hom <kahom@cisco.com>
Sat, 30 Mar 2013 00:50:22 +0000 (17:50 -0700)
module

Change-Id: I9235a653d1740faf3a143b7fea48d80c91584124
Signed-off-by: Kalvin Hom <kahom@cisco.com>
opendaylight/switchmanager/src/test/java/org/opendaylight/controller/switchmanager/SubnetTest.java
opendaylight/switchmanager/src/test/java/org/opendaylight/controller/switchmanager/SwitchTest.java

index 0b9fb9c..0cb51f0 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -9,6 +8,7 @@
 
 package org.opendaylight.controller.switchmanager;
 
+import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.util.HashSet;
 import java.util.Set;
@@ -20,43 +20,74 @@ import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
 
-
 public class SubnetTest {
-       
-       @Test
-       public void testSubnet() throws Exception  {
-               InetAddress ipaddr = InetAddress.getByName("100.0.0.1");
-               Subnet subnet = new Subnet(ipaddr, (short)24, (short)5);
-               InetAddress subnetAddr = InetAddress.getByName("100.0.0.100");
-               
-               Assert.assertTrue(subnet.isFlatLayer2() == true);
-               
-               Set<NodeConnector> ncSet = new HashSet<NodeConnector>();
-               Node node = NodeCreator.createOFNode(((long)10));
-               NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector((short)20, node);
-               NodeConnector nc1 = NodeConnectorCreator.createOFNodeConnector((short)30, node);
-               NodeConnector nc2 = NodeConnectorCreator.createOFNodeConnector((short)40, node);
-               
-               ncSet.add(nc0);
-               ncSet.add(nc1);
-               ncSet.add(nc2);
-               
-               subnet.addNodeConnectors(ncSet);
-               
-               Set<NodeConnector> resultncSet = subnet.getNodeConnectors();
-               Assert.assertEquals(resultncSet, ncSet);
-               
-               subnet.setNetworkAddress(subnetAddr);
-               Assert.assertTrue(subnet.getNetworkAddress().equals(subnetAddr));
-               
-               subnet.setSubnetMaskLength((short) 16);
-               Assert.assertTrue(subnet.getSubnetMaskLength() == 16);
-               
-               subnet.setVlan((short)100);
-               Assert.assertTrue(subnet.getVlan() == 100);
-               
-               Assert.assertTrue(subnet.isFlatLayer2() == false);
-
-       }
+
+    @Test
+    public void testSubnet() throws Exception {
+        InetAddress ipaddr = InetAddress.getByName("100.0.0.1");
+        Subnet subnet = new Subnet(ipaddr, (short) 24, (short) 5);
+        Assert.assertTrue(subnet.equals(subnet));
+        Assert.assertFalse(subnet.equals(null));
+        Assert.assertFalse(subnet.equals(ipaddr));
+        Subnet subnet2 = new Subnet(ipaddr, (short) 24, (short) 5);
+        Inet6Address ipv6 = (Inet6Address) Inet6Address
+                .getByName("1111::2222:3333:4444:5555:6666");
+        Subnet subnet3 = new Subnet(ipv6, (short) 24, (short) 5);
+        Assert.assertTrue(subnet.equals(subnet2));
+        Assert.assertFalse(subnet.isMutualExclusive(subnet2));
+        Assert.assertTrue(subnet.isMutualExclusive(subnet3));
+        InetAddress subnetAddr = InetAddress.getByName("200.0.0.100");
+
+        Assert.assertTrue(subnet.isFlatLayer2() == true);
+
+        Set<NodeConnector> ncSet = new HashSet<NodeConnector>();
+        Node node = NodeCreator.createOFNode(((long) 10));
+        NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 20, node);
+        NodeConnector nc1 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 30, node);
+        NodeConnector nc2 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 40, node);
+
+        ncSet.add(nc0);
+        ncSet.add(nc1);
+        ncSet.add(nc2);
+
+        Assert.assertTrue(subnet.hasNodeConnector(nc0));
+        Assert.assertFalse(subnet.hasNodeConnector(null));
+        subnet.addNodeConnectors(ncSet);
+        Assert.assertTrue(subnet.hasNodeConnector(nc0));
+
+        Set<NodeConnector> resultncSet = subnet.getNodeConnectors();
+        Assert.assertEquals(resultncSet, ncSet);
+        subnet.addNodeConnectors(null);
+        Assert.assertEquals(subnet.getNodeConnectors(), ncSet);
+
+        subnet.deleteNodeConnectors(null);
+        Assert.assertEquals(subnet.getNodeConnectors(), ncSet);
+
+        Set<NodeConnector> ncSet2 = new HashSet<NodeConnector>();
+        ncSet2.add(nc0);
+        subnet.deleteNodeConnectors(ncSet2);
+        Assert.assertFalse(subnet.getNodeConnectors().contains(nc0));
+        Assert.assertFalse(subnet.hasNodeConnector(nc0));
+        Assert.assertTrue(subnet.getNodeConnectors().contains(nc1));
+        Assert.assertTrue(subnet.getNodeConnectors().contains(nc2));
+
+        subnet.deleteNodeConnectors(ncSet2);
+
+        subnet.setNetworkAddress(subnetAddr);
+        Assert.assertTrue(subnet.isMutualExclusive(subnet2));
+        Assert.assertTrue(subnet.getNetworkAddress().equals(subnetAddr));
+
+        subnet.setSubnetMaskLength((short) 16);
+        Assert.assertTrue(subnet.getSubnetMaskLength() == 16);
+
+        subnet.setVlan((short) 100);
+        Assert.assertTrue(subnet.getVlan() == 100);
+
+        Assert.assertTrue(subnet.isFlatLayer2() == false);
+
+    }
 
 }
index c3ff4f1..6b0a258 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -9,6 +8,8 @@
 
 package org.opendaylight.controller.switchmanager;
 
+import static org.junit.Assert.fail;
+
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;
@@ -22,74 +23,123 @@ import org.opendaylight.controller.sal.utils.NodeCreator;
 
 public class SwitchTest {
 
-       @Test
-       public void testSwitchCreation() {
-               
-               Node node = NodeCreator.createOFNode(((long)10));
-               NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector((short)20, node);
-               NodeConnector nc1 = NodeConnectorCreator.createOFNodeConnector((short)30, node);
-               NodeConnector nc2 = NodeConnectorCreator.createOFNodeConnector((short)40, node);
-               NodeConnector nc3 = NodeConnectorCreator.createOFNodeConnector((short)50, node);
-               NodeConnector nc4 = NodeConnectorCreator.createOFNodeConnector((short)60, node);
-               NodeConnector nc5 = NodeConnectorCreator.createOFNodeConnector((short)70, node);
-
-               Set<NodeConnector> ncSet = new HashSet<NodeConnector>();
-               ArrayList<NodeConnector> portList  = new ArrayList<NodeConnector>();
-               
-               Switch sw = new Switch(node);
-               ncSet.add(nc0);
-               ncSet.add(nc1);
-               ncSet.add(nc2);
-               sw.addNodeConnector(nc3);
-               
-               portList.add(nc4);
-               portList.add(nc5);
-
-               sw.setNodeConnectors(ncSet);
-               sw.addSpanPorts(portList);
-               
-               byte[] dlAddress = {(byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06};
-               sw.setDataLayerAddress(dlAddress);
-               
-               Node resultNode = sw.getNode();
-               Set<NodeConnector> resultncSet = sw.getNodeConnectors();
-               byte[] resultdlAddress = sw.getDataLayerAddress();              
-               ArrayList<NodeConnector> resultSpanPort = (ArrayList<NodeConnector>) sw.getSpanPorts();
-               
-               Assert.assertEquals(node, resultNode);
-               for (int i = 0; i < dlAddress.length; i++)
-                       Assert.assertEquals(dlAddress[i], resultdlAddress[i]);  
-               
-               Assert.assertTrue(ncSet.equals(resultncSet));
-               
-               for (int i = 0; i < portList.size(); i++)
-                       Assert.assertEquals(portList.get(i), resultSpanPort.get(i));
-       }
-       
-       @Test
-       public void testSwitchAddRemovePort() {
-               Node node = NodeCreator.createOFNode(((long)10));
-
-               NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector((short)20, node);
-               NodeConnector nc1 = NodeConnectorCreator.createOFNodeConnector((short)30, node);
-               NodeConnector nc4 = NodeConnectorCreator.createOFNodeConnector((short)60, node);
-               NodeConnector nc5 = NodeConnectorCreator.createOFNodeConnector((short)70, node);
-               ArrayList<NodeConnector> portList  = new ArrayList<NodeConnector>();
-               
-               portList.add(nc4);
-               portList.add(nc5);
-
-               Set<NodeConnector> ncSet = new HashSet<NodeConnector>();
-               ncSet.add(nc0);
-               ncSet.add(nc1);
-               
-               Switch sw = new Switch(node);
-               sw.setNodeConnectors(ncSet);
-               sw.removeNodeConnector(nc0);
-               Assert.assertFalse(ncSet.contains(nc0));
-               
-               sw.removeSpanPorts(portList);
-               Assert.assertTrue(sw.getSpanPorts().isEmpty());
-
-       }
+    @Test
+    public void testSwitchCreation() {
+
+        Node node = NodeCreator.createOFNode(((long) 10));
+        Node node2 = NodeCreator.createOFNode(((long) 11));
+        NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 20, node);
+        NodeConnector nc1 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 30, node);
+        NodeConnector nc2 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 40, node);
+        NodeConnector nc3 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 50, node);
+        NodeConnector nc4 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 60, node);
+        NodeConnector nc5 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 70, node);
+
+        Set<NodeConnector> ncSet = new HashSet<NodeConnector>();
+        ArrayList<NodeConnector> portList = new ArrayList<NodeConnector>();
+
+        Switch sw = new Switch(node);
+        Switch sw2 = new Switch(node);
+        Assert.assertTrue(sw.equals(sw2));
+        sw2.setNode(node2);
+        Assert.assertTrue(sw2.getNode().equals(node2));
+        Assert.assertFalse(sw.equals(sw2));
+
+        ncSet.add(nc0);
+        ncSet.add(nc1);
+        ncSet.add(nc2);
+        sw.addNodeConnector(nc3);
+        try {
+            sw.addNodeConnector(nc3);
+        } catch (Exception e) {
+            fail("Attempted to add duplicate NodeConnector to set");
+        }
+
+        portList.add(nc4);
+        portList.add(nc5);
+
+        sw.setNodeConnectors(ncSet);
+        sw.addSpanPorts(portList);
+
+        sw.setDataLayerAddress(null);
+        Assert.assertNull(sw.getDataLayerAddress());
+        byte[] dlAddress = { (byte) 0x01, (byte) 0x02, (byte) 0x03,
+                (byte) 0x04, (byte) 0x05, (byte) 0x06 };
+        sw.setDataLayerAddress(dlAddress);
+
+        Node resultNode = sw.getNode();
+        Set<NodeConnector> resultncSet = sw.getNodeConnectors();
+        byte[] resultdlAddress = sw.getDataLayerAddress();
+        ArrayList<NodeConnector> resultSpanPort = (ArrayList<NodeConnector>) sw
+                .getSpanPorts();
+
+        Assert.assertEquals(node, resultNode);
+        for (int i = 0; i < dlAddress.length; i++)
+            Assert.assertEquals(dlAddress[i], resultdlAddress[i]);
+
+        Assert.assertTrue(ncSet.equals(resultncSet));
+
+        for (int i = 0; i < portList.size(); i++)
+            Assert.assertEquals(portList.get(i), resultSpanPort.get(i));
+    }
+
+    @Test
+    public void testSwitchAddRemovePort() {
+        Node node = NodeCreator.createOFNode(((long) 10));
+
+        NodeConnector nc0 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 20, node);
+        NodeConnector nc1 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 30, node);
+        NodeConnector nc4 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 60, node);
+        NodeConnector nc5 = NodeConnectorCreator.createOFNodeConnector(
+                (short) 70, node);
+        ArrayList<NodeConnector> portList = new ArrayList<NodeConnector>();
+
+        portList.add(nc4);
+        portList.add(nc5);
+
+        Set<NodeConnector> ncSet = new HashSet<NodeConnector>();
+        ncSet.add(nc0);
+        ncSet.add(nc1);
+
+        Switch sw = new Switch(node);
+        sw.setNodeConnectors(ncSet);
+        sw.removeNodeConnector(nc0);
+        Assert.assertFalse(ncSet.contains(nc0));
+
+        sw.removeSpanPorts(portList);
+        Assert.assertTrue(sw.getSpanPorts().isEmpty());
+
+    }
+    
+    @Test
+    public void testSwitchConfig(){
+        SwitchConfig sc = new SwitchConfig(null, null, null, null);
+        SwitchConfig sc2 = new SwitchConfig(null, null, null, null);
+        Assert.assertTrue(sc.equals(sc2));
+        
+        Assert.assertNull(sc.getMode());
+        Assert.assertNull(sc.getNodeId());
+        Assert.assertNull(sc.getTier());
+        Assert.assertNull(sc.getNodeName());
+        
+        SwitchConfig sc3 = new SwitchConfig("123", "name", "tier", "mode");
+        SwitchConfig sc4 = new SwitchConfig("123", "name", "tier", "mode");
+        Assert.assertFalse(sc.equals(sc3));
+        Assert.assertTrue(sc3.equals(sc4));
+        
+        Assert.assertTrue(sc3.getNodeId().equals("123"));
+        Assert.assertTrue(sc3.getNodeName().equals("name"));
+        Assert.assertTrue(sc3.getTier().equals("tier"));
+        Assert.assertTrue(sc3.getMode().equals("mode"));
+    }
+
 }