Search in sources :

Example 11 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class CommonConfigHandler method postConfig.

public ResponseEntity<String> postConfig(final R restModel) {
    if (restModel == null) {
        return createResponse(HttpStatus.BAD_REQUEST, "", "Required request body is missing " + configRestModelClass.getSimpleName());
    }
    if (!configActions.doesConfigExist(restModel.getId())) {
        try {
            configActions.validateConfig(restModel);
            configActions.configurationChangeTriggers(restModel);
            try {
                final D updatedEntity = configActions.saveConfig(restModel);
                return createResponse(HttpStatus.CREATED, updatedEntity.getId(), "Created");
            } catch (final AlertException e) {
                logger.error(e.getMessage(), e);
                return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, restModel.getId(), e.getMessage());
            }
        } catch (final AlertFieldException e) {
            final ResponseBodyBuilder responseBuilder = new ResponseBodyBuilder(configActions.getObjectTransformer().stringToLong(restModel.getId()), "There were errors with the configuration.");
            responseBuilder.putErrors(e.getFieldErrors());
            return new ResponseEntity<>(responseBuilder.build(), HttpStatus.BAD_REQUEST);
        }
    }
    return createResponse(HttpStatus.CONFLICT, restModel.getId(), "Provided id must not be in use. To update an existing configuration, use PUT.");
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 12 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class CommonConfigHandler method putConfig.

public ResponseEntity<String> putConfig(final R restModel) {
    if (restModel == null) {
        return createResponse(HttpStatus.BAD_REQUEST, "", "Required request body is missing " + configRestModelClass.getSimpleName());
    }
    if (configActions.doesConfigExist(restModel.getId())) {
        try {
            configActions.validateConfig(restModel);
            configActions.configurationChangeTriggers(restModel);
            try {
                final D updatedEntity = configActions.saveNewConfigUpdateFromSavedConfig(restModel);
                return createResponse(HttpStatus.ACCEPTED, updatedEntity.getId(), "Updated");
            } catch (final AlertException e) {
                logger.error(e.getMessage(), e);
                return createResponse(HttpStatus.INTERNAL_SERVER_ERROR, restModel.getId(), e.getMessage());
            }
        } catch (final AlertFieldException e) {
            final ResponseBodyBuilder responseBuilder = new ResponseBodyBuilder(configActions.getObjectTransformer().stringToLong(restModel.getId()), "There were errors with the configuration.");
            responseBuilder.putErrors(e.getFieldErrors());
            return new ResponseEntity<>(responseBuilder.build(), HttpStatus.BAD_REQUEST);
        }
    }
    return createResponse(HttpStatus.BAD_REQUEST, restModel.getId(), "No configuration with the specified id.");
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) AlertException(com.blackducksoftware.integration.hub.alert.exception.AlertException) ResponseBodyBuilder(com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)

Example 13 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class GlobalSchedulingConfigActionsTest method validateConfigWithValidArgsTest.

@Test
public void validateConfigWithValidArgsTest() {
    final GlobalSchedulingConfigActions configActions = new GlobalSchedulingConfigActions(null, null, null, null, new ObjectTransformer(), null, null, null);
    final GlobalSchedulingConfigRestModel restModel = getGlobalRestModelMockUtil().createGlobalRestModel();
    String validationString = null;
    AlertFieldException caughtException = null;
    try {
        validationString = configActions.validateConfig(restModel);
    } catch (final AlertFieldException e) {
        caughtException = e;
    }
    assertNull(caughtException);
    assertEquals("Valid", validationString);
}
Also used : ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Test(org.junit.Test) GlobalActionsTest(com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)

Example 14 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class GlobalSchedulingConfigActionsTest method testInvalidConfig.

