1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.panels.teststeps.amf;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import javax.swing.ImageIcon;
23
24 import com.eviware.soapui.SoapUI;
25 import com.eviware.soapui.config.AMFRequestTestStepConfig;
26 import com.eviware.soapui.config.ModelItemConfig;
27 import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
28 import com.eviware.soapui.impl.wsdl.teststeps.AMFRequestTestStep;
29 import com.eviware.soapui.impl.wsdl.teststeps.TestRequest;
30 import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
31 import com.eviware.soapui.impl.wsdl.teststeps.assertions.TestAssertionRegistry.AssertableType;
32 import com.eviware.soapui.model.ModelItem;
33 import com.eviware.soapui.model.iface.Attachment;
34 import com.eviware.soapui.model.iface.Interface;
35 import com.eviware.soapui.model.iface.MessagePart;
36 import com.eviware.soapui.model.iface.Operation;
37 import com.eviware.soapui.model.iface.Submit;
38 import com.eviware.soapui.model.iface.SubmitContext;
39 import com.eviware.soapui.model.iface.SubmitListener;
40 import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
41 import com.eviware.soapui.model.settings.Settings;
42 import com.eviware.soapui.model.support.AbstractAnimatableModelItem;
43 import com.eviware.soapui.model.support.ModelSupport;
44 import com.eviware.soapui.model.testsuite.Assertable;
45 import com.eviware.soapui.model.testsuite.AssertionsListener;
46 import com.eviware.soapui.model.testsuite.TestAssertion;
47 import com.eviware.soapui.model.testsuite.TestProperty;
48 import com.eviware.soapui.monitor.TestMonitor;
49 import com.eviware.soapui.support.UISupport;
50 import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
51 import com.eviware.soapui.support.types.StringToObjectMap;
52 import com.eviware.soapui.support.types.StringToStringMap;
53
54 public class AMFRequest extends AbstractAnimatableModelItem<ModelItemConfig> implements Assertable, TestRequest
55 {
56 public static final String AMF_SCRIPT_HEADERS = "AMF_SCRIPT_HEADERS";
57 public static final String AMF_SCRIPT_PARAMETERS = "AMF_SCRIPT_PARAMETERS";
58 public static final String AMF_SCRIPT_ERROR = "AMF_SCRIPT_ERROR";
59 public static final String AMF_RESPONSE_CONTENT = "AMF_RESPONSE_CONTENT";
60 public static final String AMF_REQUEST = "AMF_REQUEST";
61 public static final String RAW_AMF_REQUEST = "RAW_AMF_REQUEST";
62 public static final String AMF_RESPONSE_PROPERTY = "response";
63
64 private final AMFRequestTestStep testStep;
65 private Set<SubmitListener> submitListeners = new HashSet<SubmitListener>();
66 private AMFResponse response;
67 private SoapUIScriptEngine scriptEngine;
68 private String endpoint;
69 private String amfCall;
70 private String script;
71 private HashMap<String, TestProperty> propertyMap;
72 private String[] propertyNames;
73 private List<Object> arguments = new ArrayList<Object>();
74 private StringToStringMap httpHeaders;
75 private StringToObjectMap amfHeaders;
76 private StringToStringMap amfHeadersString;
77
78 private boolean forLoadTest;
79 private AssertionStatus currentStatus;
80
81
82 private RequestIconAnimator<?> iconAnimator;
83 private ImageIcon validRequestIcon;
84 private ImageIcon failedRequestIcon;
85 private ImageIcon disabledRequestIcon;
86 private ImageIcon unknownRequestIcon;
87
88 public AMFRequest( AMFRequestTestStep testStep )
89 {
90 this.testStep = testStep;
91 initIcons();
92 }
93
94 public AMFSubmit submit( SubmitContext submitContext, boolean async ) throws SubmitException
95 {
96
97 return new AMFSubmit( this, submitContext, async );
98 }
99
100 public boolean executeAmfScript( SubmitContext context )
101 {
102 boolean scriptOK = true;
103 HashMap<String, Object> parameters = new HashMap<String, Object>();
104 HashMap<String, Object> amfHeadersTemp = new HashMap<String, Object>();
105 try
106 {
107 scriptEngine.setScript( script );
108 scriptEngine.setVariable( "parameters", parameters );
109 scriptEngine.setVariable( "amfHeaders", amfHeadersTemp );
110 scriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
111 scriptEngine.setVariable( "context", context );
112
113 scriptEngine.run();
114
115 context.setProperty( AMF_SCRIPT_PARAMETERS, parameters );
116 context.setProperty( AMF_SCRIPT_HEADERS, amfHeadersTemp );
117
118 for( String name : propertyNames )
119 {
120 if( name.equals( "ResponseAsXML" ) )
121 continue;
122
123 TestProperty propertyValue = propertyMap.get( name );
124 if( parameters.containsKey( name ) )
125 {
126 addArgument( parameters.get( name ) );
127 }
128 else
129 {
130 addArgument( PropertyExpander.expandProperties( context, propertyValue.getValue() ) );
131 }
132 }
133
134 StringToObjectMap stringToObjectMap = new StringToObjectMap();
135 for( String key : getAmfHeadersString().getKeys() )
136 {
137 if( amfHeadersTemp.containsKey( key ) )
138 {
139 stringToObjectMap.put( key, amfHeadersTemp.get( key ) );
140 }
141 else
142 {
143 stringToObjectMap.put( key, PropertyExpander
144 .expandProperties( context, getAmfHeadersString().get( key ) ) );
145 }
146 }
147 setAmfHeaders( stringToObjectMap );
148
149 }
150 catch( Throwable e )
151 {
152 SoapUI.logError( e );
153 scriptOK = false;
154 context.setProperty( AMF_SCRIPT_ERROR, e );
155 }
156 finally
157 {
158 scriptEngine.clearVariables();
159 }
160 return scriptOK;
161 }
162
163 public AssertionStatus getAssertionStatus()
164 {
165 currentStatus = AssertionStatus.UNKNOWN;
166
167 if( getResponse() == null )
168 return currentStatus;
169
170 int cnt = getAssertionCount();
171 if( cnt == 0 )
172 return currentStatus;
173
174 boolean hasEnabled = false;
175
176 for( int c = 0; c < cnt; c++ )
177 {
178 if( !getAssertionAt( c ).isDisabled() )
179 hasEnabled = true;
180
181 if( getAssertionAt( c ).getStatus() == AssertionStatus.FAILED )
182 {
183 currentStatus = AssertionStatus.FAILED;
184 break;
185 }
186 }
187
188 if( currentStatus == AssertionStatus.UNKNOWN && hasEnabled )
189 currentStatus = AssertionStatus.VALID;
190
191 return currentStatus;
192 }
193
194 public Map<String, TestAssertion> getAssertions()
195 {
196 return testStep.getAssertions();
197 }
198
199 public String getDefaultAssertableContent()
200 {
201 return testStep.getDefaultAssertableContent();
202 }
203
204 public Interface getInterface()
205 {
206 return testStep.getInterface();
207 }
208
209 public ModelItem getModelItem()
210 {
211 return testStep.getModelItem();
212 }
213
214 public TestAssertion moveAssertion( int ix, int offset )
215 {
216 return testStep.moveAssertion( ix, offset );
217 }
218
219 public void removeAssertion( TestAssertion assertion )
220 {
221 testStep.removeAssertion( assertion );
222 }
223
224 public void removeAssertionsListener( AssertionsListener listener )
225 {
226 testStep.removeAssertionsListener( listener );
227 }
228
229 public void setResponse( AMFResponse response )
230 {
231 AMFResponse old = this.response;
232 this.response = response;
233 notifyPropertyChanged( AMF_RESPONSE_PROPERTY, old, response );
234 }
235
236 public AMFResponse getResponse()
237 {
238 return response;
239 }
240
241 public String getResponseContent()
242 {
243 if( response != null )
244 {
245 return response.getResponseContentXML();
246 }
247 else
248 {
249 return "";
250 }
251 }
252
253 public void initIcons()
254 {
255 if( validRequestIcon == null )
256 validRequestIcon = UISupport.createImageIcon( "/valid_amf_request.gif" );
257
258 if( failedRequestIcon == null )
259 failedRequestIcon = UISupport.createImageIcon( "/invalid_amf_request.gif" );
260
261 if( unknownRequestIcon == null )
262 unknownRequestIcon = UISupport.createImageIcon( "/unknown_amf_request.gif" );
263
264 if( disabledRequestIcon == null )
265 disabledRequestIcon = UISupport.createImageIcon( "/disabled_amf_request.gif" );
266
267 setIconAnimator( new RequestIconAnimator<AMFRequest>( this, "/amf_request.gif", "/exec_amf_request", 3, "gif" ) );
268 }
269
270 protected RequestIconAnimator<?> initIconAnimator()
271 {
272 return new RequestIconAnimator<AMFRequest>( this, "/amf_request.gif", "/exec_amf_request", 3, "gif" );
273 }
274
275 public static class RequestIconAnimator<T extends AMFRequest> extends ModelItemIconAnimator<T> implements
276 SubmitListener
277 {
278 public RequestIconAnimator( T modelItem, String baseIcon, String animIconRoot, int iconCount, String iconExtension )
279 {
280 super( modelItem, baseIcon, animIconRoot, iconCount, iconExtension );
281 }
282
283 public boolean beforeSubmit( Submit submit, SubmitContext context )
284 {
285 if( isEnabled() && submit.getRequest() == getTarget() )
286 start();
287 return true;
288 }
289
290 public void afterSubmit( Submit submit, SubmitContext context )
291 {
292 if( submit.getRequest() == getTarget() )
293 stop();
294 }
295 }
296
297 public RequestIconAnimator<?> getIconAnimator()
298 {
299 return iconAnimator;
300 }
301
302 public void setIconAnimator( RequestIconAnimator<?> iconAnimator )
303 {
304 if( this.iconAnimator != null )
305 removeSubmitListener( this.iconAnimator );
306
307 this.iconAnimator = iconAnimator;
308 addSubmitListener( this.iconAnimator );
309 }
310
311 public ImageIcon getIcon()
312 {
313 if( forLoadTest || UISupport.isHeadless() )
314 return null;
315
316 TestMonitor testMonitor = SoapUI.getTestMonitor();
317 if( testMonitor != null && testMonitor.hasRunningLoadTest( getTestStep().getTestCase() ) )
318 return disabledRequestIcon;
319
320 ImageIcon icon = getIconAnimator().getIcon();
321 if( icon == getIconAnimator().getBaseIcon() )
322 {
323 AssertionStatus status = getAssertionStatus();
324 if( status == AssertionStatus.VALID )
325 return validRequestIcon;
326 else if( status == AssertionStatus.FAILED )
327 return failedRequestIcon;
328 else if( status == AssertionStatus.UNKNOWN )
329 return unknownRequestIcon;
330 }
331
332 return icon;
333 }
334
335 @Override
336 public void setIcon( ImageIcon icon )
337 {
338 getTestStep().setIcon( icon );
339 }
340
341 public void setPropertyNames( String[] propertyNames )
342 {
343 this.propertyNames = propertyNames;
344 }
345
346 public String[] getPropertyNames()
347 {
348 return propertyNames;
349 }
350
351 public void setScriptEngine( SoapUIScriptEngine scriptEngine )
352 {
353 this.scriptEngine = scriptEngine;
354 }
355
356 public SoapUIScriptEngine getScriptEngine()
357 {
358 return scriptEngine;
359 }
360
361 public String getEndpoint()
362 {
363 return endpoint;
364 }
365
366 public void setEndpoint( String endpoint )
367 {
368 this.endpoint = endpoint;
369 }
370
371 public String getAmfCall()
372 {
373 return amfCall;
374 }
375
376 public void setScript( String script )
377 {
378 this.script = script;
379 }
380
381 public String getScript()
382 {
383 return script;
384 }
385
386 public void setAmfCall( String amfCall )
387 {
388 this.amfCall = amfCall;
389 }
390
391 public HashMap<String, TestProperty> getPropertyMap()
392 {
393 return propertyMap;
394 }
395
396 public void setPropertyMap( HashMap<String, TestProperty> map )
397 {
398 this.propertyMap = map;
399 }
400
401 public void setArguments( List<Object> arguments )
402 {
403 this.arguments = arguments;
404 }
405
406 public void clearArguments()
407 {
408 this.arguments.clear();
409 }
410
411 public List<Object> getArguments()
412 {
413 return arguments;
414 }
415
416 public List<Object> addArgument( Object obj )
417 {
418 arguments.add( obj );
419 return arguments;
420 }
421
422 public Object[] argumentsToArray()
423 {
424 return arguments.toArray();
425 }
426
427 public void addSubmitListener( SubmitListener listener )
428 {
429 submitListeners.add( listener );
430 }
431
432 public boolean dependsOn( ModelItem modelItem )
433 {
434 return ModelSupport.dependsOn( testStep, modelItem );
435 }
436
437 public Attachment[] getAttachments()
438 {
439 return null;
440 }
441
442 public String getEncoding()
443 {
444 return null;
445 }
446
447 public Operation getOperation()
448 {
449 return null;
450 }
451
452 public String getRequestContent()
453 {
454 return requestAsXML();
455 }
456
457 public MessagePart[] getRequestParts()
458 {
459 return null;
460 }
461
462 public MessagePart[] getResponseParts()
463 {
464 return null;
465 }
466
467 public String getTimeout()
468 {
469 return null;
470 }
471
472 public void removeSubmitListener( SubmitListener listener )
473 {
474 submitListeners.remove( listener );
475 }
476
477 public void setEncoding( String string )
478 {
479 }
480
481 public List<? extends ModelItem> getChildren()
482 {
483 return null;
484 }
485
486 public String getDescription()
487 {
488 return testStep.getDescription();
489 }
490
491 public String getId()
492 {
493 return testStep.getId();
494 }
495
496 public String getName()
497 {
498 return testStep.getName();
499 }
500
501 public ModelItem getParent()
502 {
503 return testStep.getParent();
504 }
505
506 public Settings getSettings()
507 {
508 return testStep.getSettings();
509 }
510
511 public SubmitListener[] getSubmitListeners()
512 {
513 return submitListeners.toArray( new SubmitListener[submitListeners.size()] );
514 }
515
516 public AMFRequestTestStep getTestStep()
517 {
518 return testStep;
519 }
520
521 public WsdlMessageAssertion importAssertion( WsdlMessageAssertion source, boolean overwrite, boolean createCopy )
522 {
523 return testStep.importAssertion( source, overwrite, createCopy );
524 }
525
526 public TestAssertion addAssertion( String selection )
527 {
528 return testStep.addAssertion( selection );
529 }
530
531 public void addAssertionsListener( AssertionsListener listener )
532 {
533 testStep.addAssertionsListener( listener );
534 }
535
536 public TestAssertion cloneAssertion( TestAssertion source, String name )
537 {
538 return testStep.cloneAssertion( source, name );
539 }
540
541 public String getAssertableContent()
542 {
543 return testStep.getAssertableContent();
544 }
545
546 public AssertableType getAssertableType()
547 {
548 return testStep.getAssertableType();
549 }
550
551 public TestAssertion getAssertionAt( int c )
552 {
553 return testStep.getAssertionAt( c );
554 }
555
556 public TestAssertion getAssertionByName( String name )
557 {
558 return testStep.getAssertionByName( name );
559 }
560
561 public int getAssertionCount()
562 {
563 return testStep.getAssertionCount();
564 }
565
566 public List<TestAssertion> getAssertionList()
567 {
568 return testStep.getAssertionList();
569 }
570
571 public String requestAsXML()
572 {
573 StringBuffer sb = new StringBuffer();
574 sb.append( "<AMFRequest>\n" );
575 sb.append( " <endpoint>" + getEndpoint() + "</endpoint>\n" );
576 sb.append( " <amfcall>" + getAmfCall() + "</amfcall>\n" );
577
578 if( getPropertyNames() != null )
579 {
580 sb.append( " <parameters>\n" );
581 for( String name : getPropertyNames() )
582 {
583 if( name.equals( "ResponseAsXML" ) )
584 continue;
585 sb.append( " <parameter>\n" );
586 sb.append( " <name>" + name + "</name>\n" );
587 sb.append( " <value>" + getPropertyMap().get( name ).getValue() + "</value>\n" );
588 sb.append( " </parameter>\n" );
589 }
590 sb.append( " </parameters>\n" );
591 }
592
593 sb.append( " <script>" + getScript() + "</script>\n" );
594 sb.append( "</AMFRequest>" );
595 return sb.toString();
596 }
597
598 public void setHttpHeaders( StringToStringMap httpHeaders )
599 {
600 this.httpHeaders = httpHeaders;
601 }
602
603 public StringToStringMap getHttpHeaders()
604 {
605 return httpHeaders;
606 }
607
608 public void setAmfHeaders( StringToObjectMap amfHeaders )
609 {
610 this.amfHeaders = amfHeaders;
611 }
612
613 public StringToObjectMap getAmfHeaders()
614 {
615 return amfHeaders;
616 }
617
618 public void setAmfHeadersString( StringToStringMap amfHeadersString )
619 {
620 this.amfHeadersString = amfHeadersString;
621 }
622
623 public StringToStringMap getAmfHeadersString()
624 {
625 return amfHeadersString;
626 }
627
628 public String getPassword()
629 {
630
631 return null;
632 }
633
634 public String getUsername()
635 {
636
637 return null;
638 }
639 }