View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2010 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.soap;
14  
15  import org.apache.xmlbeans.XmlException;
16  import org.apache.xmlbeans.XmlObject;
17  
18  import com.eviware.soapui.SoapUI;
19  import com.eviware.soapui.config.TestAssertionConfig;
20  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.WsaAssertionConfiguration;
21  import com.eviware.soapui.impl.wsdl.submit.WsdlMessageExchange;
22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23  import com.eviware.soapui.impl.wsdl.support.wsa.WsaValidator;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion;
25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlMockResponseTestStep;
26  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AbstractTestAssertionFactory;
27  import com.eviware.soapui.model.iface.MessageExchange;
28  import com.eviware.soapui.model.iface.SubmitContext;
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 for verifying that WS-Addressing processing was ok
45   * 
46   * @author dragica.soldo
47   */
48  
49  public class WSARequestAssertion extends WsdlMessageAssertion implements RequestAssertion, ResponseAssertion
50  {
51  	public static final String ID = "WS-A Request Assertion";
52  	public static final String LABEL = "WS-Addressing Request";
53  	private WsaAssertionConfiguration wsaAssertionConfiguration;
54  	private boolean assertWsaAction;
55  	private boolean assertWsaTo;
56  	private boolean assertWsaReplyTo;
57  	private boolean assertWsaMessageId;
58  	// private boolean assertWsaRelatesTo;
59  	// private boolean assertReplyToRefParams;
60  	// private boolean assertFaultToRefParams;
61  	private XFormDialog dialog;
62  	private static final String ASSERT_ACTION = "wsa:Action";
63  	private static final String ASSERT_TO = "wsa:To";
64  	private static final String ASSERT_REPLY_TO = "wsa:ReplyTo";
65  	private static final String ASSERT_MESSAGE_ID = "wsa:MessageId";
66  
67  	// private static final String ASSERT_RELATES_TO = "wsa:RelatesTo";
68  	// private static final String ASSERT_REPLY_TO_REF_PARAMS =
69  	// "wsa:ReplyTo ReferenceParameters";
70  	// private static final String ASSERT_FAULT_TO_REF_PARAMS =
71  	// "wsa:FaultTo ReferenceParameters";
72  
73  	/***
74  	 * Constructor for our assertion.
75  	 * 
76  	 * @param assertionConfig
77  	 * @param modelItem
78  	 */
79  	public WSARequestAssertion( TestAssertionConfig assertionConfig, Assertable modelItem )
80  	{
81  		super( assertionConfig, modelItem, false, true, false, true );
82  
83  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( getConfiguration() );
84  		assertWsaAction = reader.readBoolean( "asertWsaAction", true );
85  		assertWsaTo = reader.readBoolean( "asertWsaTo", false );
86  		assertWsaReplyTo = reader.readBoolean( "assertWsaReplyTo", false );
87  		assertWsaMessageId = reader.readBoolean( "assertWsaMessageId", false );
88  		// assertWsaRelatesTo = reader.readBoolean("asertWsaRelatesTo", false);
89  		// assertReplyToRefParams = reader.readBoolean("assertReplyToRefParams",
90  		// false);
91  		// assertFaultToRefParams = reader.readBoolean("assertFaultToRefParams",
92  		// false);
93  		wsaAssertionConfiguration = new WsaAssertionConfiguration( assertWsaAction, assertWsaTo, assertWsaReplyTo,
94  				assertWsaMessageId, false, false, false );
95  	}
96  
97  	public static class Factory extends AbstractTestAssertionFactory
98  	{
99  		public Factory()
100 		{
101 			super( WSARequestAssertion.ID, WSARequestAssertion.LABEL, WSARequestAssertion.class,
102 					WsdlMockResponseTestStep.class );
103 		}
104 
105 		@Override
106 		public Class<? extends WsdlMessageAssertion> getAssertionClassType()
107 		{
108 			return WSARequestAssertion.class;
109 		}
110 	}
111 
112 	@Override
113 	protected String internalAssertRequest( MessageExchange messageExchange, SubmitContext context )
114 			throws AssertionException
115 	{
116 		try
117 		{
118 			new WsaValidator( ( WsdlMessageExchange )messageExchange, wsaAssertionConfiguration )
119 					.validateWsAddressingRequest();
120 		}
121 		catch( AssertionException e )
122 		{
123 			throw new AssertionException( new AssertionError( e.getMessage() ) );
124 		}
125 		catch( XmlException e )
126 		{
127 			SoapUI.logError( e );
128 			throw new AssertionException( new AssertionError(
129 					"There has been some XmlException, ws-a couldn't be validated properly." ) );
130 		}
131 
132 		return "Request WS-Addressing is valid";
133 	}
134 
135 	@Override
136 	protected String internalAssertResponse( MessageExchange messageExchange, SubmitContext context )
137 			throws AssertionException
138 	{
139 		// try
140 		// {
141 		// new WsaValidator( (WsdlMessageExchange) messageExchange,
142 		// wsaAssertionConfiguration ).validateWsAddressingResponse();
143 		// }
144 		// catch( AssertionException e )
145 		// {
146 		// throw new AssertionException( new AssertionError( e.getMessage() ) );
147 		// }
148 		// catch( XmlException e )
149 		// {
150 		// SoapUI.logError( e );
151 		// throw new AssertionException(
152 		// new AssertionError(
153 		// "There has been some XmlException, ws-a couldn't be validated properly."
154 		// ) );
155 		// }
156 
157 		// return "Response WS-Addressing is valid";
158 		return null;
159 	}
160 
161 	public boolean configure()
162 	{
163 		if( dialog == null )
164 			buildDialog();
165 
166 		StringToStringMap values = new StringToStringMap();
167 		values.put( ASSERT_ACTION, assertWsaAction );
168 		values.put( ASSERT_TO, assertWsaTo );
169 		values.put( ASSERT_REPLY_TO, assertWsaReplyTo );
170 		values.put( ASSERT_MESSAGE_ID, assertWsaMessageId );
171 		// values.put(ASSERT_RELATES_TO, assertWsaRelatesTo);
172 		// values.put(ASSERT_REPLY_TO_REF_PARAMS, assertReplyToRefParams);
173 		// values.put(ASSERT_FAULT_TO_REF_PARAMS, assertFaultToRefParams);
174 
175 		values = dialog.show( values );
176 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
177 		{
178 			assertWsaAction = values.getBoolean( ASSERT_ACTION );
179 			assertWsaTo = values.getBoolean( ASSERT_TO );
180 			assertWsaReplyTo = values.getBoolean( ASSERT_REPLY_TO );
181 			assertWsaMessageId = values.getBoolean( ASSERT_MESSAGE_ID );
182 			// assertWsaRelatesTo = values.getBoolean(ASSERT_RELATES_TO);
183 			// assertReplyToRefParams = values
184 			// .getBoolean(ASSERT_REPLY_TO_REF_PARAMS);
185 			// assertFaultToRefParams = values
186 			// .getBoolean(ASSERT_FAULT_TO_REF_PARAMS);
187 		}
188 
189 		wsaAssertionConfiguration = new WsaAssertionConfiguration( assertWsaAction, assertWsaTo, assertWsaReplyTo,
190 				assertWsaMessageId, false, false, false );
191 		setConfiguration( createConfiguration() );
192 		return true;
193 	}
194 
195 	private void buildDialog()
196 	{
197 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Ws-a properties to assert" );
198 		XForm mainForm = builder.createForm( "Basic" );
199 		mainForm.addCheckBox( ASSERT_ACTION, "Check if 'wsa:Action' exists" );
200 		mainForm.addCheckBox( ASSERT_TO, "Check if 'wsa:To' exists" );
201 		mainForm.addCheckBox( ASSERT_REPLY_TO, "Check if 'wsa:ReplyTo' exists" );
202 		mainForm.addCheckBox( ASSERT_MESSAGE_ID, "Check if 'wsa:MessageId' exists" );
203 		// mainForm.addCheckBox(ASSERT_RELATES_TO,
204 		// "Check if 'wsa:RelatesTo' exists");
205 		// mainForm.addCheckBox(ASSERT_REPLY_TO_REF_PARAMS,
206 		// "Check if 'wsa:ReplyTo' ReferenceParameters exist");
207 		// mainForm.addCheckBox(ASSERT_FAULT_TO_REF_PARAMS,
208 		// "Check if 'wsa:FaultTo' ReferenceParameters exist");
209 
210 		dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.SIMPLE_CONTAINS_HELP_URL ),
211 				"Specify options", UISupport.OPTIONS_ICON );
212 	}
213 
214 	protected XmlObject createConfiguration()
215 	{
216 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
217 		builder.add( "asertWsaAction", assertWsaAction );
218 		builder.add( "asertWsaTo", assertWsaTo );
219 		builder.add( "assertWsaReplyTo", assertWsaReplyTo );
220 		builder.add( "assertWsaMessageId", assertWsaMessageId );
221 		// builder.add("asertWsaRelatesTo", assertWsaRelatesTo);
222 		// builder.add("assertReplyToRefParams", assertReplyToRefParams);
223 		// builder.add("assertFaultToRefParams", assertFaultToRefParams);
224 		return builder.finish();
225 	}
226 
227 }