use of com.synopsys.integration.alert.common.rest.model.JobFieldModel in project hub-alert by blackducksoftware.
the class JobConfigActions method updateWithoutChecks.
@Override
protected ActionResponse<JobFieldModel> updateWithoutChecks(UUID id, JobFieldModel resource) {
try {
Optional<DistributionJobModel> jobModel = jobAccessor.getJobById(id);
if (jobModel.isPresent()) {
DistributionJobModel previousJob = jobModel.get();
JobFieldModel jobFieldModel = JobFieldModelPopulationUtils.createJobFieldModelWithDefaultProviderProjectState(previousJob);
Map<String, FieldModel> descriptorAndContextToPreviousFieldModel = new HashMap<>();
for (FieldModel previousJobFieldModel : jobFieldModel.getFieldModels()) {
descriptorAndContextToPreviousFieldModel.put(previousJobFieldModel.getDescriptorName() + previousJobFieldModel.getContext(), previousJobFieldModel);
}
Set<ConfigurationFieldModel> configurationFieldModels = new HashSet<>();
for (FieldModel fieldModel : resource.getFieldModels()) {
FieldModel beforeUpdateEventFieldModel = fieldModelProcessor.performBeforeUpdateAction(fieldModel);
String beforeFieldModelId = beforeUpdateEventFieldModel.getId();
Long fieldModelId = (StringUtils.isNotBlank(beforeFieldModelId)) ? Long.parseLong(beforeFieldModelId) : null;
Collection<ConfigurationFieldModel> updatedFieldModels = fieldModelProcessor.fillFieldModelWithExistingData(fieldModelId, beforeUpdateEventFieldModel);
configurationFieldModels.addAll(updatedFieldModels);
}
List<JobProviderProjectFieldModel> configuredProviderProjects = Optional.ofNullable(resource.getConfiguredProviderProjects()).orElse(List.of());
DistributionJobRequestModel jobRequestModel = createDistributionJobRequestModel(configurationFieldModels, configuredProviderProjects, previousJob.getCreatedAt(), DateUtils.createCurrentDateTimestamp());
DistributionJobModel savedJob = jobAccessor.updateJob(previousJob.getJobId(), jobRequestModel);
JobFieldModel savedJobFieldModel = JobFieldModelPopulationUtils.createJobFieldModel(savedJob, configuredProviderProjects);
Set<FieldModel> updatedFieldModels = new HashSet<>();
for (FieldModel fieldModel : savedJobFieldModel.getFieldModels()) {
FieldModel previousFieldModel = descriptorAndContextToPreviousFieldModel.get(fieldModel.getDescriptorName() + fieldModel.getContext());
FieldModel updatedModel = fieldModelProcessor.performAfterUpdateAction(previousFieldModel, fieldModel);
updatedFieldModels.add(updatedModel);
}
savedJobFieldModel.setFieldModels(updatedFieldModels);
return new ActionResponse<>(HttpStatus.OK, savedJobFieldModel);
} else {
return new ActionResponse<>(HttpStatus.NOT_FOUND);
}
} catch (AlertException ex) {
logger.error("Error creating job", ex);
return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}
}
use of com.synopsys.integration.alert.common.rest.model.JobFieldModel in project hub-alert by blackducksoftware.
the class JobConfigControllerTestIT method testValidateConfig.
@Test
@WithMockUser(roles = AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)
public void testValidateConfig() throws Exception {
final String urlPath = REQUEST_URL + "/validate";
ConfigurationModel providerGlobalConfig = addGlobalConfiguration(blackDuckProviderKey, Map.of(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME, List.of(DEFAULT_BLACK_DUCK_CONFIG), BlackDuckDescriptor.KEY_BLACKDUCK_URL, List.of("BLACKDUCK_URL"), BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, List.of("BLACKDUCK_API")));
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.post(urlPath).with(SecurityMockMvcRequestPostProcessors.user("admin").roles(AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)).with(SecurityMockMvcRequestPostProcessors.csrf());
JobFieldModel fieldModel = createTestJobFieldModel(null, null, providerGlobalConfig);
request.content(gson.toJson(fieldModel));
request.contentType(MEDIA_TYPE);
mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk());
}
use of com.synopsys.integration.alert.common.rest.model.JobFieldModel in project hub-alert by blackducksoftware.
the class JobConfigControllerTestIT method testUpdateConfig.
@Test
@WithMockUser(roles = AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)
public void testUpdateConfig() throws Exception {
ConfigurationModel providerGlobalConfig = addGlobalConfiguration(blackDuckProviderKey, Map.of(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME, List.of(DEFAULT_BLACK_DUCK_CONFIG), BlackDuckDescriptor.KEY_BLACKDUCK_URL, List.of(testProperties.getBlackDuckURL()), BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, List.of(testProperties.getBlackDuckAPIToken())));
JobFieldModel fieldModel = createTestJobFieldModel("1", "2", providerGlobalConfig);
Map<String, Collection<String>> fieldValueModels = new HashMap<>();
for (FieldModel newFieldModel : fieldModel.getFieldModels()) {
fieldValueModels.putAll(newFieldModel.getKeyToValues().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getValues())));
}
DistributionJobRequestModel jobRequestModel = createDistributionJobRequestModel(providerGlobalConfig.getConfigurationId());
DistributionJobModel distributionJobModel = addDistributionJob(jobRequestModel);
String configId = String.valueOf(distributionJobModel.getJobId());
String urlPath = REQUEST_URL + "/" + configId;
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.put(urlPath).with(SecurityMockMvcRequestPostProcessors.user("admin").roles(AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)).with(SecurityMockMvcRequestPostProcessors.csrf());
fieldModel.setJobId(configId);
request.content(gson.toJson(fieldModel));
request.contentType(MEDIA_TYPE);
mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isNoContent());
}
use of com.synopsys.integration.alert.common.rest.model.JobFieldModel in project hub-alert by blackducksoftware.
the class JobConfigControllerTestIT method createTestJobFieldModel.
private JobFieldModel createTestJobFieldModel(String channelId, String providerId, ConfigurationModel providerGlobalConfig) {
String descriptorName = ChannelKeys.SLACK.getUniversalKey();
String context = ConfigContextEnum.DISTRIBUTION.name();
FieldValueModel providerConfigField = new FieldValueModel(List.of(providerGlobalConfig.getConfigurationId().toString()), true);
FieldValueModel slackChannelName = new FieldValueModel(List.of("channelName"), true);
FieldValueModel frequency = new FieldValueModel(List.of(FrequencyType.DAILY.name()), true);
FieldValueModel name = new FieldValueModel(List.of("name"), true);
FieldValueModel provider = new FieldValueModel(List.of(blackDuckProviderKey.getUniversalKey()), true);
FieldValueModel channel = new FieldValueModel(List.of("channel_slack"), true);
FieldValueModel webhook = new FieldValueModel(List.of("http://slack_webhook_url"), true);
Map<String, FieldValueModel> fields = Map.of(SlackDescriptor.KEY_CHANNEL_NAME, slackChannelName, SlackDescriptor.KEY_WEBHOOK, webhook, ChannelDescriptor.KEY_NAME, name, ChannelDescriptor.KEY_PROVIDER_TYPE, provider, ProviderDescriptor.KEY_PROVIDER_CONFIG_ID, providerConfigField, ChannelDescriptor.KEY_CHANNEL_NAME, channel, ChannelDescriptor.KEY_FREQUENCY, frequency);
FieldModel fieldModel = new FieldModel(descriptorName, context, fields);
if (StringUtils.isNotBlank(channelId)) {
fieldModel.setId(channelId);
}
String bdDescriptorName = blackDuckProviderKey.getUniversalKey();
String bdContext = ConfigContextEnum.DISTRIBUTION.name();
FieldValueModel notificationType = new FieldValueModel(List.of("vulnerability"), true);
FieldValueModel formatType = new FieldValueModel(List.of(ProcessingType.DEFAULT.name()), true);
FieldValueModel filterByProject = new FieldValueModel(List.of("false"), true);
FieldValueModel projectNames = new FieldValueModel(List.of("project"), true);
Map<String, FieldValueModel> bdFields = Map.of(ProviderDescriptor.KEY_NOTIFICATION_TYPES, notificationType, ProviderDescriptor.KEY_PROVIDER_CONFIG_ID, providerConfigField, ProviderDescriptor.KEY_PROCESSING_TYPE, formatType, ProviderDescriptor.KEY_FILTER_BY_PROJECT, filterByProject, ProviderDescriptor.KEY_CONFIGURED_PROJECT, projectNames);
FieldModel bdFieldModel = new FieldModel(bdDescriptorName, bdContext, bdFields);
if (StringUtils.isNotBlank(providerId)) {
bdFieldModel.setId(providerId);
}
return new JobFieldModel(UUID.randomUUID().toString(), Set.of(fieldModel, bdFieldModel), List.of(new JobProviderProjectFieldModel("project", "href", false)));
}
use of com.synopsys.integration.alert.common.rest.model.JobFieldModel in project hub-alert by blackducksoftware.
the class JobConfigActions method readPageWithoutChecks.
@Override
public final ActionResponse<JobPagedModel> readPageWithoutChecks(Integer pageNumber, Integer pageSize, String searchTerm, Collection<String> permittedDescriptorsForSession) {
AlertPagedModel<DistributionJobModel> pageOfJobs = jobAccessor.getPageOfJobs(pageNumber, pageSize, searchTerm, permittedDescriptorsForSession);
List<DistributionJobModel> distributionJobModels = pageOfJobs.getModels();
List<JobFieldModel> jobFieldModels = new ArrayList<>(distributionJobModels.size());
for (DistributionJobModel distributionJobModel : distributionJobModels) {
JobFieldModel jobFieldModel = JobFieldModelPopulationUtils.createJobFieldModelWithDefaultProviderProjectState(distributionJobModel);
jobFieldModels.add(jobFieldModel);
}
JobPagedModel jobPagedModel = new JobPagedModel(pageOfJobs.getTotalPages(), pageOfJobs.getCurrentPage(), pageOfJobs.getPageSize(), jobFieldModels);
return new ActionResponse<>(HttpStatus.OK, jobPagedModel);
}
Aggregations