View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.util.ArrayList;
16  import java.util.List;
17  
18  import com.eviware.soapui.config.RestRequestConfig;
19  import com.eviware.soapui.config.RestRequestStepConfig;
20  import com.eviware.soapui.config.StringToStringMapConfig;
21  import com.eviware.soapui.config.TestStepConfig;
22  import com.eviware.soapui.config.StringToStringMapConfig.Entry;
23  import com.eviware.soapui.impl.rest.RestMethod;
24  import com.eviware.soapui.impl.rest.RestRequest;
25  import com.eviware.soapui.impl.rest.RestResource;
26  import com.eviware.soapui.impl.rest.RestService;
27  import com.eviware.soapui.impl.rest.support.RestParamProperty;
28  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
29  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep;
30  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
31  import com.eviware.soapui.model.iface.Interface;
32  import com.eviware.soapui.model.project.Project;
33  import com.eviware.soapui.support.StringUtils;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.types.TupleList;
36  
37  /***
38   * Factory for WsdlTestRequestSteps
39   * 
40   * @author Ole.Matzura
41   */
42  
43  public class RestRequestStepFactory extends WsdlTestStepFactory
44  {
45  	public static final String RESTREQUEST_TYPE = "restrequest";
46  	public static final String STEP_NAME = "Name";
47  
48  	// private XFormDialog dialog;
49  	// private StringToStringMap dialogValues = new StringToStringMap();
50  
51  	public RestRequestStepFactory()
52  	{
53  		super( RESTREQUEST_TYPE, "REST Test Request", "Submits a REST-style Request and validates its response",
54  				"/rest_request.gif" );
55  	}
56  
57  	public static class ItemDeletedException extends Exception
58  	{
59  
60  	}
61  
62  	public WsdlTestStep buildTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
63  	{
64  		try
65  		{
66  			return new RestTestRequestStep( testCase, config, forLoadTest );
67  		}
68  		catch( ItemDeletedException e )
69  		{
70  			return null;
71  		}
72  	}
73  
74  	public static TestStepConfig createConfig( RestRequest request, String stepName )
75  	{
76  		RestRequestStepConfig requestStepConfig = RestRequestStepConfig.Factory.newInstance();
77  
78  		requestStepConfig.setService( request.getOperation().getInterface().getName() );
79  		requestStepConfig.setResourcePath( request.getOperation().getFullPath() );
80  		requestStepConfig.setMethodName( request.getRestMethod().getName() );
81  		requestStepConfig.addNewRestRequest().set( request.getConfig().copy() );
82  
83  		TestStepConfig testStep = TestStepConfig.Factory.newInstance();
84  		testStep.setType( RESTREQUEST_TYPE );
85  		testStep.setConfig( requestStepConfig );
86  		testStep.setName( stepName );
87  
88  		return testStep;
89  	}
90  
91  	@SuppressWarnings("unchecked")
92  	public TestStepConfig createNewTestStep( WsdlTestCase testCase, String name )
93  	{
94  		// build list of available interfaces / restResources
95  		Project project = testCase.getTestSuite().getProject();
96  		List<String> options = new ArrayList<String>();
97  		TupleList<RestMethod, RestRequest> restMethods = new TupleList<RestMethod, RestRequest>();
98  
99  		for( int c = 0; c < project.getInterfaceCount(); c++ )
100 		{
101 			Interface iface = project.getInterfaceAt( c );
102 			if( iface instanceof RestService )
103 			{
104 				List<RestResource> resources = ( ( RestService )iface ).getAllResources();
105 
106 				for( RestResource resource : resources )
107 				{
108 					// options.add( iface.getName() + " -> " + resource.getPath() );
109 					// restMethods.add( resource, null );
110 
111 					for( RestMethod method : resource.getRestMethodList() )
112 					{
113 						String methodStr = iface.getName() + " -> " + resource.getPath() + " -> " + method.getName();
114 						restMethods.add( method, null );
115 						options.add( methodStr );
116 
117 						for( RestRequest request : method.getRequestList() )
118 						{
119 							restMethods.add( method, request );
120 							options.add( methodStr + " -> " + request.getName() );
121 						}
122 					}
123 				}
124 			}
125 		}
126 
127 		if( restMethods.size() == 0 )
128 		{
129 			UISupport.showErrorMessage( "Missing REST Methods in project" );
130 			return null;
131 		}
132 
133 		Object op = UISupport.prompt( "Select REST method to invoke for request", "New RestRequest", options.toArray() );
134 		if( op != null )
135 		{
136 			int ix = options.indexOf( op );
137 			if( ix != -1 )
138 			{
139 				TupleList<RestMethod, RestRequest>.Tuple tuple = restMethods.get( ix );
140 
141 				// if( dialog == null )
142 				// buildDialog();
143 				//
144 				// dialogValues.put( STEP_NAME, name );
145 				// dialogValues = dialog.show( dialogValues );
146 				// if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
147 				// return null;
148 
149 				return tuple.getValue2() == null ? createNewTestStep( tuple.getValue1(), name ) : createConfig( tuple
150 						.getValue2(), name );
151 			}
152 		}
153 
154 		return null;
155 	}
156 
157 	public TestStepConfig createNewTestStep( RestMethod restMethod, String name )
158 	{
159 		RestRequestStepConfig requestStepConfig = RestRequestStepConfig.Factory.newInstance();
160 		RestRequestConfig testRequestConfig = requestStepConfig.addNewRestRequest();
161 
162 		testRequestConfig.setName( name );
163 		testRequestConfig.setEncoding( "UTF-8" );
164 
165 		if( restMethod != null )
166 		{
167 			requestStepConfig.setService( restMethod.getInterface().getName() );
168 			requestStepConfig.setMethodName( restMethod.getName() );
169 			requestStepConfig.setResourcePath( restMethod.getOperation().getFullPath() );
170 
171 			String[] endpoints = restMethod.getInterface().getEndpoints();
172 			if( endpoints.length > 0 )
173 				testRequestConfig.setEndpoint( endpoints[0] );
174 
175 			testRequestConfig.addNewRequest();
176 			StringToStringMapConfig parametersConfig = testRequestConfig.addNewParameters();
177 
178 			for( RestParamProperty property : restMethod.getDefaultParams() )
179 			{
180 				if( StringUtils.hasContent( property.getDefaultValue() ) )
181 				{
182 					Entry entry = parametersConfig.addNewEntry();
183 					entry.setKey( property.getName() );
184 					entry.setValue( property.getDefaultValue() );
185 				}
186 			}
187 		}
188 
189 		TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
190 		testStepConfig.setType( RESTREQUEST_TYPE );
191 		testStepConfig.setConfig( requestStepConfig );
192 		testStepConfig.setName( name );
193 
194 		return testStepConfig;
195 	}
196 
197 	public boolean canCreate()
198 	{
199 		return true;
200 	}
201 
202 	// private void buildDialog()
203 	// {
204 	// XFormDialogBuilder builder = XFormFactory.createDialogBuilder(
205 	// "Add REST Request to TestCase" );
206 	// XForm mainForm = builder.createForm( "Basic" );
207 	//
208 	// mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL
209 	// ).setWidth( 30 );
210 	//
211 	// dialog = builder.buildDialog( builder.buildOkCancelActions(),
212 	// "Specify options for adding a new REST Request to a TestCase",
213 	// UISupport.OPTIONS_ICON );
214 	// }
215 }