Search in sources :

Example 6 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class CertificateActions method createWithoutChecks.

@Override
protected ActionResponse<CertificateModel> createWithoutChecks(CertificateModel resource) {
    String loggableAlias = escapeUtil.replaceWithUnderscore(resource.getAlias());
    logger.info("Importing certificate with alias {}", loggableAlias);
    try {
        CertificateModel certificateModel = importCertificate(resource);
        return new ActionResponse<>(HttpStatus.CREATED, certificateModel);
    } catch (AlertException ex) {
        String message = ex.getMessage();
        logger.error("There was an issue importing the certificate. {}", message);
        logger.debug(message, ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("There was an issue importing the certificate. %s", message));
    }
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 7 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class CertificateActions method importCertificate.

private CertificateModel importCertificate(CertificateModel certificateModel) throws AlertException {
    CustomCertificateModel certificateToStore = convertToDatabaseModel(certificateModel);
    try {
        CustomCertificateModel storedCertificate = certificateAccessor.storeCertificate(certificateToStore);
        trustStoreService.importCertificate(storedCertificate);
        return convertDatabaseModelToRestModel(storedCertificate);
    } catch (AlertException importException) {
        deleteByAlias(certificateToStore);
        throw importException;
    }
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 8 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class CertificateActions method validateCertificateFields.

private List<AlertFieldStatus> validateCertificateFields(CertificateModel certificateModel) {
    CustomCertificateModel convertedModel = convertToDatabaseModel(certificateModel);
    List<AlertFieldStatus> fieldErrors = new ArrayList<>();
    if (StringUtils.isBlank(certificateModel.getAlias())) {
        fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, "Alias cannot be empty."));
    } else {
        List<CustomCertificateModel> duplicateCertificates = certificateAccessor.getCertificates().stream().filter(certificate -> certificate.getAlias().equals(certificateModel.getAlias())).collect(Collectors.toList());
        if (duplicateCertificates.size() > 1) {
            fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, ERROR_DUPLICATE_ALIAS));
        } else if (duplicateCertificates.size() == 1) {
            boolean sameConfig = convertedModel.getNullableId() != null && duplicateCertificates.get(0).getNullableId().equals(convertedModel.getNullableId());
            if (!sameConfig) {
                fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, ERROR_DUPLICATE_ALIAS));
            }
        }
    }
    if (StringUtils.isBlank(certificateModel.getCertificateContent())) {
        fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_CERTIFICATE_CONTENT, "Certificate content cannot be empty."));
    } else {
        try {
            trustStoreService.validateCertificateContent(convertedModel);
        } catch (AlertException ex) {
            logger.error(ex.getMessage(), ex);
            fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_CERTIFICATE_CONTENT, String.format("Certificate content not valid: %s", ex.getMessage())));
        }
    }
    return fieldErrors;
}
Also used : AbstractResourceActions(com.synopsys.integration.alert.common.action.api.AbstractResourceActions) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) AlertTrustStoreManager(com.synopsys.integration.alert.component.certificates.AlertTrustStoreManager) IntegrationEscapeUtil(com.synopsys.integration.util.IntegrationEscapeUtil) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Logger(org.slf4j.Logger) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) CertificatesDescriptor(com.synopsys.integration.alert.component.certificates.CertificatesDescriptor) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) CertificatesDescriptorKey(com.synopsys.integration.alert.component.certificates.CertificatesDescriptorKey) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Component(org.springframework.stereotype.Component) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) CustomCertificateAccessor(com.synopsys.integration.alert.common.persistence.accessor.CustomCertificateAccessor) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 9 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class DefaultProcessingAuditAccessorTest method setAuditEntryFailureTest.

@Test
public void setAuditEntryFailureTest() {
    String testErrorMessage = "Uh oh, an error occurred!";
    String testExceptionMessage = "Something bad happened. Yikes...";
    Throwable testThrowable = new AlertException(testExceptionMessage);
    AuditEntryEntity testResultEntry = setAuditEntryStatusTest(AuditEntryStatus.FAILURE, (accessor, jobId, notificationIds) -> accessor.setAuditEntryFailure(jobId, notificationIds, testErrorMessage, testThrowable));
    assertEquals(testErrorMessage, testResultEntry.getErrorMessage());
    assertNotNull(testResultEntry.getErrorStackTrace(), "Expected the audit entry to contain an error stack trace");
    assertTrue(testResultEntry.getErrorStackTrace().contains(testExceptionMessage), "Expected the error stack trace to contain a specific message, but that message was missing");
}
Also used : AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Example 10 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class CertificateActionsTestIT method createExceptionTest.

@Test
public void createExceptionTest() throws Exception {
    String certificateContent = certTestUtil.readCertificateContents();
    CertificateModel certificate = new CertificateModel(TEST_ALIAS, certificateContent, DateUtils.createCurrentDateString(DateUtils.UTC_DATE_FORMAT_TO_MINUTE));
    AlertTrustStoreManager trustStoreService = Mockito.mock(AlertTrustStoreManager.class);
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasCreatePermission(Mockito.anyString(), Mockito.anyString())).thenReturn(Boolean.TRUE);
    Mockito.doThrow(new AlertException("Test exception")).when(trustStoreService).importCertificate(Mockito.any(CustomCertificateModel.class));
    CertificateActions certificateActions = new CertificateActions(new CertificatesDescriptorKey(), authorizationManager, certificateAccessor, trustStoreService);
    ActionResponse<CertificateModel> response = certificateActions.create(certificate);
    assertTrue(response.isError());
    assertTrue(certificateAccessor.getCertificates().isEmpty());
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) CertificatesDescriptorKey(com.synopsys.integration.alert.component.certificates.CertificatesDescriptorKey) AlertTrustStoreManager(com.synopsys.integration.alert.component.certificates.AlertTrustStoreManager) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)101 Test (org.junit.jupiter.api.Test)35 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)22 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)18 List (java.util.List)18 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)17 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)16 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)16 Optional (java.util.Optional)13 IssueTrackerModelHolder (com.synopsys.integration.alert.api.channel.issue.model.IssueTrackerModelHolder)12 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)12 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)11 HashMap (java.util.HashMap)11 IntegrationException (com.synopsys.integration.exception.IntegrationException)10 ArrayList (java.util.ArrayList)9 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)8 DistributionJobDetailsModel (com.synopsys.integration.alert.common.persistence.model.job.details.DistributionJobDetailsModel)8 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)8 UUID (java.util.UUID)8 IssueOperation (com.synopsys.integration.alert.common.channel.issuetracker.enumeration.IssueOperation)7