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.teststeps.assertions;
14  
15  import java.lang.reflect.Constructor;
16  import java.util.ArrayList;
17  import java.util.Arrays;
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.log4j.Logger;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.config.RequestAssertionConfig;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
27  import com.eviware.soapui.model.testsuite.Assertable;
28  import com.eviware.soapui.model.testsuite.RequestAssertion;
29  import com.eviware.soapui.model.testsuite.ResponseAssertion;
30  import com.eviware.soapui.model.testsuite.TestAssertion;
31  import com.eviware.soapui.support.types.StringToStringMap;
32  
33  /***
34   * Registry for WsdlAssertions
35   * 
36   * @author Ole.Matzura
37   */
38  
39  public class WsdlAssertionRegistry
40  {
41     private static WsdlAssertionRegistry instance;
42  	private Map<String,Class<? extends WsdlMessageAssertion> > availableAssertions = new HashMap<String,Class<? extends WsdlMessageAssertion> >();
43  	private StringToStringMap assertionLabels = new StringToStringMap();
44  	private final static Logger log = Logger.getLogger( WsdlAssertionRegistry.class );
45  	
46  	public WsdlAssertionRegistry()
47  	{
48  		addAssertion( SoapResponseAssertion.ID, "SOAP Response", SoapResponseAssertion.class );
49  		addAssertion( SchemaComplianceAssertion.ID, "Schema Compliance", SchemaComplianceAssertion.class );
50  		addAssertion( SimpleContainsAssertion.ID, "Contains", SimpleContainsAssertion.class );
51  		addAssertion( SimpleNotContainsAssertion.ID, "Not Contains", SimpleNotContainsAssertion.class );
52  		addAssertion( XPathContainsAssertion.ID, XPathContainsAssertion.LABEL, XPathContainsAssertion.class );
53  		addAssertion( NotSoapFaultAssertion.ID, NotSoapFaultAssertion.LABEL, NotSoapFaultAssertion.class );
54  		addAssertion( SoapFaultAssertion.ID, "SOAP Fault", SoapFaultAssertion.class );
55  		addAssertion( ResponseSLAAssertion.ID, "Response SLA", ResponseSLAAssertion.class );
56  		addAssertion( GroovyScriptAssertion.ID, GroovyScriptAssertion.LABEL,	GroovyScriptAssertion.class );
57  		addAssertion( XQueryContainsAssertion.ID, XQueryContainsAssertion.LABEL, XQueryContainsAssertion.class );
58  		addAssertion( WSSStatusAssertion.ID, "WS-Security Status", WSSStatusAssertion.class );
59  	}
60  	
61  	public void addAssertion( String id, String label, Class<? extends WsdlMessageAssertion> assertionClass )
62  	{
63  		availableAssertions.put(  id, assertionClass );
64  		assertionLabels.put( label, id );
65  	}
66  	
67  	public static synchronized WsdlAssertionRegistry getInstance()
68  	{
69  		if( instance == null )
70  			instance = new WsdlAssertionRegistry();
71  		
72  		return instance;
73  	}
74  
75  	public WsdlMessageAssertion buildAssertion(RequestAssertionConfig config, Assertable assertable)
76  	{
77  	   try
78  		{
79  			String type = config.getType();
80  			Class<? extends WsdlMessageAssertion> clazz = availableAssertions.get(type);
81  			if( clazz == null )
82  			{
83  				log.error( "Missing assertion for type [" + type + "]" );
84  			}
85  			else
86  			{
87  				Constructor<? extends WsdlMessageAssertion> ctor = clazz
88  					.getConstructor(new Class[] { RequestAssertionConfig.class,
89  							Assertable.class });
90  				
91  				return (WsdlMessageAssertion) ctor.newInstance(config, assertable);
92  			}
93  		}
94  		catch (Exception e)
95  		{
96  			SoapUI.logError( e );
97  		}
98  		
99  		return null;
100 	}
101 	
102 	public boolean canBuildAssertion( RequestAssertionConfig config )
103 	{
104 		return availableAssertions.get(config.getType()) != null;
105 	}
106 	
107 	public String getAssertionTypeForName( String name )
108 	{
109 		return assertionLabels.get( name );
110 	}
111 	
112 	public enum AssertableType { REQUEST, RESPONSE, BOTH }
113 	
114 	public String[] getAvailableAssertionNames( AssertableType type )
115 	{
116 		List<String> result = new ArrayList<String>();
117 		
118 		for( String assertion : assertionLabels.keySet() )
119 		{
120 			switch( type )
121 			{
122 				case BOTH : 
123 				{
124 				   result.add( assertion );
125 				   break;
126 				}
127 				case REQUEST :
128 				{
129 					String assertionId = assertionLabels.get( assertion );
130 					if( Arrays.asList( availableAssertions.get( assertionId ).getInterfaces() ).contains( RequestAssertion.class )) 
131 					{
132 						result.add(  assertion );
133 					}
134 					break;
135 				}
136 				
137 				case RESPONSE :
138 				{
139 					String assertionId = assertionLabels.get( assertion );
140 					if( Arrays.asList( availableAssertions.get( assertionId ).getInterfaces() ).contains( ResponseAssertion.class )) 
141 					{
142 						result.add(  assertion );
143 					}
144 					break;
145 				}
146 			}
147 		}
148 		
149       return result.toArray( new String[result.size()] );
150 	}
151 
152 	public String getAssertionNameForType( String type )
153 	{
154 		for( String assertion : assertionLabels.keySet() )
155 		{
156 			if( assertionLabels.get( assertion ).equals( type  ))
157 				return assertion;
158 		}
159 		
160 		return null;
161 	}
162 
163 	public boolean canAddMultipleAssertions( String name, Assertable assertable )
164 	{
165 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
166 		{
167 			TestAssertion assertion = assertable.getAssertionAt( c );
168 			if( assertion.isAllowMultiple() )
169 				continue;
170 			
171 			if( assertion.getClass().equals( availableAssertions.get( getAssertionTypeForName( name ))))
172 			{
173 				return false;
174 			}
175 		}
176 		
177 		return true;
178 	}
179 
180 	public boolean canAddAssertion( WsdlMessageAssertion assertion, Assertable assertable )
181 	{
182 		if( assertion.isAllowMultiple())
183 			return true;
184 		
185 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
186 		{
187 			if( assertion.getClass().equals( assertable.getAssertionAt( c ).getClass()))
188 			{
189 				return false;
190 			}
191 		}
192 		
193 		return true;
194 	}
195 }