use of com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException in project hub-alert by blackducksoftware.
the class ConfigurationManager method copyJob.
public void copyJob(String jobToCopy, String newJobName) throws IntegrationException {
String response = alertRequestUtility.executeGetRequest(String.format("/api/configuration/job?searchTerm=%s", jobToCopy), String.format("Could not copy the Job %s.", jobToCopy));
JobPagedModel jobModel = gson.fromJson(response, JobPagedModel.class);
JobFieldModel jobFieldModel = jobModel.getJobs().stream().findFirst().orElseThrow(() -> new AlertRuntimeException(String.format("Cannot find job %s", jobToCopy), null));
jobFieldModel.setJobId(null);
FieldModel channelFieldModel = jobFieldModel.getFieldModels().stream().filter(model -> ChannelKeys.getChannelKey(model.getDescriptorName()) != null).findFirst().orElseThrow(() -> new AlertRuntimeException("Cannot find channel field model", null));
Map<String, FieldValueModel> channelKeyToValues = new HashMap<>();
channelKeyToValues.putAll(channelFieldModel.getKeyToValues());
channelKeyToValues.put(ChannelDescriptor.KEY_NAME, new FieldValueModel(List.of(newJobName), true));
channelFieldModel.setKeyToValues(channelKeyToValues);
String jobConfigBody = gson.toJson(jobFieldModel);
alertRequestUtility.executePostRequest("/api/configuration/job", jobConfigBody, String.format("Could not create the Job %s.", newJobName));
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException in project hub-alert by blackducksoftware.
the class DefaultConfigurationModelConfigurationAccessor method createConfigModel.
private ConfigurationModelMutable createConfigModel(Long descriptorId, Long configId, OffsetDateTime createdAt, OffsetDateTime lastUpdated, Long contextId) {
String configContext = getContextById(contextId);
String createdAtFormatted = DateUtils.formatDate(createdAt, DateUtils.UTC_DATE_FORMAT_TO_MINUTE);
String lastUpdatedFormatted = DateUtils.formatDate(lastUpdated, DateUtils.UTC_DATE_FORMAT_TO_MINUTE);
ConfigurationModelMutable newModel = new ConfigurationModelMutable(descriptorId, configId, createdAtFormatted, lastUpdatedFormatted, configContext);
List<FieldValueEntity> fieldValueEntities = fieldValueRepository.findByConfigId(configId);
for (FieldValueEntity fieldValueEntity : fieldValueEntities) {
DefinedFieldEntity definedFieldEntity = definedFieldRepository.findById(fieldValueEntity.getFieldId()).orElseThrow(() -> new AlertRuntimeException("Field Id missing from the database"));
String fieldKey = definedFieldEntity.getKey();
ConfigurationFieldModel fieldModel = BooleanUtils.isTrue(definedFieldEntity.getSensitive()) ? ConfigurationFieldModel.createSensitive(fieldKey) : ConfigurationFieldModel.create(fieldKey);
String decryptedValue = decrypt(fieldValueEntity.getValue(), fieldModel.isSensitive());
fieldModel.setFieldValue(decryptedValue);
newModel.put(fieldModel);
}
return newModel;
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method addUser.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserModel addUser(UserModel user, boolean passwordEncoded) throws AlertConfigurationException {
String username = user.getName();
Optional<UserEntity> userWithSameUsername = userRepository.findByUserName(username);
if (userWithSameUsername.isPresent()) {
throw new AlertConfigurationException(String.format("A user with username '%s' is already present", username));
}
String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword());
AuthenticationTypeDetails authenticationType = authenticationTypeAccessor.getAuthenticationTypeDetails(user.getAuthenticationType()).orElseThrow(() -> new AlertRuntimeException("Cannot find Authentication Type."));
UserEntity newEntity = new UserEntity(username, password, user.getEmailAddress(), authenticationType.getId());
UserEntity savedEntity = userRepository.save(newEntity);
UserModel model = createModel(savedEntity);
roleAccessor.updateUserRoles(model.getId(), user.getRoles());
return model;
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException in project hub-alert by blackducksoftware.
the class AbstractJobResourceActions method checkContextAndDescriptorKey.
private boolean checkContextAndDescriptorKey(FieldModel fieldModel, BiFunction<ConfigContextEnum, DescriptorKey, Boolean> permissionChecker) {
ConfigContextEnum configContextEnum = ConfigContextEnum.valueOf(fieldModel.getContext());
DescriptorKey descriptorKey = descriptorMap.getDescriptorKey(fieldModel.getDescriptorName()).orElseThrow(() -> new AlertRuntimeException("Could not find DescriptorKey for: " + fieldModel.getDescriptorName()));
return permissionChecker.apply(configContextEnum, descriptorKey);
}
use of com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException in project hub-alert by blackducksoftware.
the class JobConfigActionsTest method testTest.
@Test
public void testTest() throws Exception {
JobConfigActions jobConfigActionsForTest = new JobConfigActions(mockedAuthorizationManager, mockedDescriptorAccessor, mockedConfigurationModelConfigurationAccessor, mockedJobAccessor, mockedFieldModelProcessor, mockedDescriptorProcessor, mockedConfigurationFieldModelConverter, mockedGlobalConfigExistsValidator, mockedPkixErrorResponseFactory, descriptorMap, (id, list) -> {
}, List.of(createChannelDistributionTestAction()), mockedJobModelExtractor);
fieldModel.setId("testID");
Mockito.when(mockedDescriptorProcessor.retrieveDescriptor(Mockito.any())).thenReturn(Optional.of(descriptor));
Mockito.when(mockedFieldModelProcessor.createCustomMessageFieldModel(Mockito.any())).thenReturn(fieldModel);
ValidationActionResponse validationActionResponse = jobConfigActionsForTest.test(jobFieldModel);
assertTrue(validationActionResponse.isSuccessful(), "Validation response was not successful");
assertEquals(HttpStatus.OK, validationActionResponse.getHttpStatus());
assertTrue(validationActionResponse.hasContent(), "Missing content");
ValidationResponseModel validationResponseModel = validationActionResponse.getContent().orElseThrow(() -> new AlertRuntimeException("Missing validation response"));
assertFalse(validationResponseModel.hasErrors(), "Validation response had errors");
}
Aggregations