View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.panels.request.components.editor.views.source;
    14
    15  import javax.swing.JPopupMenu;
    16  import javax.swing.JSeparator;
    17
    18  import com.eviware.soapui.impl.wsdl.WsdlInterface;
    19  import com.eviware.soapui.impl.wsdl.WsdlOperation;
    20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
    21  import com.eviware.soapui.impl.wsdl.actions.request.AddWSSUsernameTokenAction;
    22  import com.eviware.soapui.impl.wsdl.actions.request.AddWSTimestampAction;
    23  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
    24  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResponseMessageExchange;
    25  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResultMessageExchange;
    26  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditor;
    27  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditorView;
    28  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.support.ValidationError;
    29  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.registry.RequestEditorViewFactory;
    30  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.registry.ResponseEditorViewFactory;
    31  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
    32  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
    33  import com.eviware.soapui.impl.wsdl.teststeps.WsdlResponseMessageExchange;
    34  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
    35  import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
    36  import com.eviware.soapui.model.ModelItem;
    37  import com.eviware.soapui.support.xml.JXEditTextArea;
    38
    39  /***
    40   * Factory for default "XML" source editor view in soapUI
    41   *
    42   * @author ole.matzura
    43   */
    44
    45  public class XmlSourceEditorFactory implements ResponseEditorViewFactory, RequestEditorViewFactory
    46  {
    47  	public static final String VIEW_ID = "Source";
    48
    49  	public XmlEditorView createEditorView(XmlEditor editor)
    50  	{
    51  		return new XmlSourceEditorView( editor );
    52  	}
    53
    54  	public String getViewId()
    55  	{
    56  		return VIEW_ID;
    57  	}
    58
    59  	public XmlEditorView createRequestEditorView( XmlEditor editor, ModelItem modelItem )
    60  	{
    61  		if( modelItem instanceof WsdlRequest )
    62  		{
    63  			return new WsdlRequestXmlSourceEditor( editor, ( WsdlRequest ) modelItem );
    64  		}
    65  		else if( modelItem instanceof WsdlMockResponse )
    66  		{
    67  			return new WsdlMockRequestXmlSourceEditor( editor, ( WsdlMockResponse ) modelItem );
    68  		}
    69
    70  		return null;
    71  	}
    72
    73  	public XmlEditorView createResponseEditorView( XmlEditor editor, ModelItem modelItem )
    74  	{
    75  		if( modelItem instanceof WsdlRequest )
    76  		{
    77  			return new WsdlResponseXmlSourceEditor( editor, ( WsdlRequest ) modelItem );
    78  		}
    79  		else if( modelItem instanceof WsdlMockResponse )
    80  		{
    81  			return new WsdlMockResponseXmlSourceEditor( editor, ( WsdlMockResponse ) modelItem );
    82  		}
    83
    84  		return null;
    85  	}
    86
    87  	/***
    88  	 * XmlSource editor for a WsdlRequest
    89  	 *
    90  	 * @author ole.matzura
    91  	 */
    92
    93  	public static class WsdlRequestXmlSourceEditor extends XmlSourceEditorView
    94  	{
    95  		private final WsdlRequest request;
    96
    97  		public WsdlRequestXmlSourceEditor(XmlEditor xmlEditor, WsdlRequest request )
    98  		{
    99  			super(xmlEditor);
    100 			this.request = request;
    101 		}
    102
    103 		protected ValidationError[] validateXml( String xml )
    104 		{
    105 			WsdlOperation operation = (WsdlOperation) request.getOperation();
    106 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
    107
    108 			WsdlResponseMessageExchange wsdlResponseMessageExchange = new WsdlResponseMessageExchange( request );
    109 			wsdlResponseMessageExchange.setRequestContent( xml );
    110 			return validator.assertRequest( wsdlResponseMessageExchange, false );
    111 		}
    112
    113 		protected void buildPopup(JPopupMenu inputPopup, JXEditTextArea editArea )
    114 		{
    115 			super.buildPopup(inputPopup, editArea );
    116
    117 			inputPopup.insert( new JSeparator(), 2 );
    118 			inputPopup.insert(new AddWSSUsernameTokenAction(request),3);
    119 			inputPopup.insert(new AddWSTimestampAction(request),4);
    120 		}
    121 	}
    122
    123 	/***
    124 	 * XmlSource editor for a WsdlMockRequest
    125 	 *
    126 	 * @author ole.matzura
    127 	 */
    128
    129 	public static class WsdlMockRequestXmlSourceEditor extends XmlSourceEditorView
    130 	{
    131 		private final WsdlMockResponse mockResponse;
    132
    133 		public WsdlMockRequestXmlSourceEditor(XmlEditor xmlEditor, WsdlMockResponse mockResponse )
    134 		{
    135 			super(xmlEditor);
    136 			this.mockResponse = mockResponse;
    137 		}
    138
    139 		protected ValidationError[] validateXml( String xml )
    140 		{
    141 			WsdlOperation operation = mockResponse.getMockOperation().getOperation();
    142
    143 			if( operation == null )
    144 			{
    145 				return new ValidationError[] { new AssertionError( "Missing operation for MockResponse")};
    146 			}
    147
    148 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
    149 			return validator.assertRequest( new WsdlMockResultMessageExchange( mockResponse.getMockResult() ), false );
    150 		}
    151
    152 		protected void buildPopup(JPopupMenu inputPopup, JXEditTextArea editArea )
    153 		{
    154 			super.buildPopup(inputPopup, editArea );
    155 			inputPopup.insert( new JSeparator(), 2 );
    156 		}
    157 	}
    158
    159 	/***
    160 	 * XmlSource editor for a WsdlResponse
    161 	 *
    162 	 * @author ole.matzura
    163 	 */
    164
    165 	public static class WsdlResponseXmlSourceEditor extends XmlSourceEditorView
    166 	{
    167 		private final WsdlRequest request;
    168
    169 		public WsdlResponseXmlSourceEditor(XmlEditor xmlEditor, WsdlRequest request )
    170 		{
    171 			super(xmlEditor);
    172 			this.request = request;
    173 		}
    174
    175 		protected ValidationError[] validateXml( String xml )
    176 		{
    177 			if( request instanceof WsdlTestRequest )
    178 			{
    179 				WsdlTestRequest testRequest = (WsdlTestRequest)request;
    180 				testRequest.assertResponse( new WsdlTestRunContext(  testRequest.getTestStep() ));
    181 			}
    182
    183 			WsdlOperation operation = (WsdlOperation)request.getOperation();
    184 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
    185
    186 			return validator.assertResponse( new WsdlResponseMessageExchange( request ), false );
    187 		}
    188 	}
    189
    190 	/***
    191 	 * XmlSource editor for a WsdlMockResponse
    192 	 *
    193 	 * @author ole.matzura
    194 	 */
    195
    196 	public static class WsdlMockResponseXmlSourceEditor extends XmlSourceEditorView
    197 	{
    198 		private final WsdlMockResponse mockResponse;
    199
    200 		public WsdlMockResponseXmlSourceEditor(XmlEditor xmlEditor, WsdlMockResponse mockResponse)
    201 		{
    202 			super(xmlEditor);
    203 			this.mockResponse = mockResponse;
    204 		}
    205
    206 		protected ValidationError[] validateXml( String xml )
    207 		{
    208 			WsdlOperation operation = mockResponse.getMockOperation().getOperation();
    209 			if( operation == null )
    210 			{
    211 				return new ValidationError[] { new AssertionError( "Missing operation for MockResponse")};
    212 			}
    213
    214 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
    215 			return validator.assertResponse( new WsdlMockResponseMessageExchange( mockResponse ), false );
    216 		}
    217 	}
    218 }