Best Citrus code snippet using com.consol.citrus.generate.xml.WsdlXmlTestGenerator.compileXsd
Source:WsdlXmlTestGenerator.java
...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());120 }121 @Override122 protected Message generateOutboundMessage(Message message) {123 return outboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());124 }125 /**126 * Finds nested XML schema definition and compiles it to a schema type system instance127 * @param wsdl128 * @return129 */130 private XmlObject compileWsdl(String wsdl) {131 File wsdlFile;132 try {133 wsdlFile = new PathMatchingResourcePatternResolver().getResource(wsdl).getFile();134 } catch (IOException e) {135 wsdlFile = new File(wsdl);136 }137 if (!wsdlFile.exists()) {138 throw new CitrusRuntimeException("Unable to read WSDL - does not exist in " + wsdlFile.getAbsolutePath());139 }140 if (!wsdlFile.canRead()) {141 throw new CitrusRuntimeException("Unable to read WSDL - could not open in read mode");142 }143 try {144 return XmlObject.Factory.parse(wsdlFile, (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest().setCompileDownloadUrls());145 } catch (XmlException e) {146 for (Object error : e.getErrors()) {147 log.error(((XmlError)error).getLine() + "" + error.toString());148 }149 throw new CitrusRuntimeException("WSDL could not be parsed", e);150 } catch (Exception e) {151 throw new CitrusRuntimeException("WSDL could not be parsed", e);152 }153 }154 /**155 * Finds nested XML schema definition and compiles it to a schema type system instance.156 * @param wsdl157 * @return158 */159 private SchemaTypeSystem compileXsd(XmlObject wsdl) {160 // extract namespaces defined on wsdl-level:161 String[] namespacesWsdl = extractNamespacesOnWsdlLevel(wsdl);162 // calc the namespace-prefix of the schema-tag, default ""163 String schemaNsPrefix = extractSchemaNamespacePrefix(wsdl);164 // extract each schema-element and add missing namespaces defined on wsdl-level165 String[] schemas = getNestedSchemas(wsdl, namespacesWsdl, schemaNsPrefix);166 XmlObject[] xsd = new XmlObject[schemas.length];167 try {168 for (int i=0; i < schemas.length; i++) {169 xsd[i] = XmlObject.Factory.parse(schemas[i], (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest().setCompileDownloadUrls());170 }171 } catch (Exception e) {172 throw new CitrusRuntimeException("Failed to parse XSD schema", e);173 }174 SchemaTypeSystem schemaTypeSystem = null;175 try {176 schemaTypeSystem = XmlBeans.compileXsd(xsd, XmlBeans.getContextTypeLoader(), new XmlOptions());177 } catch (XmlException e) {178 for (Object error : e.getErrors()) {179 log.error("Line " + ((XmlError)error).getLine() + ": " + error.toString());180 }181 throw new CitrusRuntimeException("Failed to compile XSD schema", e);182 } catch (Exception e) {183 throw new CitrusRuntimeException("Failed to compile XSD schema", e);184 }185 return schemaTypeSystem;186 }187 /**188 * @param schemaTypeSystem189 * @param operation190 * @param elementName...
compileXsd
Using AI Code Generation
1WsdlXmlTestGenerator wsdlXmlTestGenerator = new WsdlXmlTestGenerator();2wsdlXmlTestGenerator.setOperation("GetQuote");3wsdlXmlTestGenerator.setTargetPackage("com.consol.citrus.wsdl");4wsdlXmlTestGenerator.setTargetProject("target");5wsdlXmlTestGenerator.setTargetSourceDirectory("src/test/java");6wsdlXmlTestGenerator.setTargetResourceDirectory("src/test/resources");7wsdlXmlTestGenerator.setTargetTestSourceDirectory("src/test/java");8wsdlXmlTestGenerator.setTargetTestResourceDirectory("src/test/resources");9wsdlXmlTestGenerator.setTargetTestJavaDirectory("src/test/java");10wsdlXmlTestGenerator.setTargetTestResourcesDirectory("src/test/resources");
compileXsd
Using AI Code Generation
1import com.consol.citrus.generate.TestGenerator;2import com.consol.citrus.generate.xml.WsdlXmlTestGenerator;3import org.springframework.core.io.ClassPathResource;4import org.springframework.core.io.Resource;5import java.io.IOException;6import java.util.Map;7public class CompileXsdTest {8 public static void main(String[] args) throws IOException {9 Resource wsdlResource = new ClassPathResource("wsdl/HelloService.wsdl");10 Resource xsdResource = new ClassPathResource("xsd/HelloService.xsd");11 TestGenerator generator = new WsdlXmlTestGenerator(wsdlResource, xsdResource);12 Map<String, String> tests = generator.generate();13 tests.forEach((name, test) -> {14 System.out.println(name);15 System.out.println(test);16 });17 }18}19package com.consol.citrus;20import com.consol.citrus.annotations.CitrusTest;21import com.consol.citrus.testng.CitrusParameters;22import com.consol.citrus.ws.actions.SoapActionBuilder;23import com.consol.citrus.ws.client.WebServiceClient;24import com.consol.citrus.ws.message.SoapMessage;25import com.consol.citrus.ws.validation.SoapAttachmentValidator;26import com.consol.citrus.ws.validation.SoapFaultDetailValidator;27import com.consol.citrus.ws.validation.SoapFaultReasonValidator;28import com.consol.citrus.ws.validation.SoapFaultValidator;29import com.consol.citrus.ws.validation.SoapHeaderValidator;30import com.consol.citrus.ws.validation.SoapMessageValidator;31import com.consol.citrus.ws.validation.SoapPayloadRootQNameValidator;32import com.consol.citrus.ws.validation.SoapPayloadXpathMessageValidator;33import com.consol.citrus.ws.validation.SoapSchemaValidationContext;34import com.consol.citrus.ws.validation.SoapSchemaValidator;35import com.consol.citrus.ws.validation.SoapSoapActionValidator;36import com.consol.citrus.ws.validation.SoapValidationContext;37import com.consol.citrus.ws.validation.SoapValidator;38import org.springframework.beans.factory.annotation.Autowired;39import org.springframework.core.io.ClassPathResource;40import org.springframework.core.io.Resource;41import org.springframework.ox
compileXsd
Using AI Code Generation
1WsdlXmlTestGenerator wsdlXmlTestGenerator = new WsdlXmlTestGenerator();2XsdXmlTestGenerator xsdXmlTestGenerator = new XsdXmlTestGenerator();3WsdlXmlTestGenerator wsdlXmlTestGenerator = new WsdlXmlTestGenerator();4wsdlXmlTestGenerator.generateXmlData("tempconvert.xsd", "tempconvert.xml");5XsdXmlTestGenerator xsdXmlTestGenerator = new XsdXmlTestGenerator();6xsdXmlTestGenerator.generateXmlData("cd_catalog.xsd", "cd_catalog.xml");7WsdlXmlTestGenerator wsdlXmlTestGenerator = new WsdlXmlTestGenerator();8wsdlXmlTestGenerator.generateXmlTest("tempconvert.xml", "tempconvert.xml");9XsdXmlTestGenerator xsdXmlTestGenerator = new XsdXmlTestGenerator();10xsdXmlTestGenerator.generateXmlTest("cd_catalog.xml", "cd_catalog.xml");11WsdlXmlTestGenerator wsdlXmlTestGenerator = new WsdlXmlTestGenerator();12wsdlXmlTestGenerator.runXmlTest("tempconvert.xml");
compileXsd
Using AI Code Generation
1WsdlXmlTestGenerator generator = new WsdlXmlTestGenerator();2generator.setTargetDirectory("src/test/resources/com/consol/citrus/wsdl");3generator.setPackageName("com.consol.citrus.wsdl");4generator.setTestName("HelloServiceIT");5generator.setEndpointName("helloServiceEndpoint");6generator.setEndpointTimeout(5000L);7generator.setEndpointAutoStart(true);8generator.setEndpointAutoStart(tru
compileXsd
Using AI Code Generation
1import com.consol.citrus.generate.xml.WsdlXmlTestGenerator2import com.consol.citrus.generate.xml.WsdlXmlTestGeneratorConfig3def config = new WsdlXmlTestGeneratorConfig(4def generator = new WsdlXmlTestGenerator(config)5generator.compileXsd()6generator.generate()7generator.writeTests()8generator.getGeneratedTests()9generator.getGeneratedTestNames()
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!!