View Javadoc

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