1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.teststeps;
14
15 import java.beans.PropertyChangeEvent;
16 import java.beans.PropertyChangeListener;
17 import java.sql.Connection;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21
22 import javax.swing.ImageIcon;
23
24 import org.apache.log4j.Logger;
25
26 import com.eviware.soapui.SoapUI;
27 import com.eviware.soapui.config.JdbcRequestTestStepConfig;
28 import com.eviware.soapui.config.TestAssertionConfig;
29 import com.eviware.soapui.config.TestStepConfig;
30 import com.eviware.soapui.impl.wsdl.MutableTestPropertyHolder;
31 import com.eviware.soapui.impl.wsdl.panels.teststeps.JdbcRequest;
32 import com.eviware.soapui.impl.wsdl.panels.teststeps.JdbcResponse;
33 import com.eviware.soapui.impl.wsdl.panels.teststeps.JdbcSubmit;
34 import com.eviware.soapui.impl.wsdl.support.JdbcMessageExchange;
35 import com.eviware.soapui.impl.wsdl.support.XmlBeansPropertiesTestPropertyHolder;
36 import com.eviware.soapui.impl.wsdl.support.assertions.AssertableConfig;
37 import com.eviware.soapui.impl.wsdl.support.assertions.AssertedXPathsContainer;
38 import com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport;
39 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
40 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
41 import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
42 import com.eviware.soapui.model.iface.Interface;
43 import com.eviware.soapui.model.iface.Submit;
44 import com.eviware.soapui.model.iface.SubmitContext;
45 import com.eviware.soapui.model.iface.Request.SubmitException;
46 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
47 import com.eviware.soapui.model.support.TestStepBeanProperty;
48 import com.eviware.soapui.model.testsuite.Assertable;
49 import com.eviware.soapui.model.testsuite.AssertionError;
50 import com.eviware.soapui.model.testsuite.AssertionsListener;
51 import com.eviware.soapui.model.testsuite.TestAssertion;
52 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
53 import com.eviware.soapui.model.testsuite.TestCaseRunner;
54 import com.eviware.soapui.model.testsuite.TestProperty;
55 import com.eviware.soapui.model.testsuite.TestPropertyListener;
56 import com.eviware.soapui.model.testsuite.TestRunContext;
57 import com.eviware.soapui.model.testsuite.TestStepResult;
58 import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
59 import com.eviware.soapui.support.StringUtils;
60
61 /***
62 * WsdlTestStep that executes a WsdlTestRequest
63 *
64 * @author dragica.soldo
65 */
66
67 public class JdbcRequestTestStep extends WsdlTestStepWithProperties implements Assertable, MutableTestPropertyHolder,
68 PropertyChangeListener
69 {
70 @SuppressWarnings( "unused" )
71 private final static Logger log = Logger.getLogger( WsdlTestRequestStep.class );
72 protected JdbcRequestTestStepConfig jdbcRequestTestStepConfig;
73 public final static String JDBCREQUEST = JdbcRequestTestStep.class.getName() + "@jdbcrequest";
74 public static final String STATUS_PROPERTY = WsdlTestRequest.class.getName() + "@status";
75 public static final String RESPONSE_PROPERTY = "response";
76 private JdbcSubmit submit;
77 protected static final String DRIVER_FIELD = "Driver";
78 protected static final String CONNSTR_FIELD = "Connection String";
79 protected static final String PASS_FIELD = "Password";
80 public static final String PASS_TEMPLATE = "PASS_VALUE";
81 public static final String QUERY_FIELD = "SQL Query";
82 protected static final String STOREDPROCEDURE_FIELD = "Stored Procedure";
83 protected static final String DATA_CONNECTION_FIELD = "Connection";
84
85 protected static final String QUERY_ELEMENT = "query";
86 protected static final String STOREDPROCEDURE_ELEMENT = "stored-procedure";
87 protected Connection connection;
88 private AssertionsSupport assertionsSupport;
89 private PropertyChangeNotifier notifier;
90 private XmlBeansPropertiesTestPropertyHolder propertyHolderSupport;
91 private JdbcRequest jdbcRequest;
92
93 public JdbcRequestTestStep( WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest )
94 {
95 super( testCase, config, true, forLoadTest );
96
97 if( getConfig().getConfig() != null )
98 {
99 jdbcRequestTestStepConfig = ( JdbcRequestTestStepConfig )getConfig().getConfig().changeType(
100 JdbcRequestTestStepConfig.type );
101
102 }
103 else
104 {
105 jdbcRequestTestStepConfig = ( JdbcRequestTestStepConfig )getConfig().addNewConfig().changeType(
106 JdbcRequestTestStepConfig.type );
107 }
108
109 if( jdbcRequestTestStepConfig.getProperties() == null )
110 jdbcRequestTestStepConfig.addNewProperties();
111
112 jdbcRequest = new JdbcRequest( this );
113
114 propertyHolderSupport = new XmlBeansPropertiesTestPropertyHolder( this, jdbcRequestTestStepConfig.getProperties() );
115
116 addResponseAsXMLVirtualProperty();
117
118 initAssertions();
119 jdbcRequest.initIcons();
120 }
121
122 private void addResponseAsXMLVirtualProperty()
123 {
124 TestStepBeanProperty responseProperty = new TestStepBeanProperty( "ResponseAsXML", false, this,
125 "responseContent", this )
126 {
127 @Override
128 public String getDefaultValue()
129 {
130 return "";
131 }
132
133 };
134
135 propertyHolderSupport.addVirtualProperty( "ResponseAsXML", responseProperty );
136 }
137
138 public JdbcRequestTestStepConfig getJdbcRequestTestStepConfig()
139 {
140 return jdbcRequestTestStepConfig;
141 }
142
143 public void resetConfigOnMove( TestStepConfig config )
144 {
145 super.resetConfigOnMove( config );
146
147 jdbcRequestTestStepConfig = ( JdbcRequestTestStepConfig )config.getConfig().changeType( JdbcRequestTestStepConfig.type );
148 propertyHolderSupport.resetPropertiesConfig( jdbcRequestTestStepConfig.getProperties() );
149
150 assertionsSupport.refresh();
151 }
152
153 @Override
154 public WsdlTestStep clone( WsdlTestCase targetTestCase, String name )
155 {
156 beforeSave();
157
158 TestStepConfig config = ( TestStepConfig )getConfig().copy();
159 JdbcRequestTestStep result = ( JdbcRequestTestStep )targetTestCase.addTestStep( config );
160
161 return result;
162 }
163
164 @Override
165 public void release()
166 {
167 super.release();
168 }
169
170 public TestStepResult run( TestCaseRunner runner, TestCaseRunContext runContext )
171 {
172 JdbcTestStepResult testStepResult = new JdbcTestStepResult( this );
173 testStepResult.startTimer();
174 runContext.setProperty( AssertedXPathsContainer.ASSERTEDXPATHSCONTAINER_PROPERTY, testStepResult );
175
176 try
177 {
178 submit = jdbcRequest.submit( runContext, false );
179 JdbcResponse response = submit.getResponse();
180
181 if( submit.getStatus() != Submit.Status.CANCELED )
182 {
183 if( submit.getStatus() == Submit.Status.ERROR )
184 {
185 testStepResult.setStatus( TestStepStatus.FAILED );
186 testStepResult.addMessage( submit.getError().toString() );
187
188 jdbcRequest.setResponse( null );
189 }
190 else if( response == null )
191 {
192 testStepResult.setStatus( TestStepStatus.FAILED );
193 testStepResult.addMessage( "Request is missing response" );
194
195 jdbcRequest.setResponse( null );
196 }
197 else
198 {
199 runContext.setProperty( AssertedXPathsContainer.ASSERTEDXPATHSCONTAINER_PROPERTY, testStepResult );
200 jdbcRequest.setResponse( response );
201
202 testStepResult.setTimeTaken( response.getTimeTaken() );
203 testStepResult.setSize( response.getContentLength() );
204
205 switch( jdbcRequest.getAssertionStatus() )
206 {
207 case FAILED :
208 testStepResult.setStatus( TestStepStatus.FAILED );
209 break;
210 case VALID :
211 testStepResult.setStatus( TestStepStatus.OK );
212 break;
213 case UNKNOWN :
214 testStepResult.setStatus( TestStepStatus.UNKNOWN );
215 break;
216 }
217
218 testStepResult.setResponse( response, testStepResult.getStatus() != TestStepStatus.FAILED );
219 }
220 }
221 else
222 {
223 testStepResult.setStatus( TestStepStatus.CANCELED );
224 testStepResult.addMessage( "Request was canceled" );
225 }
226
227 if( response != null )
228 testStepResult.setRequestContent( response.getRequestContent() );
229 else
230 testStepResult.setRequestContent( jdbcRequest.getRequestContent() );
231
232 testStepResult.stopTimer();
233 }
234 catch( SubmitException e )
235 {
236 testStepResult.setStatus( TestStepStatus.FAILED );
237 testStepResult.addMessage( "SubmitException: " + e );
238 testStepResult.stopTimer();
239 }
240 finally
241 {
242 submit = null;
243 }
244
245 if( testStepResult.getStatus() != TestStepStatus.CANCELED )
246 {
247 assertResponse( runContext );
248
249 AssertionStatus assertionStatus = jdbcRequest.getAssertionStatus();
250 switch( assertionStatus )
251 {
252 case FAILED :
253 {
254 testStepResult.setStatus( TestStepStatus.FAILED );
255 if( getAssertionCount() == 0 )
256 {
257 testStepResult.addMessage( "Invalid/empty response" );
258 }
259 else
260 for( int c = 0; c < getAssertionCount(); c++ )
261 {
262 TestAssertion assertion = getAssertionAt( c );
263 AssertionError[] errors = assertion.getErrors();
264 if( errors != null )
265 {
266 for( AssertionError error : errors )
267 {
268 testStepResult.addMessage( "[" + assertion.getName() + "] " + error.getMessage() );
269 }
270 }
271 }
272
273 break;
274 }
275
276 }
277 }
278
279 if( !runContext.hasProperty( TestRunContext.INTERACTIVE ) )
280 jdbcRequest.setResponse( null );
281
282 return testStepResult;
283 }
284
285 @Override
286 public boolean cancel()
287 {
288 if( submit == null )
289 return false;
290
291 submit.cancel();
292
293 return true;
294 }
295
296 public String getDefaultSourcePropertyName()
297 {
298 return "Response";
299 }
300
301 private void initAssertions()
302 {
303 assertionsSupport = new AssertionsSupport( this, new AssertableConfig()
304 {
305
306 public TestAssertionConfig addNewAssertion()
307 {
308 return getJdbcRequestTestStepConfig().addNewAssertion();
309 }
310
311 public List<TestAssertionConfig> getAssertionList()
312 {
313 return getJdbcRequestTestStepConfig().getAssertionList();
314 }
315
316 public void removeAssertion( int ix )
317 {
318 getJdbcRequestTestStepConfig().removeAssertion( ix );
319 }
320
321 public TestAssertionConfig insertAssertion( TestAssertionConfig source, int ix )
322 {
323 TestAssertionConfig conf = getJdbcRequestTestStepConfig().insertNewAssertion( ix );
324 conf.set( source );
325 return conf;
326 }
327 } );
328 }
329
330 private class PropertyChangeNotifier
331 {
332 private AssertionStatus oldStatus;
333 private ImageIcon oldIcon;
334
335 public PropertyChangeNotifier()
336 {
337 oldStatus = getAssertionStatus();
338 oldIcon = getIcon();
339 }
340
341 public void notifyChange()
342 {
343 AssertionStatus newStatus = getAssertionStatus();
344 ImageIcon newIcon = getIcon();
345
346 if( oldStatus != newStatus )
347 notifyPropertyChanged( STATUS_PROPERTY, oldStatus, newStatus );
348
349 if( oldIcon != newIcon )
350 notifyPropertyChanged( ICON_PROPERTY, oldIcon, getIcon() );
351
352 oldStatus = newStatus;
353 oldIcon = newIcon;
354 }
355 }
356
357 public TestAssertion addAssertion( String assertionLabel )
358 {
359 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
360
361 try
362 {
363 WsdlMessageAssertion assertion = assertionsSupport.addWsdlAssertion( assertionLabel );
364 if( assertion == null )
365 return null;
366
367 if( getJdbcRequest().getResponse() != null )
368 {
369 assertion.assertResponse( new JdbcMessageExchange( this, getJdbcRequest().getResponse() ),
370 new WsdlTestRunContext( this ) );
371 notifier.notifyChange();
372 }
373
374 return assertion;
375 }
376 catch( Exception e )
377 {
378 SoapUI.logError( e );
379 return null;
380 }
381 }
382
383 public void addAssertionsListener( AssertionsListener listener )
384 {
385 assertionsSupport.addAssertionsListener( listener );
386 }
387
388 public TestAssertion cloneAssertion( TestAssertion source, String name )
389 {
390 return assertionsSupport.cloneAssertion( source, name );
391 }
392
393 public String getAssertableContent()
394 {
395 return getJdbcRequest().getResponse() == null ? null : getJdbcRequest().getResponse().getContentAsString();
396 }
397
398
399 public String getResponseContent()
400 {
401 return getJdbcRequest().getResponse() == null ? "" : getJdbcRequest().getResponse().getContentAsString();
402 }
403
404 public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
405 {
406 return assertionsSupport.importAssertion( source, overwrite, createCopy );
407 }
408
409 public AssertableType getAssertableType()
410 {
411 return AssertableType.RESPONSE;
412 }
413
414 public TestAssertion getAssertionAt( int c )
415 {
416 return assertionsSupport.getAssertionAt( c );
417 }
418
419 public TestAssertion getAssertionByName( String name )
420 {
421 return assertionsSupport.getAssertionByName( name );
422 }
423
424 public int getAssertionCount()
425 {
426 return assertionsSupport.getAssertionCount();
427 }
428
429 public List<TestAssertion> getAssertionList()
430 {
431 return new ArrayList<TestAssertion>( assertionsSupport.getAssertionList() );
432 }
433
434 public void propertyChange( PropertyChangeEvent arg0 )
435 {
436 if( arg0.getPropertyName().equals( TestAssertion.CONFIGURATION_PROPERTY )
437 || arg0.getPropertyName().equals( TestAssertion.DISABLED_PROPERTY ) )
438 {
439 if( getJdbcRequest().getResponse() != null )
440 {
441 assertResponse( new WsdlTestRunContext( this ) );
442 }
443 }
444 }
445
446 public Map<String, TestAssertion> getAssertions()
447 {
448 return assertionsSupport.getAssertions();
449 }
450
451 public String getDefaultAssertableContent()
452 {
453 return null;
454 }
455
456 public AssertionStatus getAssertionStatus()
457 {
458 return jdbcRequest.getAssertionStatus();
459 }
460
461 public ImageIcon getIcon()
462 {
463 return jdbcRequest.getIcon();
464 }
465
466 public Interface getInterface()
467 {
468 return null;
469 }
470
471 public TestAssertion moveAssertion( int ix, int offset )
472 {
473 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
474 TestAssertion assertion = getAssertionAt( ix );
475 try
476 {
477 return assertionsSupport.moveAssertion( ix, offset );
478 }
479 finally
480 {
481 ( ( WsdlMessageAssertion )assertion ).release();
482 notifier.notifyChange();
483 }
484 }
485
486 public void removeAssertion( TestAssertion assertion )
487 {
488 PropertyChangeNotifier notifier = new PropertyChangeNotifier();
489
490 try
491 {
492 assertionsSupport.removeAssertion( ( WsdlMessageAssertion )assertion );
493
494 }
495 finally
496 {
497 ( ( WsdlMessageAssertion )assertion ).release();
498 notifier.notifyChange();
499 }
500 }
501
502 public void removeAssertionsListener( AssertionsListener listener )
503 {
504 assertionsSupport.removeAssertionsListener( listener );
505 }
506
507 public void assertResponse( SubmitContext context )
508 {
509 try
510 {
511 if( notifier == null )
512 notifier = new PropertyChangeNotifier();
513
514 JdbcMessageExchange messageExchange = new JdbcMessageExchange( this, getJdbcRequest().getResponse() );
515
516 if( this != null )
517 {
518
519 for( WsdlMessageAssertion assertion : assertionsSupport.getAssertionList() )
520 {
521 assertion.assertResponse( messageExchange, context );
522 }
523 }
524
525 notifier.notifyChange();
526 }
527 catch( Exception e )
528 {
529 e.printStackTrace();
530 }
531 }
532
533 public TestProperty addProperty( String name )
534 {
535 return propertyHolderSupport.addProperty( name );
536 }
537
538 public TestProperty removeProperty( String propertyName )
539 {
540 return propertyHolderSupport.removeProperty( propertyName );
541 }
542
543 public void removeAllProperties()
544 {
545 for( String propertyName : propertyHolderSupport.getPropertyNames() )
546 {
547 propertyHolderSupport.removeProperty( propertyName );
548 }
549 }
550
551 public boolean renameProperty( String name, String newName )
552 {
553 return PropertyExpansionUtils.renameProperty( propertyHolderSupport.getProperty( name ), newName, getTestCase() ) != null;
554 }
555
556 public void addTestPropertyListener( TestPropertyListener listener )
557 {
558 propertyHolderSupport.addTestPropertyListener( listener );
559 }
560
561 public Map<String, TestProperty> getProperties()
562 {
563 return propertyHolderSupport.getProperties();
564 }
565
566 public TestProperty getProperty( String name )
567 {
568 return propertyHolderSupport.getProperty( name );
569 }
570
571 public TestProperty getPropertyAt( int index )
572 {
573 return propertyHolderSupport.getPropertyAt( index );
574 }
575
576 public int getPropertyCount()
577 {
578 return propertyHolderSupport.getPropertyCount();
579 }
580
581 public List<TestProperty> getPropertyList()
582 {
583 return propertyHolderSupport.getPropertyList();
584 }
585
586 public String[] getPropertyNames()
587 {
588 return propertyHolderSupport.getPropertyNames();
589 }
590
591 public String getPropertyValue( String name )
592 {
593 return propertyHolderSupport.getPropertyValue( name );
594 }
595
596 public void removeTestPropertyListener( TestPropertyListener listener )
597 {
598 propertyHolderSupport.removeTestPropertyListener( listener );
599 }
600
601 public boolean hasProperty( String name )
602 {
603 return propertyHolderSupport.hasProperty( name );
604 }
605
606 public void setPropertyValue( String name, String value )
607 {
608 propertyHolderSupport.setPropertyValue( name, value );
609 }
610
611 public void setPropertyValue( String name, Object value )
612 {
613 setPropertyValue( name, String.valueOf( value ) );
614 }
615
616 public void moveProperty( String propertyName, int targetIndex )
617 {
618 propertyHolderSupport.moveProperty( propertyName, targetIndex );
619 }
620
621 public String getDriver()
622 {
623 return jdbcRequestTestStepConfig.getDriver();
624 }
625
626 public void setDriver( String d )
627 {
628 String old = getDriver();
629 jdbcRequestTestStepConfig.setDriver( d );
630 notifyPropertyChanged( "driver", old, d );
631 }
632
633 public String getConnectionString()
634 {
635 return jdbcRequestTestStepConfig.getConnectionString();
636 }
637
638 public void setConnectionString( String c )
639 {
640 String old = getConnectionString();
641 jdbcRequestTestStepConfig.setConnectionString( c );
642 notifyPropertyChanged( "connectionString", old, c );
643 }
644
645 public String getQuery()
646 {
647 return jdbcRequestTestStepConfig.getQuery();
648 }
649
650 public void setQuery( String q )
651 {
652 String old = getQuery();
653 jdbcRequestTestStepConfig.setQuery( q );
654 notifyPropertyChanged( "query", old, q );
655 }
656
657 public String getPassword()
658 {
659 return jdbcRequestTestStepConfig.getPassword();
660 }
661
662 public void setPassword( String p )
663 {
664 String old = getPassword();
665 jdbcRequestTestStepConfig.setPassword( p );
666 notifyPropertyChanged( "password", old, p );
667 }
668
669 public static boolean isNeededPassword( String connStr )
670 {
671 return !StringUtils.isNullOrEmpty( connStr ) ? connStr.contains( PASS_TEMPLATE ) : false;
672 }
673
674 public boolean isStoredProcedure()
675 {
676 return jdbcRequestTestStepConfig.getStoredProcedure();
677 }
678
679 public void setStoredProcedure( boolean sp )
680 {
681 String old = getPassword();
682 jdbcRequestTestStepConfig.setStoredProcedure( sp );
683 notifyPropertyChanged( "password", old, sp );
684 }
685
686 public JdbcRequest getJdbcRequest()
687 {
688 return jdbcRequest;
689 }
690
691 public String getQueryTimeout()
692 {
693 return jdbcRequestTestStepConfig.getQueryTimeout();
694 }
695
696 public String getMaxRows()
697 {
698 return jdbcRequestTestStepConfig.getMaxRows();
699 }
700
701 public String getFetchSize()
702 {
703 return jdbcRequestTestStepConfig.getFetchSize();
704 }
705
706 public void setQueryTimeout( String queryTimeout )
707 {
708 String old = getQueryTimeout();
709 jdbcRequestTestStepConfig.setQueryTimeout( queryTimeout );
710 notifyPropertyChanged( "queryTimeout", old, queryTimeout );
711 }
712
713 public void setMaxRows( String maxRows )
714 {
715 String old = getMaxRows();
716 jdbcRequestTestStepConfig.setMaxRows( maxRows );
717 notifyPropertyChanged( "maxRows", old, maxRows );
718 }
719
720 public void setFetchSize( String fetchSize )
721 {
722 String old = getFetchSize();
723 jdbcRequestTestStepConfig.setFetchSize( fetchSize );
724 notifyPropertyChanged( "fetchSize", old, fetchSize );
725 }
726
727 public void setResponse( JdbcResponse response, SubmitContext context )
728 {
729 JdbcResponse oldResponse = jdbcRequest.getResponse();
730 jdbcRequest.setResponse( response );
731
732 notifyPropertyChanged( RESPONSE_PROPERTY, oldResponse, response );
733 assertResponse( context );
734 }
735
736 }