View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.rest.actions.request;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.rest.RestRequest;
17  import com.eviware.soapui.impl.wsdl.WsdlProject;
18  import com.eviware.soapui.impl.wsdl.actions.support.AbstractAddToTestCaseAction;
19  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
20  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep;
21  import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory;
22  import com.eviware.soapui.support.UISupport;
23  import com.eviware.soapui.support.types.StringToStringMap;
24  import com.eviware.soapui.ui.desktop.SoapUIDesktop;
25  import com.eviware.x.form.XForm;
26  import com.eviware.x.form.XFormDialog;
27  import com.eviware.x.form.XFormDialogBuilder;
28  import com.eviware.x.form.XFormFactory;
29  import com.eviware.x.form.XFormField;
30  
31  /***
32   * Adds a WsdlRequest to a WsdlTestCase as a WsdlTestRequestStep
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public class AddRestRequestToTestCaseAction extends AbstractAddToTestCaseAction<RestRequest>
38  {
39  	public static final String SOAPUI_ACTION_ID = "AddRestRequestToTestCaseAction";
40  	
41     private static final String STEP_NAME = "Name";
42  	private static final String CLOSE_REQUEST = "Close Request Window";
43  	private static final String SHOW_TESTCASE = "Shows TestCase Editor";
44  	private static final String SHOW_REQUEST = "Shows Request Editor";
45  	
46  	private XFormDialog dialog;
47  	private StringToStringMap dialogValues = new StringToStringMap();
48  	private XFormField closeRequestCheckBox;
49  
50  	public AddRestRequestToTestCaseAction()
51     {
52        super( "Add to TestCase", "Adds this REST Request to a TestCase" );
53     }
54  
55     public void perform( RestRequest request, Object param )
56  	{
57        WsdlProject project = request.getOperation().getInterface().getProject();
58        
59        WsdlTestCase testCase = getTargetTestCase( project );
60        if( testCase != null )
61        	addRequest( testCase, request, -1 );
62  	}   
63  
64  	public RestTestRequestStep addRequest(WsdlTestCase testCase, RestRequest request, int position)
65  	{
66  		if( dialog == null )
67  			buildDialog();
68  		
69  		dialogValues.put( STEP_NAME, request.getOperation().getName() + " - " + request.getName() );
70  		dialogValues.put( CLOSE_REQUEST, "true" );
71  		dialogValues.put( SHOW_TESTCASE, "true" );
72  		dialogValues.put( SHOW_REQUEST, "true" );
73  		
74  		SoapUIDesktop desktop = SoapUI.getDesktop();
75  		closeRequestCheckBox.setEnabled( desktop != null && desktop.hasDesktopPanel( request ));
76  		
77  		dialogValues = dialog.show( dialogValues );
78  		if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
79  			return null;;
80  
81  		String name = dialogValues.get( STEP_NAME );
82  		
83  		RestTestRequestStep testStep = (RestTestRequestStep) testCase.insertTestStep( 
84  				RestRequestStepFactory.createConfig( request, name ), position );
85  		
86  		if( testStep == null )
87  			return null;
88  
89  		if( dialogValues.getBoolean( CLOSE_REQUEST ) && desktop != null )
90  		{
91  			desktop.closeDesktopPanel( request );
92  		}
93  		
94  		if( dialogValues.getBoolean( SHOW_TESTCASE ) )
95  		{
96  			UISupport.selectAndShow( testCase );
97  		}
98  		
99  		if( dialogValues.getBoolean( SHOW_REQUEST) )
100 		{
101 			UISupport.selectAndShow( testStep );
102 		}
103 			
104 		return testStep;
105 	}
106 	
107 	private void buildDialog()
108 	{
109 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Add Request to TestCase");
110 		XForm mainForm = builder.createForm( "Basic" );
111 		
112 		mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
113 
114 		closeRequestCheckBox = mainForm.addCheckBox( CLOSE_REQUEST, "(closes the current window for this request)" );
115 		mainForm.addCheckBox( SHOW_TESTCASE, "(opens the TestCase editor for the target TestCase)" );
116 		mainForm.addCheckBox( SHOW_REQUEST, "(opens the Request editor for the created TestStep)" );
117 		
118 		dialog = builder.buildDialog( builder.buildOkCancelActions(), 
119       		"Specify options for adding the request to a TestCase", UISupport.OPTIONS_ICON );		
120 	}
121 }