View Javadoc

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