View Javadoc

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