How to use XMLUtils class of com.consol.citrus.util package

Best Citrus code snippet using com.consol.citrus.util.XMLUtils

Source:JUnit4TestReportLoader.java Github

copy

Full Screen

...18import com.consol.citrus.admin.service.TestCaseService;19import com.consol.citrus.admin.service.report.TestReportLoader;20import com.consol.citrus.exceptions.CitrusRuntimeException;21import com.consol.citrus.util.FileUtils;22import com.consol.citrus.util.XMLUtils;23import com.consol.citrus.xml.xpath.XPathUtils;24import org.slf4j.Logger;25import org.slf4j.LoggerFactory;26import org.springframework.beans.factory.annotation.Autowired;27import org.springframework.core.Ordered;28import org.springframework.core.annotation.Order;29import org.springframework.core.io.FileSystemResource;30import org.springframework.core.io.Resource;31import org.springframework.stereotype.Component;32import org.springframework.util.CollectionUtils;33import org.springframework.util.StringUtils;34import org.springframework.util.xml.DomUtils;35import org.w3c.dom.*;36import java.io.File;37import java.io.IOException;38import java.util.*;39/**40 * @author Christoph Deppisch41 */42@Component43@Order(Ordered.LOWEST_PRECEDENCE)44public class JUnit4TestReportLoader implements TestReportLoader {45 /** Logger */46 private static Logger log = LoggerFactory.getLogger(JUnit4TestReportLoader.class);47 @Autowired48 private TestCaseService testCaseService;49 @Override50 public TestReport getLatest(Project activeProject, Test test) {51 TestReport report = new TestReport();52 if (hasTestResults(activeProject)) {53 try {54 String testResultsContent = getTestResultsAsString(activeProject);55 if (StringUtils.hasText(testResultsContent)) {56 Document testResults = XMLUtils.parseMessagePayload(testResultsContent);57 Element testCase = (Element) XPathUtils.evaluateAsNode(testResults, "/testsuite/testcase[@classname = '" + test.getPackageName() + "." + test.getClassName() + "']", null);58 TestResult result = getResult(test, testCase);59 report.setTotal(1);60 if (result.getStatus().equals(TestStatus.PASS)) {61 report.setPassed(1L);62 } else if (result.getStatus().equals(TestStatus.FAIL)) {63 report.setFailed(1L);64 } else if (result.getStatus().equals(TestStatus.SKIP)) {65 report.setSkipped(1L);66 }67 report.getResults().add(result);68 }69 } catch (CitrusRuntimeException e) {70 log.warn("No results found for test: " + test.getPackageName() + "." + test.getClassName() + "#" + test.getMethodName());71 }72 }73 return report;74 }75 @Override76 public TestReport getLatest(Project activeProject) {77 TestReport report = new TestReport();78 if (hasTestResults(activeProject)) {79 String testResultsContent = getTestResultsAsString(activeProject);80 if (StringUtils.hasText(testResultsContent)) {81 Document testResults = XMLUtils.parseMessagePayload(testResultsContent);82 report.setProjectName(activeProject.getName());83 report.setSuiteName(XPathUtils.evaluateAsString(testResults, "/testsuite/@name", null));84 report.setDuration(Math.round(Double.valueOf(XPathUtils.evaluateAsString(testResults, "/testsuite/@time", null)) * 1000));85 report.setFailed(Long.valueOf(XPathUtils.evaluateAsString(testResults, "/testsuite/@failures", null)));86 report.setSkipped(Long.valueOf(XPathUtils.evaluateAsString(testResults, "/testsuite/@skipped", null)));87 report.setTotal(Long.valueOf(XPathUtils.evaluateAsString(testResults, "/testsuite/@tests", null)));88 report.setPassed(report.getTotal() - report.getSkipped() - report.getFailed());89 NodeList testCases = XPathUtils.evaluateAsNodeList(testResults, "/testsuite/testcase", null);90 for (int i = 0; i < testCases.getLength(); i++) {91 Element testCase = (Element) testCases.item(i);92 String className = testCase.getAttribute("classname");93 String methodName = testCase.getAttribute("name");94 String packageName;95 if (className.indexOf(':') > 0 || className.indexOf(' ') > 0) {...

Full Screen

Full Screen

Source:XpathMessageConstructionInterceptor.java Github

copy

Full Screen

...18import com.consol.citrus.exceptions.CitrusRuntimeException;19import com.consol.citrus.exceptions.UnknownElementException;20import com.consol.citrus.message.Message;21import com.consol.citrus.message.MessageType;22import com.consol.citrus.util.XMLUtils;23import com.consol.citrus.validation.interceptor.AbstractMessageConstructionInterceptor;24import com.consol.citrus.xml.xpath.XPathUtils;25import org.slf4j.Logger;26import org.slf4j.LoggerFactory;27import org.springframework.util.StringUtils;28import org.w3c.dom.Document;29import org.w3c.dom.Node;30import java.util.Collections;31import java.util.LinkedHashMap;32import java.util.Map;33import java.util.Map.Entry;34/**35 * Interceptor implementation evaluating XPath expressions on message payload during message construction.36 * Class identifies XML elements inside the message payload via XPath expressions in order to overwrite their value.37 * 38 * @author Christoph Deppisch39 */40public class XpathMessageConstructionInterceptor extends AbstractMessageConstructionInterceptor {41 /** Overwrites message elements before validating (via XPath expressions) */42 private Map<String, String> xPathExpressions = new LinkedHashMap<>();43 44 /** Logger */45 private static Logger log = LoggerFactory.getLogger(XpathMessageConstructionInterceptor.class);46 /**47 * Default constructor.48 */49 public XpathMessageConstructionInterceptor() {50 super();51 }52 /**53 * Default constructor using fields.54 * @param xPathExpressions The xPaths to apply to the messages55 */56 public XpathMessageConstructionInterceptor(final Map<String, String> xPathExpressions) {57 super();58 this.xPathExpressions.putAll(xPathExpressions);59 }60 /**61 * Intercept the message payload construction and replace elements identified 62 * via XPath expressions.63 *64 * Method parses the message payload to DOM document representation, therefore message payload65 * needs to be XML here.66 */67 @Override68 public Message interceptMessage(final Message message, final String messageType, final TestContext context) {69 if (message.getPayload() == null || !StringUtils.hasText(message.getPayload(String.class))) {70 return message;71 }72 final Document doc = XMLUtils.parseMessagePayload(message.getPayload(String.class));73 if (doc == null) {74 throw new CitrusRuntimeException("Not able to set message elements, because no XML ressource defined");75 }76 for (final Entry<String, String> entry : xPathExpressions.entrySet()) {77 final String pathExpression = entry.getKey();78 String valueExpression = entry.getValue();79 //check if value expr is variable or function (and resolve it if yes)80 valueExpression = context.replaceDynamicContentInString(valueExpression);81 final Node node;82 if (XPathUtils.isXPathExpression(pathExpression)) {83 node = XPathUtils.evaluateAsNode(doc, pathExpression,84 context.getNamespaceContextBuilder().buildContext(message, Collections.emptyMap()));85 } else {86 node = XMLUtils.findNodeByName(doc, pathExpression);87 }88 if (node == null) {89 throw new UnknownElementException("Could not find element for expression" + pathExpression);90 }91 if (node.getNodeType() == Node.ELEMENT_NODE) {92 //fix: otherwise there will be a new line in the output93 node.setTextContent(valueExpression);94 } else {95 node.setNodeValue(valueExpression);96 }97 98 if (log.isDebugEnabled()) {99 log.debug("Element " + pathExpression + " was set to value: " + valueExpression);100 }101 }102 103 message.setPayload(XMLUtils.serialize(doc));104 return message;105 }106 @Override107 public boolean supportsMessageType(final String messageType) {108 return MessageType.XML.toString().equalsIgnoreCase(messageType) || MessageType.XHTML.toString().equalsIgnoreCase(messageType);109 }110 /**111 * @param xPathExpressions the xPathExpressions to set112 */113 public void setXPathExpressions(final Map<String, String> xPathExpressions) {114 this.xPathExpressions = xPathExpressions;115 }116 /**117 * Gets the xPathExpressions....

Full Screen

Full Screen

Source:AbstractXmlDataDictionary.java Github

copy

Full Screen

...16package com.consol.citrus.variable.dictionary.xml;17import com.consol.citrus.context.TestContext;18import com.consol.citrus.message.Message;19import com.consol.citrus.message.MessageType;20import com.consol.citrus.util.XMLUtils;21import com.consol.citrus.validation.xhtml.XhtmlMessageConverter;22import com.consol.citrus.variable.dictionary.AbstractDataDictionary;23import org.springframework.util.StringUtils;24import org.springframework.util.xml.DomUtils;25import org.w3c.dom.*;26import org.w3c.dom.ls.*;27import org.w3c.dom.traversal.NodeFilter;28import java.io.StringWriter;29/**30 * Abstract data dictionary works on XML message payloads only with parsing the document and translating each element31 * and attribute with respective value in dictionary.32 *33 * @author Christoph Deppisch34 * @since 1.435 */36public abstract class AbstractXmlDataDictionary extends AbstractDataDictionary<Node> {37 @Override38 protected Message interceptMessage(Message message, String messageType, TestContext context) {39 if (message.getPayload() == null || !StringUtils.hasText(message.getPayload(String.class))) {40 return message;41 }42 String messagePayload = message.getPayload(String.class);43 if (MessageType.XHTML.name().equalsIgnoreCase(messageType)) {44 messagePayload = new XhtmlMessageConverter().convert(messagePayload);45 }46 Document doc = XMLUtils.parseMessagePayload(messagePayload);47 LSSerializer serializer = XMLUtils.createLSSerializer();48 serializer.setFilter(new TranslateFilter(context));49 LSOutput output = XMLUtils.createLSOutput();50 String charset = XMLUtils.getTargetCharset(doc).displayName();51 output.setEncoding(charset);52 StringWriter writer = new StringWriter();53 output.setCharacterStream(writer);54 serializer.write(doc, output);55 message.setPayload(writer.toString());56 return message;57 }58 /**59 * Serializer filter uses data dictionary translation on elements and attributes.60 */61 private class TranslateFilter implements LSSerializerFilter {62 private TestContext context;63 public TranslateFilter(TestContext context) {64 this.context = context;...

Full Screen

Full Screen

XMLUtils

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.util;2import java.io.IOException;3import java.util.Map;4import java.util.Map.Entry;5import java.util.Set;6import javax.xml.parsers.DocumentBuilder;7import javax.xml.parsers.DocumentBuilderFactory;8import javax.xml.parsers.ParserConfigurationException;9import javax.xml.transform.TransformerException;10import org.slf4j.Logger;11import org.slf4j.LoggerFactory;12import org.springframework.util.CollectionUtils;13import org.springframework.util.StringUtils;14import org.w3c.dom.Document;15import org.w3c.dom.Element;16import org.w3c.dom.Node;17import org.w3c.dom.NodeList;18import org.xml.sax.SAXException;19public final class XMLUtils {20private static final Logger LOG = LoggerFactory.getLogger(XMLUtils.class);21private XMLUtils() {22}23public static String getXmlValue(String xml, String xpath) {24Document document = createDocument(xml);25NodeList nodes = (NodeList) XpathUtils.evaluateAsNodeList(xpath, document);26if (nodes.getLength() > 0) {27return nodes.item(0).getTextContent();28}29return null;30}31public static Map<String, String> getXmlValues(String xml, String xpath) {32Document document = createDocument(xml);33NodeList nodes = (NodeList) XpathUtils.evaluateAsNodeList(xpath, document);34Map<String, String> values = new HashMap<String, String>();35for (int i = 0; i < nodes.getLength(); i++) {36values.put(nodes.item(i).getNodeName(), nodes.item(i).getTextContent());37}38return values;39}40public static void addXmlValues(String xml, Map<String, String> values) {41Document document = createDocument(xml);42NodeList nodes = (NodeList) XpathUtils.evaluateAsNodeList("/*", document);43Node root = nodes.item(0);44for (Entry<String, String> entry : values.entrySet()) {45Element element = document.createElement(entry.getKey());46element.setTextContent(entry.getValue());47root.appendChild(element);48}49}50public static void updateXmlValues(String xml, Map<String, String> values) {51Document document = createDocument(xml);52for (Entry<String, String> entry : values.entrySet()) {53NodeList nodes = (NodeList) XpathUtils.evaluateAsNodeList(entry.getKey(), document);54for (int i = 0; i < nodes.getLength(); i++) {55nodes.item(i).setTextContent(entry.getValue());56}57}58}59public static void removeXmlValues(String xml, String xpath) {60Document document = createDocument(xml);

Full Screen

Full Screen

XMLUtils

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.samples;2import com.consol.citrus.util.XMLUtils;3import org.springframework.context.support.ClassPathXmlApplicationContext;4import org.springframework.core.io.Resource;5import org.testng.annotations.Test;6public class XMLUtilsTest {7 public void testXMLUtils() {8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:com/consol/citrus/samples/4.xml"});9 Resource resource = context.getResource("classpath:com/consol/citrus/samples/4.xml");10 XMLUtils.validateXML(resource);11 }12}13package com.consol.citrus.samples;14import com.consol.citrus.util.XMLUtils;15import org.springframework.context.support.ClassPathXmlApplicationContext;16import org.springframework.core.io.Resource;17import org.testng.annotations.Test;18public class XMLUtilsTest {19 public void testXMLUtils() {20 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:com/consol/citrus/samples/5.xml"});21 Resource resource = context.getResource("classpath:com/consol/citrus/samples/5.xml");22 XMLUtils.validateXML(resource);23 }24}

Full Screen

Full Screen

XMLUtils

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import com.consol.citrus.util.XMLUtils;3import org.springframework.core.io.ClassPathResource;4import org.testng.annotations.Test;5import static org.testng.Assert.assertEquals;6import static org.testng.Assert.assertNotNull;7import static org.testng.Assert.assertTrue;8public class XMLUtilsTest {9public void testXmlUtils() throws Exception {10String xml = XMLUtils.parseMessagePayload(new ClassPathResource("xml-utils-test.xml").getInputStream());11assertTrue(xml.contains("Hello"));12assertTrue(xml.contains("World"));13assertTrue(xml.contains("Citrus"));14}15}16package com.consol.citrus;17import com.consol.citrus.util.XMLUtils;18import org.springframework.core.io.ClassPathResource;19import org.testng.annotations.Test;20import static org.testng.Assert.assertEquals;21import static org.testng.Assert.assertNotNull;22import static org.testng.Assert.assertTrue;23public class XMLUtilsTest {24public void testXmlUtils() throws Exception {25String xml = XMLUtils.parseMessagePayload(new ClassPathResource("xml-utils-test.xml").getInputStream());26assertTrue(xml.contains("Hello"));27assertTrue(xml.contains("World"));28assertTrue(xml.contains("Citrus"));29}30}31package com.consol.citrus;32import com.consol.citrus.util.XMLUtils;33import org.springframework.core.io.ClassPathResource;34import org.testng.annotations.Test;35import static org.testng.Assert.assertEquals;36import static org.testng.Assert.assertNotNull;37import static org.testng.Assert.assertTrue;38public class XMLUtilsTest {39public void testXmlUtils() throws Exception {40String xml = XMLUtils.parseMessagePayload(new ClassPathResource("xml-utils-test.xml").getInputStream());41assertTrue(xml.contains("Hello"));42assertTrue(xml.contains("World"));43assertTrue(xml.contains("Citrus"));44}45}46package com.consol.citrus;47import com.consol.citrus.util.XMLUtils;48import org.springframework.core.io.ClassPathResource;49import org.testng.annotations.Test;50import static org.testng.Assert.assertEquals;51import static org.testng.Assert.assertNotNull;52import static org.testng.Assert.assertTrue;53public class XMLUtilsTest {54public void testXmlUtils() throws Exception {55String xml = XMLUtils.parseMessagePayload(new ClassPathResource

Full Screen

Full Screen

XMLUtils

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.util.XMLUtils;2import org.springframework.core.io.ClassPathResource;3import org.w3c.dom.Document;4import org.w3c.dom.NodeList;5public class 4 {6 public static void main(String[] args) throws Exception {7 Document doc = XMLUtils.parseMessagePayload(new ClassPathResource("test.xml").getInputStream());8 NodeList nodes = doc.getElementsByTagName("name");9 System.out.println(nodes.item(0).getTextContent());10 }11}

Full Screen

Full Screen

XMLUtils

Using AI Code Generation

copy

Full Screen

1import java.io.File;2import java.io.IOException;3import org.springframework.util.xml.SimpleNamespaceContext;4import org.springframework.xml.xpath.Jaxp13XPathTemplate;5import org.w3c.dom.Document;6import org.xml.sax.SAXException;7import com.consol.citrus.util.XMLUtils;8public class TestXMLUtils {9 public static void main(String[] args) throws SAXException, IOException {10 Document doc = XMLUtils.parseMessagePayload(new File("C:\\Users\\Shubham\\Desktop\\4.xml"));11 Jaxp13XPathTemplate xpathTemplate = new Jaxp13XPathTemplate();12 SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();13 xpathTemplate.setNamespaceContext(namespaceContext);14 System.out.println(value);15 }16}

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.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful