View Javadoc

1   package com.eviware.soapui.impl.wsdl.teststeps;
2   
3   import com.eviware.soapui.config.TestStepConfig;
4   import com.eviware.soapui.impl.rest.RestResource;
5   import com.eviware.soapui.impl.rest.RestService;
6   import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
7   import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
8   import com.eviware.soapui.model.iface.Interface;
9   import com.eviware.soapui.model.iface.Operation;
10  import com.eviware.soapui.model.project.Project;
11  import com.eviware.soapui.model.support.InterfaceListenerAdapter;
12  import com.eviware.soapui.model.support.ModelSupport;
13  import com.eviware.soapui.model.support.ProjectListenerAdapter;
14  import com.eviware.soapui.model.testsuite.TestStep;
15  import com.eviware.soapui.support.resolver.ChangeOperationResolver;
16  import com.eviware.soapui.support.resolver.ImportInterfaceResolver;
17  import com.eviware.soapui.support.resolver.RemoveTestStepResolver;
18  import com.eviware.soapui.support.resolver.ResolveContext;
19  import com.eviware.soapui.support.resolver.ResolveContext.PathToResolve;
20  import org.apache.log4j.Logger;
21  
22  import java.beans.PropertyChangeEvent;
23  
24  public class RestTestRequestStep extends HttpTestRequestStep
25  {
26  	private final static Logger log = Logger.getLogger(RestTestRequestStep.class);
27  	private RestResource restResource;
28  	private final InternalProjectListener projectListener = new InternalProjectListener();
29  	private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
30  
31  	public RestTestRequestStep(WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest)
32  	{
33  		super(testCase, config, forLoadTest);
34  
35  		restResource = findRestResource();
36  		initRestTestRequest(forLoadTest);
37  	}
38  
39  	private void initRestTestRequest(boolean forLoadTest)
40  	{
41  		if (restResource == null)
42  			setDisabled(true);
43  		else
44  			getTestRequest().setResource(restResource);
45  
46  		if (!forLoadTest && restResource != null)
47  		{
48  			restResource.getInterface().getProject().addProjectListener(projectListener);
49  			restResource.getInterface().addInterfaceListener(interfaceListener);
50  
51  			// we need to listen for name changes which happen when interfaces are
52  			// updated..
53  			restResource.getInterface().addPropertyChangeListener(this);
54  			restResource.addPropertyChangeListener(this);
55  		}
56  	}
57  
58  	public String getService()
59  	{
60  		return getRequestStepConfig().getService();
61  	}
62  
63  	public String getResourcePath()
64  	{
65  		return getRequestStepConfig().getResourcePath();
66  	}
67  
68  	protected String createDefaultRawResponseContent()
69  	{
70  		return restResource == null ? null : restResource.createResponse(true);
71  	}
72  
73  	protected String createDefaultResponseXmlContent()
74  	{
75  		return restResource == null ? null : restResource.createResponse(true);
76  	}
77  
78  	protected String createDefaultRequestContent()
79  	{
80  		return restResource == null ? null : restResource.createRequest(true);
81  	}
82  
83  	private RestResource findRestResource()
84  	{
85  		Project project = ModelSupport.getModelItemProject(this);
86  		RestService restService = (RestService) project.getInterfaceByName(getRequestStepConfig().getService());
87  		if (restService != null)
88  		{
89  			return restService.getResourceByFullPath(getRequestStepConfig().getResourcePath());
90  		}
91  
92  		return null;
93  	}
94  
95  	public RestResource getResource()
96  	{
97  		return restResource;
98  	}
99  
100 	@Override
101 	public void release()
102 	{
103 		super.release();
104 
105 		if (restResource != null)
106 		{
107 			restResource.removePropertyChangeListener(this);
108 			restResource.getInterface().getProject().removeProjectListener(projectListener);
109 			restResource.getInterface().removeInterfaceListener(interfaceListener);
110 			restResource.getInterface().removePropertyChangeListener(this);
111 		}
112 	}
113 
114 	public void propertyChange(PropertyChangeEvent evt)
115 	{
116 		if (evt.getSource() == restResource)
117 		{
118 			if (evt.getPropertyName().equals(RestResource.PATH_PROPERTY))
119 			{
120 				getRequestStepConfig().setResourcePath((String) evt.getNewValue());
121 			}
122 		}
123 		else if (restResource != null && evt.getSource() == restResource.getInterface())
124 		{
125 			if (evt.getPropertyName().equals(Interface.NAME_PROPERTY))
126 			{
127 				getRequestStepConfig().setService((String) evt.getNewValue());
128 			}
129 		}
130 
131 		super.propertyChange(evt);
132 	}
133 
134 	public class InternalProjectListener extends ProjectListenerAdapter
135 	{
136 		@Override
137 		public void interfaceRemoved(Interface iface)
138 		{
139 			if (restResource != null && restResource.getInterface().equals(iface))
140 			{
141 				log.debug("Removing test step due to removed interface");
142 				(getTestCase()).removeTestStep(RestTestRequestStep.this);
143 			}
144 		}
145 	}
146 
147 	public class InternalInterfaceListener extends InterfaceListenerAdapter
148 	{
149 		@Override
150 		public void operationRemoved(Operation operation)
151 		{
152 			if (operation == restResource)
153 			{
154 				log.debug("Removing test step due to removed operation");
155 				(getTestCase()).removeTestStep(RestTestRequestStep.this);
156 			}
157 		}
158 
159 		@Override
160 		public void operationUpdated(Operation operation)
161 		{
162 			if (operation == restResource)
163 			{
164 				// requestStepConfig.setResourcePath( operation.get );
165 			}
166 		}
167 	}
168 
169 	@Override
170 	public boolean dependsOn(AbstractWsdlModelItem<?> modelItem)
171 	{
172 		if (modelItem instanceof Interface && getTestRequest().getOperation() != null
173 				&& getTestRequest().getOperation().getInterface() == modelItem)
174 		{
175 			return true;
176 		}
177 		else if (modelItem instanceof Operation && getTestRequest().getOperation() == modelItem)
178 		{
179 			return true;
180 		}
181 
182 		return false;
183 	}
184 
185 	public void setResource(RestResource operation)
186 	{
187 		if (restResource == operation)
188 			return;
189 
190 		RestResource oldOperation = restResource;
191 		restResource = operation;
192 		getRequestStepConfig().setService(operation.getInterface().getName());
193 		getRequestStepConfig().setResourcePath(operation.getFullPath());
194 
195 		if (oldOperation != null)
196 			oldOperation.removePropertyChangeListener(this);
197 
198 		restResource.addPropertyChangeListener(this);
199 		getTestRequest().setResource(restResource);
200 	}
201 
202 	public Interface getInterface()
203 	{
204 		return restResource == null ? null : restResource.getInterface();
205 	}
206 
207 	public TestStep getTestStep()
208 	{
209 		return this;
210 	}
211 
212 	@SuppressWarnings("unchecked")
213 	public void resolve(ResolveContext context)
214 	{
215 		super.resolve(context);
216 
217 		if (restResource == null)
218 		{
219 			if (context.hasThisModelItem(this, "Missing REST Resource in Project", getRequestStepConfig().getService()
220 					+ "/" + getRequestStepConfig().getResourcePath()))
221 				return;
222 			context.addPathToResolve(this, "Missing REST Resource in Project",
223 					getRequestStepConfig().getService() + "/" + getRequestStepConfig().getResourcePath()).addResolvers(
224 					new RemoveTestStepResolver(this), new ImportInterfaceResolver(this)
225 					{
226 
227 						@Override
228 						protected boolean update()
229 						{
230 							restResource = findRestResource();
231 							if (restResource == null)
232 								return false;
233 
234 							initRestTestRequest(false);
235 							setDisabled(false);
236 							return true;
237 						}
238 
239 					}, new ChangeOperationResolver(this)
240 					{
241 
242 						@Override
243 						public boolean update()
244 						{
245 							restResource = (RestResource) getPickedOperation();
246 							if (restResource == null)
247 								return false;
248 
249 							initRestTestRequest(false);
250 							setDisabled(false);
251 							return true;
252 						}
253 
254 					});
255 		}
256 		else
257 		{
258 			restResource.resolve(context);
259 			if (context.hasThisModelItem(this, "Missing REST Resource in Project", getRequestStepConfig().getService()
260 					+ "/" + getRequestStepConfig().getResourcePath())) {
261 				PathToResolve path = context.getPath(this, "Missing REST Resource in Project", getRequestStepConfig().getService()
262 					+ "/" + getRequestStepConfig().getResourcePath());
263 				path.setSolved(true);
264 			}
265 		}
266 	}
267 }