@Test
@Override
public void testInvalidConfig() {
    final String invalidCron = "invalid";
    final GlobalSchedulingConfigActions configActions = new GlobalSchedulingConfigActions(null, null, null, null, new ObjectTransformer(), null, null, null);
    GlobalSchedulingConfigRestModel restModel = new GlobalSchedulingConfigRestModel("1", invalidCron, invalidCron, invalidCron, invalidCron, invalidCron);
    AlertFieldException caughtException = null;
    try {
        configActions.validateConfig(restModel);
    } catch (final AlertFieldException e) {
        caughtException = e;
    }
    assertNotNull(caughtException);
    assertEquals("Must be a number between 0 and 23", caughtException.getFieldErrors().get("dailyDigestHourOfDay"));
    assertEquals("Must be a number between 1 and 7", caughtException.getFieldErrors().get("purgeDataFrequencyDays"));
    assertEquals(2, caughtException.getFieldErrors().size());
    restModel = new GlobalSchedulingConfigRestModel("1", "-1", "-1", "-1", "-1", "-1");
    caughtException = null;
    try {
        configActions.validateConfig(restModel);
    } catch (final AlertFieldException e) {
        caughtException = e;
    }
    assertNotNull(caughtException);
    assertEquals("Must be a number between 0 and 23", caughtException.getFieldErrors().get("dailyDigestHourOfDay"));
    assertEquals("Must be a number between 1 and 7", caughtException.getFieldErrors().get("purgeDataFrequencyDays"));
    assertEquals(2, caughtException.getFieldErrors().size());
    restModel = new GlobalSchedulingConfigRestModel("1", "100000", "100000", "100000", "100000", "100000");
    caughtException = null;
    try {
        configActions.validateConfig(restModel);
    } catch (final AlertFieldException e) {
        caughtException = e;
    }
    assertNotNull(caughtException);
    assertEquals("Must be a number less than 24", caughtException.getFieldErrors().get("dailyDigestHourOfDay"));
    assertEquals("Must be a number less than 8", caughtException.getFieldErrors().get("purgeDataFrequencyDays"));
    assertEquals(2, caughtException.getFieldErrors().size());
    restModel = new GlobalSchedulingConfigRestModel("1", "", "", "", "", "");
    caughtException = null;
    try {
        configActions.validateConfig(restModel);
    } catch (final AlertFieldException e) {
        caughtException = e;
    }
    assertNotNull(caughtException);
    assertEquals("Must be a number between 0 and 23", caughtException.getFieldErrors().get("dailyDigestHourOfDay"));
    assertEquals("Must be a number between 1 and 7", caughtException.getFieldErrors().get("purgeDataFrequencyDays"));
    assertEquals(2, caughtException.getFieldErrors().size());
}
Also used : ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Test(org.junit.Test) GlobalActionsTest(com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)

Example 15 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class CommonConfigHandlerTest method postWithInvalidConfigTest.

@Test
public void postWithInvalidConfigTest() throws AlertFieldException {
    final CommonDistributionConfigActions configActions = Mockito.mock(CommonDistributionConfigActions.class);
    final CommonConfigHandler<CommonDistributionConfigEntity, CommonDistributionConfigRestModel, CommonDistributionRepositoryWrapper> handler = new CommonConfigHandler<>(CommonDistributionConfigEntity.class, CommonDistributionConfigRestModel.class, configActions, objectTransformer);
    Mockito.when(configActions.doesConfigExist(Mockito.anyString())).thenReturn(false);
    Mockito.when(configActions.validateConfig(Mockito.any())).thenThrow(new AlertFieldException(Collections.emptyMap()));
    Mockito.when(configActions.getObjectTransformer()).thenReturn(objectTransformer);
    final CommonDistributionConfigRestModel restModel = mockCommonDistributionRestModel.createRestModel();
    final ResponseEntity<String> response = handler.postConfig(restModel);
    assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
Also used : CommonDistributionConfigActions(com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions) CommonDistributionRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper) CommonDistributionConfigRestModel(com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel) CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Test(org.junit.Test)

Aggregations

AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)17 Test (org.junit.Test)8 CommonDistributionConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity)5 CommonDistributionRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper)4 CommonDistributionConfigActions (com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions)4 CommonDistributionConfigRestModel (com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel)4 ValidationResults (com.blackducksoftware.integration.validator.ValidationResults)4 HashMap (java.util.HashMap)4 GlobalActionsTest (com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)3 ResponseBodyBuilder (com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)3 AbstractValidator (com.blackducksoftware.integration.validator.AbstractValidator)3 FieldEnum (com.blackducksoftware.integration.validator.FieldEnum)3 ValidationResult (com.blackducksoftware.integration.validator.ValidationResult)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)2 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)2 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)2 IntLogger (com.blackducksoftware.integration.log.IntLogger)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1