DefaultValidationExecutor implements IValidationExecutor { @Override public ValidationResult validate(IMessageUnit userMessage, IMessageValidationSpecification validationSpec) throws MessageValidationException { if (validationSpec == null || Utils.isNullOrEmpty(validationSpec.getValidators())) { log.trace("No custom validation specified in P-Mode, skipping custom validation"); return null; } ValidationResult result = new ValidationResult(); boolean stopValidation = false; boolean rejectMessage = false; Iterator<IMessageValidatorConfiguration> validatorsToRun = validationSpec.getValidators().iterator(); do { IMessageValidatorConfiguration validatorCfg = validatorsToRun.next(); IMessageValidator validator = createValidator(validatorCfg); log.debug("Validating the message using validator {}", validatorCfg.getId()); Collection<MessageValidationError> errors = validator.validate(userMessage); if (Utils.isNullOrEmpty(errors)) log.debug("Messagge successfully validated by [{}]", validatorCfg.getId()); else { log.debug("Validator [{}] found {} errors in message", validatorCfg.getId(), errors.size()); result.addValidationErrors(validatorCfg.getId(), errors); stopValidation = containsErrorAboveThreshold(validationSpec.getStopSeverity(), errors); rejectMessage |= containsErrorAboveThreshold(validationSpec.getRejectionSeverity(), errors); } } while (!stopValidation && validatorsToRun.hasNext()); result.setExecutedAllValidators(!stopValidation); result.setShouldRejectMessage(rejectMessage); if (Utils.isNullOrEmpty(result.getValidationErrors())) log.debug("Successfully validated message unit [{}]", userMessage.getMessageId()); else { log.info("Found {} validation errors in message unit [{}]", result.getValidationErrors().size() ,userMessage.getMessageId()); } return result; }  @Override ValidationResult validate(IMessageUnit userMessage, IMessageValidationSpecification validationSpec);  }
@Test public void testValid() throws Exception { CustomValidationConfiguration validationSpec = new CustomValidationConfiguration(); validationSpec.setStopSeverity(Severity.Info); validationSpec.setRejectSeverity(Severity.Info); MessageValidatorConfiguration validator = new MessageValidatorConfiguration(); validator.setFactory(ValidatorMock.Factory.class.getName()); validator.addSetting(ValidatorMock.SEVERITY, ""); validationSpec.addValidator(validator); ValidationResult result = new DefaultValidationExecutor().validate(new UserMessage(), validationSpec); assertTrue(result.executedAllValidators()); assertTrue(Utils.isNullOrEmpty(result.getValidationErrors())); }
@Test public void testStop() throws Exception { CustomValidationConfiguration validationSpec = new CustomValidationConfiguration(); validationSpec.setStopSeverity(Severity.Info); validationSpec.setRejectSeverity(Severity.Warning); MessageValidatorConfiguration validator = new MessageValidatorConfiguration(); validator.setId("stoppingValidator"); validator.setFactory(ValidatorMock.Factory.class.getName()); validator.addSetting(ValidatorMock.SEVERITY, Severity.Info); validationSpec.addValidator(validator); validator = new MessageValidatorConfiguration(); validator.setId("notExecutedValidator"); validator.setFactory(ValidatorMock.Factory.class.getName()); validator.addSetting(ValidatorMock.SEVERITY, Severity.Warning); validationSpec.addValidator(validator); ValidationResult result = new DefaultValidationExecutor().validate(new UserMessage(), validationSpec); assertFalse(result.executedAllValidators()); assertFalse(result.shouldRejectMessage()); assertFalse(Utils.isNullOrEmpty(result.getValidationErrors())); assertNotNull(result.getValidationErrors().get("stoppingValidator")); assertNull(result.getValidationErrors().get("notExecutedValidator")); }
@Test public void testRejection() throws Exception { CustomValidationConfiguration validationSpec = new CustomValidationConfiguration(); validationSpec.setStopSeverity(Severity.Failure); validationSpec.setRejectSeverity(Severity.Warning); MessageValidatorConfiguration validator = new MessageValidatorConfiguration(); validator.setId("infoValidator"); validator.setFactory(ValidatorMock.Factory.class.getName()); validator.addSetting(ValidatorMock.SEVERITY, Severity.Info); validationSpec.addValidator(validator); validator = new MessageValidatorConfiguration(); validator.setId("warningValidator"); validator.setFactory(ValidatorMock.Factory.class.getName()); validator.addSetting(ValidatorMock.SEVERITY, Severity.Warning); validationSpec.addValidator(validator); ValidationResult result = new DefaultValidationExecutor().validate(new UserMessage(), validationSpec); assertTrue(result.executedAllValidators()); assertTrue(result.shouldRejectMessage()); assertFalse(Utils.isNullOrEmpty(result.getValidationErrors())); assertNotNull(result.getValidationErrors().get("infoValidator")); assertNotNull(result.getValidationErrors().get("warningValidator")); }