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

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

Source:DomXmlMessageValidator.java Github

copy

Full Screen

...444 for (int i = 0; i < receivedAttr.getLength(); i++) {445 doAttribute(received, receivedAttr.item(i), source, validationContext, namespaceContext, context);446 }447 //check if validation matcher on element is specified448 if (isValidationMatcherExpression(source)) {449 ValidationMatcherUtils.resolveValidationMatcher(source.getNodeName(),450 received.getFirstChild().getNodeValue().trim(),451 source.getFirstChild().getNodeValue().trim(),452 context);453 return;454 }455 doText((Element) received, (Element) source);456 //work on child nodes457 List<Element> receivedChildElements = DomUtils.getChildElements((Element) received);458 List<Element> sourceChildElements = DomUtils.getChildElements((Element) source);459 Assert.isTrue(receivedChildElements.size() == sourceChildElements.size(),460 ValidationUtils.buildValueMismatchErrorMessage("Number of child elements not equal for element '"461 + received.getLocalName() + "'", sourceChildElements.size(), receivedChildElements.size()));462 for (int i = 0; i < receivedChildElements.size(); i++) {463 this.validateXmlTree(receivedChildElements.get(i), sourceChildElements.get(i),464 validationContext, namespaceContext, context);465 }466 if (log.isDebugEnabled()) {467 log.debug("Validation successful for element: " + received.getLocalName() +468 " (" + received.getNamespaceURI() + ")");469 }470 }471 /**472 * Handle text node during validation.473 *474 * @param received475 * @param source476 */477 private void doText(Element received, Element source) {478 if (log.isDebugEnabled()) {479 log.debug("Validating node value for element: " + received.getLocalName());480 }481 String receivedText = DomUtils.getTextValue(received);482 String sourceText = DomUtils.getTextValue(source);483 if (receivedText != null) {484 Assert.isTrue(sourceText != null,485 ValidationUtils.buildValueMismatchErrorMessage("Node value not equal for element '"486 + received.getLocalName() + "'", null, receivedText.trim()));487 Assert.isTrue(receivedText.trim().equals(sourceText.trim()),488 ValidationUtils.buildValueMismatchErrorMessage("Node value not equal for element '"489 + received.getLocalName() + "'", sourceText.trim(),490 receivedText.trim()));491 } else {492 Assert.isTrue(sourceText == null,493 ValidationUtils.buildValueMismatchErrorMessage("Node value not equal for element '"494 + received.getLocalName() + "'", sourceText.trim(), null));495 }496 if (log.isDebugEnabled()) {497 log.debug("Node value '" + receivedText.trim() + "': OK");498 }499 }500 /**501 * Handle attribute node during validation.502 *503 * @param receivedElement504 * @param receivedAttribute505 * @param sourceElement506 * @param validationContext507 */508 private void doAttribute(Node receivedElement, Node receivedAttribute, Node sourceElement,509 XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) {510 if (receivedAttribute.getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) { return; }511 String receivedAttributeName = receivedAttribute.getLocalName();512 if (log.isDebugEnabled()) {513 log.debug("Validating attribute: " + receivedAttributeName + " (" + receivedAttribute.getNamespaceURI() + ")");514 }515 NamedNodeMap sourceAttributes = sourceElement.getAttributes();516 Node sourceAttribute = sourceAttributes.getNamedItemNS(receivedAttribute.getNamespaceURI(), receivedAttributeName);517 Assert.isTrue(sourceAttribute != null,518 "Attribute validation failed for element '"519 + receivedElement.getLocalName() + "', unknown attribute "520 + receivedAttributeName + " (" + receivedAttribute.getNamespaceURI() + ")");521 if (XmlValidationUtils.isAttributeIgnored(receivedElement, receivedAttribute, sourceAttribute, validationContext.getIgnoreExpressions(), namespaceContext)) {522 return;523 }524 String receivedValue = receivedAttribute.getNodeValue();525 String sourceValue = sourceAttribute.getNodeValue();526 if (isValidationMatcherExpression(sourceAttribute)) {527 ValidationMatcherUtils.resolveValidationMatcher(sourceAttribute.getNodeName(),528 receivedAttribute.getNodeValue().trim(),529 sourceAttribute.getNodeValue().trim(),530 context);531 } else if (receivedValue.contains(":") && sourceValue.contains(":")) {532 doNamespaceQualifiedAttributeValidation(receivedElement, receivedAttribute, sourceElement, sourceAttribute);533 } else {534 Assert.isTrue(receivedValue.equals(sourceValue),535 ValidationUtils.buildValueMismatchErrorMessage("Values not equal for attribute '"536 + receivedAttributeName + "'", sourceValue, receivedValue));537 }538 if (log.isDebugEnabled()) {539 log.debug("Attribute '" + receivedAttributeName + "'='" + receivedValue + "': OK");540 }541 }542 /**543 * Perform validation on namespace qualified attribute values if present. This includes the validation of namespace presence544 * and equality.545 * @param receivedElement546 * @param receivedAttribute547 * @param sourceElement548 * @param sourceAttribute549 */550 private void doNamespaceQualifiedAttributeValidation(Node receivedElement, Node receivedAttribute, Node sourceElement, Node sourceAttribute) {551 String receivedValue = receivedAttribute.getNodeValue();552 String sourceValue = sourceAttribute.getNodeValue();553 if (receivedValue.contains(":") && sourceValue.contains(":")) {554 // value has namespace prefix set, do special QName validation555 String receivedPrefix = receivedValue.substring(0, receivedValue.indexOf(':'));556 String sourcePrefix = sourceValue.substring(0, sourceValue.indexOf(':'));557 Map<String, String> receivedNamespaces = XMLUtils.lookupNamespaces(receivedAttribute.getOwnerDocument());558 receivedNamespaces.putAll(XMLUtils.lookupNamespaces(receivedElement));559 if (receivedNamespaces.containsKey(receivedPrefix)) {560 Map<String, String> sourceNamespaces = XMLUtils.lookupNamespaces(sourceAttribute.getOwnerDocument());561 sourceNamespaces.putAll(XMLUtils.lookupNamespaces(sourceElement));562 if (sourceNamespaces.containsKey(sourcePrefix)) {563 Assert.isTrue(sourceNamespaces.get(sourcePrefix).equals(receivedNamespaces.get(receivedPrefix)),564 ValidationUtils.buildValueMismatchErrorMessage("Values not equal for attribute value namespace '"565 + receivedValue + "'", sourceNamespaces.get(sourcePrefix), receivedNamespaces.get(receivedPrefix)));566 // remove namespace prefixes as they must not form equality567 receivedValue = receivedValue.substring((receivedPrefix + ":").length());568 sourceValue = sourceValue.substring((sourcePrefix + ":").length());569 } else {570 throw new ValidationException("Received attribute value '" + receivedAttribute.getLocalName() + "' describes namespace qualified attribute value," +571 " control value '" + sourceValue + "' does not");572 }573 }574 }575 Assert.isTrue(receivedValue.equals(sourceValue),576 ValidationUtils.buildValueMismatchErrorMessage("Values not equal for attribute '"577 + receivedAttribute.getLocalName() + "'", sourceValue, receivedValue));578 }579 /**580 * Handle processing instruction during validation.581 *582 * @param received583 */584 private void doPI(Node received) {585 if (log.isDebugEnabled()) {586 log.debug("Ignored processing instruction (" + received.getLocalName() + "=" + received.getNodeValue() + ")");587 }588 }589 /**590 * Counts the attributenode for an element (xmlns attributes ignored)591 * @param attributesR attributesMap592 * @return number of attributes593 */594 private int countAttributes(NamedNodeMap attributesR) {595 int cntAttributes = 0;596 for (int i = 0; i < attributesR.getLength(); i++) {597 if (!attributesR.item(i).getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {598 cntAttributes++;599 }600 }601 return cntAttributes;602 }603 /**604 * Checks whether the given node contains a validation matcher605 * @param node606 * @return true if node value contains validation matcher, false if not607 */608 private boolean isValidationMatcherExpression(Node node) {609 switch (node.getNodeType()) {610 case Node.ELEMENT_NODE:611 return node.getFirstChild() != null &&612 StringUtils.hasText(node.getFirstChild().getNodeValue()) &&613 ValidationMatcherUtils.isValidationMatcherExpression(node.getFirstChild().getNodeValue().trim());614 case Node.ATTRIBUTE_NODE:615 return StringUtils.hasText(node.getNodeValue()) &&616 ValidationMatcherUtils.isValidationMatcherExpression(node.getNodeValue().trim());617 default: return false; //validation matchers makes no sense618 }619 }620 @Override621 protected Class<XmlMessageValidationContext> getRequiredValidationContextType() {622 return XmlMessageValidationContext.class;623 }624 @Override625 public boolean supportsMessageType(String messageType, Message message) {626 if (!messageType.equalsIgnoreCase(MessageType.XML.name())) {627 return false;628 }629 if (!(message.getPayload() instanceof String)) {630 return false;...

