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.support.assertions.Assertable;
27  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
28  import com.eviware.soapui.support.types.StringToStringMap;
29  
30  /***
31   * Registry for WsdlAssertions
32   * 
33   * @author Ole.Matzura
34   */
35  
36  public class WsdlAssertionRegistry
37  {
38     private static WsdlAssertionRegistry instance;
39  	private Map<String,Class<? extends WsdlMessageAssertion> > availableAssertions = new HashMap<String,Class<? extends WsdlMessageAssertion> >();
40  	private StringToStringMap assertionLabels = new StringToStringMap();
41  	private final static Logger log = Logger.getLogger( WsdlAssertionRegistry.class );
42  	
43  	public WsdlAssertionRegistry()
44  	{
45  		addAssertion( SoapResponseAssertion.ID, "SOAP Response", SoapResponseAssertion.class );
46  		addAssertion( SchemaComplianceAssertion.ID, "Schema Compliance", SchemaComplianceAssertion.class );
47  		addAssertion( SimpleContainsAssertion.ID, "Contains", SimpleContainsAssertion.class );
48  		addAssertion( SimpleNotContainsAssertion.ID, "Not Contains", SimpleNotContainsAssertion.class );
49  		addAssertion( XPathContainsAssertion.ID, XPathContainsAssertion.LABEL, XPathContainsAssertion.class );
50  		addAssertion( NotSoapFaultAssertion.ID, NotSoapFaultAssertion.LABEL, NotSoapFaultAssertion.class );
51  		addAssertion( SoapFaultAssertion.ID, "SOAP Fault", SoapFaultAssertion.class );
52  		addAssertion( ResponseSLAAssertion.ID, "Response SLA", ResponseSLAAssertion.class );
53  		addAssertion( GroovyScriptAssertion.ID, GroovyScriptAssertion.LABEL,	GroovyScriptAssertion.class );
54  	}
55  	
56  	public void addAssertion( String id, String label, Class<? extends WsdlMessageAssertion> assertionClass )
57  	{
58  		availableAssertions.put(  id, assertionClass );
59  		assertionLabels.put( label, id );
60  	}
61  	
62  	public static synchronized WsdlAssertionRegistry getInstance()
63  	{
64  		if( instance == null )
65  			instance = new WsdlAssertionRegistry();
66  		
67  		return instance;
68  	}
69  
70  	public WsdlMessageAssertion buildAssertion(RequestAssertionConfig config, Assertable request)
71  	{
72  	   try
73  		{
74  			String type = config.getType();
75  			Class<? extends WsdlMessageAssertion> clazz = availableAssertions.get(type);
76  			if( clazz == null )
77  			{
78  				log.error( "Missing assertion for type [" + type + "]" );
79  			}
80  			else
81  			{
82  				Constructor<? extends WsdlMessageAssertion> ctor = clazz
83  					.getConstructor(new Class[] { RequestAssertionConfig.class,
84  							Assertable.class });
85  				
86  				return (WsdlMessageAssertion) ctor.newInstance(config, request);
87  			}
88  		}
89  		catch (Exception e)
90  		{
91  			SoapUI.logError( e );
92  		}
93  		
94  		return null;
95  	}
96  	
97  	public boolean canBuildAssertion( RequestAssertionConfig config )
98  	{
99  		return availableAssertions.get(config.getType()) != null;
100 	}
101 	
102 	public enum AssertionType { REQUEST, RESPONSE, BOTH };
103 	
104 	public String getAssertionTypeForName( String name )
105 	{
106 		return assertionLabels.get( name );
107 	}
108 	
109 	public String[] getAvailableAssertionNames( AssertionType type )
110 	{
111 		List<String> result = new ArrayList<String>();
112 		
113 		for( String assertion : assertionLabels.keySet() )
114 		{
115 			switch( type )
116 			{
117 				case BOTH : 
118 				{
119 				   result.add( assertion );
120 				   break;
121 				}
122 				case REQUEST :
123 				{
124 					String assertionId = assertionLabels.get( assertion );
125 					if( Arrays.asList( availableAssertions.get( assertionId ).getInterfaces() ).contains( RequestAssertion.class )) 
126 					{
127 						result.add(  assertion );
128 					}
129 					break;
130 				}
131 				
132 				case RESPONSE :
133 				{
134 					String assertionId = assertionLabels.get( assertion );
135 					if( Arrays.asList( availableAssertions.get( assertionId ).getInterfaces() ).contains( ResponseAssertion.class )) 
136 					{
137 						result.add(  assertion );
138 					}
139 					break;
140 				}
141 			}
142 		}
143 		
144       return result.toArray( new String[result.size()] );
145 	}
146 
147 	public String getAssertionNameForType( String type )
148 	{
149 		for( String assertion : assertionLabels.keySet() )
150 		{
151 			if( assertionLabels.get( assertion ).equals( type  ))
152 				return assertion;
153 		}
154 		
155 		return null;
156 	}
157 }