@Test
public void testAttachmentPointFlappingTwoCluster() throws Exception {

	Calendar c = Calendar.getInstance();
	ITopologyService mockTopology = createMock(ITopologyService.class);
	expect(mockTopology.isAttachmentPointPort(DatapathId.of(anyLong()),
			OFPort.of(anyShort()))).andReturn(true).anyTimes();
	expect(mockTopology.isBroadcastDomainPort(DatapathId.of(anyLong()),
			OFPort.of(anyShort()))).
			andReturn(false).anyTimes();
	expect(mockTopology.isInSameBroadcastDomain(DatapathId.of(anyLong()), OFPort.of(anyShort()),
			DatapathId.of(anyLong()), OFPort.of(anyShort()))).andReturn(false).anyTimes();
	expect(mockTopology.getOpenflowDomainId(DatapathId.of(1L))).
	andReturn(DatapathId.of(1L)).anyTimes();
	expect(mockTopology.getOpenflowDomainId(DatapathId.of(5L))).
	andReturn(DatapathId.of(5L)).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(1L), OFPort.of(1), DatapathId.of(1L), OFPort.of(2))).
	andReturn(false).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(1L), OFPort.of(2), DatapathId.of(5L), OFPort.of(1))).
	andReturn(false).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(5L), OFPort.of(1), DatapathId.of(5L), OFPort.of(2))).
	andReturn(false).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(1L), OFPort.of(2), DatapathId.of(1L), OFPort.of(1))).
	andReturn(false).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(1L), OFPort.of(1), DatapathId.of(5L), OFPort.of(1))).
	andReturn(false).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(1L), OFPort.of(1), DatapathId.of(5L), OFPort.of(2))).
	andReturn(false).anyTimes();
	expect(mockTopology.isConsistent(DatapathId.of(5L), OFPort.of(2), DatapathId.of(5L), OFPort.of(1))).
	andReturn(false).anyTimes();
	Date topologyUpdateTime = new Date();
	expect(mockTopology.getLastUpdateTime()).andReturn(topologyUpdateTime).
	anyTimes();
	replay(mockTopology);
	deviceManager.topology = mockTopology;
	Entity entity1 = new Entity(MacAddress.of(1L), VlanVid.ZERO, IPv4Address.NONE, IPv6Address.NONE, DatapathId.of(1L), OFPort.of(1), c.getTime());
	Entity entity2 = new Entity(MacAddress.of(1L), VlanVid.ZERO, IPv4Address.NONE, IPv6Address.NONE, DatapathId.of(1L), OFPort.of(2), c.getTime());
	Entity entity3 = new Entity(MacAddress.of(1L), VlanVid.ZERO, IPv4Address.NONE, IPv6Address.NONE, DatapathId.of(5L), OFPort.of(1), c.getTime());
	Entity entity4 = new Entity(MacAddress.of(1L), VlanVid.ZERO, IPv4Address.NONE, IPv6Address.NONE, DatapathId.of(5L), OFPort.of(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(DatapathId.of(1L), OFPort.of(2)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(2)) },
			d.getAttachmentPoints());
	assertArrayEquals(new SwitchPort[] { new SwitchPort(DatapathId.of(1L), OFPort.of(2)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(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(DatapathId.of(1L), OFPort.of(2)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(2)) },
			d.getAttachmentPoints());
	assertArrayEquals(new SwitchPort[] { new SwitchPort(DatapathId.of(1L), OFPort.of(2)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(2)),
			new SwitchPort(DatapathId.of(1L), OFPort.of(1), 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(DatapathId.of(1L), OFPort.of(1)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(2)) },
			d.getAttachmentPoints());
	assertArrayEquals(new SwitchPort[] { new SwitchPort(DatapathId.of(1L), OFPort.of(1)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(2))},
			d.getAttachmentPoints(true));
	entity3.setLastSeenTimestamp(c.getTime());
	d = deviceManager.learnDeviceByEntity(entity3);
	// no conflicts, 1 and 3 will win
	assertArrayEquals(new SwitchPort[] { new SwitchPort(DatapathId.of(1L), OFPort.of(1)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(1)) },
			d.getAttachmentPoints());
	assertArrayEquals(new SwitchPort[] { new SwitchPort(DatapathId.of(1L), OFPort.of(1)),
			new SwitchPort(DatapathId.of(5L), OFPort.of(1)) },
			d.getAttachmentPoints(true));
}