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.teststeps.registry;
14  
15  import java.awt.event.ActionEvent;
16  
17  import javax.swing.AbstractAction;
18  
19  import com.eviware.soapui.config.HttpRequestConfig;
20  import com.eviware.soapui.config.RestParametersConfig;
21  import com.eviware.soapui.config.TestStepConfig;
22  import com.eviware.soapui.impl.rest.RestRequestInterface;
23  import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
24  import com.eviware.soapui.impl.rest.support.RestUtils;
25  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
26  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
27  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
28  import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep;
29  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
30  import com.eviware.soapui.support.MessageSupport;
31  import com.eviware.soapui.support.StringUtils;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.x.form.XFormDialog;
34  import com.eviware.x.form.XFormOptionsField;
35  import com.eviware.x.form.support.ADialogBuilder;
36  import com.eviware.x.form.support.AField;
37  import com.eviware.x.form.support.AForm;
38  import com.eviware.x.form.validators.RequiredValidator;
39  
40  /***
41   * Factory for WsdlTestRequestSteps
42   * 
43   * @author Ole.Matzura
44   */
45  
46  public class HttpRequestStepFactory extends WsdlTestStepFactory
47  {
48  	public static final String HTTPREQUEST_TYPE = "httprequest";
49  	private XFormDialog dialog;
50  	public static final MessageSupport messages = MessageSupport.getMessages( HttpRequestStepFactory.class );
51  	private XmlBeansRestParamsTestPropertyHolder params;
52  	private RestParamsTable paramsTable;
53  
54  	public HttpRequestStepFactory()
55  	{
56  		super( HTTPREQUEST_TYPE, "HTTP Test Request", "Submits a HTTP Request and validates its response",
57  				"/http_request.gif" );
58  	}
59  
60  	public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
61  	{
62  		return new HttpTestRequestStep( testCase, config, forLoadTest );
63  	}
64  
65  	public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
66  	{
67  		if( dialog == null )
68  		{
69  			buildDialog();
70  		}
71  		else
72  		{
73  			dialog.setValue( Form.ENDPOINT, "" );
74  		}
75  
76  		params = new XmlBeansRestParamsTestPropertyHolder( testCase, RestParametersConfig.Factory.newInstance() );
77  
78  		paramsTable = new RestParamsTable( params, false );
79  		dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
80  		dialog.setValue( Form.STEPNAME, name );
81  
82  		try
83  		{
84  			if( dialog.show() )
85  			{
86  				HttpRequestConfig httpRequest = HttpRequestConfig.Factory.newInstance();
87  				httpRequest.setEndpoint( dialog.getValue( Form.ENDPOINT ) );
88  				httpRequest.setMethod( dialog.getValue( Form.HTTPMETHOD ) );
89  				new XmlBeansRestParamsTestPropertyHolder( testCase, httpRequest.addNewParameters() ).addParameters( params );
90  
91  				TestStepConfig testStep = TestStepConfig.Factory.newInstance();
92  				testStep.setType( HTTPREQUEST_TYPE );
93  				testStep.setConfig( httpRequest );
94  				testStep.setName( dialog.getValue( Form.STEPNAME ) );
95  
96  				return testStep;
97  			}
98  			else
99  			{
100 				return null;
101 			}
102 		}
103 		finally
104 		{
105 			paramsTable.release();
106 			paramsTable = null;
107 			params = null;
108 			dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
109 		}
110 	}
111 
112 	public boolean canCreate()
113 	{
114 		return true;
115 	}
116 
117 	private void buildDialog()
118 	{
119 		dialog = ADialogBuilder.buildDialog( Form.class );
120 		dialog.getFormField( Form.STEPNAME ).addFormFieldValidator( new RequiredValidator() );
121 		dialog.getFormField( Form.EXTRACTPARAMS ).setProperty( "action", new ExtractParamsAction() );
122 		( ( XFormOptionsField )dialog.getFormField( Form.HTTPMETHOD ) ).setOptions( RestRequestInterface.RequestMethod
123 				.getMethods() );
124 	}
125 
126 	@AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
127 	public interface Form
128 	{
129 		@AField( description = "Form.TestStepName.Description", type = AField.AFieldType.STRING )
130 		public final static String STEPNAME = messages.get( "Form.TestStepName.Label" );
131 
132 		@AField( description = "Form.Endpoint.Description", type = AField.AFieldType.STRING )
133 		public final static String ENDPOINT = messages.get( "Form.Endpoint.Label" );
134 
135 		@AField( description = "Form.ExtractParams.Description", type = AField.AFieldType.ACTION )
136 		public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
137 
138 		@AField( description = "Form.ParamsTable.Description", type = AField.AFieldType.COMPONENT )
139 		public final static String PARAMSTABLE = messages.get( "Form.ParamsTable.Label" );
140 
141 		@AField( description = "Form.HttpMethod.Description", type = AField.AFieldType.ENUMERATION )
142 		public final static String HTTPMETHOD = messages.get( "Form.HttpMethod.Label" );
143 	}
144 
145 	private class ExtractParamsAction extends AbstractAction
146 	{
147 		public ExtractParamsAction()
148 		{
149 			super( "Extract Params" );
150 		}
151 
152 		public void actionPerformed( ActionEvent e )
153 		{
154 			try
155 			{
156 				String path = RestUtils.extractParams( dialog.getValue( Form.ENDPOINT ), params, true );
157 				dialog.setValue( Form.ENDPOINT, path );
158 
159 				if( StringUtils.isNullOrEmpty( dialog.getValue( Form.STEPNAME ) ) )
160 				{
161 					setNameFromPath( path );
162 				}
163 
164 				paramsTable.refresh();
165 			}
166 			catch( Exception e1 )
167 			{
168 				UISupport.showInfoMessage( "No parameters to extract!" );
169 			}
170 		}
171 
172 		private void setNameFromPath( String path )
173 		{
174 			String[] items = path.split( "/" );
175 
176 			if( items.length > 0 )
177 			{
178 				dialog.setValue( Form.STEPNAME, items[items.length - 1] );
179 			}
180 		}
181 	}
182 
183 }