Search in sources :

Example 6 with DefinedFieldModel

use of com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel 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 DefinedFieldModel

use of com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel in project hub-alert by blackducksoftware.

the class AlertStartupInitializer method initializeConfiguration.

private void initializeConfiguration(Collection<DescriptorKey> descriptorKeys) {
    for (DescriptorKey descriptorKey : descriptorKeys) {
        logger.info(LINE_DIVIDER);
        logger.info("Descriptor: {}", descriptorKey.getUniversalKey());
        logger.info(LINE_DIVIDER);
        logger.info("  Starting Descriptor Initialization...");
        try {
            List<DefinedFieldModel> fieldsForDescriptor = descriptorAccessor.getFieldsForDescriptor(descriptorKey, ConfigContextEnum.GLOBAL).stream().sorted(Comparator.comparing(DefinedFieldModel::getKey)).collect(Collectors.toList());
            List<ConfigurationModel> foundConfigurationModels = fieldConfigurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(descriptorKey, ConfigContextEnum.GLOBAL);
            Map<String, ConfigurationFieldModel> existingConfiguredFields = new HashMap<>();
            foundConfigurationModels.forEach(config -> existingConfiguredFields.putAll(config.getCopyOfKeyToFieldMap()));
            Set<ConfigurationFieldModel> configurationModels = createFieldModelsFromDefinedFields(descriptorKey, fieldsForDescriptor, existingConfiguredFields);
            logConfiguration(configurationModels);
            updateConfigurationFields(descriptorKey, foundConfigurationModels, configurationModels);
        } catch (IllegalArgumentException | SecurityException | AlertException ex) {
            logger.error("error initializing descriptor", ex);
        } finally {
            logger.info("  Finished Descriptor Initialization...");
            logger.info(LINE_DIVIDER);
        }
    }
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) HashMap(java.util.HashMap) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 8 with DefinedFieldModel

use of com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel in project hub-alert by blackducksoftware.

the class AlertStartupInitializer method createFieldModelsFromDefinedFields.

private Set<ConfigurationFieldModel> createFieldModelsFromDefinedFields(DescriptorKey descriptorKey, List<DefinedFieldModel> fieldsForDescriptor, Map<String, ConfigurationFieldModel> existingConfiguredFields) {
    Set<ConfigurationFieldModel> configurationModels = new HashSet<>();
    logger.info("  ### Environment Variables ### ");
    for (DefinedFieldModel fieldModel : fieldsForDescriptor) {
        String key = fieldModel.getKey();
        String convertedKey = environmentUtility.convertKeyToProperty(descriptorKey, key);
        boolean hasEnvironmentValue = environmentUtility.hasEnvironmentValue(convertedKey);
        logger.info("    {}", convertedKey);
        logger.debug("         Environment Variable Found - {}", hasEnvironmentValue);
        String defaultValue = null;
        if (existingConfiguredFields.containsKey(key)) {
            Optional<String> fieldValue = existingConfiguredFields.get(key).getFieldValue();
            if (fieldValue.isPresent()) {
                defaultValue = fieldValue.get();
            }
        }
        environmentUtility.getEnvironmentValue(convertedKey, defaultValue).flatMap(value -> modelConverter.convertFromDefinedFieldModel(fieldModel, value, StringUtils.isNotBlank(value))).ifPresent(configurationModels::add);
    }
    return configurationModels;
}
Also used : DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) DescriptorAccessor(com.synopsys.integration.alert.common.persistence.accessor.DescriptorAccessor) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) DescriptorMap(com.synopsys.integration.alert.common.descriptor.DescriptorMap) ConfigurationFieldModelConverter(com.synopsys.integration.alert.common.persistence.util.ConfigurationFieldModelConverter) HashSet(java.util.HashSet) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) Map(java.util.Map) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) Order(org.springframework.core.annotation.Order) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Collectors(java.util.stream.Collectors) EnvironmentVariableProcessor(com.synopsys.integration.alert.environment.EnvironmentVariableProcessor) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) List(java.util.List) Component(org.springframework.stereotype.Component) FieldModelProcessor(com.synopsys.integration.alert.common.rest.FieldModelProcessor) Optional(java.util.Optional) SettingsUtility(com.synopsys.integration.alert.common.descriptor.accessor.SettingsUtility) Comparator(java.util.Comparator) FieldValueModel(com.synopsys.integration.alert.common.rest.model.FieldValueModel) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) HashSet(java.util.HashSet)

Example 9 with DefinedFieldModel

use of com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel in project hub-alert by blackducksoftware.

