Forum Discussion
Matt_Johnson
16 years agoNew Contributor
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>");
}
}