Skip to content

Commit

Permalink
[rewrite] #1281 missed changes in previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Oct 3, 2020
1 parent d44b9ef commit 0396621
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion karate-core2/src/main/java/com/intuit/karate/data/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ public Json(String json) {
this(JsonPath.parse(json));
}

public Json(Object o) {
public Json(Map o) {
this(JsonPath.parse(o));
}

public Json(List o) {
this(JsonPath.parse(o));
}

public Json(Object o) {
this(JsonUtils.toJson(o));
}

private Json(DocumentContext doc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.intuit.karate.match;

import com.intuit.karate.XmlUtils;
import com.intuit.karate.data.Json;
import com.intuit.karate.data.JsonUtils;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -126,6 +127,14 @@ public MatchResult is(MatchType mt, Object o) {
return mo.pass ? MatchResult.PASS : MatchResult.fail(mo.getFailureReasons());
}

public MatchResult isEqualTo(Object o) {
return is(MatchType.EQUALS, o);
}

public MatchResult isEqualToJson(String s) {
return is(MatchType.EQUALS, new Json(s).asMapOrList());
}

public String getAsXmlString() {
if (type == Type.MAP) {
Node node = XmlUtils.fromMap(getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.intuit.karate.XmlUtils;
import com.intuit.karate.graal.JsValue;
import com.intuit.karate.core.Feature;
import com.intuit.karate.data.Json;
import com.intuit.karate.data.JsonUtils;
import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -183,6 +184,8 @@ public Object getValueForJsonConversion() {
return JsonUtils.fromJsonString(json);
case XML:
return XmlUtils.toObject(getValue());
case OTHER: // pojo
return new Json(value).asMapOrList();
default:
throw new RuntimeException("cannot convert to json: " + this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.intuit.karate.data;

import com.intuit.karate.match.Match;
import com.intuit.karate.runtime.SimplePojo;
import java.util.Map;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import org.slf4j.Logger;
Expand Down Expand Up @@ -28,5 +31,14 @@ void testDetect() {
assertFalse(JsonUtils.isJson(null));
assertFalse(JsonUtils.isJson(""));
}

@Test
void testBeanConversion() {
SimplePojo pojo = new SimplePojo();
String s = JsonUtils.toJson(pojo);
assertEquals("{\"bar\":0,\"foo\":null}", s);
Map<String, Object> map = new Json(pojo).asMap();
assertTrue(Match.that(map).isEqualToJson("{ foo: null, bar: 0 }").pass);
}

}

0 comments on commit 0396621

Please sign in to comment.