View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.mockservice;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import com.eviware.soapui.impl.WsdlInterfaceFactory;
19  import com.eviware.soapui.impl.support.AbstractInterface;
20  import com.eviware.soapui.impl.wsdl.WsdlOperation;
21  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
22  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
24  import com.eviware.soapui.model.iface.Interface;
25  import com.eviware.soapui.support.UISupport;
26  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
27  
28  /***
29   * Adds a new WsdlMockOperation to a WsdlMockService
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class AddNewMockOperationAction extends AbstractSoapUIAction<WsdlMockService>
35  {
36  	public final static String SOAPUI_ACTION_ID = "AddNewMockOperationAction";
37  
38  	public AddNewMockOperationAction()
39  	{
40  		super( "New MockOperation", "Creates a new MockOperation for this MockService" );
41  	}
42  
43  	public void perform( WsdlMockService mockService, Object param )
44  	{
45  		List<OperationWrapper> operations = new ArrayList<OperationWrapper>();
46  
47  		List<AbstractInterface<?>> interfaces = mockService.getProject().getInterfaces( WsdlInterfaceFactory.WSDL_TYPE );
48  
49  		for( Interface iface : interfaces )
50  		{
51  			for( int i = 0; i < iface.getOperationCount(); i++ )
52  			{
53  				if( !mockService.hasMockOperation( iface.getOperationAt( i ) ) )
54  					operations.add( new OperationWrapper( ( WsdlOperation )iface.getOperationAt( i ) ) );
55  			}
56  		}
57  
58  		if( operations.isEmpty() )
59  		{
60  			UISupport.showErrorMessage( "No unique operations to mock in project!" );
61  			return;
62  		}
63  
64  		Object result = UISupport.prompt( "Select Operation to Mock", "New MockOperation", operations.toArray() );
65  		if( result != null )
66  		{
67  			WsdlMockOperation mockOperation = mockService.addNewMockOperation( ( ( OperationWrapper )result )
68  					.getOperation() );
69  			WsdlMockResponse mockResponse = mockOperation.addNewMockResponse( "Response 1", true );
70  			UISupport.selectAndShow( mockResponse );
71  		}
72  	}
73  
74  	public class OperationWrapper
75  	{
76  		private final WsdlOperation operation;
77  
78  		public OperationWrapper( WsdlOperation operation )
79  		{
80  			this.operation = operation;
81  		}
82  
83  		public WsdlOperation getOperation()
84  		{
85  			return operation;
86  		}
87  
88  		public String toString()
89  		{
90  			return operation.getInterface().getName() + " - " + operation.getName();
91  		}
92  	}
93  }