Search in sources :

Example 1 with NodeJS

use of com.eclipsesource.v8.NodeJS in project mdw-designer by CenturyLinkCloud.

the class NodeRunner method run.

public void run(final TestCase testCase, DesignerDataAccess dao, boolean verbose, boolean createReplace) throws ServiceException {
    PackageVO pkgVo = null;
    NodeJS nodeJS = NodeJS.createNodeJS();
    try {
        pkgVo = dao.getPackage(NODE_PKG);
    } catch (DataAccessException e) {
        throw new ServiceException(e.getMessage());
    }
    final V8Object fileObj = new V8Object(nodeJS.getRuntime()).add("file", pkgVo.getRuleSet(RUNNER).getRawFile().getAbsolutePath());
    JavaCallback callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            return fileObj;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "getRunner");
    final Result parseResult = new Result();
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            V8Object resultObj = parameters.getObject(0);
            parseResult.status = resultObj.getString("status");
            parseResult.message = resultObj.getString("message");
            resultObj.release();
            return null;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "setParseResult");
    nodeJS.exec(pkgVo.getRuleSet(PARSER).getRawFile());
    while (nodeJS.isRunning()) {
        nodeJS.handleMessage();
    }
    fileObj.release();
    nodeJS.release();
    if (!parseResult.status.equals("OK"))
        throw new ServiceException(PARSER + parseResult);
    nodeJS = NodeJS.createNodeJS();
    final V8Object testObj = new V8Object(nodeJS.getRuntime());
    testObj.add("file", testCase.getCaseFile().getAbsolutePath());
    V8Array valueFiles = new V8Array(nodeJS.getRuntime());
    // TODO hardcoded
    valueFiles.push("localhost.env");
    testObj.add("valueFiles", valueFiles);
    valueFiles.release();
    testObj.add("resultDir", testCase.getResultDirectory().getAbsolutePath());
    final Map<String, TestCaseItem> testCaseItems = new HashMap<>();
    final V8Array testItems = new V8Array(nodeJS.getRuntime());
    for (TestCaseItem item : testCase.getItems()) {
        String itemId = item.getName();
        V8Object itemObj = new V8Object(nodeJS.getRuntime()).add("name", item.getName());
        try {
            if (item.getObject().has("request")) {
                JSONObject request = item.getObject().getJSONObject("request");
                if (request.has("method")) {
                    itemObj.add("method", request.getString("method"));
                    itemId = request.getString("method") + ":" + itemId;
                }
            }
            JSONObject options = item.getOptions() == null ? new JSONObject() : item.getOptions();
            if (verbose && !options.has("debug"))
                options.put("debug", "true");
            if (createReplace && !options.has("overwriteExpected"))
                options.put("overwriteExpected", "true");
            options.put("qualifyLocations", false);
            if (JSONObject.getNames(options) != null) {
                V8Object json = nodeJS.getRuntime().getObject("JSON");
                V8Array params = new V8Array(nodeJS.getRuntime()).push(options.toString());
                V8Object jsonObj = json.executeObjectFunction("parse", params);
                itemObj.add("options", jsonObj);
                params.release();
                json.release();
                jsonObj.release();
            }
            if (item.getValues() != null) {
                V8Object json = nodeJS.getRuntime().getObject("JSON");
                V8Array params = new V8Array(nodeJS.getRuntime()).push(item.getValues().toString());
                V8Object jsonObj = json.executeObjectFunction("parse", params);
                itemObj.add("values", jsonObj);
                params.release();
                json.release();
                jsonObj.release();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        testItems.push(itemObj);
        testCaseItems.put(itemId, item);
        itemObj.release();
    }
    testObj.add("items", testItems);
    testItems.release();
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            return testObj;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "getTestCase");
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            String itemId = parameters.getString(0);
            V8Object resultObj = parameters.getObject(1);
            if (itemId == null) {
                for (TestCaseItem item : testCase.getItems()) {
                    updateItem(item, resultObj);
                }
            }
            TestCaseItem item = testCaseItems.get(itemId);
            if (item != null) {
                updateItem(item, resultObj);
            }
            resultObj.release();
            return null;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "setTestResult");
    final V8 v8 = nodeJS.getRuntime();
    callback = new JavaCallback() {

        public Object invoke(V8Object receiver, V8Array parameters) {
            String itemId = parameters.getString(0);
            V8Object responseObj = parameters.getObject(1);
            TestCaseItem item = testCaseItems.get(itemId);
            if (item != null) {
                V8Object json = v8.getObject("JSON");
                V8Array params = new V8Array(v8).push(responseObj);
                String jsonStr = json.executeStringFunction("stringify", params);
                params.release();
                json.release();
                try {
                    item.setResponse(new JSONObject(jsonStr));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            responseObj.release();
            return null;
        }
    };
    nodeJS.getRuntime().registerJavaMethod(callback, "setTestResponse");
    nodeJS.exec(pkgVo.getRuleSet(RUNNER).getRawFile());
    while (nodeJS.isRunning()) {
        nodeJS.handleMessage();
    }
    testObj.release();
    nodeJS.release();
}
Also used : V8(com.eclipsesource.v8.V8) PackageVO(com.centurylink.mdw.model.value.process.PackageVO) HashMap(java.util.HashMap) V8Object(com.eclipsesource.v8.V8Object) JavaCallback(com.eclipsesource.v8.JavaCallback) V8Array(com.eclipsesource.v8.V8Array) JSONException(org.json.JSONException) NodeJS(com.eclipsesource.v8.NodeJS) ServiceException(com.centurylink.mdw.common.service.ServiceException) JSONObject(org.json.JSONObject) V8Object(com.eclipsesource.v8.V8Object) JSONObject(org.json.JSONObject) DataAccessException(com.centurylink.mdw.common.exception.DataAccessException)

Aggregations

DataAccessException (com.centurylink.mdw.common.exception.DataAccessException)1 ServiceException (com.centurylink.mdw.common.service.ServiceException)1 PackageVO (com.centurylink.mdw.model.value.process.PackageVO)1 JavaCallback (com.eclipsesource.v8.JavaCallback)1 NodeJS (com.eclipsesource.v8.NodeJS)1 V8 (com.eclipsesource.v8.V8)1 V8Array (com.eclipsesource.v8.V8Array)1 V8Object (com.eclipsesource.v8.V8Object)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1