@Test
public void testAutomaticAndManuallyAndContractCombination() {

	String automaticRoleValue = "test" + System.currentTimeMillis();
	IdmIdentityDto identity = getHelper().createIdentity(new GuardedString());
	IdmIdentityContractDto primary = getHelper().getPrimeContract(identity);
	IdmIdentityContractDto identityContactOne = getHelper().createContract(identity);
	IdmIdentityContractDto identityContactTwo = getHelper().createContract(identity);
	identity.setDescription(automaticRoleValue);
	identity.setTitleAfter(automaticRoleValue);
	identity.setTitleBefore(automaticRoleValue);
	identity = identityService.save(identity);
	IdmRoleDto role = getHelper().createRole();
	
	IdmIdentityRoleDto primaryOne = getHelper().createIdentityRole(primary, role);
	IdmIdentityRoleDto contractOneOne = getHelper().createIdentityRole(identityContactOne, role);
	IdmIdentityRoleDto contractOneTwo = getHelper().createIdentityRole(identityContactOne, role);
	IdmIdentityRoleDto contractOneThree = getHelper().createIdentityRole(identityContactOne, role);
	IdmIdentityRoleDto contractOneFour = getHelper().createIdentityRole(identityContactOne, role);
	IdmIdentityRoleDto contractTwoOne = getHelper().createIdentityRole(identityContactTwo, role);
	IdmIdentityRoleDto contractTwoTwo = getHelper().createIdentityRole(identityContactTwo, role);
	IdmAutomaticRoleAttributeDto automaticRoleOne = getHelper().createAutomaticRole(role.getId());
	getHelper().createAutomaticRoleRule(
			automaticRoleOne.getId(),
			AutomaticRoleAttributeRuleComparison.EQUALS,
			AutomaticRoleAttributeRuleType.IDENTITY,
			IdmIdentity_.description.getName(), null, automaticRoleValue);
	
	IdmAutomaticRoleAttributeDto automaticRoleTwo = getHelper().createAutomaticRole(role.getId());
	getHelper().createAutomaticRoleRule(
			automaticRoleTwo.getId(),
			AutomaticRoleAttributeRuleComparison.EQUALS,
			AutomaticRoleAttributeRuleType.IDENTITY,
			IdmIdentity_.titleAfter.getName(), null, automaticRoleValue);
	
	IdmAutomaticRoleAttributeDto automaticRoleThree = getHelper().createAutomaticRole(role.getId());
	getHelper().createAutomaticRoleRule(
			automaticRoleThree.getId(),
			AutomaticRoleAttributeRuleComparison.EQUALS,
			AutomaticRoleAttributeRuleType.IDENTITY,
			IdmIdentity_.titleBefore.getName(), null, automaticRoleValue);
	getHelper().recalculateAutomaticRoleByAttribute(automaticRoleOne.getId());
	getHelper().recalculateAutomaticRoleByAttribute(automaticRoleTwo.getId());
	getHelper().recalculateAutomaticRoleByAttribute(automaticRoleThree.getId());
	// 7 manually added and 9 automatically
	List<IdmIdentityRoleDto> roles = identityRoleService.findAllByIdentity(identity.getId());
	assertEquals(16, roles.size());
	IdmBulkActionDto bulkAction = this.findBulkAction(IdmIdentity.class, IdentityRoleByIdentityDeduplicationBulkAction.NAME);
	bulkAction.setIdentifiers(Sets.newHashSet(identity.getId()));
	
	IdmBulkActionDto processAction = bulkActionManager.processAction(bulkAction);
	checkResultLrt(processAction, 1l, null, null);
	roles = identityRoleService.findAllByIdentity(identity.getId());
	assertEquals(9, roles.size());
	roles = identityRoleService.findAllByContract(primary.getId());
	assertEquals(3, roles.size());
	for (IdmIdentityRoleDto identityRole : roles) {
		assertNotEquals(primaryOne.getId(), identityRole.getId());
	}
	roles = identityRoleService.findAllByContract(primary.getId());
	assertEquals(3, roles.size());
	for (IdmIdentityRoleDto identityRole : roles) {
		assertNotEquals(primaryOne.getId(), identityRole.getId());
	}
	roles = identityRoleService.findAllByContract(identityContactOne.getId());
	assertEquals(3, roles.size());
	for (IdmIdentityRoleDto identityRole : roles) {
		assertNotEquals(contractOneOne.getId(), identityRole.getId());
		assertNotEquals(contractOneTwo.getId(), identityRole.getId());
		assertNotEquals(contractOneThree.getId(), identityRole.getId());
		assertNotEquals(contractOneFour.getId(), identityRole.getId());
	}
	roles = identityRoleService.findAllByContract(identityContactTwo.getId());
	assertEquals(3, roles.size());
	for (IdmIdentityRoleDto identityRole : roles) {
		assertNotEquals(contractTwoOne.getId(), identityRole.getId());
		assertNotEquals(contractTwoTwo.getId(), identityRole.getId());
	}
}