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.wsdl.actions.iface;
14  
15  import java.io.File;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.WsdlInterface;
21  import com.eviware.soapui.impl.wsdl.WsdlOperation;
22  import com.eviware.soapui.impl.wsdl.WsdlRequest;
23  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
24  import com.eviware.soapui.impl.wsdl.support.PathUtils;
25  import com.eviware.soapui.impl.wsdl.support.soap.SoapUtils;
26  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
27  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
28  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
30  import com.eviware.soapui.model.ModelItem;
31  import com.eviware.soapui.model.iface.Request;
32  import com.eviware.soapui.model.testsuite.TestCase;
33  import com.eviware.soapui.model.testsuite.TestSuite;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
36  import com.eviware.soapui.support.components.ModelItemListDesktopPanel;
37  import com.eviware.soapui.support.xml.XmlUtils;
38  import com.eviware.x.form.XFormDialog;
39  import com.eviware.x.form.XFormField;
40  import com.eviware.x.form.XFormFieldListener;
41  import com.eviware.x.form.support.ADialogBuilder;
42  import com.eviware.x.form.support.AField;
43  import com.eviware.x.form.support.AForm;
44  import com.eviware.x.form.support.AField.AFieldType;
45  
46  /***
47   * Updates the definition of a WsdlInterface.
48   * 
49   * @author Ole.Matzura
50   */
51  
52  public class UpdateInterfaceAction extends AbstractSoapUIAction<WsdlInterface>
53  {
54  	public static final String SOAPUI_ACTION_ID = "UpdateInterfaceAction";
55  	private XFormDialog dialog = null;
56  
57  	public UpdateInterfaceAction()
58  	{
59  		this( "Update Definition", "Reloads the definition for this interface and its operations" );
60  	}
61  
62  	protected UpdateInterfaceAction( String name, String description )
63  	{
64  		super( name, description );
65  	}
66  
67  	public void perform( WsdlInterface iface, Object param )
68  	{
69  		if( RemoveInterfaceAction.hasRunningDependingTests( iface ) )
70  		{
71  			UISupport.showErrorMessage( "Cannot update Interface due to running depending tests" );
72  			return;
73  		}
74  
75  		if( dialog == null )
76  		{
77  			dialog = ADialogBuilder.buildDialog( Form.class );
78  			dialog.setBooleanValue( Form.CREATE_REQUESTS, true );
79  			dialog.getFormField( Form.CREATE_BACKUPS ).setEnabled( false );
80  			dialog.getFormField( Form.RECREATE_OPTIONAL ).setEnabled( false );
81  			dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( false );
82  			dialog.getFormField( Form.RECREATE_REQUESTS ).addFormFieldListener( new XFormFieldListener()
83  			{
84  
85  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
86  				{
87  					boolean enabled = dialog.getBooleanValue( Form.RECREATE_REQUESTS );
88  
89  					dialog.getFormField( Form.CREATE_BACKUPS ).setEnabled( enabled );
90  					dialog.getFormField( Form.RECREATE_OPTIONAL ).setEnabled( enabled );
91  					dialog.getFormField( Form.KEEP_EXISTING ).setEnabled( enabled );
92  				}
93  			} );
94  		}
95  
96  		dialog.setValue( Form.DEFINITION_URL, iface.getDefinition() );
97  		dialog.getFormField( Form.DEFINITION_URL ).setToolTip( PathUtils.expandPath( iface.getDefinition(), iface ) );
98  		if( !dialog.show() )
99  			return;
100 
101 		String url = dialog.getValue( Form.DEFINITION_URL );
102 		if( url == null || url.trim().length() == 0 )
103 			return;
104 
105 		String expUrl = PathUtils.expandPath( url, iface );
106 		if( expUrl.trim().length() == 0 )
107 			return;
108 
109 		try
110 		{
111 			File file = new File( expUrl );
112 			if( file.exists() )
113 				expUrl = file.toURI().toURL().toString();
114 		}
115 		catch( Exception e )
116 		{
117 			SoapUI.logError( e );
118 		}
119 
120 		boolean createRequests = dialog.getBooleanValue( Form.CREATE_REQUESTS );
121 
122 		try
123 		{
124 			UISupport.setHourglassCursor();
125 			if( iface.updateDefinition( expUrl, createRequests ) )
126 			{
127 				afterUpdate( iface );
128 				if( !url.equals( expUrl ) )
129 					iface.setDefinition( url, false );
130 			}
131 			else
132 			{
133 				UISupport.showInfoMessage( "Update of interface failed", "Update Definition" );
134 		}
135 		}
136 		catch( Exception e1 )
137 		{
138 			UISupport.showInfoMessage( "Failed to update interface: [" + e1 + "]", "Update Definition" );
139 			SoapUI.logError( e1 );
140 		}
141 		finally
142 		{
143 			UISupport.resetCursor();
144 		}
145 	}
146 
147 	protected void afterUpdate( WsdlInterface iface ) throws Exception
148 	{
149 		if( dialog.getBooleanValue( Form.RECREATE_REQUESTS ) )
150 		{
151 			boolean buildOptional = dialog.getBooleanValue( Form.RECREATE_OPTIONAL );
152 			boolean createBackups = dialog.getBooleanValue( Form.CREATE_BACKUPS );
153 			boolean keepExisting = dialog.getBooleanValue( Form.KEEP_EXISTING );
154 			boolean keepHeaders = dialog.getBooleanValue( Form.KEEP_HEADERS );
155 
156 			List<ModelItem> updated = new ArrayList<ModelItem>();
157 
158 			updated.addAll( recreateRequests( iface, buildOptional, createBackups, keepExisting, keepHeaders ) );
159 
160 			if( dialog.getBooleanValue( Form.UPDATE_TESTREQUESTS ) )
161 				updated.addAll( recreateTestRequests( iface, buildOptional, createBackups, keepExisting, keepHeaders ) );
162 
163 			UISupport.showInfoMessage( "Update of interface successfull, [" + updated.size()
164 					+ "] Requests/TestRequests have" + " been updated.", "Update Definition" );
165 
166 			if( dialog.getBooleanValue( Form.OPEN_LIST ) )
167 			{
168 				UISupport
169 						.showDesktopPanel( new ModelItemListDesktopPanel( "Updated Requests/TestRequests",
170 								"The following Request/TestRequests where updated", updated.toArray( new ModelItem[updated
171 										.size()] ) ) );
172 			}
173 		}
174 		else
175 		{
176 			UISupport.showInfoMessage( "Update of interface successful", "Update Definition" );
177 		}
178 	}
179 
180 	public static List<Request> recreateRequests( WsdlInterface iface, boolean buildOptional, boolean createBackups,
181 			boolean keepExisting, boolean keepHeaders )
182 	{
183 		int count = 0;
184 
185 		List<Request> result = new ArrayList<Request>();
186 
187 		// first check operations
188 		for( int c = 0; c < iface.getOperationCount(); c++ )
189 		{
190 			WsdlOperation operation = iface.getOperationAt( c );
191 			String newRequest = operation.createRequest( buildOptional );
192 			List<Request> requests = operation.getRequestList();
193 
194 			for( Request request : requests )
195 			{
196 				String requestContent = request.getRequestContent();
197 
198 				if( keepHeaders )
199 				{
200 					newRequest = SoapUtils.transferSoapHeaders( requestContent, newRequest, iface.getSoapVersion() );
201 				}
202 
203 				String req = XmlUtils.transferValues( requestContent, newRequest );
204 
205 				// changed?
206 				if( !req.equals( requestContent ) )
207 				{
208 					if( !XmlUtils.prettyPrintXml( req ).equals( XmlUtils.prettyPrintXml( requestContent ) ) )
209 					{
210 						if( createBackups )
211 						{
212 							WsdlRequest backupRequest = operation.addNewRequest( "Backup of [" + request.getName() + "]" );
213 							( ( WsdlRequest )request ).copyTo( backupRequest, false, false );
214 						}
215 
216 						( ( WsdlRequest )request ).setRequestContent( req );
217 						count++ ;
218 
219 						result.add( request );
220 					}
221 				}
222 			}
223 		}
224 
225 		return result;
226 	}
227 
228 	public static List<WsdlTestRequestStep> recreateTestRequests( WsdlInterface iface, boolean buildOptional,
229 			boolean createBackups, boolean keepExisting, boolean keepHeaders )
230 	{
231 		int count = 0;
232 
233 		List<WsdlTestRequestStep> result = new ArrayList<WsdlTestRequestStep>();
234 
235 		// now check testsuites..
236 		for( TestSuite testSuite : iface.getProject().getTestSuiteList() )
237 		{
238 			for( TestCase testCase : testSuite.getTestCaseList() )
239 			{
240 				int testStepCount = testCase.getTestStepCount();
241 				for( int c = 0; c < testStepCount; c++ )
242 				{
243 					WsdlTestStep testStep = ( WsdlTestStep )testCase.getTestStepAt( c );
244 					if( testStep instanceof WsdlTestRequestStep )
245 					{
246 						WsdlTestRequest testRequest = ( ( WsdlTestRequestStep )testStep ).getTestRequest();
247 						if( testRequest != null && testRequest.getOperation() != null && testRequest.getOperation().getInterface() == iface )
248 						{
249 							String newRequest = testRequest.getOperation().createRequest( buildOptional );
250 
251 							if( keepHeaders )
252 							{
253 								newRequest = SoapUtils.transferSoapHeaders( testRequest.getRequestContent(), newRequest, iface
254 										.getSoapVersion() );
255 							}
256 
257 							if( keepExisting )
258 								newRequest = XmlUtils.transferValues( testRequest.getRequestContent(), newRequest );
259 
260 							// changed?
261 							if( !newRequest.equals( testRequest.getRequestContent() ) )
262 							{
263 								if( createBackups )
264 								{
265 									( ( WsdlTestCase )testCase ).importTestStep( testStep,
266 											"Backup of [" + testStep.getName() + "]", -1, true ).setDisabled( true );
267 								}
268 
269 								( ( WsdlRequest )testRequest ).setRequestContent( newRequest );
270 								count++ ;
271 
272 								result.add( ( WsdlTestRequestStep )testStep );
273 							}
274 						}
275 					}
276 				}
277 			}
278 		}
279 
280 		return result;
281 	}
282 
283 	@AForm( description = "Specify Update Definition options", name = "Update Definition", helpUrl = HelpUrls.UPDATE_INTERFACE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
284 	protected interface Form
285 	{
286 		@AField( name = "Definition URL", description = "The URL or file for the updated definition", type = AFieldType.FILE )
287 		public final static String DEFINITION_URL = "Definition URL";
288 
289 		@AField( name = "Create New Requests", description = "Create default requests for new methods", type = AFieldType.BOOLEAN )
290 		public final static String CREATE_REQUESTS = "Create New Requests";
291 
292 		@AField( name = "Recreate Requests", description = "Recreate existing request with the new schema", type = AFieldType.BOOLEAN )
293 		public final static String RECREATE_REQUESTS = "Recreate Requests";
294 
295 		@AField( name = "Recreate Optional", description = "Recreate optional content when updating requests", type = AFieldType.BOOLEAN )
296 		public final static String RECREATE_OPTIONAL = "Recreate Optional";
297 
298 		@AField( name = "Keep Existing", description = "Keeps existing values when recreating requests", type = AFieldType.BOOLEAN )
299 		public final static String KEEP_EXISTING = "Keep Existing";
300 
301 		@AField( name = "Keep SOAP Headers", description = "Keeps any SOAP Headers when recreating requests", type = AFieldType.BOOLEAN )
302 		public final static String KEEP_HEADERS = "Keep SOAP Headers";
303 
304 		@AField( name = "Create Backups", description = "Create backup copies of changed requests", type = AFieldType.BOOLEAN )
305 		public final static String CREATE_BACKUPS = "Create Backups";
306 
307 		@AField( name = "Update TestRequests", description = "Updates all TestRequests for operations in this Interface also", type = AFieldType.BOOLEAN )
308 		public final static String UPDATE_TESTREQUESTS = "Update TestRequests";
309 
310 		@AField( name = "Open Request List", description = "Opens a list of all requests that have been updated", type = AFieldType.BOOLEAN )
311 		public final static String OPEN_LIST = "Open Request List";
312 	}
313 }