Forum Discussion

PaulDonny's avatar
PaulDonny
Regular Contributor
11 years ago

Build SoapUI Project files with Java

My development team is wanting me to look into building project files dynamically using Java so that we can halt maintenance on our test harness and begin using SoapUI as a back end for a UI based system which I have already created.

I have read a little about the maven plugins and, quite frankly, I know very little about POM and Maven. I was wondering if anyone could give me some guidance on this.


Currently my method is a bit brute force in the sense that I am building the "Demo" project files in SoapUI it self with tags that are being recognized using Regex as it parses through it and then it replaces those with the properties from the property file. This method is extremely sensitive.

I recommended to my Dev team to dig through the source for SoapUI to find the section that builds it's project file but if it's already in Maven than I might as well just utilize that.

Any suggestions for this, essentially using SoapUI as a background (either native within the UI or via TestCaseRunner).
  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Not sure if anyone else is following this or interested but it helps me if I write stuff down and I figure I can't be the only one who wants to be able to customize SoapUI to his application's exact needs.


    So I was able to import SoapUI fully into my Java project. You can follow the steps at the link I provided or just navigate to your SoapUI lib folder (as mentioned in my previous post) and download the 2 SoapUI specific Jar files and import them as well. After that, you need to create your project and WSDL/XSD. My line of code looks like the following:


    WsdlProject project = new WsdlProject();
    project.setName("Test");
    WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project, "//Path/to/my/WSDL", true)[0];


    Next, I adjusted my endpoint to fit my test host

                String host = "//My/Test/Host";
    String endpoint = iface.getEndpoints()[0].replaceAll("http.*?//.*?/",host);
    iface.changeEndpoint(iface.getEndpoints()[0],endpoint);


    After that, the majority of stuff falls into the standard SoapUI groovy language. I will post a small demo to set up a test step

                WsdlOperation operation = (WsdlOperation) iface.getOperationByName("nameOfOperation");
    operation.setName("nameOfOperation");
    WsdlTestSuite TS = project.addNewTestSuite("Test");
    TS.setName("Test");
    TestStepConfig TStepC = WsdlTestRequestStepFactory.createConfig(operation, "Test");
    WsdlTestCase TC = TS.addNewTestCase("Test");
    WsdlTestStep TStep = TC.addTestStep(TStepC);
    String rawRequest = TStep.getPropertyValue("Request");
    TStep.setPropertyValue("Username", properties.get("username").trim());
    TStep.setPropertyValue("Password", properties.get("password").trim());
    NodeList nodes = XmlUtils.parseXml(operation.getRequestAt(0).getRequestContent()).getChildNodes();
    parseNodes(nodes,operation.getName());
    String XML = XmlUtils.prettyPrintXml(XmlUtils.createXmlObject(nodes.item(0).getParentNode()));
    XML = XML.replaceAll( "(?s)<!--.*?-->","");
    XML = insertParams(XML,operation.getName());
    TStep.setPropertyValue("Request", XML);
    project.saveAs("Test.xml");
    System.exit(0);



    I use a config file and properties file in order to set the parameters for the test case. Currently I use a function called insertParams in order to make this work how I like. The config file is stored in a map (propReader) which is stored in a map (config) which uses the testName as a key. The properties themselves are also stored in a map. Here's my insertParams function

        static String insertParams(String XML,String testName) {
    System.out.println(testName);
    Map<String, String> propReader = config.get(testName.toLowerCase());
    for (String key:propReader.keySet()) {
    System.out.println(key);
    XML = XmlUtils.setXPathContent(XML, key, properties.get(propReader.get(key).toLowerCase()));
    }
    return XML;
    }




    The majority of this is still in the testing phases but if your interested in a singular application that will allow you full control, this should do it. All you would have to do is create a GUI which builds the property files for your application and then set this to run the SoapUI project instead of just saving it. For more complex situations you will need to customize the code to build the XML properly. If anyone has any questions or suggestions please, please ask.


    Note:

    This is all built in Java, not within SoapUI itself. It uses a lot of the SoapUI functionality. I am not responsible if you accidentally wipe your HDD or something crazy. The code itself is safe and secure. It does need more optimization and such but as of now it does a decent job. I have only had about 3 days of work on it so far.
  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Sadly I do not have any Java code that would build this currently. The beautiful parts of this script is that it essentially uses all of the functions from SoapUI itself so I will set up a basic script that will set it up really quick.


    I set this up using SoapUI's groovy scripts so it might need some adjusting to go into Java (I tried to make that as friendly as possible).

    import com.eviware.soapui.model.mock.*;
    import com.eviware.soapui.model.iface.*;
    import com.eviware.soapui.impl.wsdl.mock.*;

    //Below line required for SoapUI groovy step. If you use the above code the project variable is already declared
    //def project = testRunner.testCase.testSuite.project;

    WsdlMockService ms = project.addNewMockService(project.getName());
    for (Interface inf:project.getInterfaceList()) {
    for (Operation ops:inf.getAllOperations()) {
    WsdlMockOperation mo = ms.addNewMockOperation(ops);
    WsdlMockResponse mr = mo.addNewMockResponse(ops.getName(),true);
    }
    }


    I am not sure how well this works since I don't use mockservices. If you use it, please let me know how it works out for you. Just an FYI, this will only build the project file to be ran within SoapUI. If you want to run it externally, you will have to just use the commands from SoapUI's API to run the mockservice and then remove the System.exit(0); portion at the end.
  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Since the majority of this uses the APIs built into SoapUI I am just going to post some of the data from a groovy script that will access what you need in order to complete your issue.


    context.testCase.testSuite.project.getInterfaceAt(0).getAllOperations()[0].getRequestAt(0).setMediaType("Audio")
    log.info context.testCase.testSuite.project.getInterfaceAt(0).getAllOperations()[0].getRequestAt(0).getMediaType()

    context.testCase.testSuite.project.getRestMockServiceAt(0).getMockOperationAt(0).setDispatchStyle("SCRIPT")
    log.info context.testCase.testSuite.project.getRestMockServiceAt(0).getMockOperationAt(0).getDispatchStyle()


    These will working SoapUI itself. if you need help bringing down the flow of it just let me know but they are rather simplistic and easy to follow. Just make sure that in the Java you can access these same Objects and you should be fine. If you have further questions feel free to ask.
  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Figured I would update this since I have discovered some methods that may be useful for others.

    You can import SoapUI functionality directly into a java project by following the steps here:

    http://www.soapui.org/Developers-Corner/integrating-with-soapui.html

    They are extremely vague and generic and does not describe much at all so let me explain a bit further and in detail, and that script there will not work as it seems to be outdated a bit. For this to work you have to have the soapui-4.5.1.jar file (easy to get, google it), also your going to need the soapui-xmlbeans-4.5.1.jar file. The rest of the imports can be found in your lib folder of your SoapUI install (much much easier than finding all of those libraries on the interwebs like I was doing originally).

    This gives you what seems to essentially be nearly all of the SoapUI functionality within a java script. Now time to start creating projects with it. If anyone is interested in it please post here and I will try and create a java project that is a bit more generic and not designed specifically for my company's use in order to be able to distribute.
  • PaulDonny's avatar
    PaulDonny
    Regular Contributor
    Thank you.

    The next phase of development (Ability to handle multiple WSDLs simultaneously, a UI for creating the XPath and variable definitions) is under way. I am running into several issues with the project currently but my first suite of testing has been extremely successful so far.

    I will likely start developing this in my personal time so that I can use it at more than just my current job and be able to make it a lot more versatile.

    Again, suggestions are extremely helpful so if you see anything that can be improved please let me know.
  • srikanthrapolu's avatar
    srikanthrapolu
    Occasional Contributor
    Hi Paul,

    Thank you very much for your post. I have a very similar requirement what you have described:

    Generate the Soapui Project and a mock service from it using Java or any other scripting.
    I am not from a Java background so would it be possible for you to share the project which does the below:

    - Take WSDL as input and create soapui project
    - Generate mock service out of the soapui project
    - embed a static response for the request

    It would be very great if you share the code for the above point if you have it already.

    Thanks
    Srikanth Rapolu
  • srikanthrapolu's avatar
    srikanthrapolu
    Occasional Contributor
    Hi Paul,

    I was able to create the SOAP mock service creation through SOAPUI java API and progrmatically configure all the parameters.
    Now i want to do similar stuff for REST services/Rest Mock services. Do you by any chance have worked on it?

    I am able to create REST project, load WADL, Create Mock Service and add response files. But i am not able to set the media type, Dispatch Type as Script. Any pointers would be of help.

    Thanks
    Srikanth