the class DefaultDescriptorAccessorTest method getFieldsForDescriptorTest.

@Test
public void getFieldsForDescriptorTest() {
    final String name = "name-test";
    final Long typeId = 1L;
    final ConfigContextEnum configContextEnum = ConfigContextEnum.GLOBAL;
    final ConfigContextEnum invalidConfigContextEnum = ConfigContextEnum.DISTRIBUTION;
    final String definedFieldsKey = "defined-field-key-test";
    Boolean isSensitive = Boolean.TRUE;
    RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(name, typeId);
    registeredDescriptorEntity.setId(1L);
    DescriptorKey descriptorKey = createDescriptorKey("descriptorKey-test");
    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.findFirstByName(descriptorKey.getUniversalKey())).thenReturn(Optional.of(registeredDescriptorEntity));
    Mockito.when(configContextRepository.findFirstByContext(configContextEnum.name())).thenReturn(Optional.of(configContextEntity));
    // Used to test the optional expression
    Mockito.when(configContextRepository.findFirstByContext(invalidConfigContextEnum.name())).thenReturn(Optional.empty());
    Mockito.when(configContextRepository.save(Mockito.any())).thenReturn(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.getFieldsForDescriptor(descriptorKey, configContextEnum);
    List<DefinedFieldModel> emptyConfigContextDefinedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, invalidConfigContextEnum);
    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));
    assertEquals(1, emptyConfigContextDefinedFieldModelList.size());
    DefinedFieldModel emptyConfigContextDefinedFieldModel = emptyConfigContextDefinedFieldModelList.get(0);
    assertEquals(definedFieldsKey, emptyConfigContextDefinedFieldModel.getKey());
    assertEquals(isSensitive, emptyConfigContextDefinedFieldModel.getSensitive());
    List<ConfigContextEnum> configContextList2 = new ArrayList<>(emptyConfigContextDefinedFieldModel.getContexts());
    assertEquals(invalidConfigContextEnum, configContextList2.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) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) 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 10 with DefinedFieldModel

use of com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel in project hub-alert by blackducksoftware.

the class DescriptorAccessorTestIT method getFieldsForDescriptorByIdTest.

@Test
public void getFieldsForDescriptorByIdTest() {
    final String field1Key = "field1";
    final String field2Key = "field2";
    DefinedFieldModel field1 = new DefinedFieldModel(field1Key, ConfigContextEnum.DISTRIBUTION, Boolean.FALSE);
    DefinedFieldModel field2 = new DefinedFieldModel(field2Key, ConfigContextEnum.DISTRIBUTION, Boolean.TRUE);
    descriptorMocker.registerDescriptor(DESCRIPTOR_NAME, DescriptorType.CHANNEL, Arrays.asList(field1, field2));
    RegisteredDescriptorModel registeredDescriptorModel = descriptorAccessor.getRegisteredDescriptorByKey(createDescriptorKey()).orElseThrow(() -> new AlertRuntimeException("This descriptor should exist"));
    List<DefinedFieldModel> descriptorFields = descriptorAccessor.getFieldsForDescriptorById(registeredDescriptorModel.getId(), ConfigContextEnum.DISTRIBUTION);
    assertEquals(2, descriptorFields.size());
    assertTrue(descriptorFields.contains(field1));
    assertTrue(descriptorFields.contains(field2));
}
Also used : DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) RegisteredDescriptorModel(com.synopsys.integration.alert.common.persistence.model.RegisteredDescriptorModel) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

DefinedFieldModel (com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel)13 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)10 Test (org.junit.jupiter.api.Test)10 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)9 DescriptorAccessor (com.synopsys.integration.alert.common.persistence.accessor.DescriptorAccessor)8 ConfigurationFieldModelConverter (com.synopsys.integration.alert.common.persistence.util.ConfigurationFieldModelConverter)7 EncryptionUtility (com.synopsys.integration.alert.common.security.EncryptionUtility)7 MockDescriptorAccessor (com.synopsys.integration.alert.mock.MockDescriptorAccessor)6 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)4 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)4 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)3 HashMap (java.util.HashMap)3 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)2 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)2 FieldUtility (com.synopsys.integration.alert.common.persistence.accessor.FieldUtility)2 RegisteredDescriptorModel (com.synopsys.integration.alert.common.persistence.model.RegisteredDescriptorModel)2 FieldValueModel (com.synopsys.integration.alert.common.rest.model.FieldValueModel)2 ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)2 DefinedFieldEntity (com.synopsys.integration.alert.database.configuration.DefinedFieldEntity)2 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)2