How to use evaluateAsString method of com.consol.citrus.generate.xml.WsdlXmlTestGenerator class

Best Citrus code snippet using com.consol.citrus.generate.xml.WsdlXmlTestGenerator.evaluateAsString

Source:WsdlXmlTestGenerator.java Github

copy

Full Screen

...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 elementName191 * @return192 */193 private SchemaType getSchemaType(SchemaTypeSystem schemaTypeSystem, String operation, String elementName) {194 for (SchemaType elem : schemaTypeSystem.documentTypes()) {195 if (elem.getContentModel().getName().getLocalPart().equals(elementName)) {196 return elem;197 }198 }199 throw new CitrusRuntimeException("Unable to find schema type declaration '" + elementName + "'" +200 " for WSDL operation '" + operation + "'");201 }202 /**203 * Removes namespace prefix if present.204 * @param elementName205 * @return206 */207 private String removeNsPrefix(String elementName) {208 return elementName.indexOf(':') != -1 ? elementName.substring(elementName.indexOf(':') + 1) : elementName;209 }210 /**211 * Finds nested schema definitions and puts globally WSDL defined namespaces to schema level.212 *213 * @param wsdl214 * @param namespacesWsdl215 * @param schemaNsPrefix216 */217 private String[] getNestedSchemas(XmlObject wsdl, String[] namespacesWsdl, String schemaNsPrefix) {218 List<String> schemas = new ArrayList<>();219 String openedStartTag = "<" + schemaNsPrefix + "schema";220 String endTag = "</" + schemaNsPrefix + "schema>";221 int cursor = 0;222 while (wsdl.xmlText().indexOf(openedStartTag, cursor) != -1) {223 int begin = wsdl.xmlText().indexOf(openedStartTag, cursor);224 int end = wsdl.xmlText().indexOf(endTag, begin) + endTag.length();225 int insertPointNamespacesWsdl = wsdl.xmlText().indexOf(" ", begin);226 StringBuilder builder = new StringBuilder();227 builder.append(wsdl.xmlText().substring(begin, insertPointNamespacesWsdl)).append(" ");228 for (String nsWsdl : namespacesWsdl) {229 String nsPrefix = nsWsdl.substring(0, nsWsdl.indexOf("="));230 if (!wsdl.xmlText().substring(begin, end).contains(nsPrefix)) {231 builder.append(nsWsdl).append(" ");232 }233 }234 builder.append(wsdl.xmlText().substring(insertPointNamespacesWsdl, end));235 schemas.add(builder.toString());236 cursor = end;237 }238 return schemas.toArray(new String[] {});239 }240 /**241 * Finds schema tag and extracts the namespace prefix.242 * @param wsdl243 * @return244 */245 private String extractSchemaNamespacePrefix(XmlObject wsdl) {246 String schemaNsPrefix = "";247 if (wsdl.xmlText().contains(":schema")) {248 int cursor = wsdl.xmlText().indexOf(":schema");249 for (int i = cursor; i > cursor - 100; i--) {250 schemaNsPrefix = wsdl.xmlText().substring(i, cursor);251 if (schemaNsPrefix.startsWith("<")) {252 return schemaNsPrefix.substring(1) + ":";253 }254 }255 }256 return schemaNsPrefix;257 }258 /**259 * Returns an array of all namespace declarations, found on wsdl-level.260 *261 * @param wsdl262 * @return263 */264 private String[] extractNamespacesOnWsdlLevel(XmlObject wsdl) {265 int cursor = wsdl.xmlText().indexOf(":") + ":definitions ".length();266 String nsWsdlOrig = wsdl.xmlText().substring(cursor, wsdl.xmlText().indexOf(">", cursor));267 int noNs = StringUtils.countOccurrencesOf(nsWsdlOrig, "xmlns:");268 String[] namespacesWsdl = new String[noNs];269 cursor = 0;270 for (int i=0; i<noNs; i++) {271 int begin = nsWsdlOrig.indexOf("xmlns:", cursor);272 int end = nsWsdlOrig.indexOf("\"", begin + 20);273 namespacesWsdl[i] = nsWsdlOrig.substring(begin, end) + "\"";274 cursor = end;275 }276 return namespacesWsdl;277 }278 /**279 * Returns the value of an xml-attribute280 *281 * @param rootObject282 * @param pathToAttribute283 * @return284 */285 private String evaluateAsString(XmlObject rootObject, String pathToAttribute) {286 XmlObject[] xmlObject = rootObject.selectPath(pathToAttribute);287 if (xmlObject.length == 0) {288 throw new CitrusRuntimeException("Unable to find element attribute " + pathToAttribute);289 }290 int begin = xmlObject[0].xmlText().indexOf(">") + 1;291 int end = xmlObject[0].xmlText().lastIndexOf("</");292 return xmlObject[0].xmlText().substring(begin, end);293 }294 /**295 * Set the wsdl schema resource to use.296 * @param wsdlResource297 * @return298 */299 public WsdlXmlTestGenerator withWsdl(String wsdlResource) {...

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.xml.WsdlXmlTestGenerator2def testGenerator = new WsdlXmlTestGenerator()3testGenerator.setOperation("Add")4testGenerator.setTestName("AddRequestTest")5testGenerator.setTestCaseName("AddRequestTestCase")6def xml = testGenerator.evaluateAsString()7import com.consol.citrus.generate.xml.WsdlXmlTestGenerator8def testGenerator = new WsdlXmlTestGenerator()

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1WsdlXmlTestGenerator generator = new WsdlXmlTestGenerator.Builder()2 .operation("sayHello")3 .requestPayload(new File("src/test/resources/soap/soap_request.xml"))4 .responsePayload(new File("src/test/resources/soap/soap_response.xml"))5 .build();6String testXml = generator.evaluateAsString();7System.out.println(testXml);

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.xml.WsdlXmlTestGenerator2def wsdlTestGenerator = new WsdlXmlTestGenerator()3println(xmlTest)4import com.consol.citrus.generate.xml.WsdlXmlTestGenerator5def wsdlTestGenerator = new WsdlXmlTestGenerator()6println(javaTest)7at com.consol.citrus.generate.xml.WsdlXmlTestGenerator.generateJavaTest(WsdlXmlTestGenerator.java:107)8at com.consol.citrus.generate.xml.WsdlXmlTestGenerator.generateJavaTest(WsdlXmlTestGenerator.java:86)9at WsdlTest.main(WsdlTest.groovy:11)10at java.net.URLClassLoader.findClass(URLClassLoader.java:381)11at java.lang.ClassLoader.loadClass(ClassLoader.java:424)12at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)13at java.lang.ClassLoader.loadClass(ClassLoader.java:357)14import com.consol.citrus.generate.xml.WsdlXmlTestGenerator

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1WsdlXmlTestGenerator generator = new WsdlXmlTestGenerator()2generator.setOperation("sayHello")3generator.setPackageName("com.consol.citrus.samples")4generator.setTargetDirectory("src/test/java")5generator.setTestName("HelloWorldServiceIT")6generator.setWsdlVersion("2.0")7generator.setEndpointName("helloWorldEndpoint")8generator.setEndpointTimeout(10000)9generator.setEndpointAutoStart(true)10generator.setEndpointAutoStart(true)

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.xml.WsdlXmlTestGenerator2import com.consol.citrus.generate.xml.WsdlXmlTestGeneratorBuilder3import com.consol.citrus.generate.xml.WsdlXmlTestGeneratorConfig4import java.io.File5import java.nio.file.Paths6def wsdlXmlTestGeneratorConfig = new WsdlXmlTestGeneratorConfig()7wsdlXmlTestGeneratorConfig.setWsdlFile(new File("src/test/resources/wsdl/echo.wsdl"))8wsdlXmlTestGeneratorConfig.setTestName("EchoTest")9wsdlXmlTestGeneratorConfig.setPackageName("com.consol.citrus.samples.wsdl")10wsdlXmlTestGeneratorConfig.setTargetDirectory(Paths.get("src/test/resources/wsdl").toAbsolutePath().toString())11wsdlXmlTestGeneratorConfig.setEndpoint("echoServiceEndpoint")12wsdlXmlTestGeneratorConfig.setEndpointConfiguration("echoServiceEndpointConfiguration")13wsdlXmlTestGeneratorConfig.setEndpointConfigurationClass("com.consol.citrus.samples.wsdl.EchoServiceEndpointConfiguration")14wsdlXmlTestGeneratorConfig.setTestTarget(true)15wsdlXmlTestGeneratorConfig.setTestTargetClass("com.consol.citrus.samples.wsdl.EchoServiceClient")16wsdlXmlTestGeneratorConfig.setTestTargetMethod("sendAndReceive")17def wsdlXmlTestGenerator = new WsdlXmlTestGenerator(wsdlXmlTestGeneratorConfig)18wsdlXmlTestGenerator.generate()19import com.consol.citrus.generate.xml.WsdlXmlTestGenerator20import com.consol.citrus.generate.xml.WsdlXmlTestGeneratorBuilder21import com.consol.citrus.generate.xml.WsdlXmlTestGeneratorConfig22import java.io.File23import java.nio.file.Paths24def wsdlXmlTestGeneratorConfig = new WsdlXmlTestGeneratorConfig()25wsdlXmlTestGeneratorConfig.setWsdlFile(new File("src/test/resources/wsdl/echo.wsdl"))

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.xml.WsdlXmlTestGenerator2import com.consol.citrus.generate.xml.XsdXmlTestGenerator3def wsdlXmlTestGenerator = new WsdlXmlTestGenerator()4def xsdXmlTestGenerator = new XsdXmlTestGenerator()5def javaTestCase = xsdXmlTestGenerator.generateAsString(xmlTestCase)6println(javaTestCase)7import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;8import com.consol.citrus.ws.actions.SoapActionBuilder;9import com.consol.citrus.ws.actions.SoapActionBuilder.SoapAction;10public class StockQuoteIT extends TestNGCitrusTestRunner {11 public void configure() {12 http()13 .client("httpClient")14 .send()15 .post()16 " <urn:stockName>citrus:randomNumber(4)</urn:stockName>\n" +17 "</soapenv:Envelope>");18 http()19 .client("httpClient")20 .receive()21 .response(HttpStatus.OK)

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