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 WSAAssertion.Factory() );
57  	}
58  	
59  	public void addAssertion( TestAssertionFactory factory)
60  	{
61  		availableAssertions.put( factory.getAssertionId(), factory );
62  		assertionLabels.put(factory.getAssertionLabel(), factory.getAssertionId() );
63  	}
64  
65  	public static synchronized TestAssertionRegistry getInstance()
66  	{
67  		if( instance == null )
68  			instance = new TestAssertionRegistry();
69  		
70  		return instance;
71  	}
72  
73  	public WsdlMessageAssertion buildAssertion(TestAssertionConfig config, Assertable assertable)
74  	{
75  	   try
76  		{
77  			String type = config.getType();
78  			TestAssertionFactory factory = availableAssertions.get(type);
79  			if( factory == null )
80  			{
81  				log.error( "Missing assertion for type [" + type + "]" );
82  			}
83  			else
84  			{
85  				return (WsdlMessageAssertion) factory.buildAssertion(config, assertable);
86  			}
87  		}
88  		catch (Exception e)
89  		{
90  			SoapUI.logError( e );
91  		}
92  		
93  		return null;
94  	}
95  	
96  	public boolean canBuildAssertion( TestAssertionConfig config )
97  	{
98  		return availableAssertions.containsKey(config.getType());
99  	}
100 	
101 	public String getAssertionTypeForName( String name )
102 	{
103 		return assertionLabels.get( name );
104 	}
105 	
106 	public enum AssertableType { REQUEST, RESPONSE, BOTH }
107 	
108 	public String[] getAvailableAssertionNames( Assertable assertable )
109 	{
110 		List<String> result = new ArrayList<String>();
111 		
112 		for( TestAssertionFactory assertion : availableAssertions.values() )
113 		{
114 			if( assertion.canAssert( assertable ))
115 				result.add( assertion.getAssertionLabel() );
116 		}
117 		
118       return result.toArray( new String[result.size()] );
119 	}
120 
121 	public String getAssertionNameForType( String type )
122 	{
123 		for( String assertion : assertionLabels.keySet() )
124 		{
125 			if( assertionLabels.get( assertion ).equals( type  ))
126 				return assertion;
127 		}
128 		
129 		return null;
130 	}
131 
132 	public boolean canAddMultipleAssertions( String name, Assertable assertable )
133 	{
134 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
135 		{
136 			TestAssertion assertion = assertable.getAssertionAt( c );
137 			if( assertion.isAllowMultiple() )
138 				continue;
139 			
140 			if( assertion.getClass().equals( availableAssertions.get( getAssertionTypeForName( name ))))
141 			{
142 				return false;
143 			}
144 		}
145 		
146 		return true;
147 	}
148 
149 	public boolean canAddAssertion( WsdlMessageAssertion assertion, Assertable assertable )
150 	{
151 		if( assertion.isAllowMultiple())
152 			return true;
153 		
154 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
155 		{
156 			if( assertion.getClass().equals( assertable.getAssertionAt( c ).getClass()))
157 			{
158 				return false;
159 			}
160 		}
161 		
162 		return true;
163 	}
164 }