View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  
18  import javax.swing.ImageIcon;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import com.eviware.soapui.config.CallConfig;
24  import com.eviware.soapui.config.RequestStepConfig;
25  import com.eviware.soapui.config.TestStepConfig;
26  import com.eviware.soapui.impl.wsdl.WsdlInterface;
27  import com.eviware.soapui.impl.wsdl.WsdlOperation;
28  import com.eviware.soapui.impl.wsdl.WsdlRequest;
29  import com.eviware.soapui.impl.wsdl.WsdlTestCase;
30  import com.eviware.soapui.impl.wsdl.WsdlTestStep;
31  import com.eviware.soapui.impl.wsdl.teststeps.actions.CloneTestRequestStepAction;
32  import com.eviware.soapui.model.PanelBuilder;
33  import com.eviware.soapui.model.iface.Interface;
34  import com.eviware.soapui.model.iface.Operation;
35  import com.eviware.soapui.model.iface.Response;
36  import com.eviware.soapui.model.iface.Submit;
37  import com.eviware.soapui.model.project.Project;
38  import com.eviware.soapui.model.support.InterfaceListenerAdapter;
39  import com.eviware.soapui.model.support.ProjectListenerAdapter;
40  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
41  import com.eviware.soapui.model.testsuite.TestRunner;
42  import com.eviware.soapui.model.testsuite.TestStep;
43  import com.eviware.soapui.model.tree.AbstractTreeNode;
44  import com.eviware.soapui.model.tree.SoapUITreeNode;
45  
46  /***
47   * WsdlTestStep that executes a WsdlTestRequest
48   * 
49   * @author Ole.Matzura
50   */
51  
52  public class WsdlTestRequestStep extends WsdlTestStep implements PropertyChangeListener 
53  {
54  	private final static Log log = LogFactory.getLog( WsdlTestRequestStep.class );
55     private RequestStepConfig requestStepConfig;
56     private WsdlTestRequest testRequest;
57  	private WsdlOperation wsdlOperation;
58  	private Submit submit;
59  	private final InternalProjectListener internalProjectListener = new InternalProjectListener();
60  	private final InternalInterfaceListener internalInterfaceListener = new InternalInterfaceListener();
61  	private final InternalTestSuiteListener internalTestSuiteListener = new InternalTestSuiteListener();
62  
63     public WsdlTestRequestStep(WsdlTestCase testCase, TestStepConfig config)
64     {
65        super( testCase, config );
66        
67        testCase.getTestSuite().addTestSuiteListener( internalTestSuiteListener );
68         
69        if( config.getConfig() != null )
70        {
71  			requestStepConfig = (RequestStepConfig) config.getConfig().changeType(RequestStepConfig.type);
72           
73           wsdlOperation = findWsdlOperation();
74  			if( wsdlOperation == null )
75           {   
76              log.error( "Could not find operation for test request" );
77              requestStepConfig.setRequest(null);
78           }
79           else
80           {
81           	wsdlOperation.getInterface().getProject().addProjectListener( internalProjectListener );
82           	wsdlOperation.getInterface().addInterfaceListener( internalInterfaceListener );
83              testRequest = new WsdlTestRequest( wsdlOperation, requestStepConfig.getRequest(), this );
84              testRequest.addPropertyChangeListener( this );
85           }
86        }
87        else
88        {
89           requestStepConfig = (RequestStepConfig) config.addNewConfig().changeType( RequestStepConfig.type );
90        }
91        
92        addAction( new CloneTestRequestStepAction( this ) );
93     }
94     
95     public WsdlTestRequestStep(WsdlTestCase testCase, TestStepConfig testStep, WsdlRequest request)
96  	{
97  		this( testCase, testStep );
98  
99        requestStepConfig.setInterface( request.getOperation().getInterface().getName() );
100       requestStepConfig.setOperation( request.getOperation().getName() );
101 
102       CallConfig testRequestConfig = requestStepConfig.getRequest();
103       if( testRequestConfig == null ) testRequestConfig = requestStepConfig.addNewRequest();
104       
105       testRequestConfig.setName( request.getName() );
106       testRequestConfig.setEncoding( request.getEncoding() );
107       testRequestConfig.setEndpoint( request.getEndpoint() );
108       testRequestConfig.setRequest( request.getRequestContent() );
109 
110       testRequest = new WsdlTestRequest( (WsdlOperation) request.getOperation(), testRequestConfig, this );
111       testRequest.addPropertyChangeListener( this );
112       
113       wsdlOperation = findWsdlOperation();
114       if( wsdlOperation == null )
115       	throw new RuntimeException( "Failed to find operation for test request" );
116       
117       wsdlOperation.getInterface().addInterfaceListener( internalInterfaceListener );
118       wsdlOperation.getInterface().getProject().addProjectListener( internalProjectListener );
119 	}
120 
121 	private WsdlOperation findWsdlOperation()
122    {
123    	WsdlTestCase testCase = (WsdlTestCase) getTestCase();
124       Project project = testCase.getTestSuite().getProject();
125       WsdlOperation operation = null;
126       for( int c = 0; c < project.getInterfaceCount(); c++ )
127       {
128          if( project.getInterfaceAt( c ).getName().equals( requestStepConfig.getInterface()))
129          {
130             WsdlInterface iface = (WsdlInterface) project.getInterfaceAt( c );
131             for( int i = 0; i < iface.getOperationCount(); i++ )
132             {
133                if( iface.getOperationAt( i ).getName().equals( requestStepConfig.getOperation() ))
134                {
135                   operation = (WsdlOperation) iface.getOperationAt( i );
136                   break;
137                }
138             }
139             
140             break;
141          }
142       }
143       return operation;
144    }
145 
146 	protected void resetConfigOnMove(TestStepConfig config)
147 	{
148 		super.resetConfigOnMove(config);
149 
150 		requestStepConfig = (RequestStepConfig) config.getConfig().changeType(RequestStepConfig.type);
151 		testRequest.updateConfig( requestStepConfig.getRequest() );
152 	}
153 
154 	public ImageIcon getIcon()
155    {
156       return testRequest.getIcon();
157    }
158 
159    public String getName()
160    {
161       return testRequest.getName();
162    }
163    
164    public WsdlTestRequest getTestRequest()
165    {
166       return testRequest;
167    }
168    
169 	public void setName(String name)
170 	{
171 		testRequest.setName( name );
172 	}
173 
174 	protected SoapUITreeNode createTreeNode()
175    {
176       return new TestRequestTreeNode();
177    }
178    
179    private class TestRequestTreeNode extends AbstractTreeNode 
180    {
181       public TestRequestTreeNode()
182       {
183          super( WsdlTestRequestStep.this, getTestCase() );
184          
185          testRequest.addPropertyChangeListener( WsdlTestRequest.STATUS_PROPERTY, this );
186       }
187       
188       public void release()
189       {
190       	super.release();
191       	
192       	testRequest.removePropertyChangeListener( this );
193       }
194    }
195 
196    public PanelBuilder getPanelBuilder()
197    {
198       return testRequest.getPanelBuilder();
199    }
200 
201    public void propertyChange(PropertyChangeEvent arg0) 
202    {
203 	  notifyPropertyChanged( arg0.getPropertyName(), arg0.getOldValue(), arg0.getNewValue());
204    }
205 
206 	public void run( TestRunner runner ) 
207 	{
208    	submit = testRequest.submit();
209 		Response response = submit.getResponse( true );
210 		if( submit.getStatus() != Submit.Status.CANCELED )
211 			testRequest.setResponse( response.getContentAsString() );
212 		
213 		submit = null;
214 	}
215 	
216 	public WsdlAssertion getAssertionAt(int index)
217 	{
218 		return testRequest.getAssertionAt( index );
219 	}
220 
221 	public int getAssertionCount()
222 	{
223 		return testRequest.getAssertionCount();
224 	}
225 	
226 	public class InternalProjectListener extends ProjectListenerAdapter 
227 	{
228 		public void interfaceRemoved(Interface iface)
229 		{
230 			if( wsdlOperation != null && wsdlOperation.getInterface() == iface )
231 			{
232 				log.debug( "Removing test step due to removed interface" );
233 				
234 				((WsdlTestCase)getTestCase()).removeTestStep( WsdlTestRequestStep.this );
235 			}
236 		}
237 	}
238 
239 	public class InternalInterfaceListener extends InterfaceListenerAdapter
240 	{
241 		public void operationRemoved(Operation operation)
242 		{
243 			if( operation == wsdlOperation )
244 			{
245 				log.debug( "Removing test step due to removed operation" );
246 				
247 				((WsdlTestCase)getTestCase()).removeTestStep( WsdlTestRequestStep.this );
248 			}
249 		}
250 	}
251 	
252 	public boolean cancel()
253 	{
254 		if( submit != null )
255 			submit.cancel();
256 		
257 		return submit != null;
258 	}
259 
260 	private class InternalTestSuiteListener extends TestSuiteListenerAdapter
261 	{
262 		public void testStepRemoved(TestStep testStep, int index)
263 		{
264 			if( testStep == WsdlTestRequestStep.this )
265 			{
266 				log.debug( "Releasing WsdlTestRequestStep" );
267 		      wsdlOperation.getInterface().removeInterfaceListener( internalInterfaceListener );
268 		      wsdlOperation.getInterface().getProject().removeProjectListener( internalProjectListener );
269 		      getTestCase().getTestSuite().removeTestSuiteListener( internalTestSuiteListener );
270 			}
271 		}
272 		
273 	}
274 }