Best Citrus code snippet using com.consol.citrus.validation.xml.DomXmlMessageValidator.doElement
Source:DomXmlMessageValidator.java
...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");199 return;200 } else if (!StringUtils.hasText(controlMessagePayload)) {201 return;202 }203 LOG.debug("Start XML tree validation ...");204 Document received = XMLUtils.parseMessagePayload(receivedMessage.getPayload(String.class));205 Document source = XMLUtils.parseMessagePayload(controlMessagePayload);206 XMLUtils.stripWhitespaceNodes(received);207 XMLUtils.stripWhitespaceNodes(source);208 if (LOG.isDebugEnabled()) {209 log.debug("Received message:\n" + context.getLogModifier().mask(XMLUtils.serialize(received)));210 log.debug("Control message:\n" + context.getLogModifier().mask(XMLUtils.serialize(source)));211 }212 validateXmlTree(received, source, validationContext, getNamespaceContextBuilder(context)213 .buildContext(receivedMessage, validationContext.getNamespaces()), context);214 }215 /**216 * Validates XML header fragment data.217 * @param receivedHeaderData218 * @param controlHeaderData219 * @param validationContext220 * @param context221 */222 private void validateXmlHeaderFragment(String receivedHeaderData, String controlHeaderData,223 XmlMessageValidationContext validationContext, TestContext context) {224 LOG.debug("Start XML header data validation ...");225 Document received = XMLUtils.parseMessagePayload(receivedHeaderData);226 Document source = XMLUtils.parseMessagePayload(controlHeaderData);227 XMLUtils.stripWhitespaceNodes(received);228 XMLUtils.stripWhitespaceNodes(source);229 if (LOG.isDebugEnabled()) {230 LOG.debug("Received header data:\n" + XMLUtils.serialize(received));231 LOG.debug("Control header data:\n" + XMLUtils.serialize(source));232 }233 validateXmlTree(received, source, validationContext,234 getNamespaceContextBuilder(context)235 .buildContext(new DefaultMessage(receivedHeaderData), validationContext.getNamespaces()), context);236 }237 /**238 * Walk the XML tree and validate all nodes.239 *240 * @param received241 * @param source242 * @param validationContext243 */244 private void validateXmlTree(Node received, Node source,245 XmlMessageValidationContext validationContext, NamespaceContext namespaceContext, TestContext context) {246 switch(received.getNodeType()) {247 case Node.DOCUMENT_TYPE_NODE:248 doDocumentTypeDefinition(received, source, validationContext, namespaceContext, context);249 break;250 case Node.DOCUMENT_NODE:251 validateXmlTree(received.getFirstChild(), source.getFirstChild(),252 validationContext, namespaceContext, context);253 break;254 case Node.ELEMENT_NODE:255 doElement(received, source, validationContext, namespaceContext, context);256 break;257 case Node.ATTRIBUTE_NODE:258 throw new IllegalStateException();259 case Node.COMMENT_NODE: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++) {...
doElement
Using AI Code Generation
1package com.consol.citrus.validation.xml;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.exceptions.ValidationException;4import org.springframework.util.xml.DomUtils;5import org.w3c.dom.Document;6import org.w3c.dom.Element;7import java.util.List;8public class DomXmlMessageValidator extends XmlMessageValidator {9 public void validateMessagePayload(String messagePayload, TestContext context) {10 try {11 Document document = getXmlUtils().parseMessagePayload(messagePayload);12 Element rootElement = document.getDocumentElement();13 List<Element> elements = DomUtils.getChildElementsByTagName(rootElement, "element");14 doElement(elements, rootElement, context);15 } catch (Exception e) {16 throw new ValidationException("Failed to validate XML message payload", e);17 }18 }19 private void doElement(List<Element> elements, Element rootElement, TestContext context) {20 for (Element element : elements) {21 String name = element.getAttribute("name");22 String value = element.getAttribute("value");23 if (name.equals("author")) {24 rootElement.setAttribute(name, value);25 }26 if (name.equals("title")) {27 rootElement.setAttribute(name, value);28 }29 if (name.equals("genre")) {30 rootElement.setAttribute(name, value);31 }32 if (name.equals("price")) {33 rootElement.setAttribute(name, value);34 }35 if (name.equals("publish_date")) {36 rootElement.setAttribute(name, value);37 }38 if (name.equals("description")) {39 rootElement.setAttribute(name, value);40 }41 }42 }43}44[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ citrus-demo ---45[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ citrus-demo ---46[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ citrus-demo ---
doElement
Using AI Code Generation
1import com.consol.citrus.dsl.builder.HttpClientActionBuilder2import com.consol.citrus.dsl.builder.HttpServerActionBuilder3import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder4import com.consol.citrus.dsl.builder.HttpActionBuilder5import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder6import com.consol.citrus.dsl.builder.HttpServerActionBuilder7import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder8import com.consol.citrus.dsl.builder.HttpActionBuilder9import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder10import com.consol.citrus.dsl.builder.HttpServerActionBuilder11import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder12import com.consol.citrus.dsl.builder.HttpActionBuilder13import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder14import com.consol.citrus.dsl.builder.HttpServerActionBuilder15import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder16import com.consol.citrus.dsl.builder.HttpActionBuilder17import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder18import com.consol.citrus.dsl.builder.HttpServerActionBuilder19import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder20import com.consol.citrus.dsl.builder.HttpActionBuilder21import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder22import com.consol.citrus.dsl.builder.HttpServerActionBuilder23import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder24import com.consol.citrus.dsl.builder.HttpActionBuilder25import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder26import com.consol.citrus.dsl.builder.HttpServerActionBuilder27import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder28import com.consol.citrus.dsl.builder.HttpActionBuilder29import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder30import com.consol.citrus.dsl.builder.HttpServerActionBuilder31import com.consol.citrus.dsl.builder.HttpServerResponseActionBuilder32import com.consol.citrus.dsl.builder.HttpActionBuilder33import com.consol.citrus.dsl.builder.HttpServerRequestActionBuilder34import com.consol.citrus.dsl.design.TestDesigner35import com.consol.citrus.dsl.design.TestDesigner36import com.consol
doElement
Using AI Code Generation
1package com.consol.citrus.validation.xml;2import java.util.Map;3import com.consol.citrus.exceptions.CitrusRuntimeException;4import com.consol.citrus.message.Message;5import com.consol.citrus.validation.MessageValidator;6import com.consol.citrus.validation.context.ValidationContext;7import com.consol.citrus.validation.matcher.ValidationMatcherUtils;8import com.consol.citrus.validation.xml.XmlMessageValidationContext;9import org.slf4j.Logger;10import org.slf4j.LoggerFactory;11import org.springframework.util.StringUtils;12import org.w3c.dom.Document;13import org.w3c.dom.Node;14import org.w3c.dom.NodeList;15public class DomXmlMessageValidator extends AbstractXmlMessageValidator implements MessageValidator<Document> {16 private static Logger log = LoggerFactory.getLogger(DomXmlMessageValidator.class);17 public void validateMessagePayload(Message receivedMessage, Message controlMessage, XmlMessageValidationContext context) {18 if (log.isDebugEnabled()) {19 log.debug("Validating XML payload ...");20 }21 if (receivedMessage.getPayload() instanceof Document) {22 Document receivedDocument = (Document) receivedMessage.getPayload();23 Document controlDocument = (Document) controlMessage.getPayload();24 validateDocument(receivedDocument, controlDocument, context);25 } else {26 throw new CitrusRuntimeException("Invalid payload type for XML message validation. Expected 'Document' but was '" +27 receivedMessage.getPayload().getClass() + "'");28 }29 }30 protected void validateDocument(Document receivedDocument, Document controlDocument, XmlMessageValidationContext context) {31 Node controlNode = controlDocument.getDocumentElement();32 Node receivedNode = receivedDocument.getDocumentElement();33 if (context.isIgnoreNamespaces()) {34 controlNode = stripNamespaces(controlNode);35 receivedNode = stripNamespaces(receivedNode);36 }37 if (context.isIgnoreWhitespace()) {38 controlNode = stripWhitespace(controlNode);39 receivedNode = stripWhitespace(receivedNode);40 }41 doElement(receivedNode, controlNode, context);42 }
doElement
Using AI Code Generation
1import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;2import com.consol.citrus.dsl.runner.TestRunner;3import com.consol.citrus.dsl.runner.TestRunnerBeforeTestSupport;4import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;5import com.consol.citrus.dsl.junit.JUnit4CitrusTest;6import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;7import com.consol.citrus.dsl.junit.JUnit4CitrusTest;8import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;9import com.consol.citrus.dsl.junit.JUnit4CitrusTest;10import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;11import com.consol.citrus.dsl.junit.JUnit4CitrusTest;12import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;13import com.consol.citrus.dsl.junit.JUnit4CitrusTest;14import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;15import com.consol.citrus.dsl.junit.JUnit4CitrusTest;16import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;17import com.consol.citrus.dsl.junit.JUnit4CitrusTest;18import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;19import com.consol.citrus.dsl.junit.JUnit4CitrusTest;20import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;21import com.consol.citrus.dsl.junit.JUnit4CitrusTest;22import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;23import com.consol.citrus.dsl.junit.JUnit4CitrusTest;24import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;25import com.consol.citrus.dsl.junit.JUnit4CitrusTest;26import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;27import com.consol.citrus.dsl.junit.JUnit4CitrusTest;28import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;29import com.consol.citrus.dsl.junit.JUnit4CitrusTest;30import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;31import com.consol.citrus.dsl.junit
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!!