Best Citrus code snippet using com.consol.citrus.validation.xml.DomXmlMessageValidator.doPI
Source:DomXmlMessageValidator.java
...260 validateXmlTree(received.getNextSibling(), source,261 validationContext, namespaceContext, context);262 break;263 case Node.PROCESSING_INSTRUCTION_NODE:264 doPI(received);265 break;266 }267 }268 /**269 * Handle document type definition with validation of publicId and systemId.270 * @param received271 * @param source272 * @param validationContext273 * @param namespaceContext274 */275 private void doDocumentTypeDefinition(Node received, Node source,276 XmlMessageValidationContext validationContext,277 NamespaceContext namespaceContext, TestContext context) {278 Assert.isTrue(source instanceof DocumentType, "Missing document type definition in expected xml fragment");279 DocumentType receivedDTD = (DocumentType) received;280 DocumentType sourceDTD = (DocumentType) source;281 if (LOG.isDebugEnabled()) {282 LOG.debug("Validating document type definition: " +283 receivedDTD.getPublicId() + " (" + receivedDTD.getSystemId() + ")");284 }285 if (!StringUtils.hasText(sourceDTD.getPublicId())) {286 Assert.isNull(receivedDTD.getPublicId(),287 ValidationUtils.buildValueMismatchErrorMessage("Document type public id not equal",288 sourceDTD.getPublicId(), receivedDTD.getPublicId()));289 } else if (sourceDTD.getPublicId().trim().equals(CitrusSettings.IGNORE_PLACEHOLDER)) {290 if (LOG.isDebugEnabled()) {291 LOG.debug("Document type public id: '" + receivedDTD.getPublicId() +292 "' is ignored by placeholder '" + CitrusSettings.IGNORE_PLACEHOLDER + "'");293 }294 } else {295 Assert.isTrue(StringUtils.hasText(receivedDTD.getPublicId()) &&296 receivedDTD.getPublicId().equals(sourceDTD.getPublicId()),297 ValidationUtils.buildValueMismatchErrorMessage("Document type public id not equal",298 sourceDTD.getPublicId(), receivedDTD.getPublicId()));299 }300 if (!StringUtils.hasText(sourceDTD.getSystemId())) {301 Assert.isNull(receivedDTD.getSystemId(),302 ValidationUtils.buildValueMismatchErrorMessage("Document type system id not equal",303 sourceDTD.getSystemId(), receivedDTD.getSystemId()));304 } else if (sourceDTD.getSystemId().trim().equals(CitrusSettings.IGNORE_PLACEHOLDER)) {305 if (LOG.isDebugEnabled()) {306 LOG.debug("Document type system id: '" + receivedDTD.getSystemId() +307 "' is ignored by placeholder '" + CitrusSettings.IGNORE_PLACEHOLDER + "'");308 }309 } else {310 Assert.isTrue(StringUtils.hasText(receivedDTD.getSystemId()) &&311 receivedDTD.getSystemId().equals(sourceDTD.getSystemId()),312 ValidationUtils.buildValueMismatchErrorMessage("Document type system id not equal",313 sourceDTD.getSystemId(), receivedDTD.getSystemId()));314 }315 validateXmlTree(received.getNextSibling(),316 source.getNextSibling(), validationContext, namespaceContext, context);317 }318 /**319 * Handle element node.320 *321 * @param received322 * @param source323 * @param validationContext324 */325 private void doElement(Node received, Node source,326 XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) {327 doElementNameValidation(received, source);328 doElementNamespaceValidation(received, source);329 //check if element is ignored either by xpath or by ignore placeholder in source message330 if (XmlValidationUtils.isElementIgnored(source, received, validationContext.getIgnoreExpressions(), namespaceContext)) {331 return;332 }333 //work on attributes334 if (LOG.isDebugEnabled()) {335 LOG.debug("Validating attributes for element: " + received.getLocalName());336 }337 NamedNodeMap receivedAttr = received.getAttributes();338 NamedNodeMap sourceAttr = source.getAttributes();339 Assert.isTrue(countAttributes(receivedAttr) == countAttributes(sourceAttr),340 ValidationUtils.buildValueMismatchErrorMessage("Number of attributes not equal for element '"341 + received.getLocalName() + "'", countAttributes(sourceAttr), countAttributes(receivedAttr)));342 for (int i = 0; i < receivedAttr.getLength(); i++) {343 doAttribute(received, receivedAttr.item(i), source, validationContext, namespaceContext, context);344 }345 //check if validation matcher on element is specified346 if (isValidationMatcherExpression(source)) {347 ValidationMatcherUtils.resolveValidationMatcher(source.getNodeName(),348 received.getFirstChild().getNodeValue().trim(),349 source.getFirstChild().getNodeValue().trim(),350 context);351 return;352 }353 doText((Element) received, (Element) source);354 //work on child nodes355 List<Element> receivedChildElements = DomUtils.getChildElements((Element) received);356 List<Element> sourceChildElements = DomUtils.getChildElements((Element) source);357 Assert.isTrue(receivedChildElements.size() == sourceChildElements.size(),358 ValidationUtils.buildValueMismatchErrorMessage("Number of child elements not equal for element '"359 + received.getLocalName() + "'", sourceChildElements.size(), receivedChildElements.size()));360 for (int i = 0; i < receivedChildElements.size(); i++) {361 this.validateXmlTree(receivedChildElements.get(i), sourceChildElements.get(i),362 validationContext, namespaceContext, context);363 }364 if (LOG.isDebugEnabled()) {365 LOG.debug("Validation successful for element: " + received.getLocalName() +366 " (" + received.getNamespaceURI() + ")");367 }368 }369 /**370 * Handle text node during validation.371 *372 * @param received373 * @param source374 */375 private void doText(Element received, Element source) {376 if (LOG.isDebugEnabled()) {377 LOG.debug("Validating node value for element: " + received.getLocalName());378 }379 String receivedText = DomUtils.getTextValue(received);380 String sourceText = DomUtils.getTextValue(source);381 if (receivedText != null) {382 Assert.isTrue(sourceText != null,383 ValidationUtils.buildValueMismatchErrorMessage("Node value not equal for element '"384 + received.getLocalName() + "'", null, receivedText.trim()));385 Assert.isTrue(receivedText.trim().equals(sourceText.trim()),386 ValidationUtils.buildValueMismatchErrorMessage("Node value not equal for element '"387 + received.getLocalName() + "'", sourceText.trim(),388 receivedText.trim()));389 } else {390 Assert.isTrue(sourceText == null,391 ValidationUtils.buildValueMismatchErrorMessage("Node value not equal for element '"392 + received.getLocalName() + "'", sourceText.trim(), null));393 }394 if (LOG.isDebugEnabled()) {395 LOG.debug("Node value '" + receivedText.trim() + "': OK");396 }397 }398 /**399 * Handle attribute node during validation.400 *401 * @param receivedElement402 * @param receivedAttribute403 * @param sourceElement404 * @param validationContext405 */406 private void doAttribute(Node receivedElement, Node receivedAttribute, Node sourceElement,407 XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) {408 if (receivedAttribute.getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) { return; }409 String receivedAttributeName = receivedAttribute.getLocalName();410 if (LOG.isDebugEnabled()) {411 LOG.debug("Validating attribute: " + receivedAttributeName + " (" + receivedAttribute.getNamespaceURI() + ")");412 }413 NamedNodeMap sourceAttributes = sourceElement.getAttributes();414 Node sourceAttribute = sourceAttributes.getNamedItemNS(receivedAttribute.getNamespaceURI(), receivedAttributeName);415 Assert.isTrue(sourceAttribute != null,416 "Attribute validation failed for element '"417 + receivedElement.getLocalName() + "', unknown attribute "418 + receivedAttributeName + " (" + receivedAttribute.getNamespaceURI() + ")");419 if (XmlValidationUtils.isAttributeIgnored(receivedElement, receivedAttribute, sourceAttribute, validationContext.getIgnoreExpressions(), namespaceContext)) {420 return;421 }422 String receivedValue = receivedAttribute.getNodeValue();423 String sourceValue = sourceAttribute.getNodeValue();424 if (isValidationMatcherExpression(sourceAttribute)) {425 ValidationMatcherUtils.resolveValidationMatcher(sourceAttribute.getNodeName(),426 receivedAttribute.getNodeValue().trim(),427 sourceAttribute.getNodeValue().trim(),428 context);429 } else if (receivedValue.contains(":") && sourceValue.contains(":")) {430 doNamespaceQualifiedAttributeValidation(receivedElement, receivedAttribute, sourceElement, sourceAttribute);431 } else {432 Assert.isTrue(receivedValue.equals(sourceValue),433 ValidationUtils.buildValueMismatchErrorMessage("Values not equal for attribute '"434 + receivedAttributeName + "'", sourceValue, receivedValue));435 }436 if (LOG.isDebugEnabled()) {437 LOG.debug("Attribute '" + receivedAttributeName + "'='" + receivedValue + "': OK");438 }439 }440 /**441 * Perform validation on namespace qualified attribute values if present. This includes the validation of namespace presence442 * and equality.443 * @param receivedElement444 * @param receivedAttribute445 * @param sourceElement446 * @param sourceAttribute447 */448 private void doNamespaceQualifiedAttributeValidation(Node receivedElement, Node receivedAttribute, Node sourceElement, Node sourceAttribute) {449 String receivedValue = receivedAttribute.getNodeValue();450 String sourceValue = sourceAttribute.getNodeValue();451 if (receivedValue.contains(":") && sourceValue.contains(":")) {452 // value has namespace prefix set, do special QName validation453 String receivedPrefix = receivedValue.substring(0, receivedValue.indexOf(':'));454 String sourcePrefix = sourceValue.substring(0, sourceValue.indexOf(':'));455 Map<String, String> receivedNamespaces = XMLUtils.lookupNamespaces(receivedAttribute.getOwnerDocument());456 receivedNamespaces.putAll(XMLUtils.lookupNamespaces(receivedElement));457 if (receivedNamespaces.containsKey(receivedPrefix)) {458 Map<String, String> sourceNamespaces = XMLUtils.lookupNamespaces(sourceAttribute.getOwnerDocument());459 sourceNamespaces.putAll(XMLUtils.lookupNamespaces(sourceElement));460 if (sourceNamespaces.containsKey(sourcePrefix)) {461 Assert.isTrue(sourceNamespaces.get(sourcePrefix).equals(receivedNamespaces.get(receivedPrefix)),462 ValidationUtils.buildValueMismatchErrorMessage("Values not equal for attribute value namespace '"463 + receivedValue + "'", sourceNamespaces.get(sourcePrefix), receivedNamespaces.get(receivedPrefix)));464 // remove namespace prefixes as they must not form equality465 receivedValue = receivedValue.substring((receivedPrefix + ":").length());466 sourceValue = sourceValue.substring((sourcePrefix + ":").length());467 } else {468 throw new ValidationException("Received attribute value '" + receivedAttribute.getLocalName() + "' describes namespace qualified attribute value," +469 " control value '" + sourceValue + "' does not");470 }471 }472 }473 Assert.isTrue(receivedValue.equals(sourceValue),474 ValidationUtils.buildValueMismatchErrorMessage("Values not equal for attribute '"475 + receivedAttribute.getLocalName() + "'", sourceValue, receivedValue));476 }477 /**478 * Handle processing instruction during validation.479 *480 * @param received481 */482 private void doPI(Node received) {483 if (LOG.isDebugEnabled()) {484 LOG.debug("Ignored processing instruction (" + received.getLocalName() + "=" + received.getNodeValue() + ")");485 }486 }487 /**488 * Counts the attributenode for an element (xmlns attributes ignored)489 * @param attributesR attributesMap490 * @return number of attributes491 */492 private int countAttributes(NamedNodeMap attributesR) {493 int cntAttributes = 0;494 for (int i = 0; i < attributesR.getLength(); i++) {495 if (!attributesR.item(i).getNodeName().startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {496 cntAttributes++;...
doPI
Using AI Code Generation
1com.consol.citrus.validation.xml.DomXmlMessageValidator validator = new com.consol.citrus.validation.xml.DomXmlMessageValidator();2validator.setNamespaceAware(true);3validator.setSchemaValidation(true);4validator.setSchema("classpath:com/consol/citrus/validation/xml/employee.xsd");5validator.setSchemaValidationType("XSD");6validator.setSchemaValidationEnforce(true);7validator.setDoctype("employee");8validator.setDoctypeSystem("classpath:com/consol/citrus/validation/xml/employee.dtd");9validator.setDoctypeValidate(true);10validator.setDoctypeEnforce(true);11validator.setDoPI(true);12validator.setPiTarget("employee");13validator.setPiEnforce(true);14validator.setIgnoreWhitespace(true);15validator.setIgnoreComments(true);16validator.setIgnoreProcessingInstructions(true);17validator.setIgnoreEmptyElements(true);18validator.setIgnoreAttributeOrder(true);19validator.setIgnoreNamespacePrefixes(true);20validator.setIgnoreNamespaces(true);21validator.setIgnoreDiffNamespaceAttributes(true);22validator.setIgnoreDiffNamespaceElements(true);23validator.setIgnoreDiffNamespacePrefixes(true);24validator.setIgnoreDiffNamespaceMappings(true);
doPI
Using AI Code Generation
1[org.springframework.context.support.AbstractApplicationContext]: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c1a8f8: startup date [Wed Mar 11 13:52:26 IST 2015]; root of context hierarchy2[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.DomXmlMessageValidator' of type [class com.consol.citrus.validation.xml.DomXmlMessageValidator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)3[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XpathMessageValidationContext' of type [class com.consol.citrus.validation.xml.XpathMessageValidationContext] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)4[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XsdSchemaRepository' of type [class com.consol.citrus.validation.xml.XsdSchemaRepository] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)5[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XsdSchemaRepository' of type [class com.consol.citrus.validation.xml.XsdSchemaRepository] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)6[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XpathMessageValidationContext' of type [class com.consol.citrus.validation.xml.XpathMessageValidationContext] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)7[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.DomXmlMessageValidator' of type [class com.consol.citrus.validation.xml.DomXmlMessageValidator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying
doPI
Using AI Code Generation
1[org.springframework.context.support.AbstractApplicationContext]: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c1a8f8: startup date [Wed Mar 11 13:52:26 IST 2015]; root of context hierarchy2[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.DomXmlMessageValidator' of type [class com.consol.citrus.validation.xml.DomXmlMessageValidator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)3[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XpathMessageValidationContext' of type [class com.consol.citrus.validation.xml.XpathMessageValidationContext] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)4[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XsdSchemaRepository' of type [class com.consol.citrus.validation.xml.XsdSchemaRepository] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)5[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XsdSchemaRepository' of type [class com.consol.citrus.validation.xml.XsdSchemaRepository] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)6[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.XpathMessageValidationContext' of type [class com.consol.citrus.validation.xml.XpathMessageValidationContext] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)7[org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker]: Bean 'com.consol.citrus.validation.xml.DomXmlMessageValidator' of type [class com.consol.citrus.validation.xml.DomXmlMessageValidator] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying
doPI
Using AI Code Generation
1public class MyTest {2 public void myTest() {3 variable("xml", "<myxml><a>1</a><b>2</b></myxml>");4 variable("expectedXml", "<myxml><a>${doPI('myuri', 'mydata')}</a><b>2</b></myxml>");5 echo("XML: ${xml}");6 echo("Expected XML: ${expectedXml}");7 validate("${xml}", "${expectedXml}");8 }9}
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!!