How to use validateMessageContent method of com.consol.citrus.validation.xml.DomXmlMessageValidator class

Best Citrus code snippet using com.consol.citrus.validation.xml.DomXmlMessageValidator.validateMessageContent

Source:DomXmlMessageValidator.java Github

copy

Full Screen

...84 if (validationContext.isSchemaValidationEnabled()) {85 schemaValidator.validate(receivedMessage, context, validationContext);86 }87 validateNamespaces(validationContext.getControlNamespaces(), receivedMessage);88 validateMessageContent(receivedMessage, controlMessage, validationContext, context);89 if (controlMessage != null) {90 Assert.isTrue(controlMessage.getHeaderData().size() <= receivedMessage.getHeaderData().size(),91 "Failed to validate header data XML fragments - found " +92 receivedMessage.getHeaderData().size() + " header fragments, expected " + controlMessage.getHeaderData().size());93 for (int i = 0; i < controlMessage.getHeaderData().size(); i++) {94 validateXmlHeaderFragment(receivedMessage.getHeaderData().get(i),95 controlMessage.getHeaderData().get(i), validationContext, context);96 }97 }98 LOG.info("XML message validation successful: All values OK");99 } catch (ClassCastException | DOMException | LSException e) {100 throw new CitrusRuntimeException(e);101 } catch (IllegalArgumentException e) {102 LOG.error("Failed to validate:\n" + XMLUtils.prettyPrint(receivedMessage.getPayload(String.class)));103 throw new ValidationException("Validation failed:", e);104 } catch (ValidationException ex) {105 LOG.error("Failed to validate:\n" + XMLUtils.prettyPrint(receivedMessage.getPayload(String.class)));106 throw ex;107 }108 }109 /**110 * Validate namespaces in message. The method compares namespace declarations in the root111 * element of the received message to expected namespaces. Prefixes are important too, so112 * differing namespace prefixes will fail the validation.113 *114 * @param expectedNamespaces115 * @param receivedMessage116 */117 protected void validateNamespaces(Map<String, String> expectedNamespaces, Message receivedMessage) {118 if (CollectionUtils.isEmpty(expectedNamespaces)) { return; }119 if (receivedMessage.getPayload() == null || !StringUtils.hasText(receivedMessage.getPayload(String.class))) {120 throw new ValidationException("Unable to validate message namespaces - receive message payload was empty");121 }122 LOG.debug("Start XML namespace validation");123 Document received = XMLUtils.parseMessagePayload(receivedMessage.getPayload(String.class));124 Map<String, String> foundNamespaces = NamespaceContextBuilder.lookupNamespaces(receivedMessage.getPayload(String.class));125 if (foundNamespaces.size() != expectedNamespaces.size()) {126 throw new ValidationException("Number of namespace declarations not equal for node " +127 XMLUtils.getNodesPathName(received.getFirstChild()) + " found " +128 foundNamespaces.size() + " expected " + expectedNamespaces.size());129 }130 for (Entry<String, String> entry : expectedNamespaces.entrySet()) {131 String namespace = entry.getKey();132 String url = entry.getValue();133 if (foundNamespaces.containsKey(namespace)) {134 if (!foundNamespaces.get(namespace).equals(url)) {135 throw new ValidationException("Namespace '" + namespace +136 "' values not equal: found '" + foundNamespaces.get(namespace) +137 "' expected '" + url + "' in reference node " +138 XMLUtils.getNodesPathName(received.getFirstChild()));139 } else {140 if (LOG.isDebugEnabled()) {141 LOG.debug("Validating namespace " + namespace + " value as expected " + url + " - value OK");142 }143 }144 } else {145 throw new ValidationException("Missing namespace " + namespace + "(" + url + ") in node " +146 XMLUtils.getNodesPathName(received.getFirstChild()));147 }148 }149 LOG.info("XML namespace validation successful: All values OK");150 }151 private void doElementNameValidation(Node received, Node source) {152 //validate element name153 if (LOG.isDebugEnabled()) {154 LOG.debug("Validating element: " + received.getLocalName() + " (" + received.getNamespaceURI() + ")");155 }156 Assert.isTrue(received.getLocalName().equals(source.getLocalName()),157 ValidationUtils.buildValueMismatchErrorMessage("Element names not equal", source.getLocalName(), received.getLocalName()));158 }159 private void doElementNamespaceValidation(Node received, Node source) {160 //validate element namespace161 if (LOG.isDebugEnabled()) {162 LOG.debug("Validating namespace for element: " + received.getLocalName());163 }164 if (received.getNamespaceURI() != null) {165 Assert.isTrue(source.getNamespaceURI() != null,166 ValidationUtils.buildValueMismatchErrorMessage("Element namespace not equal for element '" +167 received.getLocalName() + "'", null, received.getNamespaceURI()));168 Assert.isTrue(received.getNamespaceURI().equals(source.getNamespaceURI()),169 ValidationUtils.buildValueMismatchErrorMessage("Element namespace not equal for element '" +170 received.getLocalName() + "'", source.getNamespaceURI(), received.getNamespaceURI()));171 } else {172 Assert.isTrue(source.getNamespaceURI() == null,173 ValidationUtils.buildValueMismatchErrorMessage("Element namespace not equal for element '" +174 received.getLocalName() + "'", source.getNamespaceURI(), null));175 }176 }177 /**178 * Validate message payloads by comparing to a control message.179 *180 * @param receivedMessage181 * @param validationContext182 * @param context183 */184 protected void validateMessageContent(Message receivedMessage, Message controlMessage,185 XmlMessageValidationContext validationContext, TestContext context) {186 if (controlMessage == null || controlMessage.getPayload() == null) {187 LOG.debug("Skip message payload validation as no control message was defined");188 return;189 }190 if (!(controlMessage.getPayload() instanceof String)) {191 throw new IllegalArgumentException(192 "DomXmlMessageValidator does only support message payload of type String, " +193 "but was " + controlMessage.getPayload().getClass());194 }195 String controlMessagePayload = controlMessage.getPayload(String.class);196 if (receivedMessage.getPayload() == null || !StringUtils.hasText(receivedMessage.getPayload(String.class))) {197 Assert.isTrue(!StringUtils.hasText(controlMessagePayload),198 "Unable to validate message payload - received message payload was empty, control message payload is not");...

Full Screen

Full Screen

validateMessageContent

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner;2import com.consol.citrus.dsl.runner.TestRunner;3import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;4import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;5import com.consol.citrus.message.Message;6import com.consol.citrus.validation.xml.DomXmlMessageValidator;7import org.springframework.core.io.ClassPathResource;8import org.springframework.xml.xpath.Jaxp13XPathTemplate;9import org.springframework.xml.xpath.XPathExpression;10import org.springframework.xml.xpath.XPathOperations;11import org.testng.annotations.Test;12import java.io.IOException;13public class ValidateXmlMessageTest extends TestNGCitrusTestDesigner {14 public void validateXmlMessage() throws IOException {15 "</soap:Envelope>";16 Message message = new Message(xmlMessage);17 validateMessageContent(message, new DomXmlMessageValidator() {18 public void validateMessage(Message receivedMessage, Message controlMessage, TestRunner context) {

Full Screen

Full Screen

validateMessageContent

Using AI Code Generation

copy

Full Screen

1public void test() {2 run(new TestRunner() {3 public void execute() {4 variable("messageId", "123");5 variable("timestamp", "2018-01-01T00:00:00.000Z");6 variable("exceptionMessage", "Test");7 variable("exceptionText", "Internal Server Error");8 variable("exceptionCode", "Server");9 send("webServiceClient")10 + "<ns1:MessageID>${messageId}</ns1:MessageID>"11 + "<ns1:Timestamp>${timestamp}</ns1:Timestamp>"12 + "<ns1:Value>${exceptionCode}</ns1:Value>"13 + "<ns1:Text xml:lang=\"en\">${exceptionText}</ns1:Text>"

Full Screen

Full Screen

validateMessageContent

Using AI Code Generation

copy

Full Screen

1[16]: # public void validateMessageContent() {2[18]: # variable("messageId", UUID.randomUUID().toString());3[20]: # send("messageEndpoint")4[21]: # .payload("<TestMessage>" +5[23]: # " <id>${messageId}</id>" +6[25]: # "</TestMessage>");7[27]: # receive("messageEndpoint")8[28]: # .messageType(MessageType.PLAINTEXT)9[29]: # .payload("<TestMessage>" +10[31]: # " <id>${messageId}</id>" +11[34]: # .validateMessageContent(new DomXmlMessageValidator() {12[36]: # public void validateMessage(Message receivedMessage, Message controlMessage) {13[37]: # Assert.assertEquals(receivedMessage.getPayload(String.class), controlMessage.getPayload(String.class));14[38]: # }15[39]: # });16[40]: # }17[52]: # public void validateMessageContent() {18[54]: # variable("messageId", UUID.randomUUID().toString());

Full Screen

Full Screen

validateMessageContent

Using AI Code Generation

copy

Full Screen

1validateMessageContent(2 message: new DefaultXmlMessage("<testMessage><text>Hello Citrus!</text></testMessage>"),3 validationContext: new XmlValidationContext().schemaValidation(true)4 .schemaRepository("citrusXmlSchemaRepository")5 .schemaResourcePath("classpath:com/consol/citrus/validation/schema/test.xsd")6 .ignoreWhitespace(true)7 .ignoreComments(true)8 .ignoreDiffNamespace(true)9 .ignoreAttributeOrder(true)10 .ignoreXPathNamespace(true)11 .ignoreXPathValue(true)12 .ignoreXPathExpression("/testMessage/text")13 .ignoreXPathExpression("/testMessage/number")14 .ignoreDtd(true)15 .ignoreSchemaLocation(true)16 .ignoreSchema(true)17 .ignoreNamespaces(true)18 .ignoreNamespacePrefixes(true)19 .reportAllErrors(true)20 .reportErrors(true)21 .reportWarnings(true)22 .reportInfos(true)23 .reportFatal(true)24 .reportXpathErrors(true)25 .reportXpathWarnings(true)26 .reportXpathInfos(true)27 .reportXpathFatal(true)28 .reportXpathErrors(true)29 .reportXpathWarnings(true)30 .reportXpathInfos(true)31 .reportXpathFatal(true)32 .ignoreXpathNamespace(t

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful