protected Response handleConstraintViolationException(ConstraintViolationException cve) { ViolationReport report = new ViolationReport(); for (ConstraintViolation cv : cve.getConstraintViolations()) { report.addConstraintViolation(new RestConstraintViolation( cv.getPropertyPath().toString(), cv.getMessage(), cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString())); } // TODO for now just do xml output return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build(); } }
protected Response handleConstraintViolationException(ConstraintViolationException cve) { ViolationReport report = new ViolationReport(); for (ConstraintViolation cv : cve.getConstraintViolations()) { report.addConstraintViolation(new RestConstraintViolation( cv.getPropertyPath().toString(), cv.getMessage(), cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString())); } // TODO for now just do xml output return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(report).type(ContentType.TEXT_XML_UTF_8).build(); } }
@ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) ResponseMessage handleConstraintViolationException(ConstraintViolationException e) { SimpleValidateResults results = new SimpleValidateResults(); for (ConstraintViolation<?> violation : e.getConstraintViolations()) { results.addResult(violation.getPropertyPath().toString(), violation.getMessage()); } List<ValidateResults.Result> errorResults = results.getResults(); return ResponseMessage .error(400, errorResults.isEmpty() ? "" : errorResults.get(0).getMessage()) .result(errorResults); }
@ExceptionHandler(ValidationException.class) @ResponseBody public Object badArgumentHandler(ValidationException e) { e.printStackTrace(); if (e instanceof ConstraintViolationException) { ConstraintViolationException exs = (ConstraintViolationException) e; Set<ConstraintViolation<?>> violations = exs.getConstraintViolations(); for (ConstraintViolation<?> item : violations) { String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage(); return ResponseUtil.fail(402, message); } } return ResponseUtil.badArgumentValue(); }
private static ErrorResponse constraintViolationExceptionToErrorResponse(ConstraintViolationException exception) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setStatus(Response.Status.BAD_REQUEST.getStatusCode()); errorResponse.setMessage("Validation failed, check below errors for detail."); List<ValidationError> validationErrors = new ArrayList<>(); exception.getConstraintViolations().forEach(e -> validationErrors.add(new ValidationError(getViolationPath(e), e.getMessage(), getViolationInvalidValue(e.getInvalidValue())))); errorResponse.setValidationErrors(validationErrors); return errorResponse; }
@Test(expected = ConstraintViolationException.class) public void testStartWorkflowNull() { try{ workflowService.startWorkflow(null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("StartWorkflowRequest cannot be null")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testGetExecutionStatusNoWorkflowId() { try{ workflowService.getExecutionStatus("", true); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("WorkflowId cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testInvalidPauseWorkflow() { try{ workflowService.pauseWorkflow(null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("WorkflowId cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testTerminateWorkflowNull() { try{ workflowService.terminateWorkflow(null, null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("WorkflowId cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testPoll(){ try{ taskService.poll(null, null, null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains( "TaskType cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testRestartWorkflowNull(){ try{ workflowBulkService.restart(null, false); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("WorkflowIds list cannot be null.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testRetryWorkflowNull(){ try{ workflowBulkService.retry(null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("WorkflowIds list cannot be null.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testGetWorkflowsNoName() { try{ workflowService.getWorkflows("", "c123", true, true); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("Workflow name cannot be null or empty")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testInvalidDeleteWorkflow() { try{ workflowService.deleteWorkflow(null, true); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("WorkflowId cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testInvalidWorkflowNameGetRunningWorkflows() { try{ workflowService.getRunningWorkflows(null, 123, null, null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("Workflow name cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testGetTask(){ try{ taskService.getTask(null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("TaskId cannot be null or empty.")); throw ex; } }
@Test(expected = ConstraintViolationException.class) public void testRemoveEventHandlerStatus(){ try{ eventService.removeEventHandlerStatus(null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("EventHandler name cannot be null or empty.")); throw ex; } fail("eventService.removeEventHandlerStatus did not throw ConstraintViolationException !"); }
@Test(expected = ConstraintViolationException.class) public void testUpdateEventHandler(){ try{ eventService.updateEventHandler(null); } catch (ConstraintViolationException ex){ assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("EventHandler cannot be null.")); throw ex; } fail("eventService.updateEventHandler did not throw ConstraintViolationException !"); }
@Test(expected = ConstraintViolationException.class) public void testUpdateTaskDefNull() { try{ metadataService.updateTaskDef(null); } catch (ConstraintViolationException ex) { assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("TaskDef cannot be null")); throw ex; } fail("metadataService.updateTaskDef did not throw ConstraintViolationException !"); }
@Test(expected = ConstraintViolationException.class) public void testValidateEventNull() { try{ metadataService.addEventHandler(null); } catch (ConstraintViolationException ex) { assertEquals(1, ex.getConstraintViolations().size()); Set<String> messages = getConstraintViolationMessages(ex.getConstraintViolations()); assertTrue(messages.contains("EventHandler cannot be null")); throw ex; } fail("metadataService.addEventHandler did not throw ConstraintViolationException !"); }