1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.tools;
14
15 import java.io.File;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.cli.CommandLineParser;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.PosixParser;
27 import org.apache.log4j.Logger;
28
29 import com.eviware.soapui.SoapUI;
30 import com.eviware.soapui.impl.wsdl.WsdlProject;
31 import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
32 import com.eviware.soapui.model.mock.MockResult;
33 import com.eviware.soapui.model.mock.MockRunListener;
34 import com.eviware.soapui.model.mock.MockRunner;
35 import com.eviware.soapui.model.mock.MockService;
36
37 /***
38 * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
39 * directly from other classes.
40 * <p>
41 * For standalone usage, set the project file (with setProjectFile) and other desired properties before
42 * calling run</p>
43 *
44 * @author Ole.Matzura
45 */
46
47 public class SoapUIMockServiceRunner
48 {
49 private final static Logger log = Logger.getLogger( SoapUIMockServiceRunner.class );
50
51 private String projectFile;
52 private String mockService;
53 private String port;
54 private String path;
55
56 public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " MockService Runner";
57
58 /***
59 * Runs the specified MockService in the specified soapUI project file, see soapUI xdocs for details.
60 *
61 * @param args
62 * @throws Exception
63 */
64
65 @SuppressWarnings("static-access")
66 public static void main( String [] args) throws Exception
67 {
68 System.out.println( TITLE );
69 SoapUI.initSoapUILog();
70 SoapUIMockServiceRunner runner = new SoapUIMockServiceRunner();
71
72 Options options = new Options();
73 options.addOption( "m", true, "Sets the MockService" );
74 options.addOption( "p", true, "Sets the local port to listen on" );
75 options.addOption( "a", true, "Sets the url path to listen on" );
76 options.addOption( "s", false, "Sets the soapui-settings.xml file to use" );
77
78 CommandLineParser parser = new PosixParser();
79 CommandLine cmd = parser.parse( options, args);
80
81 String[] args2 = cmd.getArgs();
82 if( args2.length != 1 || !cmd.hasOption( "m") )
83 {
84 HelpFormatter formatter = new HelpFormatter();
85 formatter.printHelp( "mockservicerunner [options] <soapui-project-file>", options );
86
87 System.err.println( "Missing soapUI project file and/or MockService.." );
88 return;
89 }
90
91 runner.setProjectFile( args2[0] );
92 runner.setMockService( cmd.getOptionValue( "m") );
93
94 if( cmd.hasOption( "a"))
95 runner.setPath( cmd.getOptionValue( "a" ) );
96
97 if( cmd.hasOption( "p"))
98 runner.setPort( cmd.getOptionValue( "p" ) );
99
100 if( cmd.hasOption( "s"))
101 SoapUI.initSettings( cmd.getOptionValue( "s" ));
102
103 try
104 {
105 SoapUI.loadExtLibs();
106
107 runner.run();
108 System.exit( 0 );
109 }
110 catch (Exception e)
111 {
112 log.error( e.toString() );
113 e.printStackTrace();
114 System.exit( 1 );
115 }
116 }
117
118 public void setProjectFile(String projectFile)
119 {
120 this.projectFile = projectFile;
121 }
122
123 public void setMockService(String mockService)
124 {
125 this.mockService = mockService;
126 }
127
128 public void setPath( String path )
129 {
130 this.path = path;
131 }
132
133 public void setPort( String port )
134 {
135 this.port = port;
136 }
137
138 public SoapUIMockServiceRunner()
139 {
140 }
141
142 public void run() throws Exception
143 {
144 if( !new File( projectFile ).exists() )
145 throw new Exception( "soapUI project file [" + projectFile + "] not found" );
146
147 WsdlProject project = new WsdlProject( projectFile, null );
148 log.info( "Running MockService [" + mockService + "] in project [" + project.getName() + "]" );
149 log.info( "Press any key to terminate" );
150
151 long startTime = System.nanoTime();
152
153 for( int c = 0; c < project.getMockServiceCount(); c++ )
154 {
155 MockService ms = project.getMockServiceAt( c );
156 if( ms.getName().equals( mockService ))
157 runMockService( ( WsdlMockService ) ms );
158 }
159
160 long timeTaken = (System.nanoTime()-startTime)/1000000;
161 log.info( "time taken: " + timeTaken + "ms" );
162 }
163
164 /***
165 * Runs the configured tool for the specified interface.. needs to be refactored to use
166 * some kind of registry/factory pattern for tools
167 *
168 * @param iface
169 */
170
171 public void runMockService( WsdlMockService mockService )
172 {
173 try
174 {
175 if( path != null )
176 mockService.setPath( path );
177
178 if( port != null )
179 mockService.setPort( Integer.parseInt( port ));
180
181 mockService.addMockRunListener( new LogListener() );
182 MockRunner runner = mockService.start();
183
184 System.in.read();
185 runner.stop();
186 }
187 catch (Exception e)
188 {
189 e.printStackTrace();
190 }
191 }
192
193 public void log(String msg)
194 {
195 System.out.print( msg );
196 }
197
198 public void logError( String msg )
199 {
200 System.err.println( msg );
201 }
202
203 public class LogListener implements MockRunListener
204 {
205 private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
206 private int responseCount;
207
208 public void onMockRunnerStart( MockRunner mockRunner )
209 {
210 log.info( "MockService started on port " + mockRunner.getMockService().getPort() + " at path [" +
211 mockRunner.getMockService().getPath() + "]" );
212 }
213
214 public void onMockRunnerStop( MockRunner mockRunner )
215 {
216 log.info( "MockService stopped, handled " + responseCount + " requests" );
217 }
218
219 public void onMockResult( MockResult result )
220 {
221 responseCount++;
222 log.info( "Handled request " + responseCount + "; [" + result.getMockResponse().getMockOperation().getName() +
223 "] with [" + result.getMockResponse().getName() + "] in [" + result.getTimeTaken() +
224 "ms] at [" + dateFormat.format( new Date( result.getTimestamp())) + "]" );
225 }
226
227 public void onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
228 {
229
230
231 }
232 }
233 }