private void registerConstraintReport(Resource resource, InvalidContentException error) { ConstraintReport report= resource. addConstraintReport( error.getConstraints(), new Date(), currentRequest()); this.constraintReportRepository.add(report); LOGGER.debug("Constraint validation failed. Registered constraint report {}",report.id()); error.setConstraintsId(report.id().failureId()); }
public InvalidContentDiagnosedException(OperationContext context, InvalidContentException cause) { super(context,cause,diagnose(cause)); this.constraintReportLink = RetrievalScenario. constraintReportLink( context, cause.getConstraintsId()); }
private void prepareTest(InvalidContentException failure) { cause = failure; cause.setConstraintsId("constraintId"); new MockUp<RetrievalScenario>() { @Mock String constraintReportLink(OperationContext aContext, String constraintReportId) { assertThat(aContext,sameInstance(context)); assertThat(constraintReportId,equalTo("constraintId")); return "link"; } }; sut=new InvalidContentDiagnosedException(context, cause); assertThat(sut.getConstraintReportLink(),equalTo("link")); }
private static Diagnosis diagnose(InvalidContentException rootCause) { Diagnosis diagnosis = Diagnosis.create(rootCause); if(rootCause instanceof InconsistentContentException) { diagnosis= diagnosis. statusCode(Status.CONFLICT). diagnostic("Specified values for application-managed properties are not consistent with the actual resource state: %s",rootCause.getMessage()); } else if(rootCause instanceof UnsupportedContentException) { diagnosis= diagnosis. statusCode(MoreHttp.UNPROCESSABLE_ENTITY_STATUS_CODE). diagnostic("Could not understand content: %s",rootCause.getMessage()); } else { diagnosis=diagnosis.statusCode(Status.BAD_REQUEST); } return diagnosis; }
@Test public void testDiagnoseOtherInvalidContentException(@Mocked final Constraints constraints) throws Exception { prepareTest(new org.ldp4j.application.ext.InvalidContentException("Failure",constraints)); assertThat(sut.getDiagnosis().statusCode(),equalTo(Status.BAD_REQUEST.getStatusCode())); assertThat(sut.getDiagnosis().diagnostic(),equalTo(Throwables.getStackTraceAsString(cause))); }
private OperationContextException diagnoseApplicationExecutionException(OperationContext context, ApplicationExecutionException exception) { OperationContextException result = null; final Throwable rootCause=exception.getCause(); if(rootCause instanceof InvalidContentException) { final InvalidContentException ice = (InvalidContentException)exception.getCause(); if(ice.getConstraintsId()==null) { result=new InternalServerException(context,NO_CONSTRAINT_REPORT_ID_DEFINED_ERROR,ice); } else { result=new InvalidContentDiagnosedException(context,ice); } } else if (rootCause instanceof UnknownResourceException) { throw new WebApplicationException(Status.NOT_FOUND); } else if (rootCause instanceof InvalidQueryException) { result=new InvalidQueryDiagnosedException(context, (InvalidQueryException)rootCause); } else if (rootCause instanceof ApplicationRuntimeException) { result=new InternalServerException(context,rootCause); } else { result=new InternalServerException(context,exception); } return result; }