View Javadoc

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