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  	public static final String ID = "Simple Contains";
47  	private static final String CONTENT = "Content";
48  	private static final String IGNORE_CASE = "Ignore Case";
49  
50  	public SimpleContainsAssertion( RequestAssertionConfig assertionConfig, Assertable assertable )
51  	{
52  		super( assertionConfig, assertable, true, true );
53  
54  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
55  		token = reader.readString( "token", null );
56  		ignoreCase = reader.readBoolean( "ignoreCase", false );
57  	}
58  
59  	public String internalAssertResponse( WsdlMessageExchange messageExchange, SubmitContext context )
60  				throws AssertionException
61  	{
62  		return assertContent( context, messageExchange.getResponseContent(), "Response" );
63  	}
64  
65  	private String assertContent( SubmitContext context, String content, String type ) throws AssertionException
66  	{
67  		if( token == null )
68  			token = "";
69  		String replToken = PropertyExpansionRequestFilter.expandProperties( context, token );
70  
71  		if( replToken.length() > 0 )
72  		{
73  			int ix = ignoreCase ? content.toUpperCase().indexOf( replToken.toUpperCase() ) : content
74  						.indexOf( replToken );
75  
76  			if( ix == -1 )
77  				throw new AssertionException( new AssertionError( "Missing token [" + replToken + "] in " + type ) );
78  		}
79  		
80  		return "Response contains token [" + replToken + "]";
81  	}
82  
83  	public boolean configure()
84  	{
85  		if( dialog == null )
86  			buildDialog();
87  
88  		StringToStringMap values = new StringToStringMap();
89  		values.put( CONTENT, token );
90  		values.put( IGNORE_CASE, ignoreCase );
91  
92  		values = dialog.show( values );
93  		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
94  		{
95  			token = values.get( CONTENT );
96  			ignoreCase = values.getBoolean( IGNORE_CASE );
97  		}
98  
99  		setConfiguration( createConfiguration() );
100 		return true;
101 	}
102 	
103 
104 	public boolean isIgnoreCase()
105 	{
106 		return ignoreCase;
107 	}
108 
109 	public void setIgnoreCase( boolean ignoreCase )
110 	{
111 		this.ignoreCase = ignoreCase;
112 		setConfiguration( createConfiguration() );
113 	}
114 
115 	public String getToken()
116 	{
117 		return token;
118 	}
119 
120 	public void setToken( String token )
121 	{
122 		this.token = token;
123 		setConfiguration( createConfiguration() );
124 	}
125 
126 	protected XmlObject createConfiguration()
127 	{
128 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
129 		builder.add( "token", token );
130 		builder.add( "ignoreCase", ignoreCase );
131 		return builder.finish();
132 	}
133 
134 	private void buildDialog()
135 	{
136 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Simple Contains Assertion" );
137 		XForm mainForm = builder.createForm( "Basic" );
138 
139 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
140 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
141 
142 		dialog = builder.buildDialog( builder
143 					.buildOkCancelHelpActions( HelpUrls.SIMPLE_CONTAINS_HELP_URL ), "Specify options",
144 					UISupport.OPTIONS_ICON );
145 	}
146 
147 	@Override
148 	protected String internalAssertRequest( WsdlMessageExchange messageExchange,
149 				SubmitContext context ) throws AssertionException
150 	{
151 		return assertContent( context, messageExchange.getRequestContent(), "Request" );
152 	}
153 }