Fixed the tests I broke by changing the DeviceStorageImpl interface and rewrote tests to account for the fact that IP addresses are now vertices in the database
diff --git a/src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java b/src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java
index e99ca81..b40d2af 100644
--- a/src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java
+++ b/src/test/java/net/onrc/onos/graph/GraphDBOperationTest.java
@@ -3,16 +3,19 @@
  */
 package net.onrc.onos.graph;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
 
 import junit.framework.TestCase;
-
-import net.onrc.onos.graph.GraphDBOperation;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
 import net.onrc.onos.ofcontroller.core.ISwitchStorage.SwitchState;
@@ -25,12 +28,14 @@
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.powermock.api.easymock.PowerMock;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 
+import com.google.common.net.InetAddresses;
 import com.thinkaurelius.titan.core.TitanFactory;
 import com.thinkaurelius.titan.core.TitanGraph;
 import com.tinkerpop.blueprints.Vertex;
@@ -334,14 +339,14 @@
 		
 		IDeviceObject device = op.newDevice();
 		device.setMACAddress("11:22:33:44:55:66");
-		device.setIPAddress("192.168.1.1");
+		//device.setIPAddress("192.168.1.1");
 		op.commit();
 		
 		Iterator<Vertex> vertices = testdb.getVertices("type", "device").iterator();
 		assertTrue(vertices.hasNext());
 		Vertex v = vertices.next();
 		assertEquals("11:22:33:44:55:66", v.getProperty("dl_addr").toString());
-		assertEquals("192.168.1.1", v.getProperty("nw_addr").toString());
+		//assertEquals("192.168.1.1", v.getProperty("nw_addr").toString());
 	}
 
 	/**
@@ -395,6 +400,53 @@
 		op.commit();
 		assertNull(op.searchDevice("11:22:33:44:55:66"));
 	}
+	
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBOperation#newIpv4Address(net.onrc.onos.graph.GraphDBConnection, net.floodlightcontroller.core.INetMapTopologyObjects.IIpv4Address)}.
+	 */
+	@Test
+	public final void testNewIpv4Address() {
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.10.1"));
+		
+		assertFalse(testdb.getVertices("type", "ipv4Address").iterator().hasNext());
+		
+		IIpv4Address ipv4Address = op.newIpv4Address();
+		ipv4Address.setIpv4Address(intIpv4Address);
+		//device.setIPAddress("192.168.1.1");
+		op.commit();
+		
+		Iterator<Vertex> vertices = testdb.getVertices("type", "ipv4Address").iterator();
+		assertTrue(vertices.hasNext());
+		Vertex v = vertices.next();
+		assertEquals(intIpv4Address, ((Integer) v.getProperty("ipv4_address")).intValue());
+	}
+	
+	/**
+	 * Test method for {@link net.onrc.onos.graph.GraphDBOperation#searchIpv4Address(net.onrc.onos.graph.GraphDBConnection, net.floodlightcontroller.core.INetMapTopologyObjects.IIpv4Address)}.
+	 */
+	@Test
+	public final void testSearchIpv4Address() {
+		int addr1 = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.20.1"));
+		int addr2 = InetAddresses.coerceToInteger(InetAddresses.forString("59.203.2.15"));
+		
+		assertNull(op.searchIpv4Address(addr1));
+		assertNull(op.searchIpv4Address(addr2));
+
+		op.newIpv4Address().setIpv4Address(addr1);
+		op.commit();
+		
+		IIpv4Address ipv4Address = op.searchIpv4Address(addr1);
+		assertNotNull(ipv4Address);
+		assertEquals(addr1, ipv4Address.getIpv4Address());
+		
+		assertNull(op.searchIpv4Address(addr2));
+	}
+	
+	@Ignore
+	@Test
+	public final void testEnsureIpv4Address() {
+		// TODO not yet implemented
+	}
 
 	/**
 	 * Test method for {@link net.onrc.onos.graph.GraphDBOperation#newFlowPath(net.onrc.onos.graph.GraphDBConnection)}.
diff --git a/src/test/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjectsIDeviceObjectTest.java b/src/test/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjectsIDeviceObjectTest.java
index 880335b..7bd75d2 100644
--- a/src/test/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjectsIDeviceObjectTest.java
+++ b/src/test/java/net/onrc/onos/ofcontroller/core/INetMapTopologyObjectsIDeviceObjectTest.java
@@ -1,16 +1,19 @@
 package net.onrc.onos.ofcontroller.core;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.util.HashMap;
 
 import net.onrc.onos.graph.GraphDBConnection;
 import net.onrc.onos.graph.GraphDBOperation;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
 import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
 import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager;
+
 import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
@@ -18,9 +21,10 @@
 import org.junit.runner.RunWith;
 import org.powermock.api.easymock.PowerMock;
 import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.slf4j.LoggerFactory;
 import org.powermock.modules.junit4.PowerMockRunner;
+import org.slf4j.LoggerFactory;
 
+import com.google.common.net.InetAddresses;
 import com.thinkaurelius.titan.core.TitanFactory;
 import com.thinkaurelius.titan.core.TitanGraph;
 
@@ -89,10 +93,12 @@
 	 */
 	@Test
 	public void testSetGetIPAddress() {
-		String ipaddr = "192.168.0.1";
+		int ipaddr = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.0.1"));
 		IDeviceObject devObj = ope.newDevice();
-		devObj.setIPAddress(ipaddr);
-		assertEquals(devObj.getIPAddress(), ipaddr);
+		IIpv4Address ipv4Address = ope.newIpv4Address();
+		ipv4Address.setIpv4Address(ipaddr);
+		devObj.addIpv4Address(ipv4Address);
+		assertEquals(devObj.getIpv4Address(ipaddr), ipv4Address);
 	}
 	
 	/**
diff --git a/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java b/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java
index 89d4b92..b81370a 100644
--- a/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java
+++ b/src/test/java/net/onrc/onos/ofcontroller/devicemanager/internal/DeviceStorageImplTest.java
@@ -6,21 +6,21 @@
 import static org.easymock.EasyMock.verify;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import net.floodlightcontroller.devicemanager.IDevice;
 import net.floodlightcontroller.devicemanager.SwitchPort;
-import net.floodlightcontroller.packet.IPv4;
 import net.onrc.onos.graph.GraphDBConnection;
 import net.onrc.onos.graph.GraphDBOperation;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
+import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
 import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
 import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
 import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
-import net.floodlightcontroller.devicemanager.internal.Device;
+
 import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
@@ -33,12 +33,14 @@
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import com.google.common.net.InetAddresses;
 import com.thinkaurelius.titan.core.TitanFactory;
 
 //Add Powermock preparation
 @RunWith(PowerMockRunner.class)
 @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, DeviceStorageImpl.class})
-public class DeviceStorageImplTest{ //extends FloodlightTestCase{
+public class DeviceStorageImplTest{
 	
 	protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
 	
@@ -61,288 +63,187 @@
 		//PowerMock.mockStatic(GraphDBOperation.class);
 		mockOpe = PowerMock.createMock(GraphDBOperation.class);
 		PowerMock.expectNew(GraphDBOperation.class, new Class<?>[]{String.class}, conf).andReturn(mockOpe);
-		mockOpe.close();
+		//mockOpe.close();
 		PowerMock.replay(GraphDBOperation.class);
         // Replace the conf to dummy conf
 		// String conf = "/tmp/cassandra.titan";
 
-		
+		deviceImpl.init(conf);
 
 	}
 
 	@After
 	public void tearDown() throws Exception {	
-		deviceImpl.close();
-		deviceImpl = null;
-		
 		verify(mockOpe);
 	}
 	
-	private String makeIPStringFromArray(Integer[] ipaddresses){
-        String multiIntString = "";
-        for(Integer intValue : ipaddresses)
-        {
-        	if (multiIntString == null || multiIntString.isEmpty()){
-        		multiIntString = "[" + IPv4.fromIPv4Address(intValue);
-        	}
-        	else{
-           		multiIntString += "," + IPv4.fromIPv4Address(intValue);
-        	}
-        }
-        return multiIntString + "]";
+	private IPortObject getMockPort(long dpid, short port) {
+		IPortObject mockPortObject = createMock(IPortObject.class);
+		expect(mockPortObject.getNumber()).andReturn(port).anyTimes();
+		expect(mockPortObject.getDesc()).andReturn("test port").anyTimes();
+		return mockPortObject;
 	}
 	
-
+	private IDevice getMockDevice(String strMacAddress, long attachmentDpid, 
+			short attachmentPort, int ipv4Address) {
+		IDevice mockIDevice = createMock(IDevice.class);
+		
+		
+		long longMacAddress = HexString.toLong(strMacAddress);
+		
+		SwitchPort[] attachmentSwitchPorts = {new SwitchPort(attachmentDpid, attachmentPort)};
+		
+		expect(mockIDevice.getMACAddress()).andReturn(longMacAddress).anyTimes();
+		expect(mockIDevice.getMACAddressString()).andReturn(strMacAddress).anyTimes();
+		expect(mockIDevice.getAttachmentPoints()).andReturn(attachmentSwitchPorts).anyTimes();
+		expect(mockIDevice.getIPv4Addresses()).andReturn(new Integer[] {ipv4Address}).anyTimes();
+		
+		replay(mockIDevice);
+		
+		return mockIDevice;
+	}
+	
 	/**
-	 * Desc:
+	 * Description:
 	 *  Test method for addDevice method.
-	 * Codition:
-	 *  N/A
+	 * Condition:
+	 *  The device does not already exist in the database
 	 * Expect:
 	 * 	Get proper IDeviceObject
 	 */
-	//@Ignore
 	@Test
 	public void testAddNewDevice() {
-		try 
-		{	   
-			//Make mockDevice
-			IDevice mockDev = createMock(Device.class);
-			// Mac addr for test device.
-			String macAddr = "99:99:99:99:99:99";
-			// IP addr for test device
-			String ip = "192.168.100.1";
-			Integer[] ipaddrs = {IPv4.toIPv4Address(ip)};
-			// Mac addr for attached switch
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			// Port number for attached switch
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
 		
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs).anyTimes();
-			expect(mockDev.getAttachmentPoints()).andReturn(sps).anyTimes();
-			replay(mockDev);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList);
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-			
-			//Add the device
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-			
-			verify(mockIDev);
-			
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); 
+		IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(null);
+		expect(mockOpe.newDevice()).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList());
+		expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
+		mockPortObject.setDevice(mockDeviceObject);
+		expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
+		expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
+		mockDeviceObject.addIpv4Address(mockIpv4Address);
+		expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
+		expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
+		
+		mockDeviceObject.setMACAddress(strMacAddress);
+		mockDeviceObject.setType("device");
+		mockDeviceObject.setState("ACTIVE");
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(mockPortObject);
+		replay(mockIpv4Address);
+		replay(mockOpe);
+		
+		IDeviceObject addedObject = deviceImpl.addDevice(device);
+		assertNotNull(addedObject);
+		
+		verify(mockDeviceObject);
 	}
 	
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for addDevice method.
 	 * Condition:
-	 * 	Already added device is existed.
+	 *  The device already exists in the database.
 	 * Expect:
 	 * 	Get proper IDeviceObject still.
 	 *  Check the IDeviceObject properties set expectedly. 
 	 */
-	//@Ignore
 	@Test
-	public void testAddDeviceExisting() {
-		try 
-		{	   
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			Integer[] ipaddrs = {IPv4.toIPv4Address(ip)};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
+	public void testAddExistingDevice() {
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
 		
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs).times(2);
-			expect(mockDev.getAttachmentPoints()).andReturn(sps).times(2);
-			replay(mockDev);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes();
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-			
-			//Add the device
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-			
-			//Add the same device
-	        IDeviceObject obj2 = deviceImpl.addDevice(mockDev);
-			assertNotNull(obj2);
-
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); 
+		IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject));
+		expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
+		expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
+		expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
+		expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
+		
+		mockDeviceObject.setMACAddress(strMacAddress);
+		mockDeviceObject.setType("device");
+		mockDeviceObject.setState("ACTIVE");
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(mockPortObject);
+		replay(mockIpv4Address);
+		replay(mockOpe);
+		
+		IDeviceObject addedObject = deviceImpl.addDevice(device);
+		assertNotNull(addedObject);
+		
+		verify(mockDeviceObject);
 	}
 	
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for updateDevice method.
+	 *  NB. this is the same test as testAddExistingDevice
 	 * Condition:
-	 * 	The mac address and attachment point are the same. 
+	 * 	The MAC address and attachment point are the same. 
 	 *  All of the other parameter are different.
 	 * Expect:
 	 * 	Changed parameters are set expectedly.
 	 */
-	//@Ignore
 	@Test
-	public void testUpdateDevice() {
-		try
-		{
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			Integer ipInt = IPv4.toIPv4Address(ip);
-			Integer[] ipaddrs = {ipInt};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
-			
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev);
-			
-			//Dev2 (attached port is the same)
-			IDevice mockDev2 = createMock(Device.class);
-			String ip2 = "192.168.100.2";
-			Integer ipInt2 = IPv4.toIPv4Address(ip2);
-			Integer[] ipaddrs2 = {ipInt2};
-			
-			expect(mockDev2.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
-			expect(mockDev2.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev2);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes();
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs2));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-			
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-			
-			//update theDevice
-			IDeviceObject obj2 = deviceImpl.updateDevice(mockDev2);
-			assertNotNull(obj2);
-			
-			verify(mockIDev);
-			
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+	public void testAddUpdateDevice() {
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
+		
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); 
+		IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singleton(mockPortObject));
+		expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
+		expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
+		expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singleton(mockIpv4Address));
+		expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
+		
+		mockDeviceObject.setMACAddress(strMacAddress);
+		mockDeviceObject.setType("device");
+		mockDeviceObject.setState("ACTIVE");
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(mockPortObject);
+		replay(mockIpv4Address);
+		replay(mockOpe);
+		
+		IDeviceObject addedObject = deviceImpl.updateDevice(device);
+		assertNotNull(addedObject);
+		
+		verify(mockDeviceObject);
 	}
 
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for testRemoveDevice method.
 	 * Condition:
 	 * 	1. Unregistered IDeviceObject argument is put. 
@@ -350,395 +251,174 @@
 	 *  1. Nothing happen when unregistered IDeviceObject is put
 	 * 	2. IDeviceObject will be removed.
 	 */
-	//@Ignore
 	@Test
 	public void testRemoveDevice() {
-		try
-		{
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			Integer ipInt = IPv4.toIPv4Address(ip);
-			Integer[] ipaddrs = {ipInt};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
-			
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getAttachmentPoints()).andReturn(sps);
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
-			replay(mockDev);
-			
-			//Dev2 (attached port is the same)
-			IDevice mockDev2 = createMock(Device.class);
-			String macAddr2 = "33:33:33:33:33:33";
-			expect(mockDev2.getMACAddressString()).andReturn(macAddr2).anyTimes();
-			expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev2.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev2);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList);
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr2)).andReturn(null);
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			mockOpe.removeDevice(mockIDev);	
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-			
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
+		
+		IIpv4Address ipv4AddressObject = createMock(IIpv4Address.class);
+		IDeviceObject deviceObject = createMock(IDeviceObject.class);
+		expect(deviceObject.getIpv4Addresses()).andReturn(Collections.singleton(ipv4AddressObject));
+		replay(deviceObject);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(deviceObject);
+		mockOpe.removeIpv4Address(ipv4AddressObject);
+		mockOpe.removeDevice(deviceObject);
+		mockOpe.commit();
+		replay(mockOpe);
+		
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
 
-			deviceImpl.removeDevice(mockDev2);
-		    IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
-		    assertNotNull(dev);
-			
-			deviceImpl.removeDevice(mockDev);		
-		    IDeviceObject dev2 = deviceImpl.getDeviceByMac(macAddr);
-		    assertNull(dev2);
-		    
-			verify(mockIDev);
-
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+		deviceImpl.removeDevice(device);
+		
+		verify(mockOpe);
 	}
 
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for getDeviceByMac
 	 * Condition:
-	 * 	1. Unregistered mac address argument is set
+	 * 	1. Unregistered MAC address argument is set
 	 * Expect:
-	 * 	1.Nothing happen when you put unregistered mac address
+	 * 	1.Nothing happen when you put unregistered MAC address
 	 *  2.Get the proper IDeviceObject.
 	 *  3.Check the IDeviceObject properties set expectedly.
 	 */
-	//@Ignore
 	@Test
 	public void testGetDeviceByMac() {
-		try
-		{
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			Integer ipInt = IPv4.toIPv4Address(ip);
-			Integer[] ipaddrs = {ipInt};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
-			
-			String dummyMac = "33:33:33:33:33:33";
-			
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList);
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(dummyMac)).andReturn(null);
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-			
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-			
-		    IDeviceObject dummyDev = deviceImpl.getDeviceByMac(dummyMac);
-		    assertNull(dummyDev);	
-			
-		    IDeviceObject dev = deviceImpl.getDeviceByMac(macAddr);
-		    assertNotNull(dev);
-		    
-			verify(mockIDev);
-
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+		String mac = "99:99:99:99:99:99";
+		
+		IDeviceObject mockDevice = createMock(IDeviceObject.class);
+		
+		expect(mockOpe.searchDevice(mac)).andReturn(mockDevice);
+		
+		replay(mockOpe);
+		
+		IDeviceObject result = deviceImpl.getDeviceByMac(mac);
+		assertNotNull(result);
+		
+		verify(mockOpe);
 	}
-
+	
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for getDeviceByIP method.
 	 * Condition:
-	 * 	1. Unregistered ip address argument is set
+	 * 	1. Unregistered IP address argument is set
 	 * Expect:
-	 * 	1. Nothing happen when you put unregistered mac address
+	 * 	1. Nothing happen when you put unregistered IP address
 	 * 	2. Get the proper IDeviceObject.
 	 *  3. Check the IDeviceObject properties set expectedly.
 	 */
-	//@Ignore
 	@Test
 	public void testGetDeviceByIP() {
-		try
-		{
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			String ip2 = "192.168.100.2";
-			Integer ipInt = IPv4.toIPv4Address(ip);
-			Integer ipInt2 = IPv4.toIPv4Address(ip2);
-			Integer[] ipaddrs = {ipInt, ipInt2};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
-			
-			String dummyIP = "222.222.222.222";
-			
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList);
-			expect(mockIDev.getIPAddress()).andReturn(makeIPStringFromArray(ipaddrs)).times(2);
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			
-			//Make mock Iterator for IDeviceObject
-			List<IDeviceObject> deviceList = new ArrayList<IDeviceObject>();
-			deviceList.add(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.getDevices()).andReturn(deviceList).times(2);
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-	
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-			
-		    IDeviceObject dummyDev = deviceImpl.getDeviceByIP(dummyIP);
-		    assertNull(dummyDev);
-			
-		    IDeviceObject dev = deviceImpl.getDeviceByIP(ip);
-		    assertNotNull(dev);
-		    
-			verify(mockIDev);
-
-			
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+		int nonExistingIp = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.10.50"));
+		int existingIp = InetAddresses.coerceToInteger(InetAddresses.forString("10.5.12.128"));
+		
+		IDeviceObject mockDevice = createMock(IDeviceObject.class);
+		IIpv4Address mockExistingIp = createMock(IIpv4Address.class);
+		expect(mockExistingIp.getDevice()).andReturn(mockDevice);
+		
+		expect(mockOpe.searchIpv4Address(nonExistingIp)).andReturn(null);
+		expect(mockOpe.searchIpv4Address(existingIp)).andReturn(mockExistingIp);
+		
+		replay(mockExistingIp);
+		replay(mockOpe);
+		
+		IDeviceObject result = deviceImpl.getDeviceByIP(nonExistingIp);
+		assertNull(result);
+		
+		result = deviceImpl.getDeviceByIP(existingIp);
+		assertNotNull(result);
+		
+		verify(mockOpe);
 	}
 
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for testChangeDeviceAttachmentsIDevice
 	 * Condition:
-	 * 	1. Unexisting attachment point argument is set
+	 * 	1. The device is not currently attached to any point.
 	 * Expect:
