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