FormUrlEncodedMessageValidator implements MessageValidator<ValidationContext> { @Override public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, List<ValidationContext> validationContexts) throws ValidationException { log.info("Start " + MESSAGE_TYPE + " message validation"); try { Message formMessage = new DefaultMessage(receivedMessage); StringResult result = new StringResult(); formMarshaller.marshal(createFormData(receivedMessage), result); formMessage.setPayload(result.toString()); getXmlMessageValidator(context).validateMessage(formMessage, controlMessage, context, prepareValidationContexts(validationContexts)); } catch (IllegalArgumentException e) { throw new ValidationException("Failed to validate " + MESSAGE_TYPE + " message", e); } log.info("Validation of " + MESSAGE_TYPE + " message finished successfully: All values OK"); }  @Override void validateMessage(Message receivedMessage, Message controlMessage,
                                TestContext context, List<ValidationContext> validationContexts); @Override boolean supportsMessageType(String messageType, Message message); static final String MESSAGE_TYPE; static final String DEFAULT_XML_MESSAGE_VALIDATOR; }
@Test public void testValidateMessagePayload() throws Exception { Message controlMessage = new DefaultMessage(expectedFormData); Message receivedMessage = new DefaultMessage("password=s%21cr%21t&username=test") .setHeader(HttpMessageHeaders.HTTP_CONTENT_TYPE, "application/x-www-form-urlencoded") .setHeader(HttpMessageHeaders.HTTP_REQUEST_URI, "/form-test"); validator.validateMessage(receivedMessage, controlMessage, context, Collections.singletonList(validationContext)); }
@Test(expectedExceptions = ValidationException.class) public void testValidationError() throws Exception { Message controlMessage = new DefaultMessage(expectedFormData); Message receivedMessage = new DefaultMessage("password=s%21cr%21t&username=other") .setHeader(HttpMessageHeaders.HTTP_CONTENT_TYPE, "application/x-www-form-urlencoded") .setHeader(HttpMessageHeaders.HTTP_REQUEST_URI, "/form-test"); validator.validateMessage(receivedMessage, controlMessage, context, Collections.singletonList(validationContext)); }
@Test(expectedExceptions = ValidationException.class) public void testValidationErrorMissingFormAction() throws Exception { Message controlMessage = new DefaultMessage(expectedFormData); Message receivedMessage = new DefaultMessage("password=s%21cr%21t&username=test"); validator.validateMessage(receivedMessage, controlMessage, context, Collections.singletonList(validationContext)); }