<commit_before>package eu.bcvsolutions.idm.acc.service.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import eu.bcvsolutions.idm.acc.TestHelper;
import eu.bcvsolutions.idm.acc.domain.AttributeMappingStrategyType;
import eu.bcvsolutions.idm.acc.domain.ProvisioningContext;
import eu.bcvsolutions.idm.acc.domain.ProvisioningEventType;
import eu.bcvsolutions.idm.acc.dto.ProvisioningAttributeDto;
import eu.bcvsolutions.idm.acc.dto.SysAttributeControlledValueDto;
import eu.bcvsolutions.idm.acc.dto.SysProvisioningArchiveDto;
import eu.bcvsolutions.idm.acc.dto.SysRoleSystemAttributeDto;
import eu.bcvsolutions.idm.acc.dto.SysRoleSystemDto;
import eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto;
import eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto;
import eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto;
import eu.bcvsolutions.idm.acc.dto.SysSystemDto;
import eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto;
import eu.bcvsolutions.idm.acc.dto.filter.SysAttributeControlledValueFilter;
import eu.bcvsolutions.idm.acc.dto.filter.SysProvisioningOperationFilter;
import eu.bcvsolutions.idm.acc.dto.filter.SysSchemaAttributeFilter;
import eu.bcvsolutions.idm.acc.service.api.SysAttributeControlledValueService;
import eu.bcvsolutions.idm.acc.service.api.SysProvisioningArchiveService;
import eu.bcvsolutions.idm.acc.service.api.SysRoleSystemAttributeService;
import eu.bcvsolutions.idm.acc.service.api.SysRoleSystemService;
import eu.bcvsolutions.idm.acc.service.api.SysSchemaAttributeService;
import eu.bcvsolutions.idm.acc.service.api.SysSchemaObjectClassService;
import eu.bcvsolutions.idm.acc.service.api.SysSystemAttributeMappingService;
import eu.bcvsolutions.idm.core.api.domain.ContractState;
import eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto;
import eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto;
import eu.bcvsolutions.idm.core.api.dto.IdmRoleDto;
import eu.bcvsolutions.idm.core.api.entity.AbstractEntity_;
import eu.bcvsolutions.idm.core.api.service.IdmIdentityContractService;
import eu.bcvsolutions.idm.core.api.service.IdmIdentityService;
import eu.bcvsolutions.idm.test.api.AbstractIntegrationTest;

/**
 * Test for provisioning merge
 *
 * @author <NAME>
 *
 */
public class ProvisioningMergeTest extends AbstractIntegrationTest {
	private static final String RIGHTS_ATTRIBUTE = "RIGHTS";
	private static final String ONE_VALUE = "ONE";
	private static final String TWO_VALUE = "TWO";

	@Autowired
	private SysSystemAttributeMappingService attributeMappingService;
	@Autowired
	private SysSchemaObjectClassService schemaService;
	@Autowired
	private SysSchemaAttributeService schemaAttributeService;
	@Autowired
	private SysRoleSystemAttributeService roleSystemAttributeService;
	@Autowired
	private SysAttributeControlledValueService attributeControlledValueService;
	@Autowired
	private TestHelper helper;
	@Autowired
	private SysRoleSystemService roleSystemService;
	@Autowired
	private SysProvisioningArchiveService provisioningArchiveService;
	@Autowired
	private IdmIdentityContractService identityContractService;
	@Autowired
	private IdmIdentityService identityService;

