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