Forum Discussion

Matt_Johnson's avatar
Matt_Johnson
New Contributor
16 years ago

[HELP] Trying to use SoapUI JAR to make changes

I am trying to use Eclipse and the SoapUI JAR to make changes to project files. I am trying to add test steps to existing test cases, but I cannot find a way to use the API to do this. I have tried trudging through the Javadoc, but that is a pretty frustrating trip because of the lack of information in the Javadoc. I would appreciate any help I can get. Here is a sniplet of my current code:


public static void main(String[] args) {
try {
WsdlProjectPro project = new WsdlProjectPro("C:\\SoapUI\\soap.xml");
for (TestSuite testSuite : project.getTestSuiteList()) {
for (TestCase testCase : testSuite.getTestCaseList()) {
for (TestStep testStep : testCase.getTestStepList()) {
String name = testStep.getName();
if (name.endsWith("-correct")) {
GenerateSQLFaults(testStep);
}
}
}
}

} catch (XmlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SoapUIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static void GenerateSQLFaults(TestStep testStep) {
WsdlTestCase testCase = (WsdlTestCase) testStep.getTestCase();
TestSuite testSuite = testCase.getTestSuite();
HttpRequestStepFactory factory = new HttpRequestStepFactory();
TestStepConfig cfg = factory.createNewTestStep(testCase, "name"); // <-- this fails because of UI calls it is trying to make
}

3 Replies

  • Hello,

    Sorry that the Javadoc isn't very well documented. I hate to say this, but the easiest way to integrate soapUI with your own Java code is to read the soapUI source code. Here're the relevant parts from HttpRequestStepFactory.createNewTestStep():


    XmlBeansRestParamsTestPropertyHolder params = new XmlBeansRestParamsTestPropertyHolder( testCase, RestParametersConfig.Factory.newInstance() );

    ...

    HttpRequestConfig httpRequest = HttpRequestConfig.Factory.newInstance();
    httpRequest.setEndpoint( dialog.getValue( Form.ENDPOINT ) );
    httpRequest.setMethod( dialog.getValue( Form.HTTPMETHOD ) );
    new XmlBeansRestParamsTestPropertyHolder( testCase, httpRequest.addNewParameters() ).addParameters( params );

    TestStepConfig testStep = TestStepConfig.Factory.newInstance();
    testStep.setType( HTTPREQUEST_TYPE );
    testStep.setConfig( httpRequest );
    testStep.setName( dialog.getValue( Form.STEPNAME ) );

    return testStep;


    Maybe you can modify this and replace the dialog values with your own? Good luck!

    Regards,
    Dain
    eviware.com
  • Thank you for your response. Through lots of trial and error (8 hours worth) I was able to figure out how to clone an existing operation. Which is what I really needed anyways. Now I am having trouble adding an assertion. I don't get to copy this one. I am pasting all my code for any others down the road that stumble upon this thread.


    import java.io.IOException;

    import org.apache.xmlbeans.XmlException;

    import com.eviware.soapui.impl.wsdl.WsdlProjectPro;
    import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
    import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
    import com.eviware.soapui.impl.wsdl.teststeps.assertions.soap.SoapFaultAssertion;
    import com.eviware.soapui.model.ModelItem;
    import com.eviware.soapui.model.testsuite.TestCase;
    import com.eviware.soapui.model.testsuite.TestProperty;
    import com.eviware.soapui.model.testsuite.TestStep;
    import com.eviware.soapui.model.testsuite.TestSuite;
    import com.eviware.soapui.support.SoapUIException;

    /**
    * Opens an existing SoapUI project. Clones TestSteps which are marked '-correct' and creates various auto steps.
    * @author mjohnson
    *
    */
    public class AutoProject {
    private String projectFile;
    private WsdlProjectPro project;

    /**
    * Constructs object to prepare for modifying the file.
    * @param projectFile Location of the project file
    */
    public AutoProject(String projectFile) {
    this.projectFile = projectFile;
    openProject();
    }

    /**
    * Performs the actions against the file
    */
    public void run() {
    for (TestSuite testSuite : project.getTestSuiteList()) {
    for (TestCase testCase : testSuite.getTestCaseList()) {
    //remove any autos that have been added
    cleanAutos((WsdlTestCase) testCase);
    for (TestStep testStep : testCase.getTestStepList()) {
    String name = testStep.getName();
    //look for the test step to clone
    if (name.endsWith("-correct")) {
    generateAutos((WsdlTestStep)testStep);
    }
    }
    }
    }
    }

    /**
    * Saves the changes made to the file
    */
    public void commit() {
    try {
    project.save();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    /**
    * Opens project and handles errors
    */
    private void openProject() {
    try {
    project = new WsdlProjectPro(projectFile);
    } catch (XmlException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SoapUIException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    /**
    * Removes all existing test steps marked with '(auto)'
    * @param testCase test case to parse through
    */
    private void cleanAutos(WsdlTestCase testCase) {
    for (TestStep testStep : testCase.getTestStepList()) {
    WsdlTestStep wsdlTestStep = (WsdlTestStep) testStep;
    String name = wsdlTestStep.getName();
    if (name.endsWith("(auto)")) {
    testCase.removeTestStep(wsdlTestStep);
    }
    }
    }

    /**
    * Generates the "auto" steps
    * @param testStep the test step found with the '-correct' label
    */
    private void generateAutos(WsdlTestStep testStep) {
    setupSQLInjectSqlString(testStep);
    }

    /**
    * Creates a test step with sql in the username. This is designed to test against sql injection
    * @param correctStep
    */
    private void setupSQLInjectSqlString(WsdlTestStep correctStep) {
    WsdlTestStep newStep = cloneStep(correctStep, "sql string");
    //fix request
    TestProperty request = newStep.getProperty("Request");
    String xml = request.getValue();
    xml = setUsername(xml, "select * from usertable");
    xml = setPassword(xml, "1");
    //fix assertions
    for (ModelItem assertion : newStep.getChildren()) {
    if (assertion.getClass().getName().equalsIgnoreCase("NotSoapFaultAssertion")) {
    newStep.getChildren().remove(assertion);
    }
    }
    //need to add a SoapFaultAssertion ??? not sure how from here
    }

    /**
    * Clones a test step
    * @param correctStep the step with the mark '-correct'
    * @param name the new name of the cloned step. Must be unique to the test case
    * @return the cloned test step. This is already attached to the test case
    */
    private WsdlTestStep cloneStep(WsdlTestStep correctStep, String name) {
    return correctStep.clone(correctStep.getTestCase(), "-" + name + "(auto)");
    }

    /**
    * Uses regex to change the password field
    * @param xml xml to modify
    * @param newPassword new password
    * @return
    */
    private String setPassword(String xml, String newPassword) {
    return xml.replaceAll("<stor:Password>.*</stor:Password>", "<stor:Password>"+newPassword+"</stor:Password>");
    }

    /**
    * Uses regex to change the username field
    * @param xml xml to modify
    * @param newUsername new username
    * @return
    */
    private String setUsername(String xml, String newUsername) {
    return xml.replaceAll("<stor:Username>.*</stor:Username>", "<stor:Username>"+newUsername+"</stor:Username>");
    }
    }

  • Ok, I have figured it out. If anybody wants the code here it is. After looking at the javadoc and Type Hierarchy in Eclipse I found out that there is a class WsdlTestRequestStep which provides some good helper methods. Here is my code:


    import java.io.IOException;

    import org.apache.xmlbeans.XmlException;

    import com.eviware.soapui.impl.wsdl.WsdlProjectPro;
    import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
    import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
    import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
    import com.eviware.soapui.model.testsuite.TestAssertion;
    import com.eviware.soapui.model.testsuite.TestCase;
    import com.eviware.soapui.model.testsuite.TestProperty;
    import com.eviware.soapui.model.testsuite.TestStep;
    import com.eviware.soapui.model.testsuite.TestSuite;
    import com.eviware.soapui.support.SoapUIException;

    /**
    * Opens an existing SoapUI project. Clones TestSteps which are marked '-correct' and creates various auto steps.
    * @author mjohnson
    *
    */
    public class AutoProject {
    private String projectFile;
    private WsdlProjectPro project;

    /**
    * Constructs object to prepare for modifying the file.
    * @param projectFile Location of the project file
    */
    public AutoProject(String projectFile) {
    this.projectFile = projectFile;
    openProject();
    }

    /**
    * Performs the actions against the file
    */
    public void run() {
    for (TestSuite testSuite : project.getTestSuiteList()) {
    for (TestCase testCase : testSuite.getTestCaseList()) {
    //remove any autos that have been added
    cleanAutos((WsdlTestCase) testCase);
    for (TestStep testStep : testCase.getTestStepList()) {
    String name = testStep.getName();
    //look for the test step to clone
    if (name.endsWith("-correct")) {
    generateAutos((WsdlTestStep)testStep);
    }
    }
    }
    }
    }

    /**
    * Saves the changes made to the file
    */
    public void commit() {
    try {
    project.save();
    } catch (IOException e) {
    throw new ProjectFileException("I/O error while saving project file ["+projectFile+"].", e);
    }
    }

    /**
    * Opens project and handles errors
    */
    private void openProject() {
    try {
    project = new WsdlProjectPro(projectFile);
    } catch (XmlException e) {
    throw new ProjectFileException("Could not parse project file ["+projectFile+"].", e);
    } catch (IOException e) {
    throw new ProjectFileException("I/O error while opening project file ["+projectFile+"].", e);
    } catch (SoapUIException e) {
    throw new ProjectFileException("Unknown SoapUI Error while opening project file ["+projectFile+"].", e);
    }
    }

    /**
    * Removes all existing test steps marked with '(auto)'
    * @param testCase test case to parse through
    */
    private void cleanAutos(WsdlTestCase testCase) {
    for (TestStep testStep : testCase.getTestStepList()) {
    WsdlTestStep wsdlTestStep = (WsdlTestStep) testStep;
    String name = wsdlTestStep.getName();
    if (name.endsWith("(auto)")) {
    testCase.removeTestStep(wsdlTestStep);
    }
    }
    }

    /**
    * Generates the "auto" steps
    * @param testStep the test step found with the '-correct' label
    */
    private void generateAutos(WsdlTestStep testStep) {
    setupSQLInjectSqlString(testStep);
    }

    /**
    * Creates a test step with sql in the username. This is designed to test against sql injection
    * @param correctStep
    */
    private void setupSQLInjectSqlString(WsdlTestStep correctStep) {
    WsdlTestRequestStep newStep = (WsdlTestRequestStep) cloneStep(correctStep, "sql string");
    //fix request
    TestProperty request = newStep.getProperty("Request");
    String xml = request.getValue();
    xml = setUsername(xml, "select * from usertable");
    xml = setPassword(xml, "1");
    request.setValue(xml);
    //remove 'Not SOAP Fault' assertion
    for (TestAssertion assertion : newStep.getAssertionList()) {
    System.out.println(assertion.getName());
    if (assertion.getClass().getName().endsWith("NotSoapFaultAssertion")) {
    newStep.removeAssertion(assertion);
    }
    }
    //add 'SOAP Fault' assertion
    newStep.addAssertion("SOAP Fault");
    }

    /**
    * Clones a test step
    * @param correctStep the step with the mark '-correct'
    * @param name the new name of the cloned step. Must be unique to the test case
    * @return the cloned test step. This is already attached to the test case
    */
    private WsdlTestStep cloneStep(WsdlTestStep correctStep, String name) {
    String correctName = correctStep.getName();
    String newName = correctName.substring(0, correctName.length()-"-correct".length());
    return correctStep.clone(correctStep.getTestCase(), newName + "-" + name + "(auto)");
    }

    /**
    * Uses regex to change the password field
    * @param xml xml to modify
    * @param newPassword new password
    * @return
    */
    private String setPassword(String xml, String newPassword) {
    return xml.replaceAll("<stor:Password>.*</stor:Password>", "<stor:Password>"+newPassword+"</stor:Password>");
    }

    /**
    * Uses regex to change the username field
    * @param xml xml to modify
    * @param newUsername new username
    * @return
    */
    private String setUsername(String xml, String newUsername) {
    return xml.replaceAll("<stor:Username>.*</stor:Username>", "<stor:Username>"+newUsername+"</stor:Username>");
    }
    }