1
2
3
4
5
6
7
8
9
10
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.RestTestRequestStepInterface;
22 import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory;
23 import com.eviware.soapui.support.UISupport;
24 import com.eviware.soapui.support.types.StringToStringMap;
25 import com.eviware.soapui.ui.desktop.SoapUIDesktop;
26 import com.eviware.x.form.XForm;
27 import com.eviware.x.form.XFormDialog;
28 import com.eviware.x.form.XFormDialogBuilder;
29 import com.eviware.x.form.XFormFactory;
30 import com.eviware.x.form.XFormField;
31
32 /***
33 * Adds a WsdlRequest to a WsdlTestCase as a WsdlTestRequestStep
34 *
35 * @author Ole.Matzura
36 */
37
38 public class AddRestRequestToTestCaseAction extends AbstractAddToTestCaseAction<RestRequest>
39 {
40 public static final String SOAPUI_ACTION_ID = "AddRestRequestToTestCaseAction";
41
42 private static final String STEP_NAME = "Name";
43 private static final String CLOSE_REQUEST = "Close Request Window";
44 private static final String SHOW_TESTCASE = "Shows TestCase Editor";
45 private static final String SHOW_REQUEST = "Shows Request Editor";
46
47 private XFormDialog dialog;
48 private StringToStringMap dialogValues = new StringToStringMap();
49 private XFormField closeRequestCheckBox;
50
51 public AddRestRequestToTestCaseAction()
52 {
53 super( "Add to TestCase", "Adds this REST Request to a TestCase" );
54 }
55
56 public void perform( RestRequest request, Object param )
57 {
58 WsdlProject project = request.getOperation().getInterface().getProject();
59
60 WsdlTestCase testCase = getTargetTestCase( project );
61 if( testCase != null )
62 addRequest( testCase, request, -1 );
63 }
64
65 public RestTestRequestStepInterface addRequest( WsdlTestCase testCase, RestRequest request, int position )
66 {
67 if( dialog == null )
68 buildDialog();
69
70 dialogValues.put( STEP_NAME, request.getRestMethod().getName() + " - " + request.getName() );
71 dialogValues.put( CLOSE_REQUEST, "true" );
72 dialogValues.put( SHOW_TESTCASE, "true" );
73 dialogValues.put( SHOW_REQUEST, "true" );
74
75 SoapUIDesktop desktop = SoapUI.getDesktop();
76 closeRequestCheckBox.setEnabled( desktop != null && desktop.hasDesktopPanel( request ) );
77
78 dialogValues = dialog.show( dialogValues );
79 if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
80 return null;
81 ;
82
83 String name = dialogValues.get( STEP_NAME );
84
85 RestTestRequestStep testStep = ( RestTestRequestStep )testCase.insertTestStep( RestRequestStepFactory
86 .createConfig( request, name ), position );
87
88 if( testStep == null )
89 return null;
90
91 if( dialogValues.getBoolean( CLOSE_REQUEST ) && desktop != null )
92 {
93 desktop.closeDesktopPanel( request );
94 }
95
96 if( dialogValues.getBoolean( SHOW_TESTCASE ) )
97 {
98 UISupport.selectAndShow( testCase );
99 }
100
101 if( dialogValues.getBoolean( SHOW_REQUEST ) )
102 {
103 UISupport.selectAndShow( testStep );
104 }
105
106 return testStep;
107 }
108
109 private void buildDialog()
110 {
111 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Add Request to TestCase" );
112 XForm mainForm = builder.createForm( "Basic" );
113
114 mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
115
116 closeRequestCheckBox = mainForm.addCheckBox( CLOSE_REQUEST, "(closes the current window for this request)" );
117 mainForm.addCheckBox( SHOW_TESTCASE, "(opens the TestCase editor for the target TestCase)" );
118 mainForm.addCheckBox( SHOW_REQUEST, "(opens the Request editor for the created TestStep)" );
119
120 dialog = builder.buildDialog( builder.buildOkCancelActions(),
121 "Specify options for adding the request to a TestCase", UISupport.OPTIONS_ICON );
122 }
123 }