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.");
}
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.");
}
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);
}
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());
}
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());
}
Aggregations