	@Test
	public void testAttribteControlledValues() {
		SysSystemDto system = helper.createSystem("test_resource");
		SysSystemMappingDto mapping = helper.createMapping(system);
		IdmRoleDto roleOne = helper.createRole();
		IdmRoleDto roleTwo = helper.createRole();
		IdmRoleDto roleOneDuplicated = helper.createRole();

		SysRoleSystemDto roleSystemOne = helper.createRoleSystem(roleOne, system);
		SysRoleSystemDto roleSystemTwo = helper.createRoleSystem(roleTwo, system);
		SysRoleSystemDto roleSystemOneDuplicated = helper.createRoleSystem(roleOneDuplicated, system);

		SysSchemaAttributeDto rightsSchemaAttribute = new SysSchemaAttributeDto();
		rightsSchemaAttribute.setObjectClass(mapping.getObjectClass());
		rightsSchemaAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsSchemaAttribute.setMultivalued(true);
		rightsSchemaAttribute.setClassType(String.class.getName());
		rightsSchemaAttribute.setReadable(true);
		rightsSchemaAttribute.setUpdateable(true);

		rightsSchemaAttribute = schemaAttributeService.save(rightsSchemaAttribute);

		SysSystemAttributeMappingDto rightsAttribute = new SysSystemAttributeMappingDto();
		rightsAttribute.setSchemaAttribute(rightsSchemaAttribute.getId());
		rightsAttribute.setSystemMapping(mapping.getId());
		rightsAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsAttribute.setStrategyType(AttributeMappingStrategyType.MERGE);
		rightsAttribute = attributeMappingService.save(rightsAttribute);

		SysRoleSystemAttributeDto roleAttributeOne = new SysRoleSystemAttributeDto();
		roleAttributeOne.setName(RIGHTS_ATTRIBUTE);
		roleAttributeOne.setRoleSystem(roleSystemOne.getId());
		roleAttributeOne.setStrategyType(AttributeMappingStrategyType.MERGE);
		roleAttributeOne.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeOne.setTransformToResourceScript("return '" + ONE_VALUE + "';");
		roleAttributeOne = roleSystemAttributeService.saveInternal(roleAttributeOne);

		List<Serializable> controlledAttributeValues = attributeMappingService
				.getControlledAttributeValues(system.getId(), mapping.getEntityType(), RIGHTS_ATTRIBUTE);

		assertNotNull(controlledAttributeValues);
		assertEquals(1, controlledAttributeValues.size());
		assertEquals(ONE_VALUE, controlledAttributeValues.get(0));

		SysRoleSystemAttributeDto roleAttributeOneDuplicated = new SysRoleSystemAttributeDto();
		roleAttributeOneDuplicated.setName(RIGHTS_ATTRIBUTE);
		roleAttributeOneDuplicated.setRoleSystem(roleSystemOneDuplicated.getId());
		roleAttributeOneDuplicated.setStrategyType(AttributeMappingStrategyType.MERGE);
		roleAttributeOneDuplicated.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeOneDuplicated.setTransformToResourceScript("return '" + TWO_VALUE + "';");
		roleAttributeOneDuplicated = roleSystemAttributeService.saveInternal(roleAttributeOneDuplicated);

		SysRoleSystemAttributeDto roleAttributeTwo = new SysRoleSystemAttributeDto();
		roleAttributeTwo.setName(RIGHTS_ATTRIBUTE);
		roleAttributeTwo.setRoleSystem(roleSystemTwo.getId());
		roleAttributeTwo.setStrategyType(AttributeMappingStrategyType.MERGE);
		roleAttributeTwo.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeTwo.setTransformToResourceScript("return '" + TWO_VALUE + "';");
		roleAttributeTwo = roleSystemAttributeService.saveInternal(roleAttributeTwo);

		controlledAttributeValues = attributeMappingService.getControlledAttributeValues(system.getId(),
				mapping.getEntityType(), RIGHTS_ATTRIBUTE);

		assertNotNull(controlledAttributeValues);
		assertEquals(2, controlledAttributeValues.size());
		assertTrue(controlledAttributeValues.contains(ONE_VALUE));
		assertTrue(controlledAttributeValues.contains(TWO_VALUE));
	}

