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