Skip to content

Commit

Permalink
Allow property store to be populated with system properties.
Browse files Browse the repository at this point in the history
Some minor fixes for steps.
  • Loading branch information
israelcolomer committed Oct 14, 2016
1 parent 31de105 commit 10dd91a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public Multimap<String, String> headers() {
public Status status() {
return status;
}

@Override
public String toString() {
return "ResponseImpl{" +
"headers=" + headers +
", status=" + status +
", body=" + body +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,21 @@ public void move(String from, String to) throws IOException {

@When("^" + Phrases.SUBJECT + " creates \"(.+)\" in (?:a|the) temporary directory with content:$")
public void createFile(String file, String content) throws IOException {
doCreateFile(file, content);
}

@When("^" + Phrases.SUBJECT + " creates \"(.+)\" in (?:a|the) temporary directory with content as provided:$")
public void createFileWithResolvedContent(String file, String content) throws IOException {
doCreateFile(file, placeholderResolver.resolve(content));
}

public void doCreateFile(String file, String content) throws IOException {
File temporaryDirectory = getOrCreateTemporaryDirectory();

String resolvedFile = placeholderResolver.resolve(file);
String resolvedContent = placeholderResolver.resolve(content);

File fileWithinDirectory = new File(temporaryDirectory, resolvedFile);
Files.write(resolvedContent, fileWithinDirectory, Charset.defaultCharset());
Files.write(content, fileWithinDirectory, Charset.defaultCharset());
}

@When("^" + Phrases.SUBJECT + " appends \"(.+)\" in the temporary directory with content:$")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import foundation.stack.datamill.http.Method;
import foundation.stack.datamill.http.Response;
import foundation.stack.datamill.http.Status;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -25,6 +27,8 @@
* @author Israel Colomer (israelcolomer@gmail.com)
*/
public class HttpSteps {
private final static Logger logger = LoggerFactory.getLogger(HttpSteps.class);

final static String RESPONSE_KEY = "$$response";
final static String LAST_RESPONSE_BODY_KEY = "$$lastResponseBody";
final static String HEADER_KEY = "$$header";
Expand Down Expand Up @@ -75,7 +79,11 @@ public void assertStatusAndJsonResponse(int statusCode, String expectedJson) {
String actualJsonBody = (String) propertyStore.get(LAST_RESPONSE_BODY_KEY);
if (actualJsonBody != null && !actualJsonBody.isEmpty()) {
String resolvedExpectedJson = placeholderResolver.resolve(expectedJson);
assertTrue(FuzzyJsonTester.isJsonSimilarEnough(resolvedExpectedJson, actualJsonBody));
boolean similarEnough = FuzzyJsonTester.isJsonSimilarEnough(resolvedExpectedJson, actualJsonBody);
if (!similarEnough) {
logger.debug("Not similar enough expected [{}] actual [{}]", resolvedExpectedJson, actualJsonBody);
}
assertTrue(similarEnough);
} else {
fail("Response was empty when expecting a non-empty JSON body!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package foundation.stack.datamill.cucumber;

import com.jayway.jsonpath.JsonPath;
import foundation.stack.datamill.configuration.Properties;
import foundation.stack.datamill.values.Value;
import foundation.stack.datamill.security.impl.BCrypt;
import org.slf4j.Logger;
Expand All @@ -24,6 +25,7 @@ public class PlaceholderResolver {
private static final String HASHED_PLACEHOLDER_PREFIX = "blowfish:";
private static final String BASE64_PLACEHOLDER_PREFIX = "base64:";
private static final String DATE_FORMATTER_PLACEHOLDER_PREFIX = "currentTime:";
private static final String SYSTEM_PLACEHOLDER_PREFIX = "system:";

private final PropertyStore propertyStore;

Expand Down Expand Up @@ -124,6 +126,15 @@ private String resolveDateFormatterPlaceholder(String key) {
return null;
}

private String resolveSystemPlaceholder(String key) {
if (key.startsWith(SYSTEM_PLACEHOLDER_PREFIX)) {
key = key.substring(SYSTEM_PLACEHOLDER_PREFIX.length());
return Properties.fromSystem().get(key).orElse(null);
}

return null;
}

private String resolvePlaceholder(String key) {
String value = findPropertyInStore(key);
if (value != null) {
Expand All @@ -150,6 +161,11 @@ private String resolvePlaceholder(String key) {
return value;
}

value = resolveSystemPlaceholder(key);
if (value != null) {
return value;
}

int expressionStart = key.indexOf('.');
if (expressionStart < 0) {
expressionStart = key.indexOf('[');
Expand Down

0 comments on commit 10dd91a

Please sign in to comment.