Skip to content

Commit

Permalink
parameter substitution using values from config
Browse files Browse the repository at this point in the history
  • Loading branch information
iantmoore committed Jan 12, 2017
1 parent 65a26cb commit e12f50a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Requirements
* Corrected the path to the screenshots in the report data and the final report
* Replaced Apache config with [Typesafe Config](https://github.com/typesafehub/config) - similar functionality but provides better nesting of properties, variable substitution
* Added System property switch to use original properties files over new .conf files (`-Dsubsteps.use.dot.properties=true`)
* Enable any parameters to be substituted with values from config - user ${config.expression}. Delimitters can be specified and Charset conversion too, see core-api reference.conf for details

1.0.3
-----
Expand Down
16 changes: 15 additions & 1 deletion api/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ log.unused.uncalled=false
report.data.pretty.print=false

# the base directory under which report data will be written
report.data.base.dir=target
report.data.base.dir=target

parameter{
substitution{
enabled=true
start="${"
end="}"
normalizeValue=false
normalize{
from="ISO-8859-1"
to="UTF-8"
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void initialiseParamValues(final Step step) {
final HashMap<String, String> map = new HashMap<String, String>();

final String[] paramValues = Util.getArgs(this.parent.getPattern(),
step.getLine(), null);
step.getLine(), null, Configuration.INSTANCE.getConfig());

if (paramValues != null) {
for (int i = 0; i < paramValues.length; i++) {
Expand All @@ -98,7 +98,7 @@ public void initialiseParamValues(final int lineNumber, final String line, Strin
log.debug("initialiseParamValues with line: " + line);

final String[] paramValues = Util.getArgs(this.parent.getPattern(),
line, keywordPrecedence);
line, keywordPrecedence, Configuration.INSTANCE.getConfig());

if (paramValues != null) {

Expand Down
46 changes: 42 additions & 4 deletions core/src/main/java/com/technophobia/substeps/model/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
*/
package com.technophobia.substeps.model;

import com.technophobia.substeps.model.exception.SubstepsRuntimeException;
import com.technophobia.substeps.model.parameter.Converter;
import com.technophobia.substeps.model.parameter.ConverterFactory;
import com.typesafe.config.Config;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -35,15 +38,50 @@
public final class Util {
private static final Logger log = LoggerFactory.getLogger(Util.class);

private static final boolean substituteParameters = Configuration.INSTANCE.getConfig().getBoolean("parameter.substitution.enabled");
private static final String startDelimiter = Configuration.INSTANCE.getConfig().getString("parameter.substitution.start");
private static final String endDelimiter = Configuration.INSTANCE.getConfig().getString("parameter.substitution.end");

private static final boolean normalizeValues = Configuration.INSTANCE.getConfig().getBoolean("parameter.substitution.normalizeValue");
private static final String normalizeFrom = Configuration.INSTANCE.getConfig().getString("parameter.substitution.normalize.from");
private static final String normalizeTo = Configuration.INSTANCE.getConfig().getString("parameter.substitution.normalize.to");



private Util() {
// no op
}


public static String substituteValues(String src) {


if (src != null && substituteParameters && src.startsWith(startDelimiter)){
String key = StringUtils.stripStart(StringUtils.stripEnd(src, endDelimiter), startDelimiter);
String substitute = Configuration.INSTANCE.getString(key);
if (substitute == null){
throw new SubstepsRuntimeException("Failed to resolve property " + src + " to be substituted ");
}
String normalizedValue = substitute;

if (normalizeValues) {
// This part will support the conversion of properties files containing accented characters
try {
normalizedValue = new String(substitute.getBytes(normalizeFrom), normalizeTo);
} catch (UnsupportedEncodingException e) {
log.error("error substituting accented characters", e);
}
}

return normalizedValue;
}
return src;
}


// TODO - these two methods are both used - used to be one, but now it's two
// - could they be combined ??
public static String[] getArgs(final String patternString, final String sourceString, final String[] keywordPrecedence) {
public static String[] getArgs(final String patternString, final String sourceString, final String[] keywordPrecedence, Config cfg) {

log.debug("Util getArgs String[] with pattern: " + patternString + " and sourceStr: "
+ sourceString);
Expand Down Expand Up @@ -74,7 +112,7 @@ public static String[] getArgs(final String patternString, final String sourceSt
if (matcher.find()) {

for (int i = 1; i <= groupCount; i++) {
final String arg = matcher.group(i);
final String arg = substituteValues(matcher.group(i));

if (arg != null) {
if (argsList == null) {
Expand Down Expand Up @@ -131,8 +169,8 @@ public static List<Object> getArgs(final String patternString, final String sour
if (argsList == null) {
argsList = new ArrayList<Object>();
}

argsList.add(getObjectArg(arg, parameterTypes[argIdx], converterTypes[argIdx]));
String substituted = substituteValues(arg);
argsList.add(getObjectArg(substituted, parameterTypes[argIdx], converterTypes[argIdx]));

}
argIdx++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.technophobia.substeps.runner.setupteardown.SetupAndTearDown;
import com.technophobia.substeps.stepimplementations.MockStepImplementations;
import com.technophobia.substeps.steps.TestStepImplementations;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
Expand Down Expand Up @@ -700,22 +702,32 @@ protected Map<Class<?>, Object> getImplsCache(final ExecutionNodeRunner runner)
@Test
public void testArgSubstituion() {

Config cfg = ConfigFactory.load("localhost.conf");

String parameterFromConfig = cfg.getString("users.default.name");
Assert.assertThat(parameterFromConfig, is("bob"));

final String srcString1 = "Given a substep that takes one parameter \"src1\"";
final String srcString2 = "And a substep that takes one parameter \"src2\"";
final String srcString3 = "And a substep that takes one parameter \"${users.default.name}\"";

final String patternString = "Given a substep that takes one parameter \"([^\"]*)\"";
final String[] keywordPrecedence = new String[]{"Given", "And"};
String[] args1 = Util.getArgs(patternString, srcString1, keywordPrecedence);
String[] args1 = Util.getArgs(patternString, srcString1, keywordPrecedence, cfg);


String[] args2 = Util.getArgs(patternString, srcString2, keywordPrecedence);
String[] args2 = Util.getArgs(patternString, srcString2, keywordPrecedence, cfg);

Assert.assertNotNull(args2);
Assert.assertThat(args2[0], is("src2"));

Assert.assertNotNull(args1);
Assert.assertThat(args1[0], is("src1"));

String[] args3 = Util.getArgs(patternString, srcString3, keywordPrecedence, cfg);
Assert.assertNotNull(args3);
Assert.assertThat(args3[0], is("bob"));

}

@Ignore("wip")
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/resources/localhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ some.accesskey=${?ACCESS_KEY}

some.val="https://"${some.username}":"${some.accesskey}"@ondemand.saucelabs.com:443/wd/hub"

users{
default {
name="bob"
}
}


7 changes: 7 additions & 0 deletions core/src/test/scala/org/substeps/ConfigTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.typesafe.config.ConfigFactory
*/
class ConfigTest extends FunSuite with Matchers{



test("test property substitution with env vars in conf files") {

val configVal: String = runTest
Expand Down Expand Up @@ -43,7 +45,12 @@ class ConfigTest extends FunSuite with Matchers{

val config = ConfigFactory.load(s"localhost.$ext")

println("cfg: " +
config.root().render())

val configVal = config.getString("some.val")

configVal

}
}

0 comments on commit e12f50a

Please sign in to comment.