View Javadoc

1   /*
2    *  soapUI Pro, copyright (C) 2007 eviware software ab 
3    */
4   
5   package com.eviware.soapui.support.dnd.handlers;
6   
7   import java.util.HashSet;
8   
9   import com.eviware.soapui.SoapUI;
10  import com.eviware.soapui.impl.wsdl.WsdlInterface;
11  import com.eviware.soapui.impl.wsdl.WsdlProject;
12  import com.eviware.soapui.impl.wsdl.WsdlRequest;
13  import com.eviware.soapui.impl.wsdl.actions.request.AddRequestToTestCaseAction;
14  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
15  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
16  import com.eviware.soapui.support.UISupport;
17  import com.eviware.soapui.support.action.SoapUIAction;
18  
19  public class RequestToTestStepDropHandler extends AbstractBeforeAfterModelItemDropHandler<WsdlRequest, WsdlTestStep>
20  {
21  	public RequestToTestStepDropHandler()
22  	{
23  		super( WsdlRequest.class, WsdlTestStep.class );
24  	}
25  	
26  	@Override
27  	boolean canCopyAfter( WsdlRequest source, WsdlTestStep target )
28  	{
29  		return true;
30  	}
31  
32  	@Override
33  	boolean canMoveAfter( WsdlRequest source, WsdlTestStep target )
34  	{
35  		return true;
36  	}
37  
38  	@Override
39  	boolean copyAfter( WsdlRequest source, WsdlTestStep target )
40  	{
41  		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target )+1);
42  	}
43  
44  	private boolean addRequestToTestCase( WsdlRequest source, WsdlTestCase testCase, int index )
45  	{
46  		if( !UISupport.confirm( "Add Request [" + source.getName() + "] to TestCase [" + 
47  					testCase.getName() +"]", "Add Request to TestCase" ))
48  			return false;
49  		
50  		WsdlProject targetProject = testCase.getTestSuite().getProject();
51  		if( targetProject != source.getOperation().getInterface().getProject() )
52  		{
53  			HashSet<WsdlInterface> requiredInterfaces = new HashSet<WsdlInterface>();
54  			requiredInterfaces.add( source.getOperation().getInterface() );
55  			
56  			if( !DragAndDropSupport.importRequiredInterfaces( targetProject,  
57  						requiredInterfaces, "Add Request to TestCase" ))
58  			{
59  				return false;
60  			}
61  		}
62  		
63  		SoapUIAction<WsdlRequest> action = SoapUI.getActionRegistry().getAction( AddRequestToTestCaseAction.SOAPUI_ACTION_ID );
64  		return (( AddRequestToTestCaseAction ) action).addRequest( testCase, 
65  					source, index ) != null;
66  	}
67  
68  	@Override
69  	boolean moveAfter( WsdlRequest source, WsdlTestStep target )
70  	{
71  		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target )+1);
72  	}
73  	
74  	@Override
75  	String getCopyAfterInfo( WsdlRequest source, WsdlTestStep target )
76  	{
77  		return "Add Request [" + source.getName() + "] to TestCase [" + target.getTestCase().getName() +"]";
78  	}
79  
80  	@Override
81  	String getMoveAfterInfo( WsdlRequest source, WsdlTestStep target )
82  	{
83  		return getCopyAfterInfo( source, target );
84  	}
85  
86  	@Override
87  	boolean canCopyBefore( WsdlRequest source, WsdlTestStep target )
88  	{
89  		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target ));
90  	}
91  
92  	@Override
93  	boolean canMoveBefore( WsdlRequest source, WsdlTestStep target )
94  	{
95  		return true;
96  	}
97  
98  	@Override
99  	boolean copyBefore( WsdlRequest source, WsdlTestStep target )
100 	{
101 		return false;
102 	}
103 
104 	@Override
105 	String getCopyBeforeInfo( WsdlRequest source, WsdlTestStep target )
106 	{
107 		return getCopyAfterInfo( source, target );
108 	}
109 
110 	@Override
111 	String getMoveBeforeInfo( WsdlRequest source, WsdlTestStep target )
112 	{
113 		return getCopyAfterInfo( source, target );
114 	}
115 
116 	@Override
117 	boolean moveBefore( WsdlRequest source, WsdlTestStep target )
118 	{
119 		return addRequestToTestCase( source, target.getTestCase(), target.getTestCase().getIndexOfTestStep( target ));
120 	}
121 }