Skip to content

Commit

Permalink
chore: Support Object type value in create variables
Browse files Browse the repository at this point in the history
- Test variables support any type of value, make sure that the create-variables action DSL can set any value instead of just String
  • Loading branch information
christophd committed Jan 30, 2025
1 parent 5ce05fa commit 3985d3d
Showing 1 changed file with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class CreateVariablesAction extends AbstractTestAction {

/** New variables to set */
private final Map<String, String> variables;
private final Map<String, Object> variables;

/** Logger */
private static final Logger logger = LoggerFactory.getLogger(CreateVariablesAction.class);
Expand All @@ -51,18 +51,20 @@ private CreateVariablesAction(Builder builder) {

@Override
public void doExecute(TestContext context) {
for (Entry<String, String> entry : variables.entrySet()) {
for (Entry<String, Object> entry : variables.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
Object value = entry.getValue();

if (value.startsWith("script:<")) {
String scriptEngine = value.substring("script:<".length(), value.indexOf('>'));
value = VariableUtils.getValueFromScript(scriptEngine,
context.replaceDynamicContentInString(value.substring(value.indexOf('>') + 1)));
}
if (value instanceof String stringValue) {
if (stringValue.startsWith("script:<")) {
String scriptEngine = stringValue.substring("script:<".length(), stringValue.indexOf('>'));
stringValue = VariableUtils.getValueFromScript(scriptEngine,
context.replaceDynamicContentInString(stringValue.substring(stringValue.indexOf('>') + 1)));
}

//check if value is variable or function (and resolve it if yes)
value = context.replaceDynamicContentInString(value);
//check if value is variable or function (and resolve it if yes)
value = context.replaceDynamicContentInString(stringValue);
}

logger.info("Setting variable: " + key + " to value: " + value);

Expand All @@ -74,7 +76,7 @@ public void doExecute(TestContext context) {
* Gets the variables.
* @return the variables
*/
public Map<String, String> getVariables() {
public Map<String, Object> getVariables() {
return variables;
}

Expand All @@ -83,9 +85,9 @@ public Map<String, String> getVariables() {
*/
public static final class Builder extends AbstractTestActionBuilder<CreateVariablesAction, Builder> {

private final Map<String, String> variables = new LinkedHashMap<>();
private final Map<String, Object> variables = new LinkedHashMap<>();

public static Builder createVariable(String variableName, String value) {
public static Builder createVariable(String variableName, Object value) {
Builder builder = new Builder();
builder.variable(variableName, value);
return builder;
Expand All @@ -95,7 +97,7 @@ public static Builder createVariables() {
return new Builder();
}

public Builder variable(String variableName, String value) {
public Builder variable(String variableName, Object value) {
this.variables.put(variableName, value);
return this;
}
Expand Down

0 comments on commit 3985d3d

Please sign in to comment.