View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.wsdl.actions.request;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.WsdlProject;
17  import com.eviware.soapui.impl.wsdl.WsdlRequest;
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.WsdlTestRequestStep;
21  import com.eviware.soapui.impl.wsdl.teststeps.assertions.NotSoapFaultAssertion;
22  import com.eviware.soapui.impl.wsdl.teststeps.assertions.SchemaComplianceAssertion;
23  import com.eviware.soapui.impl.wsdl.teststeps.assertions.SoapResponseAssertion;
24  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
25  import com.eviware.soapui.support.UISupport;
26  import com.eviware.soapui.support.types.StringToStringMap;
27  import com.eviware.soapui.ui.desktop.SoapUIDesktop;
28  import com.eviware.x.form.XForm;
29  import com.eviware.x.form.XFormDialog;
30  import com.eviware.x.form.XFormDialogBuilder;
31  import com.eviware.x.form.XFormFactory;
32  import com.eviware.x.form.XFormField;
33  
34  /***
35   * Adds a WsdlRequest to a WsdlTestCase as a WsdlTestRequestStep
36   * 
37   * @author Ole.Matzura
38   */
39  
40  public class AddRequestToTestCaseAction extends AbstractAddToTestCaseAction<WsdlRequest>
41  {
42  	public static final String SOAPUI_ACTION_ID = "AddRequestToTestCaseAction";
43  	
44     private static final String STEP_NAME = "Name";
45  	private static final String ADD_SOAP_FAULT_ASSERTION = "Add Not SOAP Fault Assertion";
46  	private static final String ADD_SOAP_RESPONSE_ASSERTION = "Add SOAP Response Assertion";
47  	private static final String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
48  	private static final String CLOSE_REQUEST = "Close Request Window";
49  	private static final String SHOW_TESTCASE = "Shows TestCase Editor";
50  	private static final String COPY_ATTACHMENTS = "Copy Attachments";
51  	private static final String COPY_HTTPHEADERS = "Copy HTTP Headers";
52  	
53  	private XFormDialog dialog;
54  	private StringToStringMap dialogValues = new StringToStringMap();
55  	private XFormField closeRequestCheckBox;
56  
57  	public AddRequestToTestCaseAction()
58     {
59        super( "Add to TestCase", "Adds this request to a TestCase" );
60     }
61  
62     public void perform( WsdlRequest request, Object param )
63  	{
64        WsdlProject project = request.getOperation().getInterface().getProject();
65        
66        WsdlTestCase testCase = getTargetTestCase( project );
67        if( testCase != null )
68        	addRequest( testCase, request, -1 );
69  	}   
70  
71  	public WsdlTestRequestStep addRequest(WsdlTestCase testCase, WsdlRequest request, int position)
72  	{
73  		if( dialog == null )
74  			buildDialog();
75  		
76  		dialogValues.put( STEP_NAME, request.getOperation().getName() + " - " + request.getName() );
77  		dialogValues.put( CLOSE_REQUEST, "true" );
78  		dialogValues.put( SHOW_TESTCASE, "true" );
79  		dialog.getFormField( COPY_ATTACHMENTS ).setEnabled( request.getAttachmentCount() > 0 );
80  		dialog.setBooleanValue( COPY_ATTACHMENTS, true );
81  
82  		dialog.getFormField( COPY_HTTPHEADERS ).setEnabled( request.getRequestHeaders().size() > 0 );
83  		dialog.setBooleanValue( COPY_HTTPHEADERS, false );
84  		
85  		SoapUIDesktop desktop = SoapUI.getDesktop();
86  		closeRequestCheckBox.setEnabled( desktop != null && desktop.hasDesktopPanel( request ));
87  		
88  		dialog.getFormField( ADD_SCHEMA_ASSERTION ).setEnabled( !request.getOperation().isOneWay() );
89  		dialog.getFormField( ADD_SOAP_FAULT_ASSERTION ).setEnabled( !request.getOperation().isOneWay() );
90  		dialog.getFormField( ADD_SOAP_RESPONSE_ASSERTION ).setEnabled( !request.getOperation().isOneWay() );
91  		
92  		dialogValues = dialog.show( dialogValues );
93  		if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
94  			return null;;
95  
96  		String name = dialogValues.get( STEP_NAME );
97  		
98  		WsdlTestRequestStep test = (WsdlTestRequestStep) testCase.insertTestStep( 
99  				WsdlTestRequestStepFactory.createConfig( request, name ), position );
100 
101 		if( dialogValues.getBoolean( COPY_ATTACHMENTS ))
102 			request.copyAttachmentsTo( test.getTestRequest() );
103 		
104 		if( dialogValues.getBoolean( COPY_HTTPHEADERS ))
105 			test.getTestRequest().setRequestHeaders( request.getRequestHeaders() );
106 
107 		if( !request.getOperation().isOneWay() )
108 		{
109 			if( dialogValues.getBoolean( ADD_SOAP_RESPONSE_ASSERTION ))
110 				test.getTestRequest().addAssertion( SoapResponseAssertion.ID );
111 			
112 			if( dialogValues.getBoolean( ADD_SCHEMA_ASSERTION ))
113 				test.getTestRequest().addAssertion( SchemaComplianceAssertion.ID );
114 			
115 			if( dialogValues.getBoolean( ADD_SOAP_FAULT_ASSERTION ))
116 				test.getTestRequest().addAssertion( NotSoapFaultAssertion.LABEL );
117 		}
118 		
119 		UISupport.selectAndShow( test );
120 		
121 		if( dialogValues.getBoolean( CLOSE_REQUEST ) && desktop != null )
122 		{
123 			desktop.closeDesktopPanel( request );
124 		}
125 		
126 		if( dialogValues.getBoolean( SHOW_TESTCASE ) )
127 		{
128 			UISupport.selectAndShow( testCase );
129 		}
130 			
131 		return test;
132 	}
133 	
134 	private void buildDialog()
135 	{
136 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Add Request to TestCase");
137 		XForm mainForm = builder.createForm( "Basic" );
138 		
139 		mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
140 
141 		mainForm.addCheckBox( ADD_SOAP_RESPONSE_ASSERTION, "(adds validation that response is a SOAP message)" );
142 		mainForm.addCheckBox( ADD_SCHEMA_ASSERTION, "(adds validation that response complies with its schema)" );
143 		mainForm.addCheckBox( ADD_SOAP_FAULT_ASSERTION, "(adds validation that response is not a SOAP Fault)" );
144 		closeRequestCheckBox = mainForm.addCheckBox( CLOSE_REQUEST, "(closes the current window for this request)" );
145 		mainForm.addCheckBox( SHOW_TESTCASE, "(opens the TestCase editor for the target TestCase)" );
146 		mainForm.addCheckBox( COPY_ATTACHMENTS, "(copies the requests attachments to the TestRequest)" );
147 		mainForm.addCheckBox( COPY_HTTPHEADERS, "(copies the requests HTTP-Headers to the TestRequest)" );
148 		
149 		dialog = builder.buildDialog( builder.buildOkCancelActions(), 
150       		"Specify options for adding the request to a TestCase", UISupport.OPTIONS_ICON );		
151 		
152 		dialogValues.put( ADD_SOAP_RESPONSE_ASSERTION, Boolean.TRUE.toString() );
153 	}
154 }