added unit tests
Change-Id: Ic743a05b907456e1414a9bc587696de631d3f382
commented the controller test class
Change-Id: Id9afb0e60afb3839f65a41b04e7129db1010ca19
added OFChannelHandler tests
Change-Id: I45169988f0e4242a6e1c0baf34b1104f53873bb7
diff --git a/of/ctl/src/test/java/net/onrc/onos/of/ctl/internal/ControllerTest.java b/of/ctl/src/test/java/net/onrc/onos/of/ctl/internal/ControllerTest.java
new file mode 100644
index 0000000..ea7d884
--- /dev/null
+++ b/of/ctl/src/test/java/net/onrc/onos/of/ctl/internal/ControllerTest.java
@@ -0,0 +1,167 @@
+/**
+ * Copyright 2011, Big Switch Networks, Inc.
+ * Originally created by David Erickson, Stanford University
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ **/
+
+package net.onrc.onos.of.ctl.internal;
+
+import junit.framework.TestCase;
+import net.onrc.onos.of.ctl.IOFSwitch;
+
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class ControllerTest extends TestCase {
+
+ private Controller controller;
+ private IOFSwitch sw;
+ private OFChannelHandler h;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+ sw = EasyMock.createMock(IOFSwitch.class);
+ h = EasyMock.createMock(OFChannelHandler.class);
+ controller = new Controller();
+ ControllerRunThread t = new ControllerRunThread();
+ t.start();
+ /*
+ * Making sure the thread is properly started before making calls
+ * to controller class.
+ */
+ Thread.sleep(200);
+ }
+
+ /**
+ * Starts the base mocks used in these tests.
+ */
+ private void startMocks() {
+ EasyMock.replay(sw, h);
+ }
+
+ /**
+ * Reset the mocks to a known state.
+ * Automatically called after tests.
+ */
+ @After
+ private void resetMocks() {
+ EasyMock.reset(sw);
+ }
+
+ /**
+ * Fetches the controller instance.
+ * @return the controller
+ */
+ public Controller getController() {
+ return controller;
+ }
+
+ /**
+ * Run the controller's main loop so that updates are processed.
+ */
+ protected class ControllerRunThread extends Thread {
+ @Override
+ public void run() {
+ controller.openFlowPort = 0; // Don't listen
+ controller.activate();
+ }
+ }
+
+ /**
+ * Verify that we are able to add a switch that just connected.
+ * If it already exists then this should fail
+ *
+ * @throws Exception error
+ */
+ @Test
+ public void testAddConnectedSwitches() throws Exception {
+ startMocks();
+ assertTrue(controller.addConnectedSwitch(0, h));
+ assertFalse(controller.addConnectedSwitch(0, h));
+ }
+
+ /**
+ * Add active master but cannot re-add active master.
+ * @throws Exception an error occurred.
+ */
+ @Test
+ public void testAddActivatedMasterSwitch() throws Exception {
+ startMocks();
+ controller.addConnectedSwitch(0, h);
+ assertTrue(controller.addActivatedMasterSwitch(0, sw));
+ assertFalse(controller.addActivatedMasterSwitch(0, sw));
+ }
+
+ /**
+ * Tests that an activated switch can be added but cannot be re-added.
+ *
+ * @throws Exception an error occurred
+ */
+ @Test
+ public void testAddActivatedEqualSwitch() throws Exception {
+ startMocks();
+ controller.addConnectedSwitch(0, h);
+ assertTrue(controller.addActivatedEqualSwitch(0, sw));
+ assertFalse(controller.addActivatedEqualSwitch(0, sw));
+ }
+
+ /**
+ * Move an equal switch to master.
+ * @throws Exception an error occurred
+ */
+ @Test
+ public void testTranstitionToMaster() throws Exception {
+ startMocks();
+ controller.addConnectedSwitch(0, h);
+ controller.addActivatedEqualSwitch(0, sw);
+ controller.transitionToMasterSwitch(0);
+ assertNotNull(controller.getMasterSwitch(0));
+ }
+
+ /**
+ * Transition a master switch to equal state.
+ * @throws Exception an error occurred
+ */
+ @Test
+ public void testTranstitionToEqual() throws Exception {
+ startMocks();
+ controller.addConnectedSwitch(0, h);
+ controller.addActivatedMasterSwitch(0, sw);
+ controller.transitionToEqualSwitch(0);
+ assertNotNull(controller.getEqualSwitch(0));
+ }
+
+ /**
+ * Remove the switch from the controller instance.
+ * @throws Exception an error occurred
+ */
+ @Test
+ public void testRemoveSwitch() throws Exception {
+ sw.cancelAllStatisticsReplies();
+ EasyMock.expectLastCall().once();
+ sw.setConnected(false);
+ EasyMock.expectLastCall().once();
+ startMocks();
+ controller.addConnectedSwitch(0, h);
+ controller.addActivatedMasterSwitch(0, sw);
+ controller.removeConnectedSwitch(0);
+ assertNull(controller.getSwitch(0));
+ EasyMock.verify(sw, h);
+ }
+}