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