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.loadtest.assertions;
14  
15  import java.util.Arrays;
16  
17  import org.apache.xmlbeans.XmlObject;
18  
19  import com.eviware.soapui.config.LoadTestAssertionConfig;
20  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
21  import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
22  import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
23  import com.eviware.soapui.impl.wsdl.support.Configurable;
24  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
25  import com.eviware.soapui.model.testsuite.LoadTestRunContext;
26  import com.eviware.soapui.model.testsuite.LoadTestRunner;
27  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
28  import com.eviware.soapui.model.testsuite.TestCaseRunner;
29  import com.eviware.soapui.model.testsuite.TestStep;
30  import com.eviware.soapui.model.testsuite.TestStepResult;
31  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.types.StringToStringMap;
34  import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
35  import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
36  import com.eviware.x.form.XForm;
37  import com.eviware.x.form.XFormDialog;
38  import com.eviware.x.form.XFormDialogBuilder;
39  import com.eviware.x.form.XFormFactory;
40  import com.eviware.x.form.XForm.FieldType;
41  
42  /***
43   * LoadTestAssertion for asserting the status of a teststep
44   * 
45   * @author Ole.Matzura
46   */
47  
48  public class TestStepStatusAssertion extends AbstractLoadTestAssertion implements Configurable
49  {
50  	private static final String NAME_FIELD = "Name";
51  	private static final String NAME_ELEMENT = "name";
52  	private static final String MINIMUM_REQUESTS_FIELD = "Minimum Requests";
53  	private static final String MIN_REQUESTS_ELEMENT = "min-requests";
54  	private static final String MAX_ERRORS_ELEMENT = "max-errors";
55  	private static final String MAX_ERRORS_FIELD = "Max Errors";
56  
57  	private int minRequests;
58  	private int maxErrors;
59  	private XFormDialog dialog;
60  	public static final String STEP_STATUS_TYPE = "Step Status";
61  
62  	public TestStepStatusAssertion( LoadTestAssertionConfig assertionConfig, WsdlLoadTest loadTest )
63  	{
64  		super( assertionConfig, loadTest );
65  
66  		init( assertionConfig );
67  		initIcon( "/status_loadtest_assertion.gif" );
68  	}
69  
70  	private void init( LoadTestAssertionConfig assertionConfig )
71  	{
72  		XmlObject configuration = assertionConfig.getConfiguration();
73  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( configuration );
74  
75  		setName( reader.readString( TestStepStatusAssertion.NAME_ELEMENT, "Step Status" ) );
76  		minRequests = reader.readInt( TestStepStatusAssertion.MIN_REQUESTS_ELEMENT, 0 );
77  		setTargetStep( reader.readString( TestStepStatusAssertion.TEST_STEP_ELEMENT, ANY_TEST_STEP ) );
78  		maxErrors = reader.readInt( MAX_ERRORS_ELEMENT, -1 );
79  	}
80  
81  	public String getDescription()
82  	{
83  		return "testStep: " + getTargetStep() + ", minRequests: " + minRequests + ", maxErrors: " + maxErrors;
84  	}
85  
86  	public String assertResult( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestStepResult result,
87  			TestCaseRunner testRunner, TestCaseRunContext runContext )
88  	{
89  		WsdlLoadTest loadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
90  		LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
91  
92  		TestStep step = result.getTestStep();
93  
94  		if( targetStepMatches( step ) )
95  		{
96  			int index = step.getTestCase().getIndexOfTestStep( step );
97  
98  			if( statisticsModel.getStatistic( index, Statistic.COUNT ) >= minRequests
99  					&& result.getStatus() == TestStepStatus.FAILED )
100 			{
101 				return returnErrorOrFail( "TestStep [" + step.getName() + "] result status is "
102 						+ result.getStatus().toString() + "; " + Arrays.toString( result.getMessages() ), maxErrors,
103 						loadTestRunner, context );
104 			}
105 			else
106 				return null;
107 		}
108 
109 		return null;
110 	}
111 
112 	public String assertResults( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestCaseRunner testRunner,
113 			TestCaseRunContext runContext )
114 	{
115 		return null;
116 	}
117 
118 	public boolean configure()
119 	{
120 		if( dialog == null )
121 		{
122 			buildDialog();
123 		}
124 
125 		StringToStringMap values = new StringToStringMap();
126 
127 		values.put( NAME_FIELD, getName() );
128 		values.put( MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ) );
129 		values.put( TEST_STEP_FIELD, getTargetStep() );
130 		values.put( MAX_ERRORS_FIELD, String.valueOf( maxErrors ) );
131 
132 		dialog.setOptions( TestStepStatusAssertion.TEST_STEP_FIELD, getTargetStepOptions( false ) );
133 		values = dialog.show( values );
134 
135 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
136 		{
137 			try
138 			{
139 				minRequests = Integer.parseInt( values.get( MINIMUM_REQUESTS_FIELD ) );
140 				maxErrors = Integer.parseInt( values.get( MAX_ERRORS_FIELD ) );
141 				setName( values.get( NAME_FIELD ) );
142 				setTargetStep( values.get( TEST_STEP_FIELD ) );
143 			}
144 			catch( Exception e )
145 			{
146 				UISupport.showErrorMessage( e.getMessage() );
147 			}
148 
149 			updateConfiguration();
150 			return true;
151 		}
152 
153 		return false;
154 	}
155 
156 	private void buildDialog()
157 	{
158 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Step Status Assertion" );
159 		XForm form = builder.createForm( "Basic" );
160 
161 		form.addTextField( NAME_FIELD, "Name of this assertion", FieldType.TEXT );
162 		form.addTextField( MINIMUM_REQUESTS_FIELD, "Minimum number of runs before asserting", FieldType.TEXT );
163 		form.addTextField( MAX_ERRORS_FIELD, "Maximum number of errors before failing", FieldType.TEXT );
164 		form.addComboBox( TEST_STEP_FIELD, new String[0], "TestStep to assert" );
165 
166 		dialog = builder.buildDialog( builder
167 				.buildOkCancelHelpActions( HelpUrls.STEP_STATUS_LOAD_TEST_ASSERTION_HELP_URL ),
168 				"Specify options for this Step Status Assertion", UISupport.OPTIONS_ICON );
169 	}
170 
171 	protected void updateConfiguration()
172 	{
173 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
174 
175 		builder.add( NAME_ELEMENT, getName() );
176 		builder.add( MIN_REQUESTS_ELEMENT, minRequests );
177 		builder.add( TEST_STEP_ELEMENT, getTargetStep() );
178 		builder.add( MAX_ERRORS_ELEMENT, maxErrors );
179 
180 		setConfiguration( builder.finish() );
181 	}
182 }