View Javadoc

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