View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.impl.wsdl.actions.iface;
14  
15  import java.awt.event.ActionEvent;
16  
17  import com.eviware.soapui.impl.wsdl.WsdlInterface;
18  import com.eviware.soapui.impl.wsdl.WsdlOperation;
19  import com.eviware.soapui.impl.wsdl.WsdlProject;
20  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
21  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
22  import com.eviware.soapui.model.support.ModelSupport;
23  import com.eviware.soapui.support.UISupport;
24  import com.eviware.x.form.XFormDialog;
25  import com.eviware.x.form.support.ADialogBuilder;
26  import com.eviware.x.form.support.AField;
27  import com.eviware.x.form.support.AForm;
28  import com.eviware.x.form.support.AField.AFieldType;
29  
30  public class GenerateMockServiceAction extends AbstractSwingAction<WsdlInterface>
31  {
32  	private static final String CREATE_MOCKSUITE_OPTION = "<create>";
33  //	private XFormDialog dialog;
34  
35  	public GenerateMockServiceAction( WsdlInterface iface )
36  	{
37  		super( "Generate MockService", "Generates a MockService containing all Operations in this Interface", iface );
38  	}
39  
40  	public void actionPerformed( ActionEvent e, WsdlInterface iface )
41  	{
42  // Cannot (?) store and reuse widgets on SWT.
43  //		if( dialog == null )
44        XFormDialog dialog = ADialogBuilder.buildDialog( Form.class );
45  		
46  		WsdlProject project = ( WsdlProject ) iface.getProject();
47  		String[] mockServices = ModelSupport.getNames( project.getMockServices(), new String[] {CREATE_MOCKSUITE_OPTION});
48  		dialog.setOptions( Form.MOCKSERVICE, mockServices );
49  		
50  		dialog.setValue( Form.PATH, "/mock" + iface.getName() );
51  		dialog.setValue( Form.PORT, "8088" );
52  		
53  		if( dialog.show() )
54  		{
55  			String mockServiceName = dialog.getValue( Form.MOCKSERVICE );
56  			WsdlMockService mockService = ( WsdlMockService ) project.getMockServiceByName( mockServiceName );
57  			
58  			if( mockService == null || mockServiceName.equals( CREATE_MOCKSUITE_OPTION ))
59  			{
60  				mockServiceName = UISupport.prompt( "Specify name of MockService to create", getName(), iface.getName() + " MockService" );
61  				if( mockServiceName == null )
62  					return;
63  				
64  				mockService = ( WsdlMockService ) project.addNewMockService( mockServiceName );
65  			}
66  			
67  			mockService.setPath( dialog.getValue( Form.PATH ) );
68  			try
69  			{
70  				mockService.setPort( Integer.parseInt( dialog.getValue( Form.PORT )) );
71  			}
72  			catch( NumberFormatException e1 )
73  			{
74  			}
75  			
76  			for( int i = 0; i < iface.getOperationCount(); i++ )
77  			{
78  				WsdlOperation operation = ( WsdlOperation ) iface.getOperationAt( i );
79  				WsdlMockOperation mockOperation = ( WsdlMockOperation ) mockService.addNewMockOperation( operation );
80  				mockOperation.addNewMockResponse( "Response 1", true );
81  			}
82  			
83  			UISupport.showDesktopPanel( mockService );
84  		}
85  	}
86  	
87  	@AForm( name="Generate MockService", 
88  				description = "Set options for generated MockOperations for this Interface")
89  	private interface Form 
90  	{
91  		@AField(name = "MockService", description = "The MockService to create or use", type = AFieldType.ENUMERATION )
92  		public final static String MOCKSERVICE = "MockService";
93  		
94  		@AField(name = "Path", description = "The URL path to mount on", type = AFieldType.STRING )
95  		public final static String PATH = "Path";
96  
97  		@AField(name = "Port", description = "The endpoint port to listen on", type = AFieldType.STRING )
98  		public final static String PORT = "Port";
99  	}
100 }