use of com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel in project hub-alert by blackducksoftware.
the class EmailGlobalConfigurationModelSaveActionsTest method createDefaultConfigurationModel.
private ConfigurationModel createDefaultConfigurationModel() {
Map<String, ConfigurationFieldModel> fieldValuesMap = new HashMap<>();
ConfigurationFieldModel fromField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_FROM_KEY.getPropertyKey());
ConfigurationFieldModel hostField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_HOST_KEY.getPropertyKey());
ConfigurationFieldModel portField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey());
ConfigurationFieldModel authField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_AUTH_KEY.getPropertyKey());
ConfigurationFieldModel passwordField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_PASSWORD_KEY.getPropertyKey());
ConfigurationFieldModel userField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_USER_KEY.getPropertyKey());
ConfigurationFieldModel ehloField = ConfigurationFieldModel.create(EmailPropertyKeys.JAVAMAIL_EHLO_KEY.getPropertyKey());
fromField.setFieldValue(TEST_FROM);
hostField.setFieldValue(TEST_SMTP_HOST);
portField.setFieldValue(TEST_SMTP_PORT);
authField.setFieldValue(TEST_AUTH_REQUIRED);
passwordField.setFieldValue(TEST_AUTH_PASSWORD);
userField.setFieldValue(TEST_AUTH_USER);
ehloField.setFieldValue("true");
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_FROM_KEY.getPropertyKey(), fromField);
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_HOST_KEY.getPropertyKey(), hostField);
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey(), portField);
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_AUTH_KEY.getPropertyKey(), authField);
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_PASSWORD_KEY.getPropertyKey(), passwordField);
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_USER_KEY.getPropertyKey(), userField);
fieldValuesMap.put(EmailPropertyKeys.JAVAMAIL_EHLO_KEY.getPropertyKey(), ehloField);
return new ConfigurationModel(1L, 1L, "", "", ConfigContextEnum.GLOBAL, fieldValuesMap);
}
use of com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel in project hub-alert by blackducksoftware.
the class EmailGlobalConfigurationModelConverter method convert.
@Override
public Optional<EmailGlobalConfigModel> convert(ConfigurationModel globalConfigurationModel) {
Optional<EmailGlobalConfigModel> convertedModel = Optional.empty();
EmailGlobalConfigModel model = new EmailGlobalConfigModel();
try {
globalConfigurationModel.getField(EmailPropertyKeys.JAVAMAIL_FROM_KEY.getPropertyKey()).flatMap(ConfigurationFieldModel::getFieldValue).ifPresent(model::setSmtpFrom);
globalConfigurationModel.getField(EmailPropertyKeys.JAVAMAIL_HOST_KEY.getPropertyKey()).flatMap(ConfigurationFieldModel::getFieldValue).ifPresent(model::setSmtpHost);
globalConfigurationModel.getField(EmailPropertyKeys.JAVAMAIL_PORT_KEY.getPropertyKey()).flatMap(ConfigurationFieldModel::getFieldValue).map(Integer::valueOf).ifPresent(model::setSmtpPort);
globalConfigurationModel.getField(EmailPropertyKeys.JAVAMAIL_AUTH_KEY.getPropertyKey()).flatMap(ConfigurationFieldModel::getFieldValue).map(Boolean::valueOf).ifPresent(model::setSmtpAuth);
globalConfigurationModel.getField(EmailPropertyKeys.JAVAMAIL_USER_KEY.getPropertyKey()).flatMap(ConfigurationFieldModel::getFieldValue).ifPresent(model::setSmtpUsername);
globalConfigurationModel.getField(EmailPropertyKeys.JAVAMAIL_PASSWORD_KEY.getPropertyKey()).flatMap(ConfigurationFieldModel::getFieldValue).ifPresent(model::setSmtpPassword);
model.setAdditionalJavaMailProperties(createAdditionalProperties(globalConfigurationModel));
convertedModel = Optional.of(model);
} catch (NumberFormatException ex) {
logger.error("Error converting field model to concrete email configuration", ex);
}
return convertedModel;
}
use of com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel in project hub-alert by blackducksoftware.
the class BlackDuckGlobalApiActionTest method runApiActionTest.
private void runApiActionTest(ThrowingBiFunction<BlackDuckGlobalApiAction, FieldModel, FieldModel, AlertException> apiAction) throws AlertException {
TaskManager taskManager = new TaskManager();
BlackDuckProperties properties = Mockito.mock(BlackDuckProperties.class);
Mockito.when(properties.isConfigEnabled()).thenReturn(true);
FieldModel fieldModel = Mockito.mock(FieldModel.class);
Mockito.when(fieldModel.getId()).thenReturn("1");
Mockito.when(fieldModel.getFieldValue(Mockito.eq(ProviderDescriptor.KEY_PROVIDER_CONFIG_ENABLED))).thenReturn(Optional.of("true"));
String providerConfigName = "Test Provider Config";
Mockito.when(fieldModel.getFieldValue(Mockito.eq(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME))).thenReturn(Optional.of(providerConfigName));
BlackDuckAccumulator blackDuckAccumulator = Mockito.mock(BlackDuckAccumulator.class);
Mockito.when(blackDuckAccumulator.getTaskName()).thenReturn("accumulator-task");
Mockito.when(blackDuckAccumulator.getFormatedNextRunTime()).thenReturn(Optional.of("SOON"));
BlackDuckDataSyncTask blackDuckDataSyncTask = Mockito.mock(BlackDuckDataSyncTask.class);
Mockito.when(blackDuckDataSyncTask.getTaskName()).thenReturn("data-sync-task");
Mockito.when(blackDuckDataSyncTask.getFormatedNextRunTime()).thenReturn(Optional.of("SOON"));
ConfigurationModel configurationModel = Mockito.mock(ConfigurationModel.class);
Long providerConfigId = -1L;
Mockito.when(configurationModel.getConfigurationId()).thenReturn(providerConfigId);
Mockito.when(configurationModel.getDescriptorId()).thenReturn(providerConfigId);
ConfigurationFieldModel configurationFieldModel = ConfigurationFieldModel.create(ProviderDescriptor.KEY_PROVIDER_CONFIG_ENABLED);
configurationFieldModel.setFieldValue("true");
Mockito.when(configurationModel.getField(ProviderDescriptor.KEY_PROVIDER_CONFIG_ENABLED)).thenReturn(Optional.of(configurationFieldModel));
ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
Mockito.when(configurationModelConfigurationAccessor.getProviderConfigurationByName(Mockito.anyString())).thenReturn(Optional.of(configurationModel));
Mockito.when(configurationModelConfigurationAccessor.getConfigurationsByDescriptorKeyAndContext(Mockito.any(BlackDuckProviderKey.class), Mockito.eq(ConfigContextEnum.DISTRIBUTION))).thenReturn(List.of());
BlackDuckProviderKey blackDuckProviderKey = new BlackDuckProviderKey();
StatefulProvider statefulProvider = Mockito.mock(StatefulProvider.class);
BlackDuckProvider blackDuckProvider = Mockito.mock(BlackDuckProvider.class);
Mockito.when(blackDuckProvider.validate(configurationModel)).thenReturn(true);
Mockito.when(blackDuckProvider.createStatefulProvider(configurationModel)).thenReturn(statefulProvider);
Mockito.when(statefulProvider.getKey()).thenReturn(blackDuckProviderKey);
Mockito.when(statefulProvider.getConfigId()).thenReturn(providerConfigId);
Mockito.when(statefulProvider.isConfigEnabled()).thenReturn(true);
Mockito.when(statefulProvider.getConfigName()).thenReturn(providerConfigName);
Mockito.when(statefulProvider.getProperties()).thenReturn(properties);
Mockito.when(statefulProvider.getTasks()).thenReturn(List.of(blackDuckAccumulator, blackDuckDataSyncTask));
BlackDuckPropertiesFactory propertiesFactory = Mockito.mock(BlackDuckPropertiesFactory.class);
Mockito.when(propertiesFactory.createProperties((ConfigurationModel) Mockito.any())).thenReturn(properties);
ProviderDataAccessor providerDataAccessor = Mockito.mock(ProviderDataAccessor.class);
ConfigurationFieldModelConverter fieldModelConverter = Mockito.mock(ConfigurationFieldModelConverter.class);
Mockito.when(fieldModelConverter.convertToConfigurationModel(Mockito.any())).thenReturn(configurationModel);
BlackDuckCacheHttpClientCache blackDuckCacheHttpClientCache = Mockito.mock(BlackDuckCacheHttpClientCache.class);
ProviderSchedulingManager providerLifecycleManager = new ProviderSchedulingManager(List.of(blackDuckProvider), taskManager, null);
BlackDuckGlobalApiAction blackDuckGlobalApiAction = new BlackDuckGlobalApiAction(blackDuckProvider, providerLifecycleManager, providerDataAccessor, configurationModelConfigurationAccessor, blackDuckCacheHttpClientCache);
Optional<String> initialAccumulatorNextRunTime = taskManager.getNextRunTime(blackDuckAccumulator.getTaskName());
Optional<String> initialSyncNextRunTime = taskManager.getNextRunTime(blackDuckDataSyncTask.getTaskName());
assertTrue(initialAccumulatorNextRunTime.isEmpty());
assertTrue(initialSyncNextRunTime.isEmpty());
apiAction.apply(blackDuckGlobalApiAction, fieldModel);
Optional<String> accumulatorNextRunTime = taskManager.getNextRunTime(blackDuckAccumulator.getTaskName());
Optional<String> syncNextRunTime = taskManager.getNextRunTime(blackDuckDataSyncTask.getTaskName());
assertTrue(accumulatorNextRunTime.isPresent(), "The accumulator task next run time was not present");
assertTrue(syncNextRunTime.isPresent(), "The sync task next run time was not present");
}
use of com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel in project hub-alert by blackducksoftware.
the class DefaultConfigurationModelConfigurationAccessor method createConfiguration.
private ConfigurationModel createConfiguration(String descriptorKey, ConfigContextEnum context, Collection<ConfigurationFieldModel> configuredFields) {
Long descriptorId = getDescriptorIdOrThrowException(descriptorKey);
Long configContextId = getConfigContextIdOrThrowException(context);
OffsetDateTime currentTime = DateUtils.createCurrentDateTimestamp();
DescriptorConfigEntity descriptorConfigToSave = new DescriptorConfigEntity(descriptorId, configContextId, currentTime, currentTime);
DescriptorConfigEntity savedDescriptorConfig = descriptorConfigsRepository.save(descriptorConfigToSave);
ConfigurationModelMutable createdConfig = createEmptyConfigModel(descriptorId, savedDescriptorConfig.getId(), savedDescriptorConfig.getCreatedAt(), savedDescriptorConfig.getLastUpdated(), context);
if (configuredFields != null && !configuredFields.isEmpty()) {
List<FieldValueEntity> fieldValuesToSave = new ArrayList<>(configuredFields.size());
for (ConfigurationFieldModel configuredField : configuredFields) {
String fieldKey = configuredField.getFieldKey();
if (configuredField.isSet()) {
DefinedFieldEntity associatedField = definedFieldRepository.findFirstByKey(fieldKey).orElseThrow(() -> new AlertRuntimeException(String.format("FATAL: Field with key '%s' did not exist", fieldKey)));
for (String value : configuredField.getFieldValues()) {
FieldValueEntity newFieldValueEntity = new FieldValueEntity(createdConfig.getConfigurationId(), associatedField.getId(), encrypt(value, configuredField.isSensitive()));
fieldValuesToSave.add(newFieldValueEntity);
}
}
createdConfig.put(configuredField);
}
fieldValueRepository.saveAll(fieldValuesToSave);
}
return createdConfig;
}
use of com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel in project hub-alert by blackducksoftware.
the class DefaultConfigurationModelConfigurationAccessor method updateConfiguration.
/**
* @return the config after update
*/
@Override
public ConfigurationModel updateConfiguration(Long descriptorConfigId, Collection<ConfigurationFieldModel> configuredFields) throws AlertConfigurationException {
DescriptorConfigEntity descriptorConfigEntity = descriptorConfigsRepository.findById(descriptorConfigId).orElseThrow(() -> new AlertConfigurationException(String.format("Config with id '%d' did not exist", descriptorConfigId)));
List<FieldValueEntity> oldValues = fieldValueRepository.findByConfigId(descriptorConfigId);
fieldValueRepository.deleteAll(oldValues);
fieldValueRepository.flush();
ConfigurationModelMutable updatedConfig = createEmptyConfigModel(descriptorConfigEntity.getDescriptorId(), descriptorConfigEntity.getId(), descriptorConfigEntity.getCreatedAt(), descriptorConfigEntity.getLastUpdated(), descriptorConfigEntity.getContextId());
if (configuredFields != null && !configuredFields.isEmpty()) {
List<FieldValueEntity> fieldValuesToSave = new ArrayList<>(configuredFields.size());
for (ConfigurationFieldModel configFieldModel : configuredFields) {
String fieldKey = configFieldModel.getFieldKey();
Long fieldId = getFieldIdOrThrowException(fieldKey);
boolean isSensitive = isFieldSensitive(fieldKey);
for (String value : configFieldModel.getFieldValues()) {
FieldValueEntity newFieldValue = new FieldValueEntity(descriptorConfigId, fieldId, encrypt(value, isSensitive));
fieldValuesToSave.add(newFieldValue);
}
updatedConfig.put(configFieldModel);
}
fieldValueRepository.saveAll(fieldValuesToSave);
fieldValueRepository.flush();
}
descriptorConfigEntity.setLastUpdated(DateUtils.createCurrentDateTimestamp());
descriptorConfigsRepository.save(descriptorConfigEntity);
return updatedConfig;
}
Aggregations