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.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.TestRunContext;
    28  import com.eviware.soapui.model.testsuite.TestRunner;
    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, TestRunner testRunner, TestRunContext runContext)
    87  	{
    88  		WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
    89  		LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
    90
    91  		TestStep step = result.getTestStep();
    92
    93  		if( targetStepMatches( step ))
    94  		{
    95  			int index = step.getTestCase().getIndexOfTestStep( step );
    96
    97  			if( statisticsModel.getStatistic( index, Statistic.COUNT ) >= minRequests &&
    98  				  result.getStatus() == TestStepStatus.FAILED )
    99  			{
    100 				return returnErrorOrFail( "TestStep [" + step.getName() + "] result status is " +
    101 						result.getStatus().toString() + "; " + Arrays.toString(result.getMessages()), maxErrors,
    102 						loadTestRunner, context );
    103 			}
    104 			else
    105 				return null;
    106 		}
    107
    108 		return null;
    109 	}
    110
    111 	public String assertResults(LoadTestRunner loadTestRunner, LoadTestRunContext context, TestRunner testRunner, TestRunContext runContext)
    112 	{
    113 		return null;
    114 	}
    115
    116 	public boolean configure()
    117 	{
    118 		if( dialog == null )
    119 		{
    120 			buildDialog();
    121 		}
    122
    123 		StringToStringMap values = new StringToStringMap();
    124
    125 		values.put( NAME_FIELD, getName() );
    126 		values.put( MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ));
    127 		values.put( TEST_STEP_FIELD, getTargetStep() );
    128 		values.put( MAX_ERRORS_FIELD, String.valueOf( maxErrors ));
    129
    130 		dialog.setOptions( TestStepStatusAssertion.TEST_STEP_FIELD, getTargetStepOptions( false ) );
    131 		values = dialog.show( values );
    132
    133 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
    134 		{
    135 			try
    136 			{
    137 				minRequests = Integer.parseInt( values.get( MINIMUM_REQUESTS_FIELD ));
    138 				maxErrors = Integer.parseInt( values.get( MAX_ERRORS_FIELD ));
    139 				setName( values.get( NAME_FIELD ));
    140 				setTargetStep( values.get( TEST_STEP_FIELD ));
    141 			}
    142 			catch( Exception e )
    143 			{
    144 				UISupport.showErrorMessage( e.getMessage() );
    145 			}
    146
    147 			updateConfiguration();
    148 			return true;
    149 		}
    150
    151 		return false;
    152 	}
    153
    154 	private void buildDialog()
    155 	{
    156 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "TestStep Status Assertion" );
    157 	   XForm form = builder.createForm( "Basic" );
    158
    159 	   form.addTextField( NAME_FIELD, "Name of this assertion", FieldType.TEXT );
    160 	   form.addTextField( MINIMUM_REQUESTS_FIELD, "Minimum number of runs before asserting", FieldType.TEXT );
    161 	   form.addTextField( MAX_ERRORS_FIELD, "Maximum number of errors before failing", FieldType.TEXT );
    162 	   form.addComboBox( TEST_STEP_FIELD, new String[0], "TestStep to assert" );
    163
    164 	   dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.STEP_STATUS_LOAD_TEST_ASSERTION_HELP_URL),
    165 			"Specify options for this TestStep Status Assertion", UISupport.OPTIONS_ICON );
    166 	}
    167
    168 	protected void updateConfiguration()
    169 	{
    170 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    171
    172 		builder.add( NAME_ELEMENT, getName() );
    173 		builder.add( MIN_REQUESTS_ELEMENT, minRequests );
    174 		builder.add( TEST_STEP_ELEMENT, getTargetStep() );
    175 		builder.add( MAX_ERRORS_ELEMENT, maxErrors );
    176
    177 		setConfiguration( builder.finish() );
    178 	}
    179 }