@Test
public void testAttachmentPointFlappingTwoCluster() 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(1L)).
    andReturn(1L).anyTimes();
    expect(mockTopology.getL2DomainId(5L)).
    andReturn(5L).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)1, 1L, (short)2)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)2, 5L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(5L, (short)1, 5L, (short)2)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)2, 1L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)1)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(1L, (short)1, 5L, (short)2)).
    andReturn(false).anyTimes();
    expect(mockTopology.isConsistent(5L, (short)2, 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 entity2 = new Entity(1L, null, null, 1L, 2, c.getTime());
    Entity entity3 = new Entity(1L, null, null, 5L, 1, c.getTime());
    Entity entity4 = new Entity(1L, null, null, 5L, 2, c.getTime());
    entity1.setLastSeenTimestamp(c.getTime());
    c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT/2);
    c.add(Calendar.MILLISECOND, 1);
    entity2.setLastSeenTimestamp(c.getTime());
    c.add(Calendar.MILLISECOND, 1);
    entity3.setLastSeenTimestamp(c.getTime());
    c.add(Calendar.MILLISECOND, 1);
    entity4.setLastSeenTimestamp(c.getTime());
    deviceManager.learnDeviceByEntity(entity1);
    deviceManager.learnDeviceByEntity(entity2);
    deviceManager.learnDeviceByEntity(entity3);
    IDevice d = deviceManager.learnDeviceByEntity(entity4);
    // all entities are active, so entities 2,4 should win
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 2),
                                         new SwitchPort(5L, 2) },
                                         d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 2),
                                         new SwitchPort(5L, 2)},
                                         d.getAttachmentPoints(true));
    c.add(Calendar.MILLISECOND, 1);
    entity1.setLastSeenTimestamp(c.getTime());
    d = deviceManager.learnDeviceByEntity(entity1);
    // all entities are active, so entities 2,4 should win
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 2) },
                                         d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 2),
                                         new SwitchPort(1L, 2, ErrorStatus.DUPLICATE_DEVICE)},
                                         d.getAttachmentPoints(true));
    c.add(Calendar.MILLISECOND, Entity.ACTIVITY_TIMEOUT+1);
    entity1.setLastSeenTimestamp(c.getTime());
    d = deviceManager.learnDeviceByEntity(entity1);
    // entities 3,4 are still in conflict, but 1 should be resolved
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 2) },
                                         d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 2)},
                                         d.getAttachmentPoints(true));
    entity3.setLastSeenTimestamp(c.getTime());
    d = deviceManager.learnDeviceByEntity(entity3);
    // no conflicts, 1 and 3 will win
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 1) },
                                         d.getAttachmentPoints());
    assertArrayEquals(new SwitchPort[] { new SwitchPort(1L, 1),
                                         new SwitchPort(5L, 1) },
                                         d.getAttachmentPoints(true));
}