1
2
3
4
5
6
7
8
9
10
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.PropertyExpander;
27 import com.eviware.soapui.model.propertyexpansion.PropertyExpansion;
28 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils;
29 import com.eviware.soapui.model.testsuite.Assertable;
30 import com.eviware.soapui.model.testsuite.AssertionError;
31 import com.eviware.soapui.model.testsuite.AssertionException;
32 import com.eviware.soapui.model.testsuite.RequestAssertion;
33 import com.eviware.soapui.model.testsuite.ResponseAssertion;
34 import com.eviware.soapui.support.UISupport;
35 import com.eviware.soapui.support.types.StringToStringMap;
36 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
37 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
38 import com.eviware.x.form.XForm;
39 import com.eviware.x.form.XFormDialog;
40 import com.eviware.x.form.XFormDialogBuilder;
41 import com.eviware.x.form.XFormFactory;
42
43 /***
44 * Assertion that checks for the non-existence of a specified text token in the
45 * associated WsdlTestRequests response message
46 *
47 * @author Ole.Matzura
48 */
49
50 public class SimpleNotContainsAssertion extends WsdlMessageAssertion implements RequestAssertion, ResponseAssertion
51 {
52 private String token;
53 private boolean ignoreCase;
54 private XFormDialog dialog;
55 private boolean useRegEx;
56 public static final String ID = "Simple NotContains";
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 = "Not Contains";
61
62 public SimpleNotContainsAssertion( 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
83 String replToken = PropertyExpander.expandProperties( context, token );
84
85 if( replToken.length() > 0 )
86 {
87 int ix = -1;
88
89 if( useRegEx )
90 {
91 if( content.matches( replToken ) )
92 ix = 0;
93 }
94 else
95 {
96 ix = ignoreCase ? content.toUpperCase().indexOf( replToken.toUpperCase() ) : content.indexOf( replToken );
97 }
98
99 if( ix != -1 )
100 throw new AssertionException( new AssertionError( type + " contains token [" + replToken + "]" ) );
101 }
102
103 return type + " does not contain 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 protected XmlObject createConfiguration()
129 {
130 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
131 builder.add( "token", token );
132 builder.add( "ignoreCase", ignoreCase );
133 builder.add( "useRegEx", useRegEx );
134 return builder.finish();
135 }
136
137 private void buildDialog()
138 {
139 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "NotContains Assertion" );
140 XForm mainForm = builder.createForm( "Basic" );
141
142 mainForm.addTextField( CONTENT, "Content to check for", XForm.FieldType.TEXTAREA ).setWidth( 40 );
143 mainForm.addCheckBox( IGNORE_CASE, "Ignore case in comparison" );
144 mainForm.addCheckBox( USE_REGEX, "Use token as Regular Expression" );
145
146 dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.SIMPLE_NOT_CONTAINS_HELP_URL ),
147 "Specify options", UISupport.OPTIONS_ICON );
148 }
149
150 protected String internalAssertRequest( MessageExchange messageExchange, SubmitContext context )
151 throws AssertionException
152 {
153 return assertContent( context, messageExchange.getRequestContent(), "Request" );
154 }
155
156 public boolean isIgnoreCase()
157 {
158 return ignoreCase;
159 }
160
161 public void setIgnoreCase( boolean ignoreCase )
162 {
163 this.ignoreCase = ignoreCase;
164 setConfiguration( createConfiguration() );
165 }
166
167 public String getToken()
168 {
169 return token;
170 }
171
172 public void setToken( String token )
173 {
174 this.token = token;
175 setConfiguration( createConfiguration() );
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
187 public static class Factory extends AbstractTestAssertionFactory
188 {
189 public Factory()
190 {
191 super( SimpleNotContainsAssertion.ID, SimpleNotContainsAssertion.LABEL, SimpleNotContainsAssertion.class );
192 }
193 }
194 }