use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.
the class JobConfigActions method testWithoutChecks.
@Override
protected ValidationActionResponse testWithoutChecks(JobFieldModel resource) {
ValidationResponseModel responseModel;
String jobIdString = resource.getJobId();
UUID jobId = Optional.ofNullable(jobIdString).filter(StringUtils::isNotBlank).map(UUID::fromString).orElse(null);
try {
Collection<FieldModel> otherJobModels = new LinkedList<>();
FieldModel channelFieldModel = getChannelFieldModelAndPopulateOtherJobModels(resource, otherJobModels);
if (null != channelFieldModel) {
String channelDescriptorName = channelFieldModel.getDescriptorName();
Map<String, ConfigurationFieldModel> fields = createFieldsMap(channelFieldModel, otherJobModels);
// The custom message fields are not written to the database or defined fields in the database. Need to manually add them.
// TODO Create a mechanism to create the field accessor with a combination of fields in the database and fields that are not.
Optional<ConfigurationFieldModel> topicField = convertFieldToConfigurationField(channelFieldModel, FieldModelTestAction.KEY_CUSTOM_TOPIC);
Optional<ConfigurationFieldModel> messageField = convertFieldToConfigurationField(channelFieldModel, FieldModelTestAction.KEY_CUSTOM_MESSAGE);
topicField.ifPresent(model -> fields.put(FieldModelTestAction.KEY_CUSTOM_TOPIC, model));
messageField.ifPresent(model -> fields.put(FieldModelTestAction.KEY_CUSTOM_MESSAGE, model));
MessageResult providerTestResult = testProviderConfig(new FieldUtility(fields), jobIdString, channelFieldModel);
if (providerTestResult.hasErrors()) {
responseModel = ValidationResponseModel.fromStatusCollection(providerTestResult.getStatusMessage(), providerTestResult.getFieldStatuses());
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
List<BlackDuckProjectDetailsModel> projectFilterDetails = Optional.ofNullable(resource.getConfiguredProviderProjects()).orElse(List.of()).stream().map(jobProject -> new BlackDuckProjectDetailsModel(jobProject.getName(), jobProject.getHref())).collect(Collectors.toList());
DistributionJobModel testJobModel = distributionJobModelExtractor.convertToJobModel(jobId, fields, DateUtils.createCurrentDateTimestamp(), null, projectFilterDetails);
DistributionChannelTestAction distributionChannelTestAction = channelTestActionMap.findRequiredAction(channelDescriptorName);
MessageResult testActionResult = distributionChannelTestAction.testConfig(testJobModel, testJobModel.getName(), topicField.flatMap(ConfigurationFieldModel::getFieldValue).orElse(null), messageField.flatMap(ConfigurationFieldModel::getFieldValue).orElse(null));
List<AlertFieldStatus> resultFieldStatuses = testActionResult.getFieldStatuses();
List<AlertFieldStatus> allStatuses = Stream.concat(resultFieldStatuses.stream(), providerTestResult.fieldWarnings().stream()).collect(Collectors.toList());
responseModel = ValidationResponseModel.fromStatusCollection(testActionResult.getStatusMessage(), allStatuses);
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
responseModel = ValidationResponseModel.generalError("No field model of type channel was was sent to test.");
return new ValidationActionResponse(HttpStatus.BAD_REQUEST, responseModel);
} catch (IntegrationRestException e) {
logger.error(e.getMessage(), e);
return ValidationActionResponse.createResponseFromIntegrationRestException(e);
} catch (AlertFieldException e) {
logger.error("Test Error with field Errors", e);
responseModel = ValidationResponseModel.fromStatusCollection(e.getMessage(), e.getFieldErrors());
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (IntegrationException e) {
// TODO this is not necessarily a PKIX
responseModel = pkixErrorResponseFactory.createSSLExceptionResponse(e).orElse(ValidationResponseModel.generalError(e.getMessage()));
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (Exception e) {
logger.error(e.getMessage(), e);
responseModel = pkixErrorResponseFactory.createSSLExceptionResponse(e).orElse(ValidationResponseModel.generalError(e.getMessage()));
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
}
use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.
the class JobConfigActions method deleteWithoutChecks.
@Override
protected ActionResponse<JobFieldModel> deleteWithoutChecks(UUID id) {
try {
Optional<DistributionJobModel> job = jobAccessor.getJobById(id);
if (job.isPresent()) {
LinkedList<FieldModel> processedFieldModels = new LinkedList<>();
DistributionJobModel distributionJobModel = job.get();
JobFieldModel jobFieldModel = JobFieldModelPopulationUtils.createJobFieldModelWithDefaultProviderProjectState(distributionJobModel);
for (FieldModel fieldModel : jobFieldModel.getFieldModels()) {
FieldModel preProcessedFieldModel = fieldModelProcessor.performBeforeDeleteAction(fieldModel);
processedFieldModels.add(preProcessedFieldModel);
}
jobAccessor.deleteJob(distributionJobModel.getJobId());
for (FieldModel preProcessedFieldModel : processedFieldModels) {
fieldModelProcessor.performAfterDeleteAction(preProcessedFieldModel);
}
} else {
return new ActionResponse<>(HttpStatus.NOT_FOUND);
}
} catch (AlertException ex) {
logger.error("Error reading job", ex);
return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}
return new ActionResponse<>(HttpStatus.NO_CONTENT);
}
use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel 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.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.
the class JobEmailAddressValidatorTest method validateTest.
@Test
public void validateTest() {
UUID testJobId = UUID.randomUUID();
Long testBlackDuckGlobalConfigId = 33L;
String testValidEmailAddress1 = "valid1";
String testValidEmailAddress2 = "valid2";
String testValidEmailAddress3 = "valid3";
String testInvalidEmailAddress1 = "invalid1";
String testInvalidEmailAddress2 = "invalid2";
Set<String> testEmailAddresses = Set.of(testValidEmailAddress1, testInvalidEmailAddress1, testValidEmailAddress2, testInvalidEmailAddress2, testValidEmailAddress3);
DistributionJobModel distributionJobModel = Mockito.mock(DistributionJobModel.class);
Mockito.when(distributionJobModel.getBlackDuckGlobalConfigId()).thenReturn(testBlackDuckGlobalConfigId);
JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
Mockito.when(jobAccessor.getJobById(testJobId)).thenReturn(Optional.of(distributionJobModel));
ProviderUserModel providerUserModel = Mockito.mock(ProviderUserModel.class);
Optional<ProviderUserModel> optionalProviderUser = Optional.of(providerUserModel);
ProviderDataAccessor providerDataAccessor = Mockito.mock(ProviderDataAccessor.class);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testValidEmailAddress1)).thenReturn(optionalProviderUser);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testValidEmailAddress2)).thenReturn(optionalProviderUser);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testValidEmailAddress3)).thenReturn(optionalProviderUser);
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testInvalidEmailAddress1)).thenReturn(Optional.empty());
Mockito.when(providerDataAccessor.findFirstUserByEmailAddress(testBlackDuckGlobalConfigId, testInvalidEmailAddress2)).thenReturn(Optional.empty());
JobEmailAddressValidator validator = new JobEmailAddressValidator(jobAccessor, providerDataAccessor);
ValidatedEmailAddresses validatedEmailAddresses = validator.validate(testJobId, testEmailAddresses);
Set<String> validEmailAddresses = validatedEmailAddresses.getValidEmailAddresses();
assertEquals(3, validEmailAddresses.size());
assertValid(validEmailAddresses, testValidEmailAddress1);
assertValid(validEmailAddresses, testValidEmailAddress2);
assertValid(validEmailAddresses, testValidEmailAddress3);
Set<String> invalidEmailAddresses = validatedEmailAddresses.getInvalidEmailAddresses();
assertEquals(2, invalidEmailAddresses.size());
assertInvalid(invalidEmailAddresses, testInvalidEmailAddress1);
assertInvalid(invalidEmailAddresses, testInvalidEmailAddress2);
}
use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.
the class JobConfigControllerTestIT method testDeleteConfig.
@Test
@WithMockUser(roles = AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)
public void testDeleteConfig() throws Exception {
DistributionJobModel distributionJobModel = createAndSaveMockDistributionJob(-1L);
String jobId = String.valueOf(distributionJobModel.getJobId());
addGlobalConfiguration(blackDuckProviderKey, Map.of(BlackDuckDescriptor.KEY_BLACKDUCK_URL, List.of("BLACKDUCK_URL"), BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, List.of("BLACKDUCK_API")));
String urlPath = REQUEST_URL + "/" + jobId;
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.delete(urlPath).with(SecurityMockMvcRequestPostProcessors.user("admin").roles(AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)).with(SecurityMockMvcRequestPostProcessors.csrf());
mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isNoContent());
}
Aggregations