Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
* |
17 |
*/ |
18 |
|
19 |
package org.apache.jmeter.functions; |
20 |
|
21 |
import java.util.ArrayList; |
22 |
import java.util.Collection; |
23 |
import java.util.LinkedList; |
24 |
import java.util.List; |
25 |
import java.util.concurrent.ThreadLocalRandom; |
26 |
|
27 |
import org.apache.commons.lang3.StringUtils; |
28 |
import org.apache.jmeter.engine.util.CompoundVariable; |
29 |
import org.apache.jmeter.samplers.SampleResult; |
30 |
import org.apache.jmeter.samplers.Sampler; |
31 |
import org.apache.jmeter.threads.JMeterVariables; |
32 |
import org.apache.jmeter.util.JMeterUtils; |
33 |
import org.apache.jorphan.logging.LoggingManager; |
34 |
import org.apache.log.Logger; |
35 |
|
36 |
/** |
37 |
* Provides a RandomFromMultiResult function which returns a random element from a multi valued extracted variable. |
38 |
* Those kind of variable are extracted by: |
39 |
* - Regular Expression extractor |
40 |
* - JSON PATH extractor |
41 |
* - CSS/JQuery extractor |
42 |
* - XPath Extractor |
43 |
* |
44 |
* @since 3.1 |
45 |
*/ |
46 |
public class RandomFromMultipleVars extends AbstractFunction { |
47 |
private static final Logger log = LoggingManager.getLoggerForClass(); |
48 |
|
49 |
private static final List<String> desc = new LinkedList<>(); |
50 |
private static final String KEY = "__RandomFromMultipleVars"; //$NON-NLS-1$ |
51 |
private static final String SEPARATOR = "\\|"; //$NON-NLS-1$ |
52 |
static { |
53 |
desc.add(JMeterUtils.getResString("random_multi_result_source_variable")); //$NON-NLS-1$ |
54 |
desc.add(JMeterUtils.getResString("random_multi_result_target_variable")); //$NON-NLS-1$ |
55 |
} |
56 |
|
57 |
private CompoundVariable variablesNamesSplitBySeparator; |
58 |
private CompoundVariable varName; |
59 |
|
60 |
/** |
61 |
* No-arg constructor. |
62 |
*/ |
63 |
public RandomFromMultipleVars() { |
64 |
} |
65 |
|
66 |
/** {@inheritDoc} */ |
67 |
@Override |
68 |
public String execute(SampleResult previousResult, Sampler currentSampler) |
69 |
throws InvalidVariableException { |
70 |
|
71 |
String variablesNamesSplitBySeparatorValue = variablesNamesSplitBySeparator.execute().trim(); |
72 |
JMeterVariables vars = getVariables(); |
73 |
String outputValue = ""; |
74 |
String separator = ""; |
75 |
if (vars != null) { // vars will be null on TestPlan |
76 |
List<String> results = new ArrayList<>(); |
77 |
String[] variables = variablesNamesSplitBySeparatorValue.split(SEPARATOR); |
78 |
for (String varName : variables) { |
79 |
if(!StringUtils.isEmpty(varName)) { |
80 |
extractVariableValuesToList(varName, vars, results); |
81 |
} |
82 |
} |
83 |
|
84 |
if(results.size() > 0) { |
85 |
int randomIndex = ThreadLocalRandom.current().nextInt(0, results.size()); |
86 |
outputValue = results.get(randomIndex); |
87 |
} else { |
88 |
if(log.isDebugEnabled()) { |
89 |
log.debug("RandomFromMultiResult didn't find <var>_matchNr in variables :'"+variablesNamesSplitBySeparatorValue |
90 |
+"' using separator:'"+separator+"', will return empty value"); |
91 |
} |
92 |
} |
93 |
|
94 |
if (varName != null) { |
95 |
final String varTrim = varName.execute().trim(); |
96 |
if (varTrim.length() > 0){ |
97 |
vars.put(varTrim, outputValue); |
98 |
} |
99 |
} |
100 |
} |
101 |
return outputValue; |
102 |
|
103 |
} |
104 |
|
105 |
/** |
106 |
* @param variableName String |
107 |
* @param vars {@link JMeterVariables} |
108 |
* @param results {@link List} where results are stored |
109 |
* @throws NumberFormatException |
110 |
*/ |
111 |
private void extractVariableValuesToList(String variableName, |
112 |
JMeterVariables vars, List<String> results) |
113 |
throws NumberFormatException { |
114 |
String matchNumberAsStr = vars.get(variableName+"_matchNr"); |
115 |
if(!StringUtils.isEmpty(matchNumberAsStr)) { |
116 |
int matchNumber = Integer.parseInt(matchNumberAsStr); |
117 |
for (int i = 1; i <= matchNumber; i++) { |
118 |
results.add(vars.get(variableName+"_"+i)); |
119 |
} |
120 |
} else { |
121 |
String value = vars.get(variableName); |
122 |
if(!StringUtils.isEmpty(value)) { |
123 |
results.add(vars.get(variableName)); |
124 |
} |
125 |
} |
126 |
} |
127 |
|
128 |
/** {@inheritDoc} */ |
129 |
@Override |
130 |
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { |
131 |
checkParameterCount(parameters, 1, 2); |
132 |
Object[] values = parameters.toArray(); |
133 |
variablesNamesSplitBySeparator = (CompoundVariable) values[0]; |
134 |
if (values.length>1){ |
135 |
varName = (CompoundVariable) values[1]; |
136 |
} |
137 |
} |
138 |
|
139 |
/** {@inheritDoc} */ |
140 |
@Override |
141 |
public String getReferenceKey() { |
142 |
return KEY; |
143 |
} |
144 |
|
145 |
/** {@inheritDoc} */ |
146 |
@Override |
147 |
public List<String> getArgumentDesc() { |
148 |
return desc; |
149 |
} |
150 |
|
151 |
} |