View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.tools;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.WsdlProject;
17  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
18  import com.eviware.soapui.model.mock.MockResult;
19  import com.eviware.soapui.model.mock.MockRunListener;
20  import com.eviware.soapui.model.mock.MockRunner;
21  import com.eviware.soapui.model.mock.MockService;
22  import com.eviware.soapui.model.project.ProjectFactoryRegistry;
23  import org.apache.commons.cli.CommandLine;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import java.text.SimpleDateFormat;
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.List;
31  
32  /***
33   * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
34   * directly from other classes.
35   * <p>
36   * For standalone usage, set the project file (with setProjectFile) and other desired properties before
37   * calling run</p>
38   *
39   * @author Ole.Matzura
40   */
41  
42  public class SoapUIMockServiceRunner extends AbstractSoapUIRunner
43  {
44     private String mockService;
45     private String port;
46     private String path;
47     private List<MockRunner> runners = new ArrayList<MockRunner>();
48     private boolean block;
49     private String projectPassword;
50     private WsdlProject project;
51  
52     public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION + " MockService Runner";
53  
54     /***
55      * Runs the specified MockService in the specified soapUI project file, see soapUI xdocs for details.
56      *
57      * @param args
58      * @throws Exception
59      */
60  
61     public static void main( String[] args ) throws Exception
62     {
63        new SoapUIMockServiceRunner().runFromCommandLine( args );
64     }
65  
66     public void setMockService( String mockService )
67     {
68        this.mockService = mockService;
69     }
70  
71     public void setPath( String path )
72     {
73        this.path = path;
74     }
75  
76     public void setPort( String port )
77     {
78        this.port = port;
79     }
80  
81     public SoapUIMockServiceRunner()
82     {
83        super( TITLE );
84     }
85  
86     public SoapUIMockServiceRunner( String title )
87     {
88        super( title );
89     }
90  
91     public boolean runRunner() throws Exception
92     {
93        initGroovyLog();
94  
95        String projectFile = getProjectFile();
96  
97  //		WsdlProject project = new WsdlProject( projectFile, getProjectPassword() );
98        WsdlProject project = (WsdlProject) ProjectFactoryRegistry.getProjectFactory( "wsdl" ).createNew( projectFile, getProjectPassword() );
99        if( project.isDisabled() )
100          throw new Exception( "Failed to load soapUI project file [" + projectFile + "]" );
101 
102       initProject();
103 
104       if( mockService == null )
105          log.info( "Running all MockServices in project [" + project.getName() + "]" );
106       else
107          log.info( "Running MockService [" + mockService + "] in project [" + project.getName() + "]" );
108 
109       log.info( "Press any key to terminate" );
110 
111       long startTime = System.nanoTime();
112 
113       for( int c = 0; c < project.getMockServiceCount(); c++ )
114       {
115          MockService ms = project.getMockServiceAt( c );
116          if( mockService == null || ms.getName().equals( mockService ) )
117             runMockService( (WsdlMockService) ms );
118       }
119 
120       log.info( "Started " + runners.size() + " runner" + ( ( runners.size() == 1 ) ? "" : "s" ) );
121 
122       if( block )
123       {
124          System.out.println( "Press any key to terminate..." );
125          System.in.read();
126          for( MockRunner runner : runners )
127             runner.stop();
128       }
129 
130       long timeTaken = ( System.nanoTime() - startTime ) / 1000000;
131       log.info( "time taken: " + timeTaken + "ms" );
132 
133       exportReports();
134 
135       return block;
136    }
137 
138    protected void initProject() throws Exception
139    {
140    }
141 
142    protected void exportReports() throws Exception
143    {
144    }
145 
146    /***
147     * Runs the specified MockService
148     *
149     * @param mockService
150     */
151 
152    public void runMockService( WsdlMockService mockService )
153    {
154       try
155       {
156          if( path != null )
157             mockService.setPath( path );
158 
159          if( port != null )
160             mockService.setPort( Integer.parseInt( port ) );
161 
162          mockService.addMockRunListener( new LogListener() );
163          runners.add( mockService.start() );
164       }
165       catch( Exception e )
166       {
167          SoapUI.logError( e );
168       }
169    }
170 
171    public class LogListener implements MockRunListener
172    {
173       private SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );
174       private int responseCount;
175 
176       public void onMockRunnerStart( MockRunner mockRunner )
177       {
178          log.info( "MockService started on port " + mockRunner.getMockService().getPort() + " at path [" +
179                  mockRunner.getMockService().getPath() + "]" );
180       }
181 
182       public void onMockRunnerStop( MockRunner mockRunner )
183       {
184          log.info( "MockService stopped, handled " + responseCount + " requests" );
185       }
186 
187       public void onMockResult( MockResult result )
188       {
189          responseCount++;
190          log.info( "Handled request " + responseCount + "; [" + result.getMockResponse().getMockOperation().getName() +
191                  "] with [" + result.getMockResponse().getName() + "] in [" + result.getTimeTaken() +
192                  "ms] at [" + dateFormat.format( new Date( result.getTimestamp() ) ) + "]" );
193       }
194 
195       public void onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
196       {
197       }
198    }
199 
200    @Override
201    protected SoapUIOptions initCommandLineOptions()
202    {
203       SoapUIOptions options = new SoapUIOptions( "mockservicerunner" );
204       options.addOption( "m", true, "Specified the name of the MockService to run" );
205       options.addOption( "p", true, "Sets the local port to listen on" );
206       options.addOption( "a", true, "Sets the url path to listen on" );
207       options.addOption( "s", true, "Sets the soapui-settings.xml file to use" );
208       options.addOption( "b", false, "Turns off blocking read for termination" );
209       options.addOption( "x", true, "Sets project password for decryption if project is encrypted" );
210       options.addOption( "v", true, "Sets password for soapui-settings.xml file" );
211       options.addOption( "D", true, "Sets system property with name=value");
212       options.addOption( "G", true, "Sets global property with name=value" );
213       
214       return options;
215    }
216 
217    @Override
218    protected boolean processCommandLine( CommandLine cmd )
219    {
220       if( cmd.hasOption( "m" ) )
221          setMockService( getCommandLineOptionSubstSpace( cmd, "m" ) );
222 
223       if( cmd.hasOption( "a" ) )
224          setPath( getCommandLineOptionSubstSpace( cmd, "a" ) );
225 
226       if( cmd.hasOption( "p" ) )
227          setPort( cmd.getOptionValue( "p" ) );
228 
229       if( cmd.hasOption( "s" ) )
230          setSettingsFile( getCommandLineOptionSubstSpace( cmd, "s" ) );
231 
232       setBlock( !cmd.hasOption( 'b' ) );
233 
234       if( cmd.hasOption( "x" ) )
235       {
236          setProjectPassword( cmd.getOptionValue( "x" ) );
237       }
238 
239       if( cmd.hasOption( "v" ) )
240       {
241          setSoapUISettingsPassword( cmd.getOptionValue( "v" ) );
242       }
243 
244       if( cmd.hasOption( "D" ) )
245       {
246          setSystemProperties( cmd.getOptionValues( "D" ) );
247       }
248 
249       if( cmd.hasOption( "G" ) )
250       {
251          setGlobalProperties( cmd.getOptionValues( "G" ) );
252       }
253 
254       return true;
255    }
256 
257    public void setProjectPassword( String projectPassword )
258    {
259       this.projectPassword = projectPassword;
260    }
261 
262    public String getProjectPassword()
263    {
264       return projectPassword;
265    }
266 
267    public void setBlock( boolean block )
268    {
269       this.block = block;
270    }
271 
272    public WsdlProject getProject()
273    {
274       return project;
275 	   }
276 }