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