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  		addAssertion( XQueryContainsAssertion.ID, XQueryContainsAssertion.LABEL, XQueryContainsAssertion.class );
    55  	}
    56
    57  	public void addAssertion( String id, String label, Class<? extends WsdlMessageAssertion> assertionClass )
    58  	{
    59  		availableAssertions.put(  id, assertionClass );
    60  		assertionLabels.put( label, id );
    61  	}
    62
    63  	public static synchronized WsdlAssertionRegistry getInstance()
    64  	{
    65  		if( instance == null )
    66  			instance = new WsdlAssertionRegistry();
    67
    68  		return instance;
    69  	}
    70
    71  	public WsdlMessageAssertion buildAssertion(RequestAssertionConfig config, Assertable request)
    72  	{
    73  	   try
    74  		{
    75  			String type = config.getType();
    76  			Class<? extends WsdlMessageAssertion> clazz = availableAssertions.get(type);
    77  			if( clazz == null )
    78  			{
    79  				log.error( "Missing assertion for type [" + type + "]" );
    80  			}
    81  			else
    82  			{
    83  				Constructor<? extends WsdlMessageAssertion> ctor = clazz
    84  					.getConstructor(new Class[] { RequestAssertionConfig.class,
    85  							Assertable.class });
    86
    87  				return (WsdlMessageAssertion) ctor.newInstance(config, request);
    88  			}
    89  		}
    90  		catch (Exception e)
    91  		{
    92  			SoapUI.logError( e );
    93  		}
    94
    95  		return null;
    96  	}
    97
    98  	public boolean canBuildAssertion( RequestAssertionConfig config )
    99  	{
    100 		return availableAssertions.get(config.getType()) != null;
    101 	}
    102
    103 	public enum AssertableType { REQUEST, RESPONSE, BOTH };
    104
    105 	public String getAssertionTypeForName( String name )
    106 	{
    107 		return assertionLabels.get( name );
    108 	}
    109
    110 	public String[] getAvailableAssertionNames( AssertableType type )
    111 	{
    112 		List<String> result = new ArrayList<String>();
    113
    114 		for( String assertion : assertionLabels.keySet() )
    115 		{
    116 			switch( type )
    117 			{
    118 				case BOTH :
    119 				{
    120 				   result.add( assertion );
    121 				   break;
    122 				}
    123 				case REQUEST :
    124 				{
    125 					String assertionId = assertionLabels.get( assertion );
    126 					if( Arrays.asList( availableAssertions.get( assertionId ).getInterfaces() ).contains( RequestAssertion.class ))
    127 					{
    128 						result.add(  assertion );
    129 					}
    130 					break;
    131 				}
    132
    133 				case RESPONSE :
    134 				{
    135 					String assertionId = assertionLabels.get( assertion );
    136 					if( Arrays.asList( availableAssertions.get( assertionId ).getInterfaces() ).contains( ResponseAssertion.class ))
    137 					{
    138 						result.add(  assertion );
    139 					}
    140 					break;
    141 				}
    142 			}
    143 		}
    144
    145       return result.toArray( new String[result.size()] );
    146 	}
    147
    148 	public String getAssertionNameForType( String type )
    149 	{
    150 		for( String assertion : assertionLabels.keySet() )
    151 		{
    152 			if( assertionLabels.get( assertion ).equals( type  ))
    153 				return assertion;
    154 		}
    155
    156 		return null;
    157 	}
    158
    159 	public boolean canAddMultipleAssertions( String name, Assertable assertable )
    160 	{
    161 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
    162 		{
    163 			WsdlMessageAssertion assertion = assertable.getAssertionAt( c );
    164 			if( assertion.isAllowMultiple() )
    165 				continue;
    166
    167 			if( assertion.getClass().equals( availableAssertions.get( getAssertionTypeForName( name ))))
    168 			{
    169 				return false;
    170 			}
    171 		}
    172
    173 		return true;
    174 	}
    175
    176 	public boolean canAddAssertion( WsdlMessageAssertion assertion, Assertable assertable )
    177 	{
    178 		if( assertion.isAllowMultiple())
    179 			return true;
    180
    181 		for( int c = 0; c < assertable.getAssertionCount(); c++ )
    182 		{
    183 			if( assertion.getClass().equals( assertable.getAssertionAt( c ).getClass()))
    184 			{
    185 				return false;
    186 			}
    187 		}
    188
    189 		return true;
    190 	}
    191 }