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.iface;
    14
    15  import java.io.File;
    16  import java.util.List;
    17
    18  import com.eviware.soapui.SoapUI;
    19  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    20  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    21  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    22  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
    23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
    24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
    25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
    26  import com.eviware.soapui.model.iface.Request;
    27  import com.eviware.soapui.model.testsuite.TestCase;
    28  import com.eviware.soapui.model.testsuite.TestSuite;
    29  import com.eviware.soapui.support.UISupport;
    30  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
    31  import com.eviware.soapui.support.xml.XmlUtils;
    32  import com.eviware.x.form.XFormDialog;
    33  import com.eviware.x.form.XFormField;
    34  import com.eviware.x.form.XFormFieldListener;
    35  import com.eviware.x.form.support.ADialogBuilder;
    36  import com.eviware.x.form.support.AField;
    37  import com.eviware.x.form.support.AForm;
    38  import com.eviware.x.form.support.AField.AFieldType;
    39
    40  /***
    41   * Updates the definition of a WsdlInterface.
    42   *
    43   * @author Ole.Matzura
    44   */
    45
    46  public class UpdateInterfaceAction extends AbstractSoapUIAction<WsdlInterface>
    47  {
    48     public static final String SOAPUI_ACTION_ID = "UpdateInterfaceAction";
    49     private XFormDialog dialog = null;
    50
    51  	public UpdateInterfaceAction()
    52     {
    53        super("Update Definition", "Reloads the definition for this interface and its operations");
    54     }
    55
    56     public void perform( WsdlInterface iface, Object param )
    57  	{
    58     	if( RemoveInterfaceAction.hasRunningDependingTests( iface ))
    59     	{
    60     		UISupport.showErrorMessage( "Cannot update Interface due to running depending tests" );
    61     		return;
    62     	}
    63
    64     	if( dialog == null )
    65     	{
    66     		dialog = ADialogBuilder.buildDialog( Form.class );
    67     		dialog.setBooleanValue( Form.CREATE_REQUESTS, true );
    68     		dialog.getFormField( Form.CREATE_BACKUPS ).setEnabled( false );
    69     		dialog.getFormField( Form.RECREATE_OPTIONAL ).setEnabled( false );
    70     		dialog.getFormField( Form.RECREATE_REQUESTS ).addFormFieldListener( new XFormFieldListener() {
    71
    72  				public void valueChanged( XFormField sourceField, String newValue, String oldValue )
    73  				{
    74  					boolean enabled = dialog.getBooleanValue( Form.RECREATE_REQUESTS );
    75
    76  					dialog.getFormField( Form.CREATE_BACKUPS ).setEnabled( enabled );
    77  					dialog.getFormField( Form.RECREATE_OPTIONAL ).setEnabled( enabled );
    78  				}} );
    79     	}
    80
    81     	dialog.setValue( Form.DEFINITION_URL, iface.getDefinition() );
    82     	if( !dialog.show() )
    83     		return;
    84
    85        String url = dialog.getValue( Form.DEFINITION_URL );
    86        if (url == null || url.trim().length() == 0 )
    87           return;
    88
    89        try
    90  		{
    91  			File file = new File( url );
    92  			if( file.exists() )
    93  				url = file.toURL().toString();
    94  		}
    95  		catch( Exception e )
    96  		{
    97  			SoapUI.logError( e );
    98  		}
    99
    100       boolean createRequests = dialog.getBooleanValue( Form.CREATE_REQUESTS );
    101
    102       try
    103 		{
    104       	if( iface.updateDefinition( url, createRequests ))
    105       	{
    106       		if( dialog.getBooleanValue( Form.RECREATE_REQUESTS ))
    107       		{
    108 	      		int cnt = recreateRequests( iface, dialog.getBooleanValue( Form.RECREATE_OPTIONAL ),
    109 	      					dialog.getBooleanValue( Form.CREATE_BACKUPS )	);
    110
    111 	      		UISupport.showInfoMessage(	"Update of interface successfull, [" + cnt + "] requests have" +
    112 	      				" been udpated.",  "Update Definition" );
    113       		}
    114       		else
    115       		{
    116       			UISupport.showInfoMessage(	"Update of interface successfull",  "Update Definition" );
    117       		}
    118       	}
    119       	else
    120       		UISupport.showInfoMessage(	"Update of interface failed",  "Update Definition" );
    121 		}
    122 		catch (Exception e1)
    123 		{
    124 			UISupport.showInfoMessage(	"Failed to update interface: [" + e1 + "]", "Update Definition" );
    125 			SoapUI.logError( e1 );
    126 		}
    127    }
    128
    129 	protected int recreateRequests( WsdlInterface iface, boolean buildOptional, boolean createBackups )
    130 	{
    131 		int count = 0;
    132
    133 		// first check operations
    134 		for( int c = 0; c < iface.getOperationCount(); c++ )
    135 		{
    136 			WsdlOperation operation = iface.getOperationAt( c );
    137 			String newRequest = operation.createRequest( buildOptional );
    138 			List<Request> requests = operation.getRequests();
    139
    140 			for( Request request : requests )
    141 			{
    142 				String requestContent = request.getRequestContent();
    143 				String req = XmlUtils.transferValues( requestContent, newRequest );
    144
    145 				// changed?
    146 				if( !req.equals( requestContent ) )
    147 				{
    148 					if( !XmlUtils.prettyPrintXml( req ).equals( XmlUtils.prettyPrintXml( requestContent )))
    149 					{
    150 						if( createBackups )
    151 						{
    152 							WsdlRequest backupRequest = operation.addNewRequest(
    153 										"Backup of [" + request.getName() + "]" );
    154 							((WsdlRequest)request).copyTo( backupRequest, false, false );
    155 						}
    156
    157 						((WsdlRequest)request).setRequestContent( req );
    158 						count++;
    159 					}
    160 				}
    161 			}
    162 		}
    163
    164 		// now check testsuites..
    165 		for( TestSuite testSuite : iface.getProject().getTestSuites() )
    166 		{
    167 			for( TestCase testCase : testSuite.getTestCaseList() )
    168 			{
    169 				for( int c = 0; c < testCase.getTestStepCount(); c++ )
    170 				{
    171 					WsdlTestStep testStep = ( WsdlTestStep ) testCase.getTestStepAt( c );
    172 					if( testStep instanceof WsdlTestRequestStep )
    173 					{
    174 						WsdlTestRequest testRequest = ((WsdlTestRequestStep)testStep).getTestRequest();
    175 						String newRequest = testRequest.getOperation().createRequest( buildOptional );
    176
    177 						String req = XmlUtils.transferValues( testRequest.getRequestContent(), newRequest );
    178
    179 						// changed?
    180 						if( !req.equals( testRequest.getRequestContent() ) )
    181 						{
    182 							if( createBackups )
    183 							{
    184 								((WsdlTestCase)testCase).importTestStep( testStep,
    185 											"Backup of [" + testStep.getName() + "]", -1 ).setDisabled( true );
    186 							}
    187
    188 							((WsdlRequest)testRequest).setRequestContent( req );
    189 							count++;
    190 						}
    191 					}
    192 				}
    193 			}
    194 		}
    195
    196 		return count;
    197 	}
    198
    199    @AForm(description = "Specify Update Definition options", name = "Update Definition" )
    200 	protected interface Form
    201 	{
    202    	@AField( name="Definition URL", description = "The URL or file for the updated definition", type=AFieldType.FILE )
    203 		public final static String DEFINITION_URL = "Definition URL";
    204
    205    	@AField( name="Create New Requests", description = "Create default requests for new methods", type=AFieldType.BOOLEAN )
    206 		public final static String CREATE_REQUESTS = "Create New Requests";
    207
    208    	@AField( name="Recreate Requests", description = "Recreate existing request with the new schema", type=AFieldType.BOOLEAN )
    209 		public final static String RECREATE_REQUESTS = "Recreate Requests";
    210
    211    	@AField( name="Recreate Optional", description = "Recreate optional content when updating requests", type=AFieldType.BOOLEAN )
    212 		public final static String RECREATE_OPTIONAL = "Recreate Optional";
    213
    214    	@AField( name="Create Backups", description = "Create backup copies of changed requests",
    215    				 type=AFieldType.BOOLEAN )
    216 		public final static String CREATE_BACKUPS = "Create Backups";
    217 	}
    218 }