1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.mock;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.swing.ImageIcon;
29
30 import com.eviware.soapui.SoapUI;
31 import com.eviware.soapui.config.MockOperationConfig;
32 import com.eviware.soapui.config.MockOperationDocumentConfig;
33 import com.eviware.soapui.config.MockServiceConfig;
34 import com.eviware.soapui.config.TestCaseConfig;
35 import com.eviware.soapui.impl.wsdl.AbstractTestPropertyHolderWsdlModelItem;
36 import com.eviware.soapui.impl.wsdl.WsdlInterface;
37 import com.eviware.soapui.impl.wsdl.WsdlOperation;
38 import com.eviware.soapui.impl.wsdl.WsdlProject;
39 import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
40 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
41 import com.eviware.soapui.impl.wsdl.teststeps.BeanPathPropertySupport;
42 import com.eviware.soapui.model.ModelItem;
43 import com.eviware.soapui.model.iface.Operation;
44 import com.eviware.soapui.model.mock.MockOperation;
45 import com.eviware.soapui.model.mock.MockResult;
46 import com.eviware.soapui.model.mock.MockRunListener;
47 import com.eviware.soapui.model.mock.MockRunner;
48 import com.eviware.soapui.model.mock.MockService;
49 import com.eviware.soapui.model.mock.MockServiceListener;
50 import com.eviware.soapui.model.project.Project;
51 import com.eviware.soapui.model.support.ModelSupport;
52 import com.eviware.soapui.settings.SSLSettings;
53 import com.eviware.soapui.support.StringUtils;
54 import com.eviware.soapui.support.UISupport;
55 import com.eviware.soapui.support.resolver.ResolveContext;
56 import com.eviware.soapui.support.resolver.ResolveDialog;
57 import com.eviware.soapui.support.scripting.ScriptEnginePool;
58 import com.eviware.soapui.support.scripting.SoapUIScriptEngine;
59 import com.eviware.soapui.support.scripting.SoapUIScriptEngineRegistry;
60
61 /***
62 * A MockService for simulation WsdlInterfaces and their operations
63 *
64 * @author ole.matzura
65 */
66
67 public class WsdlMockService extends AbstractTestPropertyHolderWsdlModelItem<MockServiceConfig> implements MockService
68 {
69 private static final String REQUIRE_SOAP_VERSION = WsdlMockService.class.getName() + "@require-soap-version";
70 private static final String REQUIRE_SOAP_ACTION = WsdlMockService.class.getName() + "@require-soap-action";
71
72 public final static String START_SCRIPT_PROPERTY = WsdlMockService.class.getName() + "@startScript";
73 public final static String STOP_SCRIPT_PROPERTY = WsdlMockService.class.getName() + "@stopScript";
74 public static final String INCOMING_WSS = WsdlMockService.class.getName() + "@incoming-wss";
75 public static final String OUGOING_WSS = WsdlMockService.class.getName() + "@outgoing-wss";
76
77 private List<WsdlMockOperation> mockOperations = new ArrayList<WsdlMockOperation>();
78 private Set<MockRunListener> mockRunListeners = new HashSet<MockRunListener>();
79 private Set<MockServiceListener> mockServiceListeners = new HashSet<MockServiceListener>();
80 private MockServiceIconAnimator iconAnimator;
81 private WsdlMockRunner mockRunner;
82 private SoapUIScriptEngine startScriptEngine;
83 private SoapUIScriptEngine stopScriptEngine;
84 private BeanPathPropertySupport docrootProperty;
85 private ScriptEnginePool onRequestScriptEnginePool;
86 private ScriptEnginePool afterRequestScriptEnginePool;
87 private WsdlMockOperation faultMockOperation;
88 private String mockServiceEndpoint;
89
90 public WsdlMockService( Project project, MockServiceConfig config )
91 {
92 super( config, project, "/mockService.gif" );
93
94 List<MockOperationConfig> testStepConfigs = config.getMockOperationList();
95 for( MockOperationConfig tsc : testStepConfigs )
96 {
97 WsdlMockOperation testStep = new WsdlMockOperation( this, tsc );
98 mockOperations.add( testStep );
99 }
100
101 if( !config.isSetPort() || config.getPort() < 1 )
102 config.setPort( 8080 );
103
104 if( !config.isSetPath() )
105 config.setPath( "/" );
106
107 if( !getSettings().isSet( REQUIRE_SOAP_ACTION ) )
108 setRequireSoapAction( false );
109
110 try
111 {
112 if( !config.isSetHost() || !StringUtils.hasContent( config.getHost() ) )
113 config.setHost( InetAddress.getLocalHost().getHostName() );
114 }
115 catch( UnknownHostException e )
116 {
117 SoapUI.logError( e );
118 }
119
120 iconAnimator = new MockServiceIconAnimator();
121 addMockRunListener( iconAnimator );
122
123 for( MockRunListener listener : SoapUI.getListenerRegistry().getListeners( MockRunListener.class ) )
124 {
125 addMockRunListener( listener );
126 }
127
128 if( !getConfig().isSetProperties() )
129 getConfig().addNewProperties();
130
131 setPropertiesConfig( getConfig().getProperties() );
132 docrootProperty = new BeanPathPropertySupport( this, "docroot" );
133
134 if( getConfig().isSetFaultMockOperation() )
135 {
136 faultMockOperation = getMockOperationByName( getConfig().getFaultMockOperation() );
137 }
138 }
139
140 public void addMockRunListener( MockRunListener listener )
141 {
142 mockRunListeners.add( listener );
143 }
144
145 public String getPath()
146 {
147 return getConfig().getPath();
148 }
149
150 public WsdlMockOperation getMockOperationAt( int index )
151 {
152 return mockOperations.get( index );
153 }
154
155 public WsdlMockOperation getMockOperationByName( String name )
156 {
157 return ( WsdlMockOperation )getWsdlModelItemByName( mockOperations, name );
158 }
159
160 public int getMockOperationCount()
161 {
162 return mockOperations.size();
163 }
164
165 public WsdlProject getProject()
166 {
167 return ( WsdlProject )getParent();
168 }
169
170 public int getPort()
171 {
172 return getConfig().getPort();
173 }
174
175 public String getHost()
176 {
177 return getConfig().getHost();
178 }
179
180 public void setHost( String host )
181 {
182 getConfig().setHost( host );
183 }
184
185 public boolean getBindToHostOnly()
186 {
187 return getConfig().getBindToHostOnly();
188 }
189
190 public void setBindToHostOnly( boolean bindToHostOnly )
191 {
192 getConfig().setBindToHostOnly( bindToHostOnly );
193 }
194
195 public void removeMockRunListener( MockRunListener listener )
196 {
197 mockRunListeners.remove( listener );
198 }
199
200 public WsdlMockRunner start( WsdlTestRunContext context ) throws Exception
201 {
202 String path = getPath();
203 if( path == null || path.trim().length() == 0 || path.trim().charAt( 0 ) != '/' )
204 throw new Exception( "Invalid path; must start with '/'" );
205
206 mockRunner = new WsdlMockRunner( this, context );
207 return mockRunner;
208 }
209
210 public WsdlMockRunner getMockRunner()
211 {
212 return mockRunner;
213 }
214
215 public WsdlMockOperation getMockOperation( Operation operation )
216 {
217 for( int c = 0; c < getMockOperationCount(); c++ )
218 {
219 WsdlMockOperation mockOperation = mockOperations.get( c );
220 if( mockOperation.getOperation() == operation )
221 return mockOperation;
222 }
223
224 return null;
225 }
226
227 public WsdlMockOperation addNewMockOperation( WsdlOperation operation )
228 {
229 if( getMockOperation( operation ) != null )
230 return null;
231
232 MockOperationConfig config = getConfig().addNewMockOperation();
233 config.setName( operation.getName() );
234 WsdlMockOperation mockOperation = new WsdlMockOperation( this, config, operation );
235
236 mockOperations.add( mockOperation );
237 fireMockOperationAdded( mockOperation );
238
239 return mockOperation;
240 }
241
242 public void setPort( int port )
243 {
244 String oldEndpoint = getLocalEndpoint();
245
246 int oldPort = getPort();
247 if( port != oldPort )
248 {
249 getConfig().setPort( port );
250 notifyPropertyChanged( PORT_PROPERTY, oldPort, port );
251
252 for( WsdlInterface iface : getMockedInterfaces() )
253 {
254 if( Arrays.asList( iface.getEndpoints() ).contains( oldEndpoint ) )
255 iface.changeEndpoint( oldEndpoint, getLocalEndpoint() );
256 }
257 }
258 }
259
260 public WsdlInterface[] getMockedInterfaces()
261 {
262 Set<WsdlInterface> result = new HashSet<WsdlInterface>();
263
264 for( WsdlMockOperation mockOperation : mockOperations )
265 {
266 WsdlOperation operation = mockOperation.getOperation();
267 if( operation != null )
268 result.add( operation.getInterface() );
269 }
270
271 return result.toArray( new WsdlInterface[result.size()] );
272 }
273
274 @Override
275 public void release()
276 {
277 super.release();
278
279 if( mockRunner != null )
280 {
281 if( mockRunner.isRunning() )
282 mockRunner.stop();
283
284 mockRunner.release();
285 }
286
287 for( WsdlMockOperation operation : mockOperations )
288 operation.release();
289
290 mockServiceListeners.clear();
291
292 if( onRequestScriptEnginePool != null )
293 onRequestScriptEnginePool.release();
294
295 if( afterRequestScriptEnginePool != null )
296 afterRequestScriptEnginePool.release();
297
298 if( startScriptEngine != null )
299 startScriptEngine.release();
300
301 if( stopScriptEngine != null )
302 stopScriptEngine.release();
303 }
304
305 public void setPath( String path )
306 {
307 String oldEndpoint = getLocalEndpoint();
308
309 String oldPath = getPath();
310 if( !path.equals( oldPath ) )
311 {
312 getConfig().setPath( path );
313 notifyPropertyChanged( PATH_PROPERTY, oldPath, path );
314
315 for( WsdlInterface iface : getMockedInterfaces() )
316 {
317 if( Arrays.asList( iface.getEndpoints() ).contains( oldEndpoint ) )
318 iface.changeEndpoint( oldEndpoint, getLocalEndpoint() );
319 }
320 }
321 }
322
323 public MockRunListener[] getMockRunListeners()
324 {
325 return mockRunListeners.toArray( new MockRunListener[mockRunListeners.size()] );
326 }
327
328 public void removeMockOperation( WsdlMockOperation mockOperation )
329 {
330 int ix = mockOperations.indexOf( mockOperation );
331 if( ix == -1 )
332 throw new RuntimeException( "Unkonws MockOperation specified to removeMockOperation" );
333
334 mockOperations.remove( ix );
335 fireMockOperationRemoved( mockOperation );
336 mockOperation.release();
337 getConfig().removeMockOperation( ix );
338 }
339
340 public void addMockServiceListener( MockServiceListener listener )
341 {
342 mockServiceListeners.add( listener );
343 }
344
345 public void removeMockServiceListener( MockServiceListener listener )
346 {
347 mockServiceListeners.remove( listener );
348 }
349
350 protected void fireMockOperationAdded( WsdlMockOperation mockOperation )
351 {
352 MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
353 .size()] );
354 for( MockServiceListener listener : listeners )
355 {
356 listener.mockOperationAdded( mockOperation );
357 }
358 }
359
360 protected void fireMockOperationRemoved( WsdlMockOperation mockOperation )
361 {
362 MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
363 .size()] );
364 for( MockServiceListener listener : listeners )
365 {
366 listener.mockOperationRemoved( mockOperation );
367 }
368 }
369
370 protected void fireMockResponseAdded( WsdlMockResponse mockResponse )
371 {
372 MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
373 .size()] );
374 for( MockServiceListener listener : listeners )
375 {
376 listener.mockResponseAdded( mockResponse );
377 }
378 }
379
380 protected void fireMockResponseRemoved( WsdlMockResponse mockResponse )
381 {
382 MockServiceListener[] listeners = mockServiceListeners.toArray( new MockServiceListener[mockServiceListeners
383 .size()] );
384 for( MockServiceListener listener : listeners )
385 {
386 listener.mockResponseRemoved( mockResponse );
387 }
388 }
389
390 @Override
391 public ImageIcon getIcon()
392 {
393 return iconAnimator.getIcon();
394 }
395
396 public WsdlMockOperation getFaultMockOperation()
397 {
398 return faultMockOperation;
399 }
400
401 public void setFaultMockOperation( WsdlMockOperation mockOperation )
402 {
403 faultMockOperation = mockOperation;
404 if( faultMockOperation == null )
405 {
406 if( getConfig().isSetFaultMockOperation() )
407 {
408 getConfig().unsetFaultMockOperation();
409 }
410 }
411 else
412 {
413 getConfig().setFaultMockOperation( faultMockOperation.getName() );
414 }
415 }
416
417 private class MockServiceIconAnimator extends ModelItemIconAnimator<WsdlMockService> implements MockRunListener
418 {
419 public MockServiceIconAnimator()
420 {
421 super( WsdlMockService.this, "/mockService.gif", "/mockService", 4, "gif" );
422 }
423
424 public MockResult onMockRequest( MockRunner runner, HttpServletRequest request, HttpServletResponse response )
425 {
426 return null;
427 }
428
429 public void onMockResult( MockResult result )
430 {
431 }
432
433 public void onMockRunnerStart( MockRunner mockRunner )
434 {
435 start();
436 }
437
438 public void onMockRunnerStop( MockRunner mockRunner )
439 {
440 stop();
441 WsdlMockService.this.mockRunner = null;
442 }
443 }
444
445 public String getLocalEndpoint()
446 {
447 String host = getHost();
448 if( StringUtils.isNullOrEmpty( host ) )
449 {
450 host = "127.0.0.1";
451 }
452
453 return getProtocol() + host + ":" + getPort() + getPath();
454 }
455
456 private String getProtocol()
457 {
458 try
459 {
460 boolean sslEnabled = SoapUI.getSettings().getBoolean( SSLSettings.ENABLE_MOCK_SSL );
461 String protocol = sslEnabled ? "https://" : "http://";
462 return protocol;
463 }
464 catch( Exception e )
465 {
466 return "http://";
467 }
468 }
469
470 public boolean isRequireSoapVersion()
471 {
472 return getSettings().getBoolean( REQUIRE_SOAP_VERSION );
473 }
474
475 public void setRequireSoapVersion( boolean requireSoapVersion )
476 {
477 getSettings().setBoolean( REQUIRE_SOAP_VERSION, requireSoapVersion );
478 }
479
480 public boolean isRequireSoapAction()
481 {
482 return getSettings().getBoolean( REQUIRE_SOAP_ACTION );
483 }
484
485 public void setRequireSoapAction( boolean requireSoapAction )
486 {
487 getSettings().setBoolean( REQUIRE_SOAP_ACTION, requireSoapAction );
488 }
489
490 public WsdlMockRunner start() throws Exception
491 {
492 return start( null );
493 }
494
495 public boolean hasMockOperation( Operation operation )
496 {
497 return getMockOperation( operation ) != null;
498 }
499
500 public void setStartScript( String script )
501 {
502 String oldScript = getStartScript();
503
504 if( !getConfig().isSetStartScript() )
505 getConfig().addNewStartScript();
506
507 getConfig().getStartScript().setStringValue( script );
508
509 if( startScriptEngine != null )
510 startScriptEngine.setScript( script );
511
512 notifyPropertyChanged( START_SCRIPT_PROPERTY, oldScript, script );
513 }
514
515 public String getStartScript()
516 {
517 return getConfig().isSetStartScript() ? getConfig().getStartScript().getStringValue() : null;
518 }
519
520 public void setStopScript( String script )
521 {
522 String oldScript = getStopScript();
523
524 if( !getConfig().isSetStopScript() )
525 getConfig().addNewStopScript();
526
527 getConfig().getStopScript().setStringValue( script );
528 if( stopScriptEngine != null )
529 stopScriptEngine.setScript( script );
530
531 notifyPropertyChanged( STOP_SCRIPT_PROPERTY, oldScript, script );
532 }
533
534 public String getStopScript()
535 {
536 return getConfig().isSetStopScript() ? getConfig().getStopScript().getStringValue() : null;
537 }
538
539 public Object runStartScript( WsdlMockRunContext runContext, WsdlMockRunner runner ) throws Exception
540 {
541 String script = getStartScript();
542 if( StringUtils.isNullOrEmpty( script ) )
543 return null;
544
545 if( startScriptEngine == null )
546 {
547 startScriptEngine = SoapUIScriptEngineRegistry.create( this );
548 startScriptEngine.setScript( script );
549 }
550
551 startScriptEngine.setVariable( "context", runContext );
552 startScriptEngine.setVariable( "mockRunner", runner );
553 startScriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
554 return startScriptEngine.run();
555 }
556
557 public Object runStopScript( WsdlMockRunContext runContext, WsdlMockRunner runner ) throws Exception
558 {
559 String script = getStopScript();
560 if( StringUtils.isNullOrEmpty( script ) )
561 return null;
562
563 if( stopScriptEngine == null )
564 {
565 stopScriptEngine = SoapUIScriptEngineRegistry.create( this );
566 stopScriptEngine.setScript( script );
567 }
568
569 stopScriptEngine.setVariable( "context", runContext );
570 stopScriptEngine.setVariable( "mockRunner", runner );
571 stopScriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
572 return stopScriptEngine.run();
573 }
574
575 public void setOnRequestScript( String script )
576 {
577 String oldScript = getOnRequestScript();
578
579 if( !getConfig().isSetOnRequestScript() )
580 getConfig().addNewOnRequestScript();
581
582 getConfig().getOnRequestScript().setStringValue( script );
583
584 if( onRequestScriptEnginePool != null )
585 onRequestScriptEnginePool.setScript( script );
586
587 notifyPropertyChanged( "onRequestScript", oldScript, script );
588 }
589
590 public String getOnRequestScript()
591 {
592 return getConfig().isSetOnRequestScript() ? getConfig().getOnRequestScript().getStringValue() : null;
593 }
594
595 public void setAfterRequestScript( String script )
596 {
597 String oldScript = getAfterRequestScript();
598
599 if( !getConfig().isSetAfterRequestScript() )
600 getConfig().addNewAfterRequestScript();
601
602 getConfig().getAfterRequestScript().setStringValue( script );
603 if( afterRequestScriptEnginePool != null )
604 afterRequestScriptEnginePool.setScript( script );
605
606 notifyPropertyChanged( "afterRequestScript", oldScript, script );
607 }
608
609 public String getAfterRequestScript()
610 {
611 return getConfig().isSetAfterRequestScript() ? getConfig().getAfterRequestScript().getStringValue() : null;
612 }
613
614 public Object runOnRequestScript( WsdlMockRunContext runContext, WsdlMockRunner runner, WsdlMockRequest mockRequest )
615 throws Exception
616 {
617 String script = getOnRequestScript();
618 if( StringUtils.isNullOrEmpty( script ) )
619 return null;
620
621 if( onRequestScriptEnginePool == null )
622 {
623 onRequestScriptEnginePool = new ScriptEnginePool( this );
624 onRequestScriptEnginePool.setScript( script );
625 }
626
627 SoapUIScriptEngine scriptEngine = onRequestScriptEnginePool.getScriptEngine();
628
629 try
630 {
631 scriptEngine.setVariable( "context", runContext );
632 scriptEngine.setVariable( "mockRequest", mockRequest );
633 scriptEngine.setVariable( "mockRunner", runner );
634 scriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
635 return scriptEngine.run();
636 }
637 finally
638 {
639 onRequestScriptEnginePool.returnScriptEngine( scriptEngine );
640 }
641 }
642
643 public Object runAfterRequestScript( WsdlMockRunContext runContext, WsdlMockRunner runner, MockResult mockResult )
644 throws Exception
645 {
646 String script = getAfterRequestScript();
647 if( StringUtils.isNullOrEmpty( script ) )
648 return null;
649
650 if( afterRequestScriptEnginePool == null )
651 {
652 afterRequestScriptEnginePool = new ScriptEnginePool( this );
653 afterRequestScriptEnginePool.setScript( script );
654 }
655
656 SoapUIScriptEngine scriptEngine = afterRequestScriptEnginePool.getScriptEngine();
657
658 try
659 {
660 scriptEngine.setVariable( "context", runContext );
661 scriptEngine.setVariable( "mockResult", mockResult );
662 scriptEngine.setVariable( "mockRunner", runner );
663 scriptEngine.setVariable( "log", SoapUI.ensureGroovyLog() );
664 return scriptEngine.run();
665 }
666 finally
667 {
668 afterRequestScriptEnginePool.returnScriptEngine( scriptEngine );
669 }
670 }
671
672 public List<? extends ModelItem> getChildren()
673 {
674 return mockOperations;
675 }
676
677 public List<MockOperation> getMockOperationList()
678 {
679 return Collections.unmodifiableList( new ArrayList<MockOperation>( mockOperations ) );
680 }
681
682 public String getIncomingWss()
683 {
684 return getConfig().getIncomingWss();
685 }
686
687 public void setIncomingWss( String incomingWss )
688 {
689 String old = getIncomingWss();
690 getConfig().setIncomingWss( incomingWss );
691 notifyPropertyChanged( INCOMING_WSS, old, incomingWss );
692 }
693
694 public String getOutgoingWss()
695 {
696 return getConfig().getOutgoingWss();
697 }
698
699 public void setOutgoingWss( String outgoingWss )
700 {
701 String old = getOutgoingWss();
702 getConfig().setOutgoingWss( outgoingWss );
703 notifyPropertyChanged( OUGOING_WSS, old, outgoingWss );
704 }
705
706 public boolean isDispatchResponseMessages()
707 {
708 return getConfig().getDispatchResponseMessages();
709 }
710
711 public void setDispatchResponseMessages( boolean dispatchResponseMessages )
712 {
713 boolean old = isDispatchResponseMessages();
714 getConfig().setDispatchResponseMessages( dispatchResponseMessages );
715 notifyPropertyChanged( "dispatchResponseMessages", old, dispatchResponseMessages );
716 }
717
718 public List<WsdlOperation> getMockedOperations()
719 {
720 List<WsdlOperation> result = new ArrayList<WsdlOperation>();
721
722 for( WsdlMockOperation mockOperation : mockOperations )
723 result.add( mockOperation.getOperation() );
724
725 return result;
726 }
727
728 public void setDocroot( String docroot )
729 {
730 docrootProperty.set( docroot, true );
731 }
732
733 public String getDocroot()
734 {
735 return docrootProperty.get();
736 }
737
738 @Override
739 public void resolve( ResolveContext<?> context )
740 {
741 super.resolve( context );
742 docrootProperty.resolveFile( context, "Missing MockService docroot" );
743 }
744
745 public void replace( WsdlMockOperation mockOperation, MockOperationConfig reloadedMockOperation )
746 {
747 int ix = mockOperations.indexOf( mockOperation );
748 if( ix == -1 )
749 throw new RuntimeException( "Unkonws MockOperation specified to removeMockOperation" );
750
751 mockOperations.remove( ix );
752 fireMockOperationRemoved( mockOperation );
753 mockOperation.release();
754 getConfig().removeMockOperation( ix );
755
756 MockOperationConfig newConfig = ( MockOperationConfig )getConfig().insertNewMockOperation( ix ).set(
757 reloadedMockOperation ).changeType( MockOperationConfig.type );
758 WsdlMockOperation newOperation = new WsdlMockOperation( this, newConfig );
759 mockOperations.add( ix, newOperation );
760 newOperation.afterLoad();
761 fireMockOperationAdded( newOperation );
762 }
763
764 public void export( File file )
765 {
766 try
767 {
768 this.getConfig().newCursor().save( file );
769 }
770 catch( IOException e )
771 {
772 e.printStackTrace();
773 }
774 }
775
776 public void importMockOperation( File file )
777 {
778 MockOperationConfig mockOperationNewConfig = null;
779
780 if( !file.exists() )
781 {
782 UISupport.showErrorMessage( "Error loading mock operation." );
783 return;
784 }
785
786 try
787 {
788 mockOperationNewConfig = MockOperationDocumentConfig.Factory.parse( file ).getMockOperation();
789 }
790 catch( Exception e )
791 {
792 SoapUI.logError( e );
793 }
794
795 if( mockOperationNewConfig != null )
796 {
797 MockOperationConfig newConfig = ( MockOperationConfig )getConfig().addNewMockOperation().set(
798 mockOperationNewConfig ).changeType( TestCaseConfig.type );
799 WsdlMockOperation newMockOperation = new WsdlMockOperation( this, newConfig );
800 ModelSupport.unsetIds( newMockOperation );
801 newMockOperation.afterLoad();
802 mockOperations.add( newMockOperation );
803 fireMockOperationAdded( newMockOperation );
804
805 resolveImportedMockOperation( newMockOperation );
806
807 }
808 else
809 {
810 UISupport.showErrorMessage( "Not valild mock operation xml" );
811 }
812 }
813
814 private void resolveImportedMockOperation( WsdlMockOperation mockOperation )
815 {
816 ResolveDialog resolver = new ResolveDialog( "Validate MockOperation", "Checks MockOperation for inconsistencies",
817 null );
818 resolver.setShowOkMessage( false );
819 resolver.resolve( mockOperation );
820 }
821
822 public String toString()
823 {
824 return getName();
825 }
826
827 public String getMockServiceEndpoint()
828 {
829 return mockServiceEndpoint;
830 }
831
832 public void setMockServiceEndpoint( String mockServiceEndpoint )
833 {
834 this.mockServiceEndpoint = mockServiceEndpoint;
835 }
836
837 public String getLocalMockServiceEndpoint()
838 {
839 if( mockServiceEndpoint != null )
840 return mockServiceEndpoint + getPath();
841
842 String host = getHost();
843 if( StringUtils.isNullOrEmpty( host ) )
844 host = "127.0.0.1";
845
846 return getProtocol() + host + ":" + getPort() + getPath();
847 }
848
849 }