Skip to content

Commit

Permalink
[rewrite] #1281 wip more work on match, assign xpath / jsonpath
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 3, 2020
1 parent 9e29468 commit d44b9ef
Show file tree
Hide file tree
Showing 17 changed files with 708 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public static int wrappedLinesEstimate(String text, int colWidth) {
return estimate;
}

// TODO remove js function utils
private static final Pattern FUNCTION_PATTERN = Pattern.compile("^function[^(]*\\(");

public static boolean isJavaScriptFunction(String text) {
Expand Down
62 changes: 61 additions & 1 deletion karate-core2/src/main/java/com/intuit/karate/data/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,28 @@
package com.intuit.karate.data;

import com.intuit.karate.FileUtils;
import com.intuit.karate.ScriptValue;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.json.JsonSmartJsonProvider;
import com.jayway.jsonpath.spi.mapper.JsonSmartMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import de.siegmar.fastcsv.writer.CsvWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

/**
*
Expand Down Expand Up @@ -68,7 +80,7 @@ public Set<Option> options() {
}
});
}

public static boolean isJson(String s) {
if (s == null || s.isEmpty()) {
return false;
Expand Down Expand Up @@ -101,4 +113,52 @@ public static Object fromJsonString(String json) {
return JSONValue.parse(json);
}

public static Map<String, Object> fromYaml(String raw) {
Yaml yaml = new Yaml(new SafeConstructor());
return yaml.load(raw);
}

public static List<Map> fromCsv(String raw) {
CsvReader reader = new CsvReader();
reader.setContainsHeader(true);
try {
CsvContainer csv = reader.read(new StringReader(raw));
List<Map> rows = new ArrayList(csv.getRowCount());
for (CsvRow row : csv.getRows()) {
rows.add(row.getFieldMap());
}
return rows;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static String toCsv(List<Map<String, Object>> list) {
List<String[]> csv = new ArrayList(list.size() + 1);
// header row
boolean first = true;
for (Map<String, Object> map : list) {
int colCount = map.size();
if (first) {
Set<String> keys = map.keySet();
csv.add(keys.toArray(new String[colCount]));
first = false;
}
String[] row = new String[colCount];
List cols = new ArrayList(map.values());
for (int i = 0; i < colCount; i++) {
row[i] = new ScriptValue(cols.get(i)).getAsString();
}
csv.add(row);
}
CsvWriter csvWriter = new CsvWriter();
StringWriter sw = new StringWriter();
try {
csvWriter.write(sw, csv);
} catch (Exception e) {
throw new RuntimeException(e);
}
return sw.toString();
}

}
11 changes: 9 additions & 2 deletions karate-core2/src/main/java/com/intuit/karate/graal/JsValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public JsValue(Value v) {
value = v.as(Object.class);
type = Type.OTHER;
}
} else if (v.isHostObject()) { // pojo
value = v.asHostObject();
type = Type.OTHER;
} else if (v.canExecute()) {
value = v.as(Object.class);
type = Type.FUNCTION;
Expand Down Expand Up @@ -94,7 +97,7 @@ public Object getValue() {
return value;
}

public <T> T getValue(Class<T> clazz) {
public <T> T getOriginalAs(Class<T> clazz) {
return original.as(clazz);
}

Expand All @@ -121,7 +124,7 @@ public boolean isObject() {
public boolean isArray() {
return type == Type.ARRAY;
}

public boolean isTrue() {
if (type != Type.OTHER || !Boolean.class.equals(value.getClass())) {
return false;
Expand All @@ -133,6 +136,10 @@ public boolean isFunction() {
return type == Type.FUNCTION;
}

public boolean isOther() {
return type == Type.OTHER;
}

public String asString() {
return original.asString();
}
Expand Down
6 changes: 3 additions & 3 deletions karate-core2/src/main/java/com/intuit/karate/match/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class Match {
VALIDATORS.put("null", v -> v.isNull() ? PASS : fail("not null"));
VALIDATORS.put("number", v -> v.isNumber() ? PASS : fail("not a number"));
VALIDATORS.put("object", v -> v.isMap() ? PASS : fail("not an object or map"));
VALIDATORS.put("present", v -> PASS); // remaining logic in MatchOperation
VALIDATORS.put("notpresent", v -> fail("present")); // remaining logic in MatchOperation
VALIDATORS.put("string", v -> v.isString() ? PASS : fail("not a string"));
VALIDATORS.put("present", v -> v.isNotPresent() ? fail("not present") : PASS);
VALIDATORS.put("notpresent", v -> v.isNotPresent() ? PASS : fail("present"));
VALIDATORS.put("string", v -> v.isNotPresent() ? fail("not present") : v.isString() ? PASS : fail("not a string"));
VALIDATORS.put("uuid", v -> {
if (!v.isString()) {
return fail("not a string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public MatchOperation(MatchContext context, MatchType type, MatchValue actual, M
if (context == null) {
this.failures = new ArrayList();
if (actual.isXml()) {
this.context = new MatchContext(this, true, 0, "/", "/", -1);
this.context = new MatchContext(this, true, 0, "/", "", -1);
} else {
this.context = new MatchContext(this, false, 0, "$", "", -1);
}
Expand Down Expand Up @@ -298,9 +298,13 @@ private boolean macroEqualsExpected(String expStr) {
} else {
Validator validator = Match.VALIDATORS.get(validatorName);
if (validator != null) {
ValidatorResult vr = validator.apply(actual);
if (!vr.pass) {
return fail(vr.message);
if (optional && actual.isNotPresent()) {
// pass
} else {
ValidatorResult vr = validator.apply(actual);
if (!vr.pass) {
return fail(vr.message);
}
}
} else { // validator part was not used
macro = validatorName + macro;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public boolean isXml() {
return type == Type.XML;
}

public boolean isNotPresent() {
return "#notpresent".equals(value);
}

public boolean isMapOrListOrXml() {
switch (type) {
case MAP:
Expand Down
Loading

0 comments on commit d44b9ef

Please sign in to comment.