View Javadoc

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