How to use XmlConfigurer class of com.consol.citrus.xml package

Best Citrus code snippet using com.consol.citrus.xml.XmlConfigurer

Source:WsdlXmlTestGenerator.java Github

copy

Full Screen

...23import com.consol.citrus.message.MessageType;24import com.consol.citrus.model.testcase.ws.ObjectFactory;25import com.consol.citrus.util.XMLUtils;26import com.consol.citrus.ws.message.SoapMessage;27import com.consol.citrus.xml.XmlConfigurer;28import org.apache.xmlbeans.*;29import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil;30import org.springframework.core.io.support.PathMatchingResourcePatternResolver;31import org.springframework.util.StringUtils;32import java.io.File;33import java.io.IOException;34import java.util.*;35/**36 * Test generator creates one to many test cases based on operations defined in a XML schema XSD.37 * @author Christoph Deppisch38 * @since 2.7.439 */40public class WsdlXmlTestGenerator extends MessagingXmlTestGenerator<WsdlXmlTestGenerator> implements WsdlTestGenerator<WsdlXmlTestGenerator> {41 private String wsdl;42 private String operation;43 private String namePrefix;44 private String nameSuffix = "_IT";45 private InboundXmlDataDictionary inboundDataDictionary = new InboundXmlDataDictionary();46 private OutboundXmlDataDictionary outboundDataDictionary = new OutboundXmlDataDictionary();47 @Override48 public void create() {49 String wsdlNsDelaration = "declare namespace wsdl='http://schemas.xmlsoap.org/wsdl/' ";50 String soapNsDelaration = "declare namespace soap='http://schemas.xmlsoap.org/wsdl/soap/' ";51 // compile wsdl and xsds right now, otherwise later input is useless:52 XmlObject wsdlObject = compileWsdl(wsdl);53 SchemaTypeSystem schemaTypeSystem = compileXsd(wsdlObject);54 log.info("WSDL compilation successful");55 String serviceName = evaluateAsString(wsdlObject, wsdlNsDelaration + ".//wsdl:portType/@name");56 log.info("Found service: " + serviceName);57 if (!StringUtils.hasText(namePrefix)) {58 withNamePrefix(serviceName + "_");59 }60 log.info("Found service operations:");61 XmlObject[] messages = wsdlObject.selectPath(wsdlNsDelaration + ".//wsdl:message");62 XmlObject[] operations = wsdlObject.selectPath(wsdlNsDelaration + ".//wsdl:portType/wsdl:operation");63 for (XmlObject operation : operations) {64 log.info(evaluateAsString(operation, wsdlNsDelaration + "./@name"));65 }66 log.info("Generating test cases for service operations ...");67 for (XmlObject operation : operations) {68 SoapMessage request = new SoapMessage();69 SoapMessage response = new SoapMessage();70 String operationName = evaluateAsString(operation, wsdlNsDelaration + "./@name");71 if (StringUtils.hasText(this.operation) && !operationName.equals(this.operation)) {72 continue;73 }74 XmlObject[] bindingOperations = wsdlObject.selectPath(wsdlNsDelaration + ".//wsdl:binding/wsdl:operation");75 for (XmlObject bindingOperation : bindingOperations) {76 String bindingOperationName = evaluateAsString(bindingOperation, wsdlNsDelaration + "./@name");77 if (bindingOperationName.equals(operationName)) {78 String soapAction = removeNsPrefix(evaluateAsString(bindingOperation, soapNsDelaration + "./soap:operation/@soapAction"));79 request.soapAction(soapAction);80 break;81 }82 }83 String inputMessage = removeNsPrefix(evaluateAsString(operation, wsdlNsDelaration + "./wsdl:input/@message"));84 String outputMessage = removeNsPrefix(evaluateAsString(operation, wsdlNsDelaration + "./wsdl:output/@message"));85 String inputElement = null;86 String outputElement = null;87 for (XmlObject message : messages) {88 String messageName = evaluateAsString(message, wsdlNsDelaration + "./@name");89 if (messageName.equals(inputMessage)) {90 inputElement = removeNsPrefix(evaluateAsString(message, wsdlNsDelaration + "./wsdl:part/@element"));91 }92 if (messageName.equals(outputMessage)) {93 outputElement = removeNsPrefix(evaluateAsString(message, wsdlNsDelaration + "./wsdl:part/@element"));94 }95 }96 // Now generate it97 withName(namePrefix + operationName + nameSuffix);98 SchemaType requestElem = getSchemaType(schemaTypeSystem, operationName, inputElement);99 request.setPayload(SampleXmlUtil.createSampleForType(requestElem));100 withRequest(request);101 SchemaType responseElem = getSchemaType(schemaTypeSystem, operationName, outputElement);102 response.setPayload(SampleXmlUtil.createSampleForType(responseElem));103 withResponse(response);104 XmlConfigurer configurer = new XmlConfigurer();105 configurer.setSerializeSettings(Collections.singletonMap(XmlConfigurer.XML_DECLARATION, false));106 XMLUtils.initialize(configurer);107 super.create();108 log.info("Successfully created new test case " + getTargetPackage() + "." + getName());109 }110 }111 @Override112 protected List<String> getMarshallerContextPaths() {113 List<String> contextPaths = super.getMarshallerContextPaths();114 contextPaths.add(ObjectFactory.class.getPackage().getName());115 return contextPaths;116 }117 @Override118 protected Message generateInboundMessage(Message message) {119 return inboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());...

Full Screen

Full Screen

Source:XsdJavaTestGenerator.java Github

copy

Full Screen

...20import com.consol.citrus.generate.dictionary.InboundXmlDataDictionary;21import com.consol.citrus.generate.dictionary.OutboundXmlDataDictionary;22import com.consol.citrus.message.*;23import com.consol.citrus.util.XMLUtils;24import com.consol.citrus.xml.XmlConfigurer;25import org.apache.xmlbeans.*;26import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil;27import org.springframework.core.io.support.PathMatchingResourcePatternResolver;28import org.springframework.util.StringUtils;29import java.io.File;30import java.io.IOException;31import java.util.Collections;32import java.util.Map;33/**34 * Test generator creates one to many test cases based on operations defined in a XML schema XSD.35 * @author Christoph Deppisch36 * @since 2.7.437 */38public class XsdJavaTestGenerator extends MessagingJavaTestGenerator<XsdJavaTestGenerator> implements XsdTestGenerator<XsdJavaTestGenerator> {39 private String xsd;40 private String requestMessage;41 private String responseMessage;42 private String nameSuffix = "IT";43 private InboundXmlDataDictionary inboundDataDictionary = new InboundXmlDataDictionary();44 private OutboundXmlDataDictionary outboundDataDictionary = new OutboundXmlDataDictionary();45 @Override46 public void create() {47 SchemaTypeSystem schemaTypeSystem = compileXsd(xsd);48 SchemaType[] globalElems = schemaTypeSystem.documentTypes();49 SchemaType requestElem = null;50 SchemaType responseElem = null;51 if (!StringUtils.hasText(getName())) {52 withName(getTestNameSuggestion());53 }54 if (!StringUtils.hasText(responseMessage)) {55 responseMessage = getResponseMessageSuggestion();56 }57 for (SchemaType elem : globalElems) {58 if (elem.getContentModel().getName().getLocalPart().equals(requestMessage)) {59 requestElem = elem;60 break;61 }62 }63 for (SchemaType elem : globalElems) {64 if (elem.getContentModel().getName().getLocalPart().equals(responseMessage)) {65 responseElem = elem;66 break;67 }68 }69 if (requestElem != null) {70 withRequest(new DefaultMessage(SampleXmlUtil.createSampleForType(requestElem)));71 } else {72 throw new CitrusRuntimeException(String.format("Unable to find element with name '%s' in XSD %s", requestMessage, xsd));73 }74 if (responseElem != null) {75 withResponse(new DefaultMessage(SampleXmlUtil.createSampleForType(responseElem)));76 } else {77 withResponse(null);78 }79 XmlConfigurer configurer = new XmlConfigurer();80 configurer.setSerializeSettings(Collections.singletonMap(XmlConfigurer.XML_DECLARATION, false));81 XMLUtils.initialize(configurer);82 super.create();83 }84 @Override85 protected Message generateInboundMessage(Message message) {86 return inboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());87 }88 @Override89 protected Message generateOutboundMessage(Message message) {90 return outboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());91 }92 /**93 * Suggest name of response element based on request message element name.94 * @return...

Full Screen

Full Screen

Source:XsdXmlTestGenerator.java Github

copy

Full Screen

...20import com.consol.citrus.generate.dictionary.InboundXmlDataDictionary;21import com.consol.citrus.generate.dictionary.OutboundXmlDataDictionary;22import com.consol.citrus.message.*;23import com.consol.citrus.util.XMLUtils;24import com.consol.citrus.xml.XmlConfigurer;25import org.apache.xmlbeans.*;26import org.apache.xmlbeans.impl.xsd2inst.SampleXmlUtil;27import org.springframework.core.io.support.PathMatchingResourcePatternResolver;28import org.springframework.util.StringUtils;29import java.io.File;30import java.io.IOException;31import java.util.Collections;32import java.util.Map;33/**34 * Test generator creates one to many test cases based on operations defined in a XML schema XSD.35 * @author Christoph Deppisch36 * @since 2.7.437 */38public class XsdXmlTestGenerator extends MessagingXmlTestGenerator<XsdXmlTestGenerator> implements XsdTestGenerator<XsdXmlTestGenerator> {39 private String xsd;40 private String requestMessage;41 private String responseMessage;42 private String nameSuffix = "IT";43 private InboundXmlDataDictionary inboundDataDictionary = new InboundXmlDataDictionary();44 private OutboundXmlDataDictionary outboundDataDictionary = new OutboundXmlDataDictionary();45 @Override46 public void create() {47 SchemaTypeSystem schemaTypeSystem = compileXsd(xsd);48 SchemaType[] globalElems = schemaTypeSystem.documentTypes();49 SchemaType requestElem = null;50 SchemaType responseElem = null;51 if (!StringUtils.hasText(getName())) {52 withName(getTestNameSuggestion());53 }54 if (!StringUtils.hasText(responseMessage)) {55 responseMessage = getResponseMessageSuggestion();56 }57 for (SchemaType elem : globalElems) {58 if (elem.getContentModel().getName().getLocalPart().equals(requestMessage)) {59 requestElem = elem;60 break;61 }62 }63 for (SchemaType elem : globalElems) {64 if (elem.getContentModel().getName().getLocalPart().equals(responseMessage)) {65 responseElem = elem;66 break;67 }68 }69 if (requestElem != null) {70 withRequest(new DefaultMessage(SampleXmlUtil.createSampleForType(requestElem)));71 } else {72 throw new CitrusRuntimeException(String.format("Unable to find element with name '%s' in XSD %s", requestMessage, xsd));73 }74 if (responseElem != null) {75 withResponse(new DefaultMessage(SampleXmlUtil.createSampleForType(responseElem)));76 } else {77 withResponse(null);78 }79 XmlConfigurer configurer = new XmlConfigurer();80 configurer.setSerializeSettings(Collections.singletonMap(XmlConfigurer.XML_DECLARATION, false));81 XMLUtils.initialize(configurer);82 super.create();83 }84 @Override85 protected Message generateInboundMessage(Message message) {86 return inboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());87 }88 @Override89 protected Message generateOutboundMessage(Message message) {90 return outboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());91 }92 /**93 * Suggest name of response element based on request message element name.94 * @return...

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.xml;2import com.consol.citrus.context.TestContext;3import com.consol.citrus.exceptions.CitrusRuntimeException;4import com.consol.citrus.util.FileUtils;5import com.consol.citrus.util.XMLUtils;6import org.springframework.util.StringUtils;7import org.springframework.xml.xpath.XPathExpression;8import org.springframework.xml.xpath.XPathExpressionFactory;9import org.springframework.xml.xpath.XPathOperations;10import org.springframework.xml.xpath.XPathOperationsImpl;11import org.w3c.dom.Document;12import org.w3c.dom.Node;13import org.xml.sax.SAXException;14import javax.xml.namespace.NamespaceContext;15import javax.xml.parsers.ParserConfigurationException;16import javax.xml.transform.*;17import javax.xml.transform.dom.DOMSource;18import javax.xml.transform.stream.StreamResult;19import java.io.*;20import java.util.HashMap;21import java.util.Map;22import java.util.Properties;23public class XmlConfigurer {24 private Document document;25 private XPathOperations xpathOperations = new XPathOperationsImpl();26 private Properties properties = new Properties();27 public XmlConfigurer(Document document) {28 this.document = document;29 }30 public XmlConfigurer(Document document, Properties properties) {31 this.document = document;32 this.properties = properties;33 }34 public XmlConfigurer(Document document, XPathOperations xpathOperations) {35 this.document = document;36 this.xpathOperations = xpathOperations;37 }38 public XmlConfigurer(Document document, XPathOperations xpathOperations, Properties properties) {39 this.document = document;40 this.xpathOperations = xpathOperations;41 this.properties = properties;42 }

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.xml;2import java.util.HashMap;3import java.util.Map;4import org.testng.annotations.Test;5import com.consol.citrus.context.TestContext;6import com.consol.citrus.context.TestContextFactory;7import com.consol.citrus.exceptions.CitrusRuntimeException;8import com.consol.citrus.testng.AbstractTestNGUnitTest;9public class XmlConfigurerTest extends AbstractTestNGUnitTest {10 public void testXmlConfigurer() {11 Map<String, String> namespaces = new HashMap<String, String>();12 XmlConfigurer configurer = new XmlConfigurer(namespaces);13 TestContext context = TestContextFactory.newInstance().create();14 context.setVariable("name", "John");15 context.setVariable("lastName", "Doe");16 context.setVariable("age", 25);17 String xml = configurer.configure("<ns:Person><ns:Name>${name}</ns:Name><ns:LastName>${lastName}</ns:LastName><ns:Age>${age}</ns:Age></ns:Person>", context);18 assertXpathEvaluatesTo("John", "/ns:Person/ns:Name", xml);19 assertXpathEvaluatesTo("Doe", "/ns:Person/ns:LastName", xml);20 assertXpathEvaluatesTo("25", "/ns:Person/ns:Age", xml);21 }22 @Test(expectedExceptions = CitrusRuntimeException.class)23 public void testXmlConfigurerWithMissingNamespace() {24 Map<String, String> namespaces = new HashMap<String, String>();25 XmlConfigurer configurer = new XmlConfigurer(namespaces);26 TestContext context = TestContextFactory.newInstance().create();27 context.setVariable("name", "John");28 context.setVariable("lastName", "Doe");29 context.setVariable("age", 25);30 configurer.configure("<ns:Person><ns:Name>${name}</ns:Name><ns:LastName>${lastName}</ns:LastName><ns:Age>${age}</ns:Age></ns:Person>", context);31 }32}

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.xml.XmlConfigurer;2import org.springframework.core.io.ClassPathResource;3import org.springframework.core.io.Resource;4import org.springframework.util.xml.DomUtils;5import org.w3c.dom.Document;6import org.w3c.dom.Element;7import javax.xml.parsers.DocumentBuilder;8import javax.xml.parsers.DocumentBuilderFactory;9import java.io.File;10import java.io.IOException;11public class 4 {12 public static void main(String[] args) throws IOException {13 "</my:root>";14 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();15 factory.setNamespaceAware(true);16 DocumentBuilder builder = factory.newDocumentBuilder();17 Document document = builder.parse(new File("src/main/resources/4.xml"));18 Element rootElement = document.getDocumentElement();19 XmlConfigurer configurer = new XmlConfigurer();20 configurer.setDocument(document);21 configurer.setRootElement(rootElement);22 configurer.setNamespaceAware(true);23 configurer.configureElement(DomUtils.getChildElementByTagName(rootElement, "my:child"), "Child value");24 configurer.configureElement(DomUtils.getChildElementByTagName(rootElement, "my:child"), "Child value");25 System.out.println(configurer.getDocument().getDocumentElement().getFirstChild().getFirstChild().getNodeValue());26 }27}28import com.consol.citrus.xml.XmlUtils;29import org.springframework.core.io.ClassPathResource;30import org.springframework.core.io.Resource;31import org.springframework.util.xml.DomUtils;32import org.w3c.dom.Document;33import org.w3c.dom.Element;34import javax.xml.parsers.DocumentBuilder;35import javax.xml.parsers.DocumentBuilderFactory;36import java.io.File;37import java.io.IOException;38public class 5 {39 public static void main(String[] args) throws IOException {40 "</my:root>";41 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();42 factory.setNamespaceAware(true);43 DocumentBuilder builder = factory.newDocumentBuilder();

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.xml;2import org.springframework.xml.xpath.XPathExpression;3import org.springframework.xml.xpath.XPathExpressionFactory;4import org.testng.Assert;5import org.testng.annotations.Test;6import org.w3c.dom.Document;7import org.w3c.dom.Element;8import org.w3c.dom.NodeList;9import javax.xml.parsers.DocumentBuilderFactory;10import java.io.ByteArrayInputStream;11public class XmlConfigurerTest {12 public void testXmlConfigurer() throws Exception {13 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream("<test><name>John</name></test>".getBytes()));14 XPathExpression expression = XPathExpressionFactory.createXPathExpression("/test/name");15 NodeList nodes = expression.evaluateAsNodeList(document);16 Element element = (Element) nodes.item(0);17 String name = element.getTextContent();18 Assert.assertEquals(name, "John");19 XmlConfigurer xmlConfigurer = new XmlConfigurer();20 xmlConfigurer.setDocument(document);21 xmlConfigurer.setElement(element);22 xmlConfigurer.setValue("Peter");23 name = element.getTextContent();24 Assert.assertEquals(name, "Peter");25 }26}

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.xml.XmlConfigurer;2import org.springframework.context.support.ClassPathXmlApplicationContext;3public class 4 {4public static void main(String[] args) {5ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("4.xml");6XmlConfigurer configurer = context.getBean(XmlConfigurer.class);7configurer.configure();8}9}10<value>${xml.file}</value>11<prop key="var1">${var1}</prop>12<prop key="var2">${var2}</prop>13<prop key="var3">${var3}</prop>

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.xml;2import org.testng.Assert;3import org.testng.annotations.Test;4public class XmlConfigurerTest {5 public void testXmlConfigurer() {6 XmlConfigurer xmlConfigurer = new XmlConfigurer("classpath:com/consol/citrus/xml/configurer-test.xml");7 Assert.assertEquals(xmlConfigurer.getXmlString(), "Hello World!");8 }9}10package com.consol.citrus.xml;11import org.testng.Assert;12import org.testng.annotations.Test;13import org.w3c.dom.Document;14import org.w3c.dom.Node;15import org.w3c.dom.NodeList;16import org.xml.sax.InputSource;17import javax.xml.parsers.DocumentBuilder;18import javax.xml.parsers.DocumentBuilderFactory;19import javax.xml.transform.Source;20import javax.xml.transform.dom.DOMSource;21import javax.xml.transform.stream.StreamSource;22import javax.xml.xpath.XPath;23import javax.xml.xpath.XPathConstants;24import javax.xml.xpath.XPathExpression;25import javax.xml.xpath.XPathExpressionException;26import javax.xml.xpath.XPathFactory;27import java.io.File;28import java.io.StringReader;29import java.util.HashMap;30import java.util.Map;31public class XmlNamespaceContextBuilderTest {32 public void testXmlNamespaceContextBuilder() throws Exception {33 XmlNamespaceContextBuilder builder = new XmlNamespaceContextBuilder();34 Map<String, String> namespaces = new HashMap<String, String>();35 builder.setNamespaces(namespaces);36 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();37 factory.setNamespaceAware(true);38 DocumentBuilder documentBuilder = factory.newDocumentBuilder();39 Document document = documentBuilder.parse(new File("src/test/resources/com/consol/citrus/xml/configurer-test.xml"));40 XPathFactory xPathFactory = XPathFactory.newInstance();41 XPath xPath = xPathFactory.newXPath();42 xPath.setNamespaceContext(builder.build());43 NodeList nodes = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);44 Assert.assertEquals(nodes.getLength(), 1);45 nodes = (NodeList) xPathExpression.evaluate(document, XPathConstants

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.xml;2import java.io.File;3import java.io.IOException;4import java.util.List;5import java.util.Map;6import org.springframework.core.io.Resource;7import org.springframework.util.FileCopyUtils;8import org.springframework.util.StringUtils;9import org.springframework.xml.transform.StringResult;10import org.springframework.xml.transform.StringSource;11import org.w3c.dom.Document;12import org.w3c.dom.Node;13import org.w3c.dom.NodeList;14import org.xml.sax.SAXException;15import com.consol.citrus.exceptions.CitrusRuntimeException;16import com.consol.citrus.util.XMLUtils;17import com.consol.citrus.xml.namespace.NamespaceContextBuilder;18import com.consol.citrus.xml.namespace.SimpleNamespaceContextBuilder;19public class XmlConfigurer {20 private Document document;21 private String xml;22 private Resource resource;23 private TestContext context;24 private NamespaceContextBuilder namespaceContextBuilder = new SimpleNamespaceContextBuilder();25 public XmlConfigurer() {26 super();27 }28 public XmlConfigurer(Document document, TestContext context) {29 this.document = document;30 this.context = context;31 }32 public XmlConfigurer(String xml, TestContext context) {33 this.xml = xml;34 this.context = context;35 }36 public XmlConfigurer(Resource resource, TestContext context) {37 this.resource = resource;38 this.context = context;39 }40 public void configure() {41 try {42 if (document == null) {43 document = XMLUtils.parseMessagePayload(getXml());44 }45 NodeList nodes = document.getElementsByTagName("*");46 for (int i = 0; i < nodes.getLength(); i++) {

Full Screen

Full Screen

XmlConfigurer

Using AI Code Generation

copy

Full Screen

1public class 4 {2public static void main(String[] args) {3XmlConfigurer configurer = new XmlConfigurer();4configurer.setXmlResource(new FileSystemResource("src/main/resources/4.xml"));5configurer.afterPropertiesSet();6System.out.println(configurer.getXml());7}8}

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