1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.actions;
14
15 import java.io.File;
16 import java.net.URL;
17
18 import com.eviware.soapui.SoapUI;
19 import com.eviware.soapui.impl.WorkspaceImpl;
20 import com.eviware.soapui.impl.WsdlInterfaceFactory;
21 import com.eviware.soapui.impl.rest.RestService;
22 import com.eviware.soapui.impl.rest.RestServiceFactory;
23 import com.eviware.soapui.impl.rest.actions.project.NewRestServiceAction;
24 import com.eviware.soapui.impl.rest.actions.service.GenerateRestTestSuiteAction;
25 import com.eviware.soapui.impl.rest.support.WadlImporter;
26 import com.eviware.soapui.impl.wsdl.WsdlInterface;
27 import com.eviware.soapui.impl.wsdl.WsdlProject;
28 import com.eviware.soapui.impl.wsdl.actions.iface.GenerateMockServiceAction;
29 import com.eviware.soapui.impl.wsdl.actions.iface.GenerateWsdlTestSuiteAction;
30 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
31 import com.eviware.soapui.impl.wsdl.support.PathUtils;
32 import com.eviware.soapui.support.MessageSupport;
33 import com.eviware.soapui.support.SoapUIException;
34 import com.eviware.soapui.support.StringUtils;
35 import com.eviware.soapui.support.UISupport;
36 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
37 import com.eviware.x.form.XFormDialog;
38 import com.eviware.x.form.XFormField;
39 import com.eviware.x.form.XFormFieldListener;
40 import com.eviware.x.form.support.ADialogBuilder;
41 import com.eviware.x.form.support.AField;
42 import com.eviware.x.form.support.AForm;
43 import com.eviware.x.form.support.AField.AFieldType;
44
45 /***
46 * Action for creating a new WSDL project
47 *
48 * @author Ole.Matzura
49 */
50
51 public class NewWsdlProjectAction extends AbstractSoapUIAction<WorkspaceImpl>
52 {
53 public static final String SOAPUI_ACTION_ID = "NewWsdlProjectAction";
54 private XFormDialog dialog;
55
56 public static final MessageSupport messages = MessageSupport.getMessages( NewWsdlProjectAction.class );
57
58 public NewWsdlProjectAction()
59 {
60 super( messages.get( "Title" ), messages.get( "Description" ) );
61 }
62
63 public void perform( WorkspaceImpl workspace, Object param )
64 {
65 if( dialog == null )
66 {
67 dialog = ADialogBuilder.buildDialog( Form.class );
68 dialog.setValue( Form.CREATEREQUEST, Boolean.toString( true ) );
69 dialog.getFormField( Form.INITIALWSDL ).addFormFieldListener( new XFormFieldListener()
70 {
71 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
72 {
73 String value = newValue.toLowerCase().trim();
74
75 dialog.getFormField( Form.CREATEREQUEST )
76 .setEnabled( value.length() > 0 && !newValue.endsWith( ".wadl" ) );
77 dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled(
78 newValue.trim().length() > 0 && !newValue.endsWith( ".wadl" ) );
79 dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( newValue.trim().length() > 0 );
80 dialog.getFormField( Form.ADDRESTSERVICE ).setEnabled( newValue.trim().length() == 0 );
81
82 initProjectName( newValue );
83 }
84 } );
85 }
86 else
87 {
88 dialog.setValue( Form.INITIALWSDL, "" );
89 dialog.setValue( Form.PROJECTNAME, "" );
90 dialog.setBooleanValue( Form.ADDRESTSERVICE, false );
91
92 dialog.getFormField( Form.CREATEREQUEST ).setEnabled( false );
93 dialog.getFormField( Form.GENERATEMOCKSERVICE ).setEnabled( false );
94 dialog.getFormField( Form.GENERATETESTSUITE ).setEnabled( false );
95 dialog.getFormField( Form.ADDRESTSERVICE ).setEnabled( true );
96 }
97
98 if( param instanceof String )
99 {
100 dialog.setValue( Form.INITIALWSDL, param.toString() );
101 initProjectName( param.toString() );
102 }
103
104 while( dialog.show() )
105 {
106 WsdlProject project = null;
107 try
108 {
109 String projectName = dialog.getValue( Form.PROJECTNAME ).trim();
110 if( projectName.length() == 0 )
111 {
112 UISupport.showErrorMessage( messages.get( "MissingProjectNameError" ) );
113 }
114 else
115 {
116 project = workspace.createProject( projectName, null );
117
118 if( project != null )
119 {
120 UISupport.select( project );
121 String url = dialog.getValue( Form.INITIALWSDL ).trim();
122
123 if( dialog.getBooleanValue( Form.RELATIVEPATHS ) )
124 {
125 String folder = workspace.getProjectRoot();
126
127 if( PathUtils.isFilePath( url ) && PathUtils.isAbsolutePath( url ) )
128 {
129 folder = new File( url ).getParent().toString();
130 }
131
132 if( !project.save( folder ) )
133 {
134 UISupport
135 .showErrorMessage( "Project was not saved, paths will not be stored relatively until configured." );
136 }
137 else
138 {
139 project.setResourceRoot( "${projectDir}" );
140 }
141 }
142
143 if( url.length() > 0 )
144 {
145 if( new File( url ).exists() )
146 url = new File( url ).toURI().toURL().toString();
147
148 if( url.toUpperCase().endsWith( "WADL" ) )
149 importWadl( project, url );
150 else
151 importWsdl( project, url );
152 }
153 else if( dialog.getBooleanValue( Form.ADDRESTSERVICE ) )
154 {
155 SoapUI.getActionRegistry().getAction( NewRestServiceAction.SOAPUI_ACTION_ID ).perform( project,
156 project );
157 }
158
159 break;
160 }
161 }
162 }
163 catch( Exception ex )
164 {
165 UISupport.showErrorMessage( ex );
166 if( project != null )
167 {
168 workspace.removeProject( project );
169 }
170 }
171 }
172 }
173
174 public void initProjectName( String newValue )
175 {
176 if( StringUtils.isNullOrEmpty( dialog.getValue( Form.PROJECTNAME ) )
177 && StringUtils.hasContent( newValue ) )
178 {
179 int ix = newValue.lastIndexOf( '.' );
180 if( ix > 0 )
181 newValue = newValue.substring( 0, ix );
182
183 ix = newValue.lastIndexOf( '/' );
184 if( ix == -1 )
185 ix = newValue.lastIndexOf( '//' );
186
187 if( ix != -1 )
188 dialog.setValue( Form.PROJECTNAME, newValue.substring( ix + 1 ) );
189 }
190 }
191
192 private void importWadl( WsdlProject project, String url )
193 {
194 RestService restService = ( RestService )project
195 .addNewInterface( project.getName(), RestServiceFactory.REST_TYPE );
196 UISupport.select( restService );
197 try
198 {
199 new WadlImporter( restService ).initFromWadl( url );
200
201 if( dialog.getBooleanValue( Form.GENERATETESTSUITE ) )
202 {
203 GenerateRestTestSuiteAction generateTestSuiteAction = new GenerateRestTestSuiteAction();
204 generateTestSuiteAction.generateTestSuite( restService, true );
205 }
206 }
207 catch( Exception e )
208 {
209 UISupport.showErrorMessage( e );
210 }
211 }
212
213 private void importWsdl( WsdlProject project, String url ) throws SoapUIException
214 {
215 WsdlInterface[] results = WsdlInterfaceFactory.importWsdl( project, url, dialog.getValue( Form.CREATEREQUEST )
216 .equals( "true" ) );
217 for( WsdlInterface iface : results )
218 {
219 UISupport.select( iface );
220
221 if( dialog.getBooleanValue( Form.GENERATETESTSUITE ) )
222 {
223 GenerateWsdlTestSuiteAction generateTestSuiteAction = new GenerateWsdlTestSuiteAction();
224 generateTestSuiteAction.generateTestSuite( iface, true );
225 }
226
227 if( dialog.getBooleanValue( Form.GENERATEMOCKSERVICE ) )
228 {
229 GenerateMockServiceAction generateMockAction = new GenerateMockServiceAction();
230 generateMockAction.generateMockService( iface, false );
231 }
232 }
233 }
234
235 @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWPROJECT_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
236 public interface Form
237 {
238 @AField( description = "Form.ProjectName.Description", type = AFieldType.STRING )
239 public final static String PROJECTNAME = messages.get( "Form.ProjectName.Label" );
240
241 @AField( description = "Form.InitialWsdl.Description", type = AFieldType.FILE )
242 public final static String INITIALWSDL = messages.get( "Form.InitialWsdl.Label" );
243
244 @AField( description = "Form.CreateRequests.Description", type = AFieldType.BOOLEAN, enabled = false )
245 public final static String CREATEREQUEST = messages.get( "Form.CreateRequests.Label" );
246
247 @AField( description = "Form.GenerateTestSuite.Description", type = AFieldType.BOOLEAN, enabled = false )
248 public final static String GENERATETESTSUITE = messages.get( "Form.GenerateTestSuite.Label" );
249
250 @AField( description = "Form.GenerateMockService.Description", type = AFieldType.BOOLEAN, enabled = false )
251 public final static String GENERATEMOCKSERVICE = messages.get( "Form.GenerateMockService.Label" );
252
253 @AField( description = "Form.AddRestService.Description", type = AFieldType.BOOLEAN, enabled = true )
254 public final static String ADDRESTSERVICE = messages.get( "Form.AddRestService.Label" );
255
256 @AField( description = "Form.RelativePaths.Description", type = AFieldType.BOOLEAN, enabled = true )
257 public final static String RELATIVEPATHS = messages.get( "Form.RelativePaths.Label" );
258
259
260
261
262
263 }
264 }