View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 java.awt.event.ActionEvent;
16  
17  import javax.swing.Action;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.WsdlProject;
21  import com.eviware.soapui.impl.wsdl.WsdlRequest;
22  import com.eviware.soapui.impl.wsdl.actions.support.AbstractAddToTestCaseAction;
23  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
25  import com.eviware.soapui.impl.wsdl.teststeps.assertions.SchemaComplianceAssertion;
26  import com.eviware.soapui.impl.wsdl.teststeps.assertions.NotSoapFaultAssertion;
27  import com.eviware.soapui.impl.wsdl.teststeps.assertions.SoapResponseAssertion;
28  import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.soapui.support.types.StringToStringMap;
31  import com.eviware.soapui.ui.desktop.SoapUIDesktop;
32  import com.eviware.x.form.XForm;
33  import com.eviware.x.form.XFormDialog;
34  import com.eviware.x.form.XFormDialogBuilder;
35  import com.eviware.x.form.XFormFactory;
36  import com.eviware.x.form.XFormField;
37  
38  /***
39   * Adds a WsdlRequest to a WsdlTestCase
40   * 
41   * @author Ole.Matzura
42   */
43  
44  public class AddRequestToTestCaseAction extends AbstractAddToTestCaseAction
45  {
46     private static final String STEP_NAME = "Name";
47  	private static final String ADD_SOAP_FAULT_ASSERTION = "Add SOAP Fault Assertion";
48  	private static final String ADD_SOAP_RESPONSE_ASSERTION = "Add SOAP Response Assertion";
49  	private static final String ADD_SCHEMA_ASSERTION = "Add Schema Assertion";
50  	private static final String CLOSE_REQUEST = "Close Request Window";
51  	private static final String SHOW_TESTCASE = "Shows TestCase Editor";
52  	private final WsdlRequest request;
53  	private XFormDialog dialog;
54  	private StringToStringMap dialogValues = new StringToStringMap();
55  	private XFormField closeRequestCheckBox;
56  
57  	public AddRequestToTestCaseAction( WsdlRequest request )
58     {
59        super( "Add to TestCase" );
60  		this.request = request;
61        putValue( Action.SHORT_DESCRIPTION, "Adds this request to a TestCase" );
62        putValue( Action.SMALL_ICON, UISupport.createImageIcon("/addToTestCase.gif" ));
63        putValue( Action.ACCELERATOR_KEY, UISupport.getKeyStroke( "menu alt A" ));
64     }
65     
66     public void actionPerformed(ActionEvent e)
67  	{
68        WsdlProject project = request.getOperation().getInterface().getProject();
69        
70        WsdlTestCase testCase = getTargetTestCase( project );
71        if( testCase != null )
72        	addRequest( testCase );
73  	}   
74  
75  	private boolean addRequest(WsdlTestCase testCase)
76  	{
77  		if( dialog == null )
78  			buildDialog();
79  		
80  		dialogValues.put( STEP_NAME, request.getOperation().getName() + " - " + request.getName() );
81  		dialogValues.put( CLOSE_REQUEST, "true" );
82  		dialogValues.put( SHOW_TESTCASE, "true" );
83  		
84  		SoapUIDesktop desktop = SoapUI.getDesktop();
85  		closeRequestCheckBox.setEnabled( desktop != null && desktop.hasDesktopPanel( request ));
86  		
87  		dialogValues = dialog.show( dialogValues );
88  		if( dialog.getReturnValue() != XFormDialog.OK_OPTION )
89  			return false;
90  
91  		String name = dialogValues.get( STEP_NAME );
92  		
93  		WsdlTestRequestStep test = (WsdlTestRequestStep) testCase.insertTestStep( 
94  				WsdlTestRequestStepFactory.createConfig( request, name ), -1 );
95  
96  		request.copyAttachmentsTo( test.getTestRequest() );
97  
98  		if( dialogValues.getBoolean( ADD_SOAP_RESPONSE_ASSERTION ))
99  			test.getTestRequest().addAssertion( SoapResponseAssertion.ID );
100 		
101 		if( dialogValues.getBoolean( ADD_SCHEMA_ASSERTION ))
102 			test.getTestRequest().addAssertion( SchemaComplianceAssertion.ID );
103 		
104 		if( dialogValues.getBoolean( ADD_SOAP_FAULT_ASSERTION ))
105 			test.getTestRequest().addAssertion( NotSoapFaultAssertion.ID );
106 		
107 		UISupport.selectAndShow( test );
108 		
109 		if( dialogValues.getBoolean( CLOSE_REQUEST ) && desktop != null )
110 		{
111 			desktop.closeDesktopPanel( request );
112 		}
113 		
114 		if( dialogValues.getBoolean( SHOW_TESTCASE ) )
115 		{
116 			UISupport.selectAndShow( testCase );
117 		}
118 			
119 		return true;
120 	}
121 	
122 	private void buildDialog()
123 	{
124 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("Add Request to TestCase");
125 		XForm mainForm = builder.createForm( "Basic" );
126 		
127 		mainForm.addTextField( STEP_NAME, "Name of TestStep", XForm.FieldType.URL ).setWidth( 30 );
128 
129 		mainForm.addCheckBox( ADD_SOAP_RESPONSE_ASSERTION, "(adds validation that response is a SOAP message)" );
130 		mainForm.addCheckBox( ADD_SCHEMA_ASSERTION, "(adds validation that response complies with its schema)" );
131 		mainForm.addCheckBox( ADD_SOAP_FAULT_ASSERTION, "(adds validation that response is not a SOAP Fault)" );
132 		closeRequestCheckBox = mainForm.addCheckBox( CLOSE_REQUEST, "(closes the current window for this request)" );
133 		mainForm.addCheckBox( SHOW_TESTCASE, "(opens the TestCase editor for the target TestCase)" );
134 		
135 		dialog = builder.buildDialog( builder.buildOkCancelActions(), 
136       		"Specify options for adding the request to a TestCase", UISupport.OPTIONS_ICON );		
137 		
138 		dialogValues.put( ADD_SOAP_RESPONSE_ASSERTION, Boolean.TRUE.toString() );
139 	}
140 }