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));
}
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);
}
}
}
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;
}
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));
}
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));
}
Aggregations