use of org.junit.Assert.assertNotNull in project CzechIdMng by bcvsolutions.
the class DefaultAuthorizationManagerIntegrationTest method testCache.
@Test
@Transactional
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCache() {
// create and login identity
IdmIdentityDto identity = getHelper().createIdentity();
UUID mockIdentity = UUID.randomUUID();
// prepare role
IdmRoleDto role = getHelper().createRole();
IdmAuthorizationPolicyDto policy = getHelper().createBasePolicy(role.getId(), IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ);
getHelper().createIdentityRole(identity, role);
//
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
cacheManager.cacheValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity, new HashMap<>());
cacheManager.cacheValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity, new HashMap<>());
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
// without login
Set<String> permissions = manager.getPermissions(role);
Assert.assertTrue(permissions.isEmpty());
//
try {
getHelper().login(identity);
//
// new entity is not supported with cache, but permissions are evaluated
permissions = manager.getPermissions(new IdmRoleDto());
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
//
// load from db
permissions = manager.getPermissions(role);
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
// load from cache
permissions = manager.getPermissions(role);
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
// check cache content - one
ValueWrapper cacheValue = cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId());
List<IdmAuthorizationPolicyDto> cachedPolicies = (List) ((Map) cacheValue.get()).get(role.getClass());
Assert.assertEquals(1, cachedPolicies.size());
Assert.assertEquals(BasePermissionEvaluator.class.getCanonicalName(), ((IdmAuthorizationPolicyDto) cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, cachedPolicies.get(0)).get()).getEvaluatorType());
cacheValue = cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId());
permissions = (Set) ((Map) cacheValue.get()).get(role.getId());
Assert.assertEquals(2, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
//
// change policy => evict whole cache
policy.setPermissions(IdmBasePermission.AUTOCOMPLETE, IdmBasePermission.READ, IdmBasePermission.UPDATE);
authorizationPolicyService.save(policy);
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_DEFINITION_CACHE_NAME, policy.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
//
cacheManager.cacheValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity, new HashMap<>());
cacheManager.cacheValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity, new HashMap<>());
permissions = manager.getPermissions(role);
Assert.assertEquals(3, permissions.size());
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.AUTOCOMPLETE.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.READ.getName())));
Assert.assertTrue(permissions.stream().anyMatch(p -> p.equals(IdmBasePermission.UPDATE.getName())));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
} finally {
// evict logged identity cache only
logout();
}
// check cache is evicted only for logged identity
Assert.assertNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, identity.getId()));
Assert.assertNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, identity.getId()));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.AUTHORIZATION_POLICY_CACHE_NAME, mockIdentity));
Assert.assertNotNull(cacheManager.getValue(AuthorizationManager.PERMISSION_CACHE_NAME, mockIdentity));
}
use of org.junit.Assert.assertNotNull in project CzechIdMng by bcvsolutions.
the class IdentitySyncTest method testDefaultRoleToAllContracts.
@Test
public void testDefaultRoleToAllContracts() {
IdmIdentityDto identityDto = helper.createIdentity((GuardedString) null);
SysSystemDto system = initData(identityDto.getUsername());
Assert.assertNotNull(system);
IdmRoleDto defaultRole = helper.createRole();
//
SysSyncIdentityConfigDto config = doCreateSyncConfig(system);
// Set default role to sync configuration
config.setDefaultRole(defaultRole.getId());
config.setInactiveOwnerBehavior(SynchronizationInactiveOwnerBehaviorType.LINK);
config.setCreateDefaultContract(false);
config.setAssignDefaultRoleToAll(true);
config = (SysSyncIdentityConfigDto) syncConfigService.save(config);
//
// create default mapping for provisioning
helper.createMapping(system);
helper.createRoleSystem(defaultRole, system);
IdmIdentityContractDto validContract = helper.getPrimeContract(identityDto);
IdmIdentityContractDto validFutureContract = helper.createContract(identityDto, null, LocalDate.now().plusDays(10), null);
helper.createContract(identityDto, null, null, LocalDate.now().minusDays(1));
List<IdmIdentityContractDto> contracts = contractService.findAllByIdentity(identityDto.getId());
Assert.assertEquals(3, contracts.size());
helper.startSynchronization(config);
// Have to be in the success state, because default role will be assigned to the valid contracts.
SysSyncLogDto log = checkSyncLog(config, SynchronizationActionType.LINK, 1, OperationResultType.SUCCESS);
Assert.assertFalse(log.isRunning());
Assert.assertFalse(log.isContainsError());
IdmIdentityDto identity = identityService.get(identityDto);
List<IdmIdentityRoleDto> roles = identityRoleService.findAllByIdentity(identity.getId());
Assert.assertEquals(2, roles.size());
long identityRolesWithDefaultRole = roles.stream().filter(role -> role.getRole().equals(defaultRole.getId())).count();
Assert.assertEquals(2, identityRolesWithDefaultRole);
// Valid contract must have default role
long identityRolesWithValidContract = roles.stream().filter(role -> role.getIdentityContract().equals(validContract.getId())).count();
Assert.assertEquals(1, identityRolesWithValidContract);
// Future contract must have default role
long identityRolesWithFutureContract = roles.stream().filter(role -> role.getIdentityContract().equals(validFutureContract.getId())).count();
Assert.assertEquals(1, identityRolesWithFutureContract);
// Check only one identity account is created.
// Only one identity-account relation can exists, because only one
// current valid identity-role exists now (the second is future valid).
AccIdentityAccountFilter accountFilter = new AccIdentityAccountFilter();
accountFilter.setIdentityId(identity.getId());
List<AccIdentityAccountDto> identityAccounts = identityAccountService.find(accountFilter, null).getContent();
// !!!!To delete - Test doesn't pass on the Jenkins, we need to more information
if (identityAccounts.size() > 1) {
identityAccounts.forEach(identityAccountDtoOne -> {
System.out.println("Id - identityAccount: " + identityAccountDtoOne.getId());
System.out.println("Account: " + identityAccountDtoOne.getAccount());
System.out.println("RoleSystem: " + identityAccountDtoOne.getRoleSystem());
System.out.println("Identity: " + identityAccountDtoOne.getIdentity());
System.out.println("IdentityRole: " + identityAccountDtoOne.getIdentityRole());
System.out.println("----");
});
}
// !!!
Assert.assertEquals(1, identityAccounts.size());
// Delete log
syncLogService.delete(log);
syncConfigService.delete(config);
}
use of org.junit.Assert.assertNotNull in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method testDeleteEAVOnSlice.
@Test
public void testDeleteEAVOnSlice() {
IdmIdentityDto identity = helper.createIdentity();
String contractCode = "contract-one";
IdmContractSliceDto slice = helper.createContractSlice(identity, null, null, null, null);
slice.setContractCode(contractCode);
slice = contractSliceService.save(slice);
// Init form definition for identity-contract
IdmFormDefinitionDto definition = this.initIdentityContractFormDefinition();
// Create slice with EAV values
IdmFormInstanceDto formInstanceDto = formService.getFormInstance(slice, definition);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(0, formInstanceDto.getValues().size());
IdmFormAttributeDto attribute = formInstanceDto.getMappedAttributeByCode(NUMBER_OF_FINGERS);
formService.saveValues(slice, attribute, Lists.newArrayList(BigDecimal.TEN));
// We need to save slice for invoke save slice to the contract
slice = contractSliceService.save(slice);
formInstanceDto = formService.getFormInstance(slice, definition);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(1, formInstanceDto.getValues().size());
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) formInstanceDto.getValues().get(0).getValue()).longValue());
IdmContractSliceFilter filter = new IdmContractSliceFilter();
filter.setIdentity(identity.getId());
List<IdmContractSliceDto> results = contractSliceService.find(filter, null).getContent();
assertEquals(1, results.size());
IdmContractSliceDto createdSlice = results.get(0);
assertTrue(createdSlice.isValid());
assertEquals(null, createdSlice.getValidTill());
// Check created contract by that slice
IdmIdentityContractFilter contractFilter = new IdmIdentityContractFilter();
contractFilter.setIdentity(identity.getId());
List<IdmIdentityContractDto> resultsContract = //
contractService.find(filter, null).getContent().stream().filter(//
c -> contractService.get(c.getId()).getControlledBySlices()).collect(Collectors.toList());
//
assertEquals(1, resultsContract.size());
IdmIdentityContractDto contract = resultsContract.get(0);
assertEquals(slice.getContractValidFrom(), contract.getValidFrom());
assertEquals(slice.getContractValidTill(), contract.getValidTill());
assertTrue(contract.isValidNowOrInFuture());
IdmFormInstanceDto contractFormInstanceDto = formService.getFormInstance(contract);
Assert.assertNotNull(contractFormInstanceDto);
Assert.assertNotNull(contractFormInstanceDto.getFormDefinition());
Assert.assertEquals(1, contractFormInstanceDto.getValues().size());
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) contractFormInstanceDto.getValues().get(0).getValue()).longValue());
formService.saveValues(slice, attribute, null);
// We need to save slice for invoke save slice to the contract
slice = contractSliceService.save(slice);
contractFormInstanceDto = formService.getFormInstance(contract);
Assert.assertNotNull(contractFormInstanceDto);
Assert.assertNotNull(contractFormInstanceDto.getFormDefinition());
Assert.assertEquals(0, contractFormInstanceDto.getValues().size());
}
use of org.junit.Assert.assertNotNull in project CzechIdMng by bcvsolutions.
the class ContractSliceManagerTest method testModifiedEAVOnSlice.
@Test
public void testModifiedEAVOnSlice() {
IdmIdentityDto identity = helper.createIdentity();
String contractCode = "contract-one";
IdmContractSliceDto slice = helper.createContractSlice(identity, null, null, null, null);
slice.setContractCode(contractCode);
slice = contractSliceService.save(slice);
// Init form definition for identity-contract
IdmFormDefinitionDto definition = this.initIdentityContractFormDefinition();
// Create slice with EAV values
IdmFormInstanceDto formInstanceDto = formService.getFormInstance(slice, definition);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(0, formInstanceDto.getValues().size());
IdmFormAttributeDto attribute = formInstanceDto.getMappedAttributeByCode(NUMBER_OF_FINGERS);
formService.saveValues(slice, attribute, Lists.newArrayList(BigDecimal.TEN));
// We need to save slice for invoke save slice to the contract
slice = contractSliceService.save(slice);
formInstanceDto = formService.getFormInstance(slice, definition);
Assert.assertNotNull(formInstanceDto);
Assert.assertNotNull(formInstanceDto.getFormDefinition());
Assert.assertEquals(1, formInstanceDto.getValues().size());
Assert.assertEquals(BigDecimal.TEN.longValue(), ((BigDecimal) formInstanceDto.getValues().get(0).getValue()).longValue());
formService.saveValues(slice, attribute, Lists.newArrayList(BigDecimal.ONE));
// We need to save slice for invoke save slice to the contract
slice = contractSliceService.save(slice);
IdmContractSliceFilter filter = new IdmContractSliceFilter();
filter.setIdentity(identity.getId());
List<IdmContractSliceDto> results = contractSliceService.find(filter, null).getContent();
assertEquals(1, results.size());
IdmContractSliceDto createdSlice = results.get(0);
assertTrue(createdSlice.isValid());
assertEquals(null, createdSlice.getValidTill());
// Check created contract by that slice
IdmIdentityContractFilter contractFilter = new IdmIdentityContractFilter();
contractFilter.setIdentity(identity.getId());
List<IdmIdentityContractDto> resultsContract = //
contractService.find(filter, null).getContent().stream().filter(//
c -> contractService.get(c.getId()).getControlledBySlices()).collect(Collectors.toList());
//
assertEquals(1, resultsContract.size());
IdmIdentityContractDto contract = resultsContract.get(0);
assertEquals(slice.getContractValidFrom(), contract.getValidFrom());
assertEquals(slice.getContractValidTill(), contract.getValidTill());
assertTrue(contract.isValidNowOrInFuture());
IdmFormInstanceDto contractFormInstanceDto = formService.getFormInstance(contract);
Assert.assertNotNull(contractFormInstanceDto);
Assert.assertNotNull(contractFormInstanceDto.getFormDefinition());
Assert.assertEquals(1, contractFormInstanceDto.getValues().size());
Assert.assertEquals(BigDecimal.ONE.longValue(), ((BigDecimal) contractFormInstanceDto.getValues().get(0).getValue()).longValue());
}
use of org.junit.Assert.assertNotNull in project CzechIdMng by bcvsolutions.
the class IdentityPasswordProvisioningTest method testSendPasswordNotificationProvisioningFailed.
@Test
public void testSendPasswordNotificationProvisioningFailed() {
SysSystemDto system = initSystem();
IdmRoleDto role = initRole(system);
IdmIdentityDto identity = helper.createIdentity();
// => new password is sent for valid identity only
identity.setState(IdentityState.VALID);
identityService.save(identity);
// Break the system (change the password column to not exists) - we need make a exception.
IdmFormDefinitionDto savedFormDefinition = systemService.getConnectorFormDefinition(system);
IdmFormAttributeDto formAttribute = savedFormDefinition.getMappedAttributeByCode("passwordColumn");
formService.saveValues(system, formAttribute, Lists.newArrayList("not-exist-column-password"));
IdmIdentityRoleDto identityRole = helper.createIdentityRole(identity, role);
checkIdentityAccount(identity, identityRole, 1);
AccAccountDto account = accountService.getAccount(identity.getUsername(), system.getId());
Assert.assertNotNull(account);
Assert.assertFalse(account.isInProtection());
TestResource entityOnSystem = helper.findResource(account.getUid());
assertNull(entityOnSystem);
// Check for send password notification
IdmNotificationFilter notificationFilter = new IdmNotificationFilter();
notificationFilter.setTopic(AccModuleDescriptor.TOPIC_NEW_PASSWORD);
notificationFilter.setRecipient(identity.getUsername());
List<IdmNotificationLogDto> notifications = //
notificationLogService.find(notificationFilter, null).getContent().stream().filter(//
notification -> IdmEmailLog.NOTIFICATION_TYPE.equals(notification.getType())).collect(Collectors.toList());
assertEquals(0, notifications.size());
}
Aggregations