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