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 a specified text token in the associated
43   * WsdlTestRequests response XML message
44   * 
45   * @author Ole.Matzura
46   */
47  
48  public class SimpleContainsAssertion extends WsdlMessageAssertion implements RequestAssertion,
49  			ResponseAssertion
50  {
51  	private String token;
52  	private XFormDialog dialog;
53  	private boolean ignoreCase;
54  	private boolean useRegEx;
55  	public static final String ID = "Simple Contains";
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  
60  	public SimpleContainsAssertion( RequestAssertionConfig assertionConfig, Assertable assertable )
61  	{
62  		super( assertionConfig, assertable, true, true, true, true );
63  
64  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
65  		token = reader.readString( "token", null );
66  		ignoreCase = reader.readBoolean( "ignoreCase", false );
67  		useRegEx = reader.readBoolean( "useRegEx", false );
68  	}
69  
70  	public String internalAssertResponse( WsdlMessageExchange messageExchange, SubmitContext context )
71  				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 )
79  			token = "";
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 ? content.toUpperCase().indexOf( replToken.toUpperCase() ) : content
94  						.indexOf( replToken );
95  			}
96  
97  			if( ix == -1 )
98  				throw new AssertionException( new AssertionError( "Missing token [" + replToken + "] in " + type ) );
99  		}
100 		
101 		return "Response contains 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 	public boolean isIgnoreCase()
127 	{
128 		return ignoreCase;
129 	}
130 
131 	public void setIgnoreCase( boolean ignoreCase )
132 	{
133 		this.ignoreCase = ignoreCase;
134 		setConfiguration( createConfiguration() );
135 	}
136 
137 	public String getToken()
138 	{
139 		return token;
140 	}
141 
142 	public void setToken( String token )
143 	{
144 		this.token = token;
145 		setConfiguration( createConfiguration() );
146 	}
147 
148 	protected XmlObject createConfiguration()
149 	{
150 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
151 		builder.add( "token", token );
152 		builder.add( "ignoreCase", ignoreCase );
153 		builder.add( "useRegEx", useRegEx );
154 		return builder.finish();
155 	}
156 
157 	private void buildDialog()
158 	{
159 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Contains Assertion" );
160 		XForm mainForm = builder.createForm( "Basic" );
161 
162 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
163 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
164 		mainForm.addCheckBox( USE_REGEX, "Use token as Regular Expression" );
165 
166 		dialog = builder.buildDialog( builder
167 					.buildOkCancelHelpActions( HelpUrls.SIMPLE_CONTAINS_HELP_URL ), "Specify options",
168 					UISupport.OPTIONS_ICON );
169 	}
170 
171 	@Override
172 	protected String internalAssertRequest( WsdlMessageExchange messageExchange,
173 				SubmitContext context ) throws AssertionException
174 	{
175 		return assertContent( context, messageExchange.getRequestContent(), "Request" );
176 	}
177 	
178 	public PropertyExpansion[] getPropertyExpansions()
179 	{
180 		List<PropertyExpansion> result = new ArrayList<PropertyExpansion>();
181 		
182 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( getAssertable().getModelItem(), this, "token") );
183 		
184 		return result.toArray( new PropertyExpansion[result.size()] );
185 	}
186 }