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