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.util.ArrayList;
16  import java.util.List;
17  
18  import org.apache.xmlbeans.XmlObject;
19  
20  import com.eviware.soapui.config.RequestAssertionConfig;
21  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
24  import com.eviware.soapui.model.iface.SubmitContext;
25  import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
26  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
27  import com.eviware.soapui.model.testsuite.Assertable;
28  import com.eviware.soapui.model.testsuite.AssertionError;
29  import com.eviware.soapui.model.testsuite.AssertionException;
30  import com.eviware.soapui.model.testsuite.RequestAssertion;
31  import com.eviware.soapui.model.testsuite.ResponseAssertion;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.types.StringToStringMap;
34  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
35  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
36  import com.eviware.x.form.XForm;
37  import com.eviware.x.form.XFormDialog;
38  import com.eviware.x.form.XFormDialogBuilder;
39  import com.eviware.x.form.XFormFactory;
40  
41  /***
42   * Assertion that checks for the non-existence of a specified text token in the associated 
43   * WsdlTestRequests response message
44   * 
45   * @author Ole.Matzura
46   */
47  
48  public class SimpleNotContainsAssertion extends WsdlMessageAssertion implements RequestAssertion, ResponseAssertion
49  {
50     private String token;
51  	private boolean ignoreCase;
52  	private XFormDialog dialog;
53  	private boolean useRegEx;
54  	public static final String ID = "Simple NotContains";
55  	private static final String CONTENT = "Content";
56  	private static final String IGNORE_CASE = "Ignore Case";
57  	private static final String USE_REGEX = "Regular Expression";
58  
59     public SimpleNotContainsAssertion(RequestAssertionConfig assertionConfig, Assertable assertable)
60     {
61        super(assertionConfig, assertable, true, true, true, true);
62        
63        XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
64        token = reader.readString( "token", null );
65        ignoreCase = reader.readBoolean( "ignoreCase", false );
66        useRegEx = reader.readBoolean( "useRegEx", false );
67     }
68     
69     public String internalAssertResponse(WsdlMessageExchange messageExchange, SubmitContext context) throws AssertionException
70     {
71     	return assertContent( context, messageExchange.getResponseContent(), "Response" );
72     }
73  
74  	private String assertContent( SubmitContext context, String content, String type ) throws AssertionException
75  	{
76  		if( token == null ) token = "";
77  
78        String replToken = PropertyExpansionUtils.expandProperties( context, token );
79        
80        if( replToken.length() > 0 )
81        {
82        	int ix = -1;
83        	
84        	if( useRegEx )
85        	{
86        		if( content.matches( replToken ))
87        			ix = 0;
88        	}
89        	else
90        	{
91  	      	ix = ignoreCase ?
92  	      				content.toUpperCase().indexOf( replToken.toUpperCase() ) : content.indexOf( replToken );
93        	}
94           		
95           if( ix != -1 )
96           	throw new AssertionException( new AssertionError( type + " contains token [" + replToken + "]") );
97        }
98        
99        return type + " does not contain token [" + replToken + "]";
100 	}
101 
102    public boolean configure()
103    {
104    	if( dialog == null )
105    		buildDialog();
106    	
107    	StringToStringMap values = new StringToStringMap();
108    	values.put( CONTENT, token );
109    	values.put( IGNORE_CASE, ignoreCase );
110    	values.put( USE_REGEX, useRegEx );
111    	
112    	values = dialog.show( values );
113    	if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
114    	{
115    		token = values.get( CONTENT );
116    		ignoreCase = values.getBoolean( IGNORE_CASE );
117    		useRegEx = values.getBoolean( USE_REGEX );
118    	}
119       
120       setConfiguration( createConfiguration() );
121       return true;
122    }
123 
124    protected XmlObject createConfiguration()
125    {
126    	XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
127    	builder.add( "token", token );
128    	builder.add( "ignoreCase", ignoreCase );
129    	builder.add( "useRegEx", useRegEx );
130    	return builder.finish();
131    }
132    
133    private void buildDialog()
134 	{
135 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder("NotContains Assertion");
136 		XForm mainForm = builder.createForm( "Basic" );
137 		
138 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
139 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
140 		mainForm.addCheckBox( USE_REGEX, "Use token as Regular Expression" );
141 		
142 		dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.SIMPLE_NOT_CONTAINS_HELP_URL ), 
143       		"Specify options", UISupport.OPTIONS_ICON );		
144 	}
145 
146 	protected String internalAssertRequest( WsdlMessageExchange messageExchange, SubmitContext context ) throws AssertionException
147 	{
148       return assertContent( context, messageExchange.getRequestContent(), "Request" );
149 	}
150 
151 	public boolean isIgnoreCase()
152 	{
153 		return ignoreCase;
154 	}
155 
156 	public void setIgnoreCase( boolean ignoreCase )
157 	{
158 		this.ignoreCase = ignoreCase;
159 		setConfiguration( createConfiguration() );
160 	}
161 
162 	public String getToken()
163 	{
164 		return token;
165 	}
166 
167 	public void setToken( String token )
168 	{
169 		this.token = token;
170 		setConfiguration( createConfiguration() );
171 	}
172 	
173 	public PropertyExpansion[] getPropertyExpansions()
174 	{
175 		List<PropertyExpansion> result = new ArrayList<PropertyExpansion>();
176 		
177 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( getAssertable().getModelItem(), this, "token") );
178 		
179 		return result.toArray( new PropertyExpansion[result.size()] );
180 	}
181 }