use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.
the class DefaultDescriptorAccessorTest method getFieldsForDescriptorByIdTest.
@Test
public void getFieldsForDescriptorByIdTest() {
final String name = "name-test";
final Long typeId = 1L;
final ConfigContextEnum configContextEnum = ConfigContextEnum.GLOBAL;
final String definedFieldsKey = "defined-field-key-test";
Boolean isSensitive = Boolean.TRUE;
final Long descriptorId = 1L;
RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(name, typeId);
registeredDescriptorEntity.setId(1L);
ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
configContextEntity.setId(3L);
DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(definedFieldsKey, isSensitive);
RegisteredDescriptorRepository registeredDescriptorRepository = Mockito.mock(RegisteredDescriptorRepository.class);
DefinedFieldRepository definedFieldRepository = Mockito.mock(DefinedFieldRepository.class);
ConfigContextRepository configContextRepository = Mockito.mock(ConfigContextRepository.class);
DescriptorTypeRepository descriptorTypeRepository = Mockito.mock(DescriptorTypeRepository.class);
Mockito.when(registeredDescriptorRepository.findById(descriptorId)).thenReturn(Optional.of(registeredDescriptorEntity));
Mockito.when(configContextRepository.findFirstByContext(configContextEnum.name())).thenReturn(Optional.of(configContextEntity));
Mockito.when(definedFieldRepository.findByDescriptorIdAndContext(Mockito.any(), Mockito.any())).thenReturn(List.of(definedFieldEntity));
DefaultDescriptorAccessor descriptorAccessor = new DefaultDescriptorAccessor(registeredDescriptorRepository, definedFieldRepository, configContextRepository, descriptorTypeRepository);
List<DefinedFieldModel> definedFieldModelList = descriptorAccessor.getFieldsForDescriptorById(descriptorId, configContextEnum);
assertEquals(1, definedFieldModelList.size());
DefinedFieldModel definedFieldModel = definedFieldModelList.get(0);
assertEquals(definedFieldsKey, definedFieldModel.getKey());
assertEquals(isSensitive, definedFieldModel.getSensitive());
List<ConfigContextEnum> configContextList = new ArrayList<>(definedFieldModel.getContexts());
assertEquals(configContextEnum, configContextList.get(0));
}
use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.
the class DefaultRoleAccessorTest method updatePermissionsForRole.
@Test
public void updatePermissionsForRole() throws Exception {
final String roleName = "roleName";
final String contextString = "context-test";
final String descriptorName = "descriptorName";
RoleEntity roleEntity = new RoleEntity(roleName, true);
roleEntity.setId(1L);
ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString);
configContextEntity.setId(1L);
RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L);
registeredDescriptorEntity.setId(1L);
PermissionKey permissionKey = new PermissionKey(contextString, descriptorName);
PermissionMatrixModel permissionMatrix = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()));
PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
Mockito.when(roleRepository.findByRoleName(Mockito.any())).thenReturn(Optional.of(roleEntity));
mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
PermissionMatrixModel permissionMatrixModel = authorizationUtility.updatePermissionsForRole(roleName, permissionMatrix);
Mockito.verify(permissionMatrixRepository).saveAll(Mockito.any());
assertFalse(permissionMatrixModel.isEmpty());
assertEquals(permissionMatrix, permissionMatrixModel);
}
use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.
the class DefaultRoleAccessorTest method createRoleWithPermissions.
@Test
public void createRoleWithPermissions() {
final String roleName = "roleName";
final String contextString = "context-test";
final String descriptorName = "descriptorName";
RoleEntity roleEntity = new RoleEntity(roleName, true);
roleEntity.setId(1L);
ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString);
configContextEntity.setId(1L);
RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L);
registeredDescriptorEntity.setId(1L);
PermissionKey permissionKey = new PermissionKey(contextString, descriptorName);
PermissionMatrixModel permissionMatrixModel = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()));
PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
Mockito.when(roleRepository.save(Mockito.any())).thenReturn(new RoleEntity(roleName, true));
mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
UserRoleModel userRoleModel = authorizationUtility.createRoleWithPermissions(roleName, permissionMatrixModel);
Mockito.verify(permissionMatrixRepository).deleteAll(Mockito.any());
assertEquals(roleName, userRoleModel.getName());
assertTrue(userRoleModel.isCustom());
assertEquals(permissionMatrixModel, userRoleModel.getPermissions());
}
use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.
the class DefaultRoleAccessor method createModelFromPermission.
private PermissionMatrixModel createModelFromPermission(List<PermissionMatrixRelation> permissions) {
Map<PermissionKey, Integer> permissionOperations = new HashMap<>();
if (null != permissions) {
for (PermissionMatrixRelation relation : permissions) {
Optional<String> optionalContext = configContextRepository.findById(relation.getContextId()).map(ConfigContextEntity::getContext);
Optional<String> optionalDescriptorName = registeredDescriptorRepository.findById(relation.getDescriptorId()).map(RegisteredDescriptorEntity::getName);
if (optionalDescriptorName.isPresent() && optionalContext.isPresent()) {
PermissionKey permissionKey = new PermissionKey(optionalContext.get(), optionalDescriptorName.get());
int existingPermissions = permissionOperations.getOrDefault(permissionKey, 0);
permissionOperations.put(permissionKey, existingPermissions | relation.getOperations());
}
}
}
return new PermissionMatrixModel(permissionOperations);
}
use of com.synopsys.integration.alert.database.configuration.ConfigContextEntity in project hub-alert by blackducksoftware.
the class DefaultConfigurationModelConfigurationAccessorTest method getConfigurationsByDescriptorKeyTest.
@Test
public void getConfigurationsByDescriptorKeyTest() {
final Long descriptorId = 3L;
final Long configurationId = 5L;
DescriptorKey descriptorKey = createDescriptorKey("descriptorKeyName");
DescriptorKey badDescriptorKey = createDescriptorKey("bad-descriptorKey");
RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity("name-test", 1L);
registeredDescriptorEntity.setId(2L);
DescriptorConfigEntity descriptorConfigEntity = new DescriptorConfigEntity(descriptorId, 4L, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp());
descriptorConfigEntity.setId(configurationId);
ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
FieldValueEntity fieldValueEntity = new FieldValueEntity(6L, 7L, fieldValue);
DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(fieldKey, false);
definedFieldEntity.setId(8L);
Mockito.when(descriptorConfigRepository.findByDescriptorName(descriptorKey.getUniversalKey())).thenReturn(List.of(descriptorConfigEntity));
Mockito.when(registeredDescriptorRepository.findFirstByName(badDescriptorKey.getUniversalKey())).thenReturn(Optional.empty());
setupCreatConfigMocks(descriptorConfigEntity, configContextEntity, fieldValueEntity, definedFieldEntity);
DefaultConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = new DefaultConfigurationModelConfigurationAccessor(registeredDescriptorRepository, definedFieldRepository, descriptorConfigRepository, configContextRepository, fieldValueRepository, encryptionUtility);
List<ConfigurationModel> configurationModelList = configurationModelConfigurationAccessor.getConfigurationsByDescriptorKey(descriptorKey);
List<ConfigurationModel> configurationModelListEmpty = configurationModelConfigurationAccessor.getConfigurationsByDescriptorKey(badDescriptorKey);
assertEquals(1, configurationModelList.size());
assertTrue(configurationModelListEmpty.isEmpty());
ConfigurationModel configurationModel = configurationModelList.get(0);
testConfigurationModel(configurationId, descriptorId, configurationModel);
}
Aggregations