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 a specified text token in the associated
44   * WsdlTestRequests response XML message
45   * 
46   * @author Ole.Matzura
47   */
48  
49  public class SimpleContainsAssertion extends WsdlMessageAssertion implements RequestAssertion,
50  			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 = PropertyExpansionUtils.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
96  						.indexOf( replToken );
97  			}
98  
99  			if( ix == -1 )
100 				throw new AssertionException( new AssertionError( "Missing token [" + replToken + "] in " + type ) );
101 		}
102 		
103 		return "Response contains token [" + replToken + "]";
104 	}
105 
106 	public boolean configure()
107 	{
108 		if( dialog == null )
109 			buildDialog();
110 
111 		StringToStringMap values = new StringToStringMap();
112 		values.put( CONTENT, token );
113 		values.put( IGNORE_CASE, ignoreCase );
114 		values.put( USE_REGEX, useRegEx );
115 
116 		values = dialog.show( values );
117 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
118 		{
119 			token = values.get( CONTENT );
120 			ignoreCase = values.getBoolean( IGNORE_CASE );
121 			useRegEx = values.getBoolean( USE_REGEX );
122 		}
123 
124 		setConfiguration( createConfiguration() );
125 		return true;
126 	}
127 
128 	public boolean isIgnoreCase()
129 	{
130 		return ignoreCase;
131 	}
132 
133 	public void setIgnoreCase( boolean ignoreCase )
134 	{
135 		this.ignoreCase = ignoreCase;
136 		setConfiguration( createConfiguration() );
137 	}
138 
139 	public String getToken()
140 	{
141 		return token;
142 	}
143 
144 	public void setToken( String token )
145 	{
146 		this.token = token;
147 		setConfiguration( createConfiguration() );
148 	}
149 
150 	protected XmlObject createConfiguration()
151 	{
152 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
153 		builder.add( "token", token );
154 		builder.add( "ignoreCase", ignoreCase );
155 		builder.add( "useRegEx", useRegEx );
156 		return builder.finish();
157 	}
158 
159 	private void buildDialog()
160 	{
161 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Contains Assertion" );
162 		XForm mainForm = builder.createForm( "Basic" );
163 
164 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
165 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
166 		mainForm.addCheckBox( USE_REGEX, "Use token as Regular Expression" );
167 
168 		dialog = builder.buildDialog( builder
169 					.buildOkCancelHelpActions( HelpUrls.SIMPLE_CONTAINS_HELP_URL ), "Specify options",
170 					UISupport.OPTIONS_ICON );
171 	}
172 
173 	@Override
174 	protected String internalAssertRequest( MessageExchange messageExchange,
175 				SubmitContext context ) throws AssertionException
176 	{
177 		return assertContent( context, messageExchange.getRequestContent(), "Request" );
178 	}
179 	
180 	public PropertyExpansion[] getPropertyExpansions()
181 	{
182 		List<PropertyExpansion> result = new ArrayList<PropertyExpansion>();
183 		
184 		result.addAll( PropertyExpansionUtils.extractPropertyExpansions( getAssertable().getModelItem(), this, "token") );
185 		
186 		return result.toArray( new PropertyExpansion[result.size()] );
187 	}
188 	
189 	public static class Factory extends AbstractTestAssertionFactory
190 	{
191 		public Factory()
192 		{
193 			super(SimpleContainsAssertion.ID, SimpleContainsAssertion.LABEL, SimpleContainsAssertion.class);
194 		}
195 	}
196 }