	@Test
	public void testChangeValueDefinition() {
		SysSystemDto system = helper.createSystem("test_resource");
		SysSystemMappingDto mapping = helper.createMapping(system);
		IdmRoleDto roleOne = helper.createRole();
		IdmRoleDto roleTwo = helper.createRole();

		SysRoleSystemDto roleSystemOne = helper.createRoleSystem(roleOne, system);
		SysRoleSystemDto roleSystemTwo = helper.createRoleSystem(roleTwo, system);

		SysSchemaAttributeDto rightsSchemaAttribute = new SysSchemaAttributeDto();
		rightsSchemaAttribute.setObjectClass(mapping.getObjectClass());
		rightsSchemaAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsSchemaAttribute.setMultivalued(true);
		rightsSchemaAttribute.setClassType(String.class.getName());<commit_msg>#1362 added test for strategy conflict validation<commit_after>package eu.bcvsolutions.idm.acc.service.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import eu.bcvsolutions.idm.acc.TestHelper;
import eu.bcvsolutions.idm.acc.domain.AttributeMappingStrategyType;
import eu.bcvsolutions.idm.acc.domain.ProvisioningContext;
import eu.bcvsolutions.idm.acc.domain.ProvisioningEventType;
import eu.bcvsolutions.idm.acc.dto.ProvisioningAttributeDto;
import eu.bcvsolutions.idm.acc.dto.SysAttributeControlledValueDto;
import eu.bcvsolutions.idm.acc.dto.SysProvisioningArchiveDto;
import eu.bcvsolutions.idm.acc.dto.SysRoleSystemAttributeDto;
import eu.bcvsolutions.idm.acc.dto.SysRoleSystemDto;
import eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto;
import eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto;
import eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto;
import eu.bcvsolutions.idm.acc.dto.SysSystemDto;
import eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto;
import eu.bcvsolutions.idm.acc.dto.filter.SysAttributeControlledValueFilter;
import eu.bcvsolutions.idm.acc.dto.filter.SysProvisioningOperationFilter;
import eu.bcvsolutions.idm.acc.dto.filter.SysSchemaAttributeFilter;
import eu.bcvsolutions.idm.acc.exception.ProvisioningException;
import eu.bcvsolutions.idm.acc.service.api.SysAttributeControlledValueService;
import eu.bcvsolutions.idm.acc.service.api.SysProvisioningArchiveService;
import eu.bcvsolutions.idm.acc.service.api.SysRoleSystemAttributeService;
import eu.bcvsolutions.idm.acc.service.api.SysRoleSystemService;
import eu.bcvsolutions.idm.acc.service.api.SysSchemaAttributeService;
import eu.bcvsolutions.idm.acc.service.api.SysSchemaObjectClassService;
import eu.bcvsolutions.idm.acc.service.api.SysSystemAttributeMappingService;
import eu.bcvsolutions.idm.core.api.domain.ConceptRoleRequestOperation;
import eu.bcvsolutions.idm.core.api.domain.ContractState;
import eu.bcvsolutions.idm.core.api.domain.RoleRequestState;
import eu.bcvsolutions.idm.core.api.domain.RoleRequestedByType;
import eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto;
import eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto;
import eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto;
import eu.bcvsolutions.idm.core.api.dto.IdmRoleDto;
import eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto;
import eu.bcvsolutions.idm.core.api.entity.AbstractEntity_;
import eu.bcvsolutions.idm.core.api.service.IdmConceptRoleRequestService;
import eu.bcvsolutions.idm.core.api.service.IdmIdentityContractService;
import eu.bcvsolutions.idm.core.api.service.IdmIdentityService;
import eu.bcvsolutions.idm.core.api.service.IdmRoleRequestService;
import eu.bcvsolutions.idm.test.api.AbstractIntegrationTest;

/**
 * Test for provisioning merge
 *
 * @author <NAME>
 *
 */
public class ProvisioningMergeTest extends AbstractIntegrationTest {
	private static final String RIGHTS_ATTRIBUTE = "RIGHTS";
	private static final String ONE_VALUE = "ONE";
	private static final String TWO_VALUE = "TWO";

	@Autowired
	private SysSystemAttributeMappingService attributeMappingService;
	@Autowired
	private SysSchemaObjectClassService schemaService;
	@Autowired
	private SysSchemaAttributeService schemaAttributeService;
	@Autowired
	private SysRoleSystemAttributeService roleSystemAttributeService;
	@Autowired
	private SysAttributeControlledValueService attributeControlledValueService;
	@Autowired
	private TestHelper helper;
	@Autowired
	private SysRoleSystemService roleSystemService;
	@Autowired
	private SysProvisioningArchiveService provisioningArchiveService;
	@Autowired
	private IdmIdentityContractService identityContractService;
	@Autowired
	private IdmIdentityService identityService;
	@Autowired
	private IdmRoleRequestService roleRequestService;
	@Autowired
	private IdmConceptRoleRequestService conceptRoleRequestService;

	@Test
	public void testAttribteControlledValues() {
		SysSystemDto system = helper.createSystem("test_resource");
		SysSystemMappingDto mapping = helper.createMapping(system);
		IdmRoleDto roleOne = helper.createRole();
		IdmRoleDto roleTwo = helper.createRole();
		IdmRoleDto roleOneDuplicated = helper.createRole();

		SysRoleSystemDto roleSystemOne = helper.createRoleSystem(roleOne, system);
		SysRoleSystemDto roleSystemTwo = helper.createRoleSystem(roleTwo, system);
		SysRoleSystemDto roleSystemOneDuplicated = helper.createRoleSystem(roleOneDuplicated, system);

		SysSchemaAttributeDto rightsSchemaAttribute = new SysSchemaAttributeDto();
		rightsSchemaAttribute.setObjectClass(mapping.getObjectClass());
		rightsSchemaAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsSchemaAttribute.setMultivalued(true);
		rightsSchemaAttribute.setClassType(String.class.getName());
		rightsSchemaAttribute.setReadable(true);
		rightsSchemaAttribute.setUpdateable(true);

		rightsSchemaAttribute = schemaAttributeService.save(rightsSchemaAttribute);

		SysSystemAttributeMappingDto rightsAttribute = new SysSystemAttributeMappingDto();
		rightsAttribute.setSchemaAttribute(rightsSchemaAttribute.getId());
		rightsAttribute.setSystemMapping(mapping.getId());
		rightsAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsAttribute.setStrategyType(AttributeMappingStrategyType.MERGE);
		rightsAttribute = attributeMappingService.save(rightsAttribute);

		SysRoleSystemAttributeDto roleAttributeOne = new SysRoleSystemAttributeDto();
		roleAttributeOne.setName(RIGHTS_ATTRIBUTE);
		roleAttributeOne.setRoleSystem(roleSystemOne.getId());
		roleAttributeOne.setStrategyType(AttributeMappingStrategyType.MERGE);
		roleAttributeOne.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeOne.setTransformToResourceScript("return '" + ONE_VALUE + "';");
		roleAttributeOne = roleSystemAttributeService.saveInternal(roleAttributeOne);

		List<Serializable> controlledAttributeValues = attributeMappingService
				.getControlledAttributeValues(system.getId(), mapping.getEntityType(), RIGHTS_ATTRIBUTE);

		assertNotNull(controlledAttributeValues);
		assertEquals(1, controlledAttributeValues.size());
		assertEquals(ONE_VALUE, controlledAttributeValues.get(0));

		SysRoleSystemAttributeDto roleAttributeOneDuplicated = new SysRoleSystemAttributeDto();
		roleAttributeOneDuplicated.setName(RIGHTS_ATTRIBUTE);
		roleAttributeOneDuplicated.setRoleSystem(roleSystemOneDuplicated.getId());
		roleAttributeOneDuplicated.setStrategyType(AttributeMappingStrategyType.MERGE);
		roleAttributeOneDuplicated.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeOneDuplicated.setTransformToResourceScript("return '" + TWO_VALUE + "';");
		roleAttributeOneDuplicated = roleSystemAttributeService.saveInternal(roleAttributeOneDuplicated);

		SysRoleSystemAttributeDto roleAttributeTwo = new SysRoleSystemAttributeDto();
		roleAttributeTwo.setName(RIGHTS_ATTRIBUTE);
		roleAttributeTwo.setRoleSystem(roleSystemTwo.getId());
		roleAttributeTwo.setStrategyType(AttributeMappingStrategyType.MERGE);
		roleAttributeTwo.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeTwo.setTransformToResourceScript("return '" + TWO_VALUE + "';");
		roleAttributeTwo = roleSystemAttributeService.saveInternal(roleAttributeTwo);

		controlledAttributeValues = attributeMappingService.getControlledAttributeValues(system.getId(),
				mapping.getEntityType(), RIGHTS_ATTRIBUTE);

		assertNotNull(controlledAttributeValues);
		assertEquals(2, controlledAttributeValues.size());
		assertTrue(controlledAttributeValues.contains(ONE_VALUE));
		assertTrue(controlledAttributeValues.contains(TWO_VALUE));
	}

	@Test(expected = ProvisioningException.class)
	public void testAttribteConflictStrategy() {
		SysSystemDto system = helper.createSystem("test_resource");
		SysSystemMappingDto mapping = helper.createMapping(system);
		IdmRoleDto roleOne = helper.createRole();

		SysRoleSystemDto roleSystemOne = helper.createRoleSystem(roleOne, system);

		SysSchemaAttributeDto rightsSchemaAttribute = new SysSchemaAttributeDto();
		rightsSchemaAttribute.setObjectClass(mapping.getObjectClass());
		rightsSchemaAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsSchemaAttribute.setMultivalued(true);
		rightsSchemaAttribute.setClassType(String.class.getName());
		rightsSchemaAttribute.setReadable(true);
		rightsSchemaAttribute.setUpdateable(true);

		rightsSchemaAttribute = schemaAttributeService.save(rightsSchemaAttribute);

		SysSystemAttributeMappingDto rightsAttribute = new SysSystemAttributeMappingDto();
		rightsAttribute.setSchemaAttribute(rightsSchemaAttribute.getId());
		rightsAttribute.setSystemMapping(mapping.getId());
		rightsAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsAttribute.setStrategyType(AttributeMappingStrategyType.MERGE);
		rightsAttribute = attributeMappingService.save(rightsAttribute);

		SysRoleSystemAttributeDto roleAttributeOne = new SysRoleSystemAttributeDto();
		roleAttributeOne.setName(RIGHTS_ATTRIBUTE);
		roleAttributeOne.setRoleSystem(roleSystemOne.getId());
		roleAttributeOne.setStrategyType(AttributeMappingStrategyType.SET);
		roleAttributeOne.setSystemAttributeMapping(rightsAttribute.getId());
		roleAttributeOne.setTransformToResourceScript("return '" + ONE_VALUE + "';");
		roleAttributeOne = roleSystemAttributeService.saveInternal(roleAttributeOne);

		IdmIdentityDto identity = getHelper().createIdentity();
		IdmIdentityContractDto primeContract = getHelper().getPrimeContract(identity);

		getHelper().loginAdmin();

		// create request
		IdmRoleRequestDto request = new IdmRoleRequestDto();
		request.setApplicant(identity.getId());
		request.setExecuteImmediately(true);
		request.setRequestedByType(RoleRequestedByType.MANUALLY);
		request.setState(RoleRequestState.EXECUTED);
		request = roleRequestService.save(request);
		Assert.assertEquals(RoleRequestState.CONCEPT, request.getState());
		IdmConceptRoleRequestDto concept = new IdmConceptRoleRequestDto();
		concept = new IdmConceptRoleRequestDto();
		concept.setRoleRequest(request.getId());
		concept.setState(RoleRequestState.EXECUTED);
		concept.setOperation(ConceptRoleRequestOperation.ADD);
		concept.setRole(roleOne.getId());
		concept.setIdentityContract(primeContract.getId());
		concept = conceptRoleRequestService.save(concept);
		Assert.assertEquals(RoleRequestState.CONCEPT, concept.getState());

		getHelper().startRequestInternal(request, true, true);
		request = roleRequestService.get(request.getId());
	}

	@Test
	public void testChangeValueDefinition() {
		SysSystemDto system = helper.createSystem("test_resource");
		SysSystemMappingDto mapping = helper.createMapping(system);
		IdmRoleDto roleOne = helper.createRole();
		IdmRoleDto roleTwo = helper.createRole();

		SysRoleSystemDto roleSystemOne = helper.createRoleSystem(roleOne, system);
		SysRoleSystemDto roleSystemTwo = helper.createRoleSystem(roleTwo, system);

		SysSchemaAttributeDto rightsSchemaAttribute = new SysSchemaAttributeDto();
		rightsSchemaAttribute.setObjectClass(mapping.getObjectClass());
		rightsSchemaAttribute.setName(RIGHTS_ATTRIBUTE);
		rightsSchemaAttribute.setMultivalued(true);
		rightsSchemaAttribute.setClassType(String.class.getName());
{'fraction_non_alphanumeric': 0.08954008954008955, 'fraction_numerical': 0.0005087505087505087, 'mean_word_length': 8.450480769230769, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4159620', 'n_tokens_mistral': 5787, 'n_tokens_neox': 5623, 'n_words': 741}