Best Citrus code snippet using com.consol.citrus.validation.xml.DomXmlMessageValidator.doElementNameValidation
Source:DomXmlMessageValidator.java
...248 }249 }250 log.info("XML namespace validation successful: All values OK");251 }252 private void doElementNameValidation(Node received, Node source) {253 //validate element name254 if (log.isDebugEnabled()) {255 log.debug("Validating element: " + received.getLocalName() + " (" + received.getNamespaceURI() + ")");256 }257 Assert.isTrue(received.getLocalName().equals(source.getLocalName()),258 ValidationUtils.buildValueMismatchErrorMessage("Element names not equal", source.getLocalName(), received.getLocalName()));259 }260 private void doElementNamespaceValidation(Node received, Node source) {261 //validate element namespace262 if (log.isDebugEnabled()) {263 log.debug("Validating namespace for element: " + received.getLocalName());264 }265 if (received.getNamespaceURI() != null) {266 Assert.isTrue(source.getNamespaceURI() != null,267 ValidationUtils.buildValueMismatchErrorMessage("Element namespace not equal for element '" +268 received.getLocalName() + "'", null, received.getNamespaceURI()));269 Assert.isTrue(received.getNamespaceURI().equals(source.getNamespaceURI()),270 ValidationUtils.buildValueMismatchErrorMessage("Element namespace not equal for element '" +271 received.getLocalName() + "'", source.getNamespaceURI(), received.getNamespaceURI()));272 } else {273 Assert.isTrue(source.getNamespaceURI() == null,274 ValidationUtils.buildValueMismatchErrorMessage("Element namespace not equal for element '" +275 received.getLocalName() + "'", source.getNamespaceURI(), null));276 }277 }278 /**279 * Validate message payloads by comparing to a control message.280 *281 * @param receivedMessage282 * @param validationContext283 * @param context284 */285 protected void validateMessageContent(Message receivedMessage, Message controlMessage, XmlMessageValidationContext validationContext,286 TestContext context) {287 if (controlMessage == null || controlMessage.getPayload() == null) {288 log.debug("Skip message payload validation as no control message was defined");289 return;290 }291 if (!(controlMessage.getPayload() instanceof String)) {292 throw new IllegalArgumentException(293 "DomXmlMessageValidator does only support message payload of type String, " +294 "but was " + controlMessage.getPayload().getClass());295 }296 String controlMessagePayload = controlMessage.getPayload(String.class);297 if (receivedMessage.getPayload() == null || !StringUtils.hasText(receivedMessage.getPayload(String.class))) {298 Assert.isTrue(!StringUtils.hasText(controlMessagePayload),299 "Unable to validate message payload - received message payload was empty, control message payload is not");300 return;301 } else if (!StringUtils.hasText(controlMessagePayload)) {302 return;303 }304 log.debug("Start XML tree validation ...");305 Document received = XMLUtils.parseMessagePayload(receivedMessage.getPayload(String.class));306 Document source = XMLUtils.parseMessagePayload(controlMessagePayload);307 XMLUtils.stripWhitespaceNodes(received);308 XMLUtils.stripWhitespaceNodes(source);309 if (log.isDebugEnabled()) {310 log.debug("Received message:\n" + XMLUtils.serialize(received));311 log.debug("Control message:\n" + XMLUtils.serialize(source));312 }313 validateXmlTree(received, source, validationContext, namespaceContextBuilder.buildContext(314 receivedMessage, validationContext.getNamespaces()), context);315 }316 317 /**318 * Validates XML header fragment data.319 * @param receivedHeaderData320 * @param controlHeaderData321 * @param validationContext322 * @param context323 */324 private void validateXmlHeaderFragment(String receivedHeaderData, String controlHeaderData,325 XmlMessageValidationContext validationContext, TestContext context) {326 log.debug("Start XML header data validation ...");327 Document received = XMLUtils.parseMessagePayload(receivedHeaderData);328 Document source = XMLUtils.parseMessagePayload(controlHeaderData);329 XMLUtils.stripWhitespaceNodes(received);330 XMLUtils.stripWhitespaceNodes(source);331 if (log.isDebugEnabled()) {332 log.debug("Received header data:\n" + XMLUtils.serialize(received));333 log.debug("Control header data:\n" + XMLUtils.serialize(source));334 }335 validateXmlTree(received, source, validationContext, 336 namespaceContextBuilder.buildContext(new DefaultMessage(receivedHeaderData), validationContext.getNamespaces()),337 context);338 }339 /**340 * Walk the XML tree and validate all nodes.341 *342 * @param received343 * @param source344 * @param validationContext345 */346 private void validateXmlTree(Node received, Node source, 347 XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) {348 switch(received.getNodeType()) {349 case Node.DOCUMENT_TYPE_NODE:350 doDocumentTypeDefinition(received, source, validationContext, namespaceContext, context);351 break;352 case Node.DOCUMENT_NODE:353 validateXmlTree(received.getFirstChild(), source.getFirstChild(),354 validationContext, namespaceContext, context);355 break;356 case Node.ELEMENT_NODE:357 doElement(received, source, validationContext, namespaceContext, context);358 break;359 case Node.ATTRIBUTE_NODE:360 throw new IllegalStateException();361 case Node.COMMENT_NODE:362 validateXmlTree(received.getNextSibling(), source,363 validationContext, namespaceContext, context);364 break;365 case Node.PROCESSING_INSTRUCTION_NODE:366 doPI(received);367 break;368 }369 }370 /**371 * Handle document type definition with validation of publicId and systemId.372 * @param received373 * @param source374 * @param validationContext375 * @param namespaceContext376 */377 private void doDocumentTypeDefinition(Node received, Node source,378 XmlMessageValidationContext validationContext,379 NamespaceContext namespaceContext, TestContext context) {380 Assert.isTrue(source instanceof DocumentType, "Missing document type definition in expected xml fragment");381 DocumentType receivedDTD = (DocumentType) received;382 DocumentType sourceDTD = (DocumentType) source;383 if (log.isDebugEnabled()) {384 log.debug("Validating document type definition: " +385 receivedDTD.getPublicId() + " (" + receivedDTD.getSystemId() + ")");386 }387 if (!StringUtils.hasText(sourceDTD.getPublicId())) {388 Assert.isNull(receivedDTD.getPublicId(),389 ValidationUtils.buildValueMismatchErrorMessage("Document type public id not equal",390 sourceDTD.getPublicId(), receivedDTD.getPublicId()));391 } else if (sourceDTD.getPublicId().trim().equals(Citrus.IGNORE_PLACEHOLDER)) {392 if (log.isDebugEnabled()) {393 log.debug("Document type public id: '" + receivedDTD.getPublicId() +394 "' is ignored by placeholder '" + Citrus.IGNORE_PLACEHOLDER + "'");395 }396 } else {397 Assert.isTrue(StringUtils.hasText(receivedDTD.getPublicId()) &&398 receivedDTD.getPublicId().equals(sourceDTD.getPublicId()),399 ValidationUtils.buildValueMismatchErrorMessage("Document type public id not equal",400 sourceDTD.getPublicId(), receivedDTD.getPublicId()));401 }402 if (!StringUtils.hasText(sourceDTD.getSystemId())) {403 Assert.isNull(receivedDTD.getSystemId(),404 ValidationUtils.buildValueMismatchErrorMessage("Document type system id not equal",405 sourceDTD.getSystemId(), receivedDTD.getSystemId()));406 } else if (sourceDTD.getSystemId().trim().equals(Citrus.IGNORE_PLACEHOLDER)) {407 if (log.isDebugEnabled()) {408 log.debug("Document type system id: '" + receivedDTD.getSystemId() +409 "' is ignored by placeholder '" + Citrus.IGNORE_PLACEHOLDER + "'");410 }411 } else {412 Assert.isTrue(StringUtils.hasText(receivedDTD.getSystemId()) &&413 receivedDTD.getSystemId().equals(sourceDTD.getSystemId()),414 ValidationUtils.buildValueMismatchErrorMessage("Document type system id not equal",415 sourceDTD.getSystemId(), receivedDTD.getSystemId()));416 }417 validateXmlTree(received.getNextSibling(),418 source.getNextSibling(), validationContext, namespaceContext, context);419 }420 /**421 * Handle element node.422 *423 * @param received424 * @param source425 * @param validationContext426 */427 private void doElement(Node received, Node source,428 XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) {429 doElementNameValidation(received, source);430 doElementNamespaceValidation(received, source);431 //check if element is ignored either by xpath or by ignore placeholder in source message432 if (XmlValidationUtils.isElementIgnored(source, received, validationContext.getIgnoreExpressions(), namespaceContext)) {433 return;434 }435 //work on attributes436 if (log.isDebugEnabled()) {437 log.debug("Validating attributes for element: " + received.getLocalName());438 }439 NamedNodeMap receivedAttr = received.getAttributes();440 NamedNodeMap sourceAttr = source.getAttributes();441 Assert.isTrue(countAttributes(receivedAttr) == countAttributes(sourceAttr),442 ValidationUtils.buildValueMismatchErrorMessage("Number of attributes not equal for element '"443 + received.getLocalName() + "'", countAttributes(sourceAttr), countAttributes(receivedAttr)));...
doElementNameValidation
Using AI Code Generation
1public class DoElementNameValidationIT extends TestNGCitrusTestDesigner {2 public void doElementNameValidation() {3 variable("xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"4 + "</testMessage>");5 send("sender")6 .payload("${xml}");7 receive("receiver")8 .payload("${xml}")9 .validate(xml().doElementNameValidation(true));10 }11}12[import:doElementNamespaceValidation[]]13public class DoElementNamespaceValidationIT extends TestNGCitrusTestDesigner {14 public void doElementNamespaceValidation() {15 variable("xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"16 + "</testMessage>");17 send("sender")18 .payload("${xml}");19 receive("receiver")20 .payload("${xml}")21 .validate(xml().doElementNamespaceValidation(true));22 }23}24[import:doElementPrefixValidation[]]25doElementPrefixValidation() method of com.consol.cit
doElementNameValidation
Using AI Code Generation
1public void testDoElementNameValidation() {2 run(new TestAction() {3 public void doExecute(TestContext context) {4 XmlValidationContext validationContext = new XmlValidationContext();5 validationContext.setDoElementNameValidation(true);6 context.setVariable("validationContext", validationContext);7 }8 });9 send("sendRequestMessage")10 .payload("<testMessage>" +11 .header("operation", "doElementNameValidation")12 .validationContext("${validationContext}");13}14public void testXmlSchemaValidation() {15 run(new TestAction() {16 public void doExecute(TestContext context) {17 XmlValidationContext validationContext = new XmlValidationContext();18 validationContext.setSchema("classpath:com/consol/citrus/validation/xml/test-schema.xsd");19 context.setVariable("validationContext", validationContext);20 }21 });22 send("sendRequestMessage")23 .payload("<testMessage>" +24 .header("operation",
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!