Full Screen

Full Screen

isValidationMatcherExpression

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.builder.GroovyDsl2import com.consol.citrus.dsl.builder.HttpServerActionBuilder3import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder4import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder5import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder.HttpResponseBuilder6import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder.HttpResponseBuilder.HttpResponsePayloadBuilder7GroovyDsl httpServer = new GroovyDsl()8HttpServerActionBuilder httpServerActionBuilder = httpServer.http()9HttpServerRequestActionBuilder httpServerRequestActionBuilder = httpServerActionBuilder.server()10HttpServerResponseActionBuilder httpServerResponseActionBuilder = httpServerRequestActionBuilder.receive()11HttpResponseBuilder httpResponseBuilder = httpServerResponseActionBuilder.response()12HttpResponsePayloadBuilder httpResponsePayloadBuilder = httpResponseBuilder.payload()13httpResponsePayloadBuilder.xml("""14httpResponseBuilder.header("Content-Type", "application/xml")15httpResponseBuilder.header("SOAPAction", "sayHello")16httpResponseBuilder.status(200)17httpServerResponseActionBuilder.validator {18 isValidationMatcherExpression("com.consol.citrus.validation.xml.DomXmlMessageValidator")19}20httpServerResponseActionBuilder.send()21httpServerRequestActionBuilder.send()22httpServerActionBuilder.run()23import com.consol.citrus.dsl.builder.GroovyDsl24import com.consol.citrus.dsl.builder.HttpServerActionBuilder

Full Screen

Full Screen

isValidationMatcherExpression

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.dsl.runner;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import org.testng.annotations.Test;4public class ValidateXmlUsingValidationMatcherExpressionIT extends TestNGCitrusTestDesigner {5 public void validateXmlUsingValidationMatcherExpressionIT() {6 variable("xml", "<testMessage><text>Hello Citrus!</text></testMessage>");7 http().client("httpClient")8 .send()9 .post("/services/test")10 .contentType("text/xml")11 .payload("${xml}");12 http().client("httpClient")13 .receive()14 .response(HttpStatus.OK)15 .messageType(MessageType.XML)16 .validate(xml()17 .schemaValidation(false)18 .isValidationMatcherExpression("${expression}"));19 }20}21[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ citrus-integration-tests ---22[INFO] --- maven-failsafe-plugin:2.22.2:verify (default) @ citrus-integration-tests ---

Full Screen

Full Screen

isValidationMatcherExpression

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.dsl.design.TestDesigner2import com.consol.citrus.dsl.design.TestDesignerBefore3class MyTest implements TestDesignerBefore {4 void configure(TestDesigner test) {5 def validator = new DomXmlMessageValidator()6 validator.setSchemaValidation(false)7 validator.setSchemaValidation(false)

Full Screen

Full Screen

isValidationMatcherExpression

Using AI Code Generation

copy

Full Screen

1public void testMyWebService() {2 http().client(myWebServiceClient)3 .send()4 .post("/myWebService")5 .contentType("application/xml")6 .payload("<myRequest><id>citrus:randomNumber(5)</id></myRequest>");7 http().client(myWebServiceClient)8 .receive()9 .response(HttpStatus.OK)10 .contentType("application/xml")11 .payload("<myResponse><id>${citrus:randomNumber(5)}</id></myResponse>");12}

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