nl.hsac.fitnesse.fixture.slim.JsonFixture Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hsac-fitnesse-fixtures Show documentation
Show all versions of hsac-fitnesse-fixtures Show documentation
Fixtures to assist in testing via FitNesse
package nl.hsac.fitnesse.fixture.slim;
import nl.hsac.fitnesse.fixture.util.DataUrlHelper;
import nl.hsac.fitnesse.fixture.util.JsonPathHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Fixture to work with JSON (strings).
*/
public class JsonFixture extends SlimFixture {
private String content;
/**
* @param filename JSON file to be loaded.
* @return true
*/
public boolean loadFile(String filename) {
content = getFileFixture().textIn(filename);
return true;
}
/**
* @param json JSON content to be loaded.
* @return true
*/
public boolean load(String json) {
content = cleanupValue(json);
return true;
}
/**
* @return formatted loaded JSON content.
*/
public String object() {
String formatted = content;
if (content != null) {
String trimmed = content.trim();
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
formatted = getEnvironment().getHtmlForJson(trimmed);
}
}
return formatted;
}
public Object jsonPath(String path) {
String jsonPath = getPathExpr(path);
return getPathHelper().getJsonPath(content, jsonPath);
}
/**
* @param baseName base of filename to generate (a number might be added to the name to make it unique).
* @param jsonPath expression to evaluate.
* @return link to created file.
*/
public String createFileFromBase64ContentOf(String baseName, String jsonPath) {
Object base64Content = jsonPath(jsonPath);
if (base64Content instanceof String) {
String base64String = (String) base64Content;
if ("".equals(base64Content)) {
throw new SlimFixtureException(false, "No content from json path: '" + getPathExpr(jsonPath) + "'");
} else {
if (DataUrlHelper.isDataUrl(base64String)) {
base64String = DataUrlHelper.getData(base64String);
}
return createFileFromBase64(baseName, base64String);
}
} else {
throw new SlimFixtureException(false, "Non string result from json path. '" + getPathExpr(jsonPath) + "' returned: " + base64Content);
}
}
public Object elementOfJsonPath(int index, String path) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy