@Test
public void testFindDevice() {

    boolean exceptionCaught;
    deviceManager.entityClassifier= new MockEntityClassifierMac();
    deviceManager.startUp(null);
    ITopologyService mockTopology = createMock(ITopologyService.class);
    deviceManager.topology = mockTopology;
    expect(mockTopology.isAttachmentPointPort(anyLong(),
                                              anyShort())).
                                              andReturn(true).anyTimes();
    expect(mockTopology.getL2DomainId(EasyMock.anyLong())).andReturn(1L).anyTimes();
    replay(mockTopology);
    Entity entity1 = new Entity(1L, (short)1, 1, 1L, 1, new Date());
    Entity entity2 = new Entity(2L, (short)2, 2, 1L, 2, new Date());
    Entity entity2b = new Entity(22L, (short)2, 2, 1L, 2, new Date());
    
    Entity entity3 = new Entity(3L, (short)1, 3, 2L, 1, new Date());
    Entity entity4 = new Entity(4L, (short)2, 4, 2L, 2, new Date());
    
    Entity entity5 = new Entity(5L, (short)1, 5, 3L, 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(),
                                              entity1.getSwitchPort()+1));
    // VLAN changed. No device matches
    assertEquals(null, deviceManager.findDevice(entity1.getMacAddress(), 
                                              (short)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(5L, (short)1, 5, -1L, 1, new Date());
    assertEquals(null, deviceManager.findDeviceByEntity(entityNoClass));
    
    
    // Now look up destination devices
    assertEquals(d1, deviceManager.findDestDevice(d2, 
                                              entity1.getMacAddress(), 
                                              entity1.getVlan(),
                                              entity1.getIpv4Address()));
    assertEquals(d1, deviceManager.findDestDevice(d2, 
                                              entity1.getMacAddress(), 
                                              entity1.getVlan(),
                                              null));
    assertEquals(null, deviceManager.findDestDevice(d2, 
                                              entity1.getMacAddress(), 
                                              (short) -1,
                                              0));
}