Skip to content

Commit

Permalink
replaced graal-js with karate-js, all tests pass #2546
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Apr 10, 2024
1 parent 7982299 commit ba931c8
Show file tree
Hide file tree
Showing 75 changed files with 873 additions and 2,514 deletions.
15 changes: 4 additions & 11 deletions karate-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@

<properties>
<antlr.version>4.13.1</antlr.version>
<graal.version>24.0.0</graal.version>
</properties>

<dependencies>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>${graal.version}</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-language</artifactId>
<version>${graal.version}</version>
<scope>runtime</scope>
</dependency>
<groupId>io.karatelabs.js</groupId>
<artifactId>karate-js</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion karate-core/src/main/java/com/intuit/karate/Match.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package com.intuit.karate;

import com.intuit.karate.graal.JsEngine;
import com.intuit.karate.js.JsEngine;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down
37 changes: 20 additions & 17 deletions karate-core/src/main/java/com/intuit/karate/MatchOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
package com.intuit.karate;

import com.intuit.karate.graal.JsEngine;
import com.intuit.karate.graal.JsValue;
import com.intuit.karate.js.JsEngine;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -172,7 +171,7 @@ boolean execute() {
context.JS.put("_$", o);
MatchOperation mo = new MatchOperation(context.descend(i), nestedMatchType, new Match.Value(o), expected, matchEachEmptyAllowed);
mo.execute();
context.JS.bindings.removeMember("_$");
context.JS.remove("_$");
if (!mo.pass) {
return fail("match each failed at index " + i);
}
Expand Down Expand Up @@ -284,10 +283,10 @@ private boolean macroEqualsExpected(String expStr) {
}
context.JS.put("$", context.root.actual.getValue());
context.JS.put("_", actual.getValue());
JsValue jv = context.JS.eval(macro);
context.JS.bindings.removeMember("$");
context.JS.bindings.removeMember("_");
MatchOperation mo = new MatchOperation(context, nestedType, actual, new Match.Value(jv.getValue()), matchEachEmptyAllowed);
Object jv = context.JS.eval(macro);
context.JS.remove("$");
context.JS.remove("_");
MatchOperation mo = new MatchOperation(context, nestedType, actual, new Match.Value(jv), matchEachEmptyAllowed);
return mo.execute();
} else if (macro.startsWith("[")) {
int closeBracketPos = macro.indexOf(']');
Expand All @@ -307,10 +306,12 @@ private boolean macroEqualsExpected(String expStr) {
} else { // #[5] | #[$.foo]
sizeExpr = bracketContents + " == _";
}
JsValue jv = context.JS.eval(sizeExpr);
context.JS.bindings.removeMember("$");
context.JS.bindings.removeMember("_");
if (!jv.isTrue()) {
Object jv = context.JS.eval(sizeExpr);
context.JS.remove("$");
context.JS.remove("_");
if (jv instanceof Boolean && (Boolean) jv) {
// all good
} else {
return fail("actual array length is " + listSize);
}
}
Expand All @@ -331,8 +332,8 @@ private boolean macroEqualsExpected(String expStr) {
Match.Type nestedType = macroToMatchType(true, macro); // match each
int startPos = matchTypeToStartPos(nestedType);
macro = macro.substring(startPos);
JsValue jv = context.JS.eval(macro);
MatchOperation mo = new MatchOperation(context, nestedType, actual, new Match.Value(jv.getValue()), matchEachEmptyAllowed);
Object jv = context.JS.eval(macro);
MatchOperation mo = new MatchOperation(context, nestedType, actual, new Match.Value(jv), matchEachEmptyAllowed);
return mo.execute();
}
}
Expand Down Expand Up @@ -391,10 +392,12 @@ private boolean macroEqualsExpected(String expStr) {
if (macro != null && questionPos != -1) {
context.JS.put("$", context.root.actual.getValue());
context.JS.put("_", actual.getValue());
JsValue jv = context.JS.eval(macro);
context.JS.bindings.removeMember("$");
context.JS.bindings.removeMember("_");
if (!jv.isTrue()) {
Object jv = context.JS.eval(macro);
context.JS.remove("$");
context.JS.remove("_");
if (jv instanceof Boolean && (Boolean) jv) {
// all good
} else {
return fail("evaluated to 'false'");
}
}
Expand Down
64 changes: 24 additions & 40 deletions karate-core/src/main/java/com/intuit/karate/core/MockHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,15 @@
*/
package com.intuit.karate.core;

import com.intuit.karate.ScenarioActions;
import com.intuit.karate.Suite;
import com.intuit.karate.StringUtils;
import com.intuit.karate.Json;
import com.intuit.karate.KarateException;
import com.intuit.karate.graal.JsValue;
import com.intuit.karate.http.HttpClientFactory;
import com.intuit.karate.http.HttpUtils;
import com.intuit.karate.http.Request;
import com.intuit.karate.http.ResourceType;
import com.intuit.karate.http.Response;
import com.intuit.karate.http.ServerHandler;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import java.util.function.BiFunction;
import java.util.function.Function;
import com.intuit.karate.*;
import com.intuit.karate.http.*;
import io.karatelabs.js.Invokable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
*
* @author pthomas3
*/
public class MockHandler implements ServerHandler {
Expand Down Expand Up @@ -99,36 +83,36 @@ public MockHandler(String prefix, List<Feature> features, Map<String, Object> ar
corsEnabled = corsEnabled || runtime.engine.getConfig().isCorsEnabled();
globals.putAll(runtime.engine.shallowCloneVariables());
runtime.logger.info("mock server initialized: {}", feature);
scenarioRuntimes.put(feature, runtime);
scenarioRuntimes.put(feature, runtime);
});
}

public Object getVariable(String name) {
if (globals.containsKey(name)) {
Variable v = globals.get(name);
if (v != null) {
return JsValue.fromJava(v.getValue());
return v.getValue();
}
}
return null;
}

private ScenarioRuntime initRuntime(Feature feature, Map<String, Object> args) {
FeatureRuntime featureRuntime = FeatureRuntime.of(Suite.forTempUse(HttpClientFactory.DEFAULT), new FeatureCall(feature), args);
private ScenarioRuntime initRuntime(Feature feature, Map<String, Object> initArgs) {
FeatureRuntime featureRuntime = FeatureRuntime.of(Suite.forTempUse(HttpClientFactory.DEFAULT), new FeatureCall(feature), initArgs);
FeatureSection section = new FeatureSection();
section.setIndex(-1); // TODO util for creating dummy scenario
Scenario dummy = new Scenario(feature, section, -1);
section.setScenario(dummy);
ScenarioRuntime runtime = new ScenarioRuntime(featureRuntime, dummy);
runtime.logger.setLogOnly(true);
runtime.engine.setVariable(PATH_MATCHES, (Function<String, Boolean>) this::pathMatches);
runtime.engine.setVariable(PARAM_EXISTS, (Function<String, Boolean>) this::paramExists);
runtime.engine.setVariable(PARAM_VALUE, (Function<String, String>) this::paramValue);
runtime.engine.setVariable(METHOD_IS, (Function<String, Boolean>) this::methodIs);
runtime.engine.setVariable(TYPE_CONTAINS, (Function<String, Boolean>) this::typeContains);
runtime.engine.setVariable(ACCEPT_CONTAINS, (Function<String, Boolean>) this::acceptContains);
runtime.engine.setVariable(HEADER_CONTAINS, (BiFunction<String, String, Boolean>) this::headerContains);
runtime.engine.setVariable(BODY_PATH, (Function<String, Object>) this::bodyPath);
runtime.engine.setVariable(PATH_MATCHES, (Invokable) args -> this.pathMatches((String) args[0]));
runtime.engine.setVariable(PARAM_EXISTS, (Invokable) args -> this.paramExists((String) args[0]));
runtime.engine.setVariable(PARAM_VALUE, (Invokable) args-> this.paramValue((String) args[0]));
runtime.engine.setVariable(METHOD_IS, (Invokable) args -> this.methodIs((String) args[0]));
runtime.engine.setVariable(TYPE_CONTAINS, (Invokable) args -> this.typeContains((String) args[0]));
runtime.engine.setVariable(ACCEPT_CONTAINS, (Invokable) args -> this.acceptContains((String) args[0]));
runtime.engine.setVariable(HEADER_CONTAINS, (Invokable) args -> this.headerContains((String) args[0], (String) args[1]));
runtime.engine.setVariable(BODY_PATH, (Invokable) args -> this.bodyPath((String) args[0]));
runtime.engine.init();
if (feature.isBackgroundPresent()) {
// if we are within a scenario already e.g. karate.start(), preserve context
Expand All @@ -146,7 +130,7 @@ private ScenarioRuntime initRuntime(Feature feature, Map<String, Object> args) {
} finally {
ScenarioEngine.set(prevEngine);
}
}
}
return runtime;
}

Expand Down Expand Up @@ -176,7 +160,7 @@ public synchronized Response handle(Request req) { // note the [synchronized]
Feature feature = entry.getKey();
ScenarioRuntime runtime = entry.getValue();
// important for graal to work properly
Thread.currentThread().setContextClassLoader(runtime.featureRuntime.suite.classLoader);
Thread.currentThread().setContextClassLoader(runtime.featureRuntime.suite.classLoader);
LOCAL_REQUEST.set(req);
req.processBody();
ScenarioEngine engine = initEngine(runtime, globals, req);
Expand Down Expand Up @@ -242,9 +226,9 @@ public synchronized Response handle(Request req) { // note the [synchronized]
}
return new Response(404);
}

private static ScenarioEngine initEngine(ScenarioRuntime runtime, Map<String, Variable> globals, Request req) {
ScenarioEngine engine = new ScenarioEngine(runtime.engine.getConfig(), runtime, new HashMap(globals), runtime.logger);
ScenarioEngine engine = new ScenarioEngine(runtime.engine.getConfig(), runtime, new HashMap(globals), runtime.logger);
engine.init();
engine.setVariable(ScenarioEngine.REQUEST_URL_BASE, req.getUrlBase());
engine.setVariable(ScenarioEngine.REQUEST_PATH, req.getPath());
Expand Down Expand Up @@ -363,7 +347,7 @@ public Object bodyPath(String path) {
if (v.isNotPresent()) {
return null;
} else {
return JsValue.fromJava(v.getValue());
return v.getValue();
}
} else {
Json json = Json.of(body);
Expand All @@ -373,7 +357,7 @@ public Object bodyPath(String path) {
} catch (Exception e) {
return null;
}
return JsValue.fromJava(result);
return result;
}
}

Expand Down
Loading

0 comments on commit ba931c8

Please sign in to comment.