Search in sources :

Example 6 with ConfigContextEntity

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));
}
Also used : DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) ArrayList(java.util.ArrayList) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) DefinedFieldRepository(com.synopsys.integration.alert.database.configuration.repository.DefinedFieldRepository) ConfigContextRepository(com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository) DescriptorTypeRepository(com.synopsys.integration.alert.database.configuration.repository.DescriptorTypeRepository) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) RegisteredDescriptorRepository(com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 7 with ConfigContextEntity

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);
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 8 with ConfigContextEntity

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());
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 9 with ConfigContextEntity

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);
}
Also used : PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) HashMap(java.util.HashMap) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity)

Example 10 with ConfigContextEntity

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);
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DescriptorConfigEntity(com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) FieldValueEntity(com.synopsys.integration.alert.database.configuration.FieldValueEntity) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)25 Test (org.junit.jupiter.api.Test)20 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)17 DefinedFieldEntity (com.synopsys.integration.alert.database.configuration.DefinedFieldEntity)14 DescriptorConfigEntity (com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity)13 FieldValueEntity (com.synopsys.integration.alert.database.configuration.FieldValueEntity)8 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)8 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)7 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)5 PermissionMatrixRelation (com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation)5 OffsetDateTime (java.time.OffsetDateTime)5 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)4 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)4 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)3 DescriptorFieldRelation (com.synopsys.integration.alert.database.configuration.DescriptorFieldRelation)3 FieldContextRelation (com.synopsys.integration.alert.database.configuration.FieldContextRelation)3 ConfigContextRepository (com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository)3 RegisteredDescriptorRepository (com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository)3 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)3 ArrayList (java.util.ArrayList)3