@Test
public void testAttachmentPointFlapping() throws Exception {

    Calendar c = Calendar.getInstance();
    ITopologyService mockTopology = createMock(ITopologyService.class);
    expect(mockTopology.isAttachmentPointPort(anyLong(),
                                              anyShort())).andReturn(true).anyTimes();
    expect(mockTopology.isBroadcastDomainPort(anyLong(),
                                              anyShort())).
                                              andReturn(false).anyTimes();
    expect(mockTopology.isInSameBroadcastDomain(anyLong(), anyShort(),
                                                anyLong(), anyShort())).andReturn(false).anyTimes();
    expect(mockTopology.getL2DomainId(anyLong())).
    andReturn(1L).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)1, 1L, (short)1)).
    andReturn(true).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)1, 10L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(5L, (short)1, 10L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(10L, (short)1, 1L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(5L, (short)1, 1L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(10L, (short)1, 5L, (short)1)).
    andReturn(false).anyTimes();
    Date topologyUpdateTime = new Date();
    expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
    anyTimes();
    replay(mockTopology);
    deviceManager.topology = mockTopology;
    Entity entity1 = new Entity(1L, null, null, 1L, 1, c.getTime());
    Entity entity1a = new Entity(1L, null, 1, 1L, 1, c.getTime());
    Entity entity2 = new Entity(1L, null, null, 5L, 1, c.getTime());
    Entity entity3 = new Entity(1L, null, null, 10L, 1, c.getTime());
    entity1.setLastSeenTimestamp(c.getTime());
    c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT/2);
    entity1a.setLastSeenTimestamp(c.getTime());
    c.add(Calendar.MILLISECOND, 1);
    entity2.setLastSeenTimestamp(c.getTime());
    c.add(Calendar.MILLISECOND, 1);
    entity3.setLastSeenTimestamp(c.getTime());
    IDevice d;
    d = deviceManager.learnDeviceByEntity(entity1);
    d = deviceManager.learnDeviceByEntity(entity1a);
    d = deviceManager.learnDeviceByEntity(entity2);
    d = deviceManager.learnDeviceByEntity(entity3);
    // all entities are active, so entity3 should win
    assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1) },
                      d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(10L, 1),},
                          d.getAttachmentPoints(true));
    c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT/4);
    entity1.setLastSeenTimestamp(c.getTime());
    d = deviceManager.learnDeviceByEntity(entity1);
    // all are still active; entity3 should still win
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
                      d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 1,
                                                        ErrorStatus.DUPLICATE_DEVICE),
                                                        new SwitchPort(10L, 1,
                                                                       ErrorStatus.DUPLICATE_DEVICE) },
                                                                       d.getAttachmentPoints(true));
    c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT+2000);
    entity1.setLastSeenTimestamp(c.getTime());
    d = deviceManager.learnDeviceByEntity(entity1);
    assertEquals(entity1.getActiveSince(), entity1.getLastSeenTimestamp());
    // entity1 should now be the only active entity
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
                      d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1) },
                      d.getAttachmentPoints(true));
}