com.consol.citrus.actions.CreateVariablesAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of citrus-base Show documentation
Show all versions of citrus-base Show documentation
Citrus base and default implementation
/*
* Copyright 2006-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.consol.citrus.actions;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.consol.citrus.AbstractTestActionBuilder;
import com.consol.citrus.context.TestContext;
import com.consol.citrus.variable.VariableUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Action creating new test variables during a test. Existing test variables are overwritten
* by new values.
*
* @author Christoph Deppisch
* @since 2006
*/
public class CreateVariablesAction extends AbstractTestAction {
/** New variables to set */
private final Map variables;
/** Logger */
private static Logger log = LoggerFactory.getLogger(CreateVariablesAction.class);
/**
* Default constructor.
*/
private CreateVariablesAction(Builder builder) {
super("create-variables", builder);
this.variables = builder.variables;
}
@Override
public void doExecute(TestContext context) {
for (Entry entry : variables.entrySet()) {
String key = entry.getKey();
String 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)));
}
//check if value is variable or function (and resolve it if yes)
value = context.replaceDynamicContentInString(value);
log.info("Setting variable: " + key + " to value: " + value);
context.setVariable(key, value);
}
}
/**
* Gets the variables.
* @return the variables
*/
public Map getVariables() {
return variables;
}
/**
* Action builder.
*/
public static final class Builder extends AbstractTestActionBuilder {
private Map variables = new LinkedHashMap<>();
public static Builder createVariable(String variableName, String value) {
Builder builder = new Builder();
builder.variable(variableName, value);
return builder;
}
public static Builder createVariables() {
return new Builder();
}
public Builder variable(String variableName, String value) {
this.variables.put(variableName, value);
return this;
}
@Override
public CreateVariablesAction build() {
return new CreateVariablesAction(this);
}
}
}