@Test
public void testFindDevice() throws FloodlightModuleException {

	boolean exceptionCaught;
	deviceManager.entityClassifier= new MockEntityClassifierMac();
	deviceManager.startUp(null);
	ITopologyService mockTopology = createMock(ITopologyService.class);
	deviceManager.topology = mockTopology;
	expect(mockTopology.isAttachmentPointPort(DatapathId.of(anyLong()),
			OFPort.of(anyShort()))).
			andReturn(true).anyTimes();
	expect(mockTopology.getL2DomainId(DatapathId.of(EasyMock.anyLong()))).andReturn(DatapathId.of(1L)).anyTimes();
	replay(mockTopology);
	Entity entity1 = new Entity(MacAddress.of(1L), VlanVid.ofVlan(1), IPv4Address.of(1), DatapathId.of(1L), OFPort.of(1), new Date());
	Entity entity2 = new Entity(MacAddress.of(2L), VlanVid.ofVlan(2), IPv4Address.of(2), DatapathId.of(1L), OFPort.of(2), new Date());
	Entity entity2b = new Entity(MacAddress.of(22L), VlanVid.ofVlan(2), IPv4Address.of(2), DatapathId.of(1L), OFPort.of(2), new Date());
	Entity entity3 = new Entity(MacAddress.of(3L), VlanVid.ofVlan(1), IPv4Address.of(3), DatapathId.of(2L), OFPort.of(1), new Date());
	Entity entity4 = new Entity(MacAddress.of(4L), VlanVid.ofVlan(2), IPv4Address.of(4), DatapathId.of(2L), OFPort.of(2), new Date());
	Entity entity5 = new Entity(MacAddress.of(5L), VlanVid.ofVlan(1), IPv4Address.of(5), DatapathId.of(3L), OFPort.of(1), new Date());
	IDevice d1 = deviceManager.learnDeviceByEntity(entity1);
	IDevice d2 = deviceManager.learnDeviceByEntity(entity2);
	IDevice d3 = deviceManager.learnDeviceByEntity(entity3);
	IDevice d4 = deviceManager.learnDeviceByEntity(entity4);
	IDevice d5 = deviceManager.learnDeviceByEntity(entity5);
	// Make sure the entity classifier worked as expected
	assertEquals(MockEntityClassifierMac.testECMac1, d1.getEntityClass());
	assertEquals(MockEntityClassifierMac.testECMac1, d2.getEntityClass());
	assertEquals(MockEntityClassifierMac.testECMac2, d3.getEntityClass());
	assertEquals(MockEntityClassifierMac.testECMac2, d4.getEntityClass());
	assertEquals(DefaultEntityClassifier.entityClass,
			d5.getEntityClass());
	// Look up the device using findDevice() which uses only the primary
	// index
	assertEquals(d1, deviceManager.findDevice(entity1.getMacAddress(),
			entity1.getVlan(),
			entity1.getIpv4Address(),
			entity1.getSwitchDPID(),
			entity1.getSwitchPort()));
	// port changed. Device will be found through class index
	assertEquals(d1, deviceManager.findDevice(entity1.getMacAddress(),
			entity1.getVlan(),
			entity1.getIpv4Address(),
			entity1.getSwitchDPID(),
			OFPort.of(entity1.getSwitchPort().getPortNumber()+1)));
	// VLAN changed. No device matches
	assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(),
			VlanVid.ofVlan(42),
			entity1.getIpv4Address(),
			entity1.getSwitchDPID(),
			entity1.getSwitchPort()));
	assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(),
			null,
			entity1.getIpv4Address(),
			entity1.getSwitchDPID(),
			entity1.getSwitchPort()));
	assertEquals(d2, deviceManager.findDeviceByEntity(entity2));
	assertEquals(null, deviceManager.findDeviceByEntity(entity2b));
	assertEquals(d3, deviceManager.findDevice(entity3.getMacAddress(),
			entity3.getVlan(),
			entity3.getIpv4Address(),
			entity3.getSwitchDPID(),
			entity3.getSwitchPort()));
	// switch and port not set. throws exception
	exceptionCaught = false;
	try {
		assertEquals(null, deviceManager.findDevice(entity3.getMacAddress(),
				entity3.getVlan(),
				entity3.getIpv4Address(),
				null,
				null));
	}
	catch (IllegalArgumentException e) {
		exceptionCaught = true;
	}
	if (!exceptionCaught)
		fail("findDevice() did not throw IllegalArgumentException");
	assertEquals(d4, deviceManager.findDeviceByEntity(entity4));
	assertEquals(d5, deviceManager.findDevice(entity5.getMacAddress(),
			entity5.getVlan(),
			entity5.getIpv4Address(),
			entity5.getSwitchDPID(),
			entity5.getSwitchPort()));
	// switch and port not set. throws exception (swith/port are key
	// fields of IEntityClassifier but not d5.entityClass
	exceptionCaught = false;
	try {
		assertEquals(d5, deviceManager.findDevice(entity5.getMacAddress(),
				entity5.getVlan(),
				entity5.getIpv4Address(),
				null,
				null));
	}
	catch (IllegalArgumentException e) {
		exceptionCaught = true;
	}
	if (!exceptionCaught)
		fail("findDevice() did not throw IllegalArgumentException");
	Entity entityNoClass = new Entity(MacAddress.of(5L), VlanVid.ofVlan(1), IPv4Address.of(5), DatapathId.of(-1L), OFPort.of(1), new Date());
	assertEquals(null, deviceManager.findDeviceByEntity(entityNoClass));
	// Now look up destination devices
	assertEquals(d1, deviceManager.findClassDevice(d2.getEntityClass(),
			entity1.getMacAddress(),
			entity1.getVlan(),
			entity1.getIpv4Address()));
	assertEquals(d1, deviceManager.findClassDevice(d2.getEntityClass(),
			entity1.getMacAddress(),
			entity1.getVlan(),
			null));
	assertEquals(null, deviceManager.findClassDevice(d2.getEntityClass(),
			entity1.getMacAddress(),
			VlanVid.ofVlan(-1),
			IPv4Address.of(0)));
}