}
@Test
public void testEntityLearning() throws Exception {
IDeviceListener mockListener =
createMock(IDeviceListener.class);
expect(mockListener.getName()).andReturn("mockListener").atLeastOnce();
expect(mockListener.isCallbackOrderingPostreq((String)anyObject(), (String)anyObject()))
.andReturn(false).atLeastOnce();
expect(mockListener.isCallbackOrderingPrereq((String)anyObject(), (String)anyObject()))
.andReturn(false).atLeastOnce();
replay(mockListener);
deviceManager.addListener(mockListener);
verify(mockListener);
reset(mockListener);
deviceManager.entityClassifier= new MockEntityClassifier();
deviceManager.startUp(null);
ITopologyService mockTopology = createMock(ITopologyService.class);
expect(mockTopology.getL2DomainId(anyLong())).
andReturn(1L).anyTimes();
expect(mockTopology.isBroadcastDomainPort(anyLong(), anyShort())).
andReturn(false).anyTimes();
expect(mockTopology.isAttachmentPointPort(anyLong(),
anyShort())).andReturn(true).anyTimes();
expect(mockTopology.isConsistent(10L, (short)1, 10L, (short)1)).
andReturn(true).anyTimes();
expect(mockTopology.isConsistent(1L, (short)1, 1L, (short)1)).
andReturn(true).anyTimes();
expect(mockTopology.isConsistent(50L, (short)3, 50L, (short)3)).
andReturn(true).anyTimes();
Date topologyUpdateTime = new Date();
expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
anyTimes();
deviceManager.topology = mockTopology;
Entity entity1 = new Entity(1L, null, null, 1L, 1, new Date());
Entity entity2 = new Entity(1L, null, null, 10L, 1, new Date());
Entity entity3 = new Entity(1L, null, 1, 10L, 1, new Date());
Entity entity4 = new Entity(1L, null, 1, 1L, 1, new Date());
Entity entity5 = new Entity(2L, (short)4, 1, 5L, 2, new Date());
Entity entity6 = new Entity(2L, (short)4, 1, 50L, 3, new Date());
Entity entity7 = new Entity(2L, (short)4, 2, 50L, 3, new Date());
mockListener.deviceAdded(isA(IDevice.class));
replay(mockListener, mockTopology);
Device d1 = deviceManager.learnDeviceByEntity(entity1);
assertSame(d1, deviceManager.learnDeviceByEntity(entity1));
assertSame(d1, deviceManager.findDeviceByEntity(entity1));
assertEquals(DefaultEntityClassifier.entityClass ,
d1.getEntityClass());
assertArrayEquals(new Short[] { -1 }, d1.getVlanId());
assertArrayEquals(new Integer[] { }, d1.getIPv4Addresses());
assertEquals(1, deviceManager.getAllDevices().size());
verify(mockListener);
reset(mockListener);
mockListener.deviceAdded(isA(IDevice.class));
replay(mockListener);
Device d2 = deviceManager.learnDeviceByEntity(entity2);
assertFalse(d1.equals(d2));
assertNotSame(d1, d2);
assertNotSame(d1.getDeviceKey(), d2.getDeviceKey());
assertEquals(MockEntityClassifier.testEC, d2.getEntityClass());
assertArrayEquals(new Short[] { -1 }, d2.getVlanId());
assertArrayEquals(new Integer[] { }, d2.getIPv4Addresses());
assertEquals(2, deviceManager.getAllDevices().size());
verify(mockListener);
reset(mockListener);
mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
replay(mockListener);
Device d3 = deviceManager.learnDeviceByEntity(entity3);
assertNotSame(d2, d3);
assertEquals(d2.getDeviceKey(), d3.getDeviceKey());
assertEquals(MockEntityClassifier.testEC, d3.getEntityClass());
assertArrayEquals(new Integer[] { 1 },
d3.getIPv4Addresses());
assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1) },
d3.getAttachmentPoints());
assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1) },
d3.getAttachmentPoints(true));
assertArrayEquals(new Short[] { -1 },
d3.getVlanId());
assertEquals(2, deviceManager.getAllDevices().size());
verify(mockListener);
reset(mockListener);
mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
replay(mockListener);
Device d4 = deviceManager.learnDeviceByEntity(entity4);
assertNotSame(d1, d4);
assertEquals(d1.getDeviceKey(), d4.getDeviceKey());
assertEquals(DefaultEntityClassifier.entityClass, d4.getEntityClass());
assertArrayEquals(new Integer[] { 1 },
d4.getIPv4Addresses());
assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
d4.getAttachmentPoints());
assertArrayEquals(new Short[] { -1 },
d4.getVlanId());
assertEquals(2, deviceManager.getAllDevices().size());
verify(mockListener);
reset(mockListener);
mockListener.deviceAdded((isA(IDevice.class)));
replay(mockListener);
Device d5 = deviceManager.learnDeviceByEntity(entity5);
assertArrayEquals(new SwitchPort[] { new SwitchPort(5L, 2) },
d5.getAttachmentPoints());
assertArrayEquals(new Short[] { (short) 4 },
d5.getVlanId());
assertEquals(2L, d5.getMACAddress());
assertEquals("00:00:00:00:00:02", d5.getMACAddressString());
verify(mockListener);
reset(mockListener);
mockListener.deviceAdded(isA(IDevice.class));
replay(mockListener);
Device d6 = deviceManager.learnDeviceByEntity(entity6);
assertArrayEquals(new SwitchPort[] { new SwitchPort(50L, 3) },
d6.getAttachmentPoints());
assertArrayEquals(new Short[] { (short) 4 },
d6.getVlanId());
assertEquals(4, deviceManager.getAllDevices().size());
verify(mockListener);
reset(mockListener);
mockListener.deviceIPV4AddrChanged(isA(IDevice.class));
replay(mockListener);
Device d7 = deviceManager.learnDeviceByEntity(entity7);
assertNotSame(d6, d7);
assertEquals(d6.getDeviceKey(), d7.getDeviceKey());