View Javadoc

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