How to use evaluateAsString method of com.consol.citrus.generate.javadsl.WsdlJavaTestGenerator class

Best Citrus code snippet using com.consol.citrus.generate.javadsl.WsdlJavaTestGenerator.evaluateAsString

Source:WsdlJavaTestGenerator.java Github

copy

Full Screen

...50 // compile wsdl and xsds right now, otherwise later input is useless:51 XmlObject wsdlObject = compileWsdl(wsdl);52 SchemaTypeSystem schemaTypeSystem = compileXsd(wsdlObject);53 log.info("WSDL compilation successful");54 String serviceName = evaluateAsString(wsdlObject, wsdlNsDelaration + ".//wsdl:portType/@name");55 log.info("Found service: " + serviceName);56 if (!StringUtils.hasText(namePrefix)) {57 withNamePrefix(serviceName + "_");58 }59 log.info("Found service operations:");60 XmlObject[] messages = wsdlObject.selectPath(wsdlNsDelaration + ".//wsdl:message");61 XmlObject[] operations = wsdlObject.selectPath(wsdlNsDelaration + ".//wsdl:portType/wsdl:operation");62 for (XmlObject operation : operations) {63 log.info(evaluateAsString(operation, wsdlNsDelaration + "./@name"));64 }65 log.info("Generating test cases for service operations ...");66 for (XmlObject operation : operations) {67 SoapMessage request = new SoapMessage();68 SoapMessage response = new SoapMessage();69 String operationName = evaluateAsString(operation, wsdlNsDelaration + "./@name");70 if (StringUtils.hasText(this.operation) && !operationName.equals(this.operation)) {71 continue;72 }73 XmlObject[] bindingOperations = wsdlObject.selectPath(wsdlNsDelaration + ".//wsdl:binding/wsdl:operation");74 for (XmlObject bindingOperation : bindingOperations) {75 String bindingOperationName = evaluateAsString(bindingOperation, wsdlNsDelaration + "./@name");76 if (bindingOperationName.equals(operationName)) {77 String soapAction = removeNsPrefix(evaluateAsString(bindingOperation, soapNsDelaration + "./soap:operation/@soapAction"));78 request.soapAction(soapAction);79 break;80 }81 }82 String inputMessage = removeNsPrefix(evaluateAsString(operation, wsdlNsDelaration + "./wsdl:input/@message"));83 String outputMessage = removeNsPrefix(evaluateAsString(operation, wsdlNsDelaration + "./wsdl:output/@message"));84 String inputElement = null;85 String outputElement = null;86 for (XmlObject message : messages) {87 String messageName = evaluateAsString(message, wsdlNsDelaration + "./@name");88 if (messageName.equals(inputMessage)) {89 inputElement = removeNsPrefix(evaluateAsString(message, wsdlNsDelaration + "./wsdl:part/@element"));90 }91 if (messageName.equals(outputMessage)) {92 outputElement = removeNsPrefix(evaluateAsString(message, wsdlNsDelaration + "./wsdl:part/@element"));93 }94 }95 // Now generate it96 withName(namePrefix + operationName + nameSuffix);97 SchemaType requestElem = getSchemaType(schemaTypeSystem, operationName, inputElement);98 request.setPayload(SampleXmlUtil.createSampleForType(requestElem));99 withRequest(request);100 SchemaType responseElem = getSchemaType(schemaTypeSystem, operationName, outputElement);101 response.setPayload(SampleXmlUtil.createSampleForType(responseElem));102 withResponse(response);103 XmlConfigurer configurer = new XmlConfigurer();104 configurer.setSerializeSettings(Collections.singletonMap(XmlConfigurer.XML_DECLARATION, false));105 XMLUtils.initialize(configurer);106 super.create();107 log.info("Successfully created new test case " + getTargetPackage() + "." + getName());108 }109 }110 @Override111 protected Message generateInboundMessage(Message message) {112 return inboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());113 }114 @Override115 protected Message generateOutboundMessage(Message message) {116 return outboundDataDictionary.interceptMessageConstruction(message, MessageType.XML.name(), new TestContext());117 }118 /**119 * Finds nested XML schema definition and compiles it to a schema type system instance120 * @param wsdl121 * @return122 */123 private XmlObject compileWsdl(String wsdl) {124 File wsdlFile;125 try {126 wsdlFile = new PathMatchingResourcePatternResolver().getResource(wsdl).getFile();127 } catch (IOException e) {128 wsdlFile = new File(wsdl);129 }130 if (!wsdlFile.exists()) {131 throw new CitrusRuntimeException("Unable to read WSDL - does not exist in " + wsdlFile.getAbsolutePath());132 }133 if (!wsdlFile.canRead()) {134 throw new CitrusRuntimeException("Unable to read WSDL - could not open in read mode");135 }136 try {137 return XmlObject.Factory.parse(wsdlFile, (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest().setCompileDownloadUrls());138 } catch (XmlException e) {139 for (Object error : e.getErrors()) {140 log.error(((XmlError)error).getLine() + "" + error.toString());141 }142 throw new CitrusRuntimeException("WSDL could not be parsed", e);143 } catch (Exception e) {144 throw new CitrusRuntimeException("WSDL could not be parsed", e);145 }146 }147 /**148 * Finds nested XML schema definition and compiles it to a schema type system instance.149 * @param wsdl150 * @return151 */152 private SchemaTypeSystem compileXsd(XmlObject wsdl) {153 // extract namespaces defined on wsdl-level:154 String[] namespacesWsdl = extractNamespacesOnWsdlLevel(wsdl);155 // calc the namespace-prefix of the schema-tag, default ""156 String schemaNsPrefix = extractSchemaNamespacePrefix(wsdl);157 // extract each schema-element and add missing namespaces defined on wsdl-level158 String[] schemas = getNestedSchemas(wsdl, namespacesWsdl, schemaNsPrefix);159 XmlObject[] xsd = new XmlObject[schemas.length];160 try {161 for (int i=0; i < schemas.length; i++) {162 xsd[i] = XmlObject.Factory.parse(schemas[i], (new XmlOptions()).setLoadLineNumbers().setLoadMessageDigest().setCompileDownloadUrls());163 }164 } catch (Exception e) {165 throw new CitrusRuntimeException("Failed to parse XSD schema", e);166 }167 SchemaTypeSystem schemaTypeSystem = null;168 try {169 schemaTypeSystem = XmlBeans.compileXsd(xsd, XmlBeans.getContextTypeLoader(), new XmlOptions());170 } catch (XmlException e) {171 for (Object error : e.getErrors()) {172 log.error("Line " + ((XmlError)error).getLine() + ": " + error.toString());173 }174 throw new CitrusRuntimeException("Failed to compile XSD schema", e);175 } catch (Exception e) {176 throw new CitrusRuntimeException("Failed to compile XSD schema", e);177 }178 return schemaTypeSystem;179 }180 /**181 * @param schemaTypeSystem182 * @param operation183 * @param elementName184 * @return185 */186 private SchemaType getSchemaType(SchemaTypeSystem schemaTypeSystem, String operation, String elementName) {187 for (SchemaType elem : schemaTypeSystem.documentTypes()) {188 if (elem.getContentModel().getName().getLocalPart().equals(elementName)) {189 return elem;190 }191 }192 throw new CitrusRuntimeException("Unable to find schema type declaration '" + elementName + "'" +193 " for WSDL operation '" + operation + "'");194 }195 /**196 * Removes namespace prefix if present.197 * @param elementName198 * @return199 */200 private String removeNsPrefix(String elementName) {201 return elementName.indexOf(':') != -1 ? elementName.substring(elementName.indexOf(':') + 1) : elementName;202 }203 /**204 * Finds nested schema definitions and puts globally WSDL defined namespaces to schema level.205 *206 * @param wsdl207 * @param namespacesWsdl208 * @param schemaNsPrefix209 */210 private String[] getNestedSchemas(XmlObject wsdl, String[] namespacesWsdl, String schemaNsPrefix) {211 List<String> schemas = new ArrayList<>();212 String openedStartTag = "<" + schemaNsPrefix + "schema";213 String endTag = "</" + schemaNsPrefix + "schema>";214 int cursor = 0;215 while (wsdl.xmlText().indexOf(openedStartTag, cursor) != -1) {216 int begin = wsdl.xmlText().indexOf(openedStartTag, cursor);217 int end = wsdl.xmlText().indexOf(endTag, begin) + endTag.length();218 int insertPointNamespacesWsdl = wsdl.xmlText().indexOf(" ", begin);219 StringBuilder builder = new StringBuilder();220 builder.append(wsdl.xmlText().substring(begin, insertPointNamespacesWsdl)).append(" ");221 for (String nsWsdl : namespacesWsdl) {222 String nsPrefix = nsWsdl.substring(0, nsWsdl.indexOf("="));223 if (!wsdl.xmlText().substring(begin, end).contains(nsPrefix)) {224 builder.append(nsWsdl).append(" ");225 }226 }227 builder.append(wsdl.xmlText().substring(insertPointNamespacesWsdl, end));228 schemas.add(builder.toString());229 cursor = end;230 }231 return schemas.toArray(new String[] {});232 }233 /**234 * Finds schema tag and extracts the namespace prefix.235 * @param wsdl236 * @return237 */238 private String extractSchemaNamespacePrefix(XmlObject wsdl) {239 String schemaNsPrefix = "";240 if (wsdl.xmlText().contains(":schema")) {241 int cursor = wsdl.xmlText().indexOf(":schema");242 for (int i = cursor; i > cursor - 100; i--) {243 schemaNsPrefix = wsdl.xmlText().substring(i, cursor);244 if (schemaNsPrefix.startsWith("<")) {245 return schemaNsPrefix.substring(1) + ":";246 }247 }248 }249 return schemaNsPrefix;250 }251 /**252 * Returns an array of all namespace declarations, found on wsdl-level.253 *254 * @param wsdl255 * @return256 */257 private String[] extractNamespacesOnWsdlLevel(XmlObject wsdl) {258 int cursor = wsdl.xmlText().indexOf(":") + ":definitions ".length();259 String nsWsdlOrig = wsdl.xmlText().substring(cursor, wsdl.xmlText().indexOf(">", cursor));260 int noNs = StringUtils.countOccurrencesOf(nsWsdlOrig, "xmlns:");261 String[] namespacesWsdl = new String[noNs];262 cursor = 0;263 for (int i=0; i<noNs; i++) {264 int begin = nsWsdlOrig.indexOf("xmlns:", cursor);265 int end = nsWsdlOrig.indexOf("\"", begin + 20);266 namespacesWsdl[i] = nsWsdlOrig.substring(begin, end) + "\"";267 cursor = end;268 }269 return namespacesWsdl;270 }271 /**272 * Returns the value of an xml-attribute273 *274 * @param rootObject275 * @param pathToAttribute276 * @return277 */278 private String evaluateAsString(XmlObject rootObject, String pathToAttribute) {279 XmlObject[] xmlObject = rootObject.selectPath(pathToAttribute);280 if (xmlObject.length == 0) {281 throw new CitrusRuntimeException("Unable to find element attribute " + pathToAttribute);282 }283 int begin = xmlObject[0].xmlText().indexOf(">") + 1;284 int end = xmlObject[0].xmlText().lastIndexOf("</");285 return xmlObject[0].xmlText().substring(begin, end);286 }287 /**288 * Set the wsdl schema resource to use.289 * @param wsdlResource290 * @return291 */292 public WsdlJavaTestGenerator withWsdl(String wsdlResource) {...

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.javadsl.WsdlJavaTestGenerator;2public class WsdlJavaTestGeneratorTest {3 public static void main(String[] args) {4 WsdlJavaTestGenerator generator = new WsdlJavaTestGenerator();5 System.out.println(generator.evaluateAsString("src/test/resources/wsdl/HelloService.wsdl"));6 }7}8package com.consol.citrus;9import com.consol.citrus.annotations.CitrusTest;10import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;11import org.springframework.http.HttpStatus;12import org.testng.annotations.Test;13public class HelloServiceIT extends TestNGCitrusTestRunner {14 public void testHelloService() {15 http(builder -> builder.client("helloServiceClient")16 .send()17 .post()18 http(builder -> builder.client("helloServiceClient")19 .receive()20 .response(HttpStatus.OK)21 }22}

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1WsdlJavaTestGenerator generator = new WsdlJavaTestGenerator();2String javaTest = generator.evaluateAsString(wsdlUrl);3System.out.println(javaTest);4public class WeatherServiceIT extends TestNGCitrusTestDesigner {5 public void testGetWeather() {6 http()7 .client("httpClient")8 .send()9 .post()10 .fork(true);11 receive()12 .header("Content-Type", "text/xml;charset=UTF-8")13 .header("SOAPAction", "getWeather");14 send()15 .header("Content-Type", "text/xml;charset=UTF-8");16 }17}

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1String packageName = "com.consol.citrus.wsdl";2String testClassName = "GeoIpServiceTest";3String testPackage = "com.consol.citrus.wsdl.tests";4String testGroup = "wsdl";5String testAuthor = "citrus";6String testDescription = "Test for GeoIpService";7String testSoapVersion = "SOAP_11";8String testRequestPayload = "requestPayload.xml";9String testResponsePayload = "responsePayload.xml";10String testRequestPayloadData = new String(Files.readAllBytes(Paths.get("src/test/resources/requestPayload.xml")));11String testResponsePayloadData = new String(Files.readAllBytes(Paths.get("src/test/resources/responsePayload.xml")));12String testRequestMessage = "requestMessage";13String testResponseMessage = "responseMessage";14String testRequestMessageData = new String(Files.readAllBytes(Paths.get("src/test/resources/requestMessage.xml")));15String testResponseMessageData = new String(Files.readAllBytes(Paths.get("src/test/resources/responseMessage.xml")));16String testRequestHeader = "requestHeader";17String testResponseHeader = "responseHeader";18String testRequestHeaderData = new String(Files.readAllBytes(Paths.get("src/test/resources/requestHeader.xml")));19String testResponseHeaderData = new String(Files.readAllBytes(Paths.get("src/test/resources/responseHeader.xml")));20String testRequestAttachment = "requestAttachment";21String testResponseAttachment = "responseAttachment";22String testRequestAttachmentData = new String(Files.readAllBytes(Paths.get("src/test/resources/requestAttachment.xml")));23String testResponseAttachmentData = new String(Files.readAllBytes(Paths.get("src/test/resources/responseAttachment.xml")));24String testRequestAttachmentName = "requestAttachmentName";25String testResponseAttachmentName = "responseAttachmentName";26String testRequestAttachmentDataName = new String(Files.readAllBytes(Paths.get("src/test/resources/requestAttachmentName.xml")));27String testResponseAttachmentDataName = new String(Files.readAllBytes(Paths.get("src/test/resources/responseAttachmentName.xml")));28String testRequestAttachmentType = "requestAttachmentType";29String testResponseAttachmentType = "responseAttachmentType";

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.generate.javadsl;2import com.consol.citrus.dsl.builder.BuilderSupport;3import com.consol.citrus.dsl.builder.SendSoapMessageBuilder;4import com.consol.citrus.dsl.builder.ReceiveSoapMessageBuilder;5import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;6import com.consol.citrus.ws.actions.WsActionBuilder;7import com.consol.citrus.ws.message.SoapAttachment;8import org.springframework.core.io.Resource;9import org.springframework.util.StringUtils;10import org.testng.annotations.Test;11import javax.xml.namespace.QName;12import java.util.ArrayList;13import java.util.List;14public class HelloServiceIT extends JUnit4CitrusTestRunner {15 public void sayHello() {16 variable("name", "citrus:concat('Hello ', citrus:randomNumber(4))");17 WsActionBuilder soapAction = soap().client("helloServiceClient");18 soapAction.send()19 .soap()20 .version("1.1")21 .message()22 "<ns1:Text>${name}</ns1:Text>" +23 soapAction.receive()24 .soap()25 .version("1.1")26 .message()

Full Screen

Full Screen

evaluateAsString

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.generate.javadsl.WsdlJavaTestGenerator2def wsdlFile = new File("src/test/resources/wsdl/HelloService.wsdl")3def wsdlGenerator = new WsdlJavaTestGenerator(wsdlFile)4def javaCode = wsdlGenerator.evaluateAsString()5new File(wsdlFile.parent, wsdlFile.name + ".java").withWriter { out ->6}7import com.consol.citrus.generate.javadsl.WsdlJavaTestGenerator8def wsdlFile = new File("src/test/resources/wsdl/HelloService.wsdl")9def wsdlGenerator = new WsdlJavaTestGenerator(wsdlFile)10def javaCode = wsdlGenerator.evaluateAsString()11new File(wsdlFile.parent, wsdlFile.name + ".java").withWriter { out ->12}13import com.consol.citrus.generate.javadsl.WsdlJavaTestGenerator14def wsdlFile = new File("src/test/resources/wsdl/HelloService.wsdl")15def wsdlGenerator = new WsdlJavaTestGenerator(wsdlFile)16def javaCode = wsdlGenerator.evaluateAsString()17new File(wsdlFile.parent, wsdlFile.name + ".java").withWriter { out ->18}

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