-	 * 	1. Nothing happen when you put unexisting attachment point.
+	 * 	1. Nothing happen when you put nonexistent attachment point.
 	 * 	2. Set the attachment point expectedly;
 	 */
-	//@Ignore
 	@Test
-	public void testChangeDeviceAttachmentsIDevice() {
-		try
-		{
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			Integer ipInt = IPv4.toIPv4Address(ip);
-			Integer[] ipaddrs = {ipInt};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
-			
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev);
-			
-			//Dev2
-			IDevice mockDev2 = createMock(Device.class);
-			String switchMacAddr2 = "00:00:00:00:00:00:0a:02";
-			long lSwitchMacAddr2 = HexString.toLong(switchMacAddr2);
-			short portNum2 = 2; 
-			SwitchPort sp2 = new SwitchPort(lSwitchMacAddr2, portNum2);
-			SwitchPort sps2[] = {sp2};
-			
-			expect(mockDev2.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev2.getAttachmentPoints()).andReturn(sps2);
-			replay(mockDev2);
-			
-			//Dev3
-			IDevice mockDev3 = createMock(Device.class);
-			String switchMacAddr3 = "00:00:00:00:00:00:00:00";
-			long lSwitchMacAddr3 = HexString.toLong(switchMacAddr3);
-			short portNum3 = 1; 
-			SwitchPort sp3 = new SwitchPort(lSwitchMacAddr3, portNum3);
-			SwitchPort sps3[] = {sp3};
-			
-			expect(mockDev3.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev3.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev3.getAttachmentPoints()).andReturn(sps3);
-			replay(mockDev3);
-			
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			mockIPort.removeDevice(mockIDev);
-			mockIPort.removeDevice(mockIDev);
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			expect(mockIDev.getAttachedPorts()).andReturn(portList).anyTimes();
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			replay(mockIDev);	
-			
-			//Mock IPortObject 2 with dpid "00:00:00:00:00:00:0a:02" and port "2"
-			IPortObject mockIPort2 = createMock(IPortObject.class);
-			mockIPort2.setNumber(portNum2);
-			mockIPort2.setType("port");
-			String iPortDesc2 = "port 2 at LAX Switch";
-			expect(mockIPort2.getNumber()).andReturn(portNum2).anyTimes();
-			expect(mockIPort2.getDesc()).andReturn(iPortDesc2).anyTimes();
-			mockIPort2.setDevice(mockIDev);
-			replay(mockIPort2);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList2 = new ArrayList<IPortObject>();
-			portList2.add(mockIPort2);
-			
-			//Mock IPortObject 3 with dpid "00:00:00:00:00:00:00:00" and port "1"
-			IPortObject mockIPort3 = createMock(IPortObject.class);
-			mockIPort3.setNumber(portNum3);
-			mockIPort3.setType("port");
-			String iPortDesc3 = "n/a";
-			expect(mockIPort3.getNumber()).andReturn(portNum3).anyTimes();
-			expect(mockIPort3.getDesc()).andReturn(iPortDesc3).anyTimes();
-			mockIPort3.setDevice(mockIDev);
-			replay(mockIPort3);
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr2, portNum2)).andReturn(mockIPort2);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr3, portNum3)).andReturn(null);
-			mockOpe.commit();
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
+	public void testChangeDeviceAttachementsWhenUnattached() {
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
+		
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort); 
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.<IPortObject>emptyList());
+		expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
+		mockPortObject.setDevice(mockDeviceObject);
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(mockPortObject);
+		replay(mockOpe);
+		
+		deviceImpl.changeDeviceAttachments(device);
+		
+		verify(mockDeviceObject);
+		verify(mockPortObject);
+		verify(mockOpe);
+	}
 	
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-			
-		    deviceImpl.changeDeviceAttachments(mockDev2);
-			
-		    deviceImpl.changeDeviceAttachments(mockDev3);
-		    
-			verify(mockIDev);
-
-			
-		} catch(Exception e) {
-			fail(e.getMessage());
-		}
+	/**
+	 * Description:
+	 * 	Test method for testChangeDeviceAttachmentsIDevice
+	 * Condition:
+	 * 	1. The device is currently attached to a switch, but this attachment point
+	 *     has now changed.
+	 * Expect:
+	 * 	1. The device should be removed from the old attachment point.
+	 * 	2. Set the attachment point expectedly;
+	 */
+	@Test
+	public void testChangeDeviceAttachementsWhenAttached() {
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
+		
+		//Details for the port the device will be moved from
+		long alreadyAttachedDpid = HexString.toLong("00:00:00:00:00:00:0b:01");
+		short alreadyAttachedPort = 5;
+		
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		IPortObject mockPortObject = getMockPort(attachmentDpid, attachmentPort);
+		IPortObject alreadyAttachedPortObject = getMockPort(alreadyAttachedDpid, alreadyAttachedPort);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getAttachedPorts()).andReturn(Collections.singletonList(alreadyAttachedPortObject));
+		expect(mockOpe.searchPort(HexString.toHexString(attachmentDpid), attachmentPort)).andReturn(mockPortObject);
+		mockPortObject.setDevice(mockDeviceObject);
+		alreadyAttachedPortObject.removeDevice(mockDeviceObject);
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(alreadyAttachedPortObject);
+		replay(mockPortObject);
+		replay(mockOpe);
+		
+		deviceImpl.changeDeviceAttachments(device);
+		
+		verify(mockDeviceObject);
+		verify(alreadyAttachedPortObject);
+		verify(mockPortObject);
+		verify(mockOpe);
 	}
 
 	@Ignore
@@ -748,90 +428,89 @@
 	}
 
 	/**
-	 * Desc:
+	 * Description:
 	 * 	Test method for testChangeDeviceIPv4Address
 	 * Condition:
 	 * 	N/A
 	 * Expect:
-	 *  1. Set the ipadress expectedly.
+	 *  1. Set the IP address expectedly.
 	 */
-	//@Ignore
 	@Test
-	public void testChangeDeviceIPv4Address() {
-		try
-		{
-			//Dev1
-			IDevice mockDev = createMock(Device.class);
-			String macAddr = "99:99:99:99:99:99";
-			String ip = "192.168.100.1";
-			Integer ipInt = IPv4.toIPv4Address(ip);
-			Integer[] ipaddrs = {ipInt};
-			String switchMacAddr = "00:00:00:00:00:00:0a:01";		
-			long switchMacAddrL = HexString.toLong(switchMacAddr);
-			short portNum = 2; 
-			SwitchPort sp1 = new SwitchPort(switchMacAddrL, portNum);
-			SwitchPort[] sps = {sp1};
-			
-			expect(mockDev.getMACAddressString()).andReturn(macAddr).anyTimes();
-			expect(mockDev.getIPv4Addresses()).andReturn(ipaddrs);
-			expect(mockDev.getAttachmentPoints()).andReturn(sps);
-			replay(mockDev);
-			
-			//Dev2
-			IDevice mockDev2 = createMock(Device.class);
-			String ip2 = "192.168.100.2";
-			Integer ipInt2 = IPv4.toIPv4Address(ip2);
-			Integer[] ipaddrs2 = {ipInt2};
-			expect(mockDev2.getMACAddressString()).andReturn(macAddr);
-			expect(mockDev2.getIPv4Addresses()).andReturn(ipaddrs2);
-			replay(mockDev2);
-			
-			//Mock IPortObject 1 with dpid "00:00:00:00:00:00:0a:01" and port "1"
-			IPortObject mockIPort = createMock(IPortObject.class);
-			mockIPort.setNumber(portNum);
-			mockIPort.setType("port");
-			String iPortDesc = "port 1 at SEA Switch";
-			expect(mockIPort.getNumber()).andReturn(portNum).anyTimes();
-			expect(mockIPort.getDesc()).andReturn(iPortDesc).anyTimes();
-			replay(mockIPort);
-			
-			//Make Iterator for mockIport
-			List<IPortObject> portList = new ArrayList<IPortObject>();
-			portList.add(mockIPort);
-			
-			//Expectation for mockIDeviceObject
-			IDeviceObject mockIDev = createMock(IDeviceObject.class);	
-			expect(mockIDev.getAttachedPorts()).andReturn(portList);
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs));
-			mockIDev.setMACAddress(macAddr);
-			mockIDev.setType("device");
-			mockIDev.setState("ACTIVE");
-			mockIDev.setIPAddress(makeIPStringFromArray(ipaddrs2));
-			replay(mockIDev);	
-			
-			//Expectation for mockOpe
-			expect(mockOpe.searchDevice(macAddr)).andReturn(null);
-			expect(mockOpe.newDevice()).andReturn(mockIDev);
-			expect(mockOpe.searchPort(switchMacAddr, portNum)).andReturn(mockIPort);
-			mockOpe.commit();
-			expect(mockOpe.searchDevice(macAddr)).andReturn(mockIDev);
-			mockOpe.commit();
-			replay(mockOpe);				
-			
-			deviceImpl.init(conf);
-			
-	        IDeviceObject obj = deviceImpl.addDevice(mockDev);	
-			assertNotNull(obj);
-
-	        deviceImpl.changeDeviceIPv4Address(mockDev2);	
-	        
-			verify(mockIDev);
-
-					
-		} 
-		catch(Exception e) {
-			fail(e.getMessage());
-		}
+	public void testChangeDeviceIpv4Address() {
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
+		
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
+		expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
+		mockDeviceObject.addIpv4Address(mockIpv4Address);
+		expect(mockDeviceObject.getIpv4Addresses()).andReturn(Collections.singletonList(mockIpv4Address));
+		expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(mockIpv4Address);
+		replay(mockOpe);
+		
+		deviceImpl.changeDeviceIPv4Address(device);
+		
+		verify(mockDeviceObject);
+		verify(mockIpv4Address);
+		verify(mockOpe);
+	}
+	
+	/**
+	 * Description:
+	 * 	Test method for testChangeDeviceIPv4Address
+	 * Condition:
+	 * 	1. The device had an old IP address which has now changed.
+	 * Expect:
+	 *  1. The old IP address should be removed from the device.
+	 *  2. Set the IP address expectedly.
+	 */
+	@Test
+	public void testChangeDeviceIpv4AddressAndRemoveExisting() {
+		String strMacAddress = "99:99:99:99:99:99";
+		long attachmentDpid = HexString.toLong("00:00:00:00:00:00:0a:01");
+		short attachmentPort = 2;
+		int intIpv4Address = InetAddresses.coerceToInteger(InetAddresses.forString("192.168.100.1"));
+		
+		IDevice device = getMockDevice(strMacAddress, attachmentDpid, attachmentPort, intIpv4Address);
+		
+		IDeviceObject mockDeviceObject = createMock(IDeviceObject.class);
+		
+		IIpv4Address mockIpv4Address = createMock(IIpv4Address.class);
+		IIpv4Address mockDeletingIpv4Address = createMock(IIpv4Address.class);
+		List<IIpv4Address> ipv4Vertices = new ArrayList<IIpv4Address>(2);
+		ipv4Vertices.add(mockIpv4Address);
+		ipv4Vertices.add(mockDeletingIpv4Address);
+		
+		expect(mockOpe.searchDevice(strMacAddress)).andReturn(mockDeviceObject);
+		expect(mockDeviceObject.getIpv4Address(intIpv4Address)).andReturn(null);
+		expect(mockOpe.ensureIpv4Address(intIpv4Address)).andReturn(mockIpv4Address);
+		mockDeviceObject.addIpv4Address(mockIpv4Address);
+		expect(mockDeviceObject.getIpv4Addresses()).andReturn(ipv4Vertices);
+		expect(mockIpv4Address.getIpv4Address()).andReturn(intIpv4Address);
+		expect(mockDeletingIpv4Address.getIpv4Address()).andReturn(1);
+		mockDeviceObject.removeIpv4Address(mockDeletingIpv4Address);
+		mockOpe.commit();
+		
+		replay(mockDeviceObject);
+		replay(mockIpv4Address);
+		replay(mockOpe);
+		
+		deviceImpl.changeDeviceIPv4Address(device);
+		
+		verify(mockDeviceObject);
+		verify(mockIpv4Address);
+		verify(mockOpe);
 	}
 
 }