View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.panels.support.assertions.Assertable;
19  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
20  import com.eviware.soapui.impl.wsdl.submit.filters.PropertyExpansionRequestFilter;
21  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
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 );
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 	protected XmlObject createConfiguration()
104 	{
105 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
106 		builder.add( "token", token );
107 		builder.add( "ignoreCase", ignoreCase );
108 		return builder.finish();
109 	}
110 
111 	public boolean isConfigurable()
112 	{
113 		return true;
114 	}
115 
116 	private void buildDialog()
117 	{
118 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Simple Contains Assertion" );
119 		XForm mainForm = builder.createForm( "Basic" );
120 
121 		mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXT ).setWidth( 20 );
122 		mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
123 
124 		dialog = builder.buildDialog( builder
125 					.buildOkCancelHelpActions( HelpUrls.SIMPLE_CONTAINS_HELP_URL ), "Specify options",
126 					UISupport.OPTIONS_ICON );
127 	}
128 
129 	@Override
130 	protected String internalAssertRequest( WsdlMessageExchange messageExchange,
131 				SubmitContext context ) throws AssertionException
132 	{
133 		return assertContent( context, messageExchange.getRequestContent(), "Request" );
134 	}
135 }