Published

05 September 2014

Tags

Sharing

Introduction

qUnit and pavlov have been slightly modified so they can be run into a server side javascript. Modifications consist in:

  • creating a window object so the library believes it is runnning into a browser,
  • adding a renderer so the output is either in plain text, to run it in debug mode, or in xml unit format to integrate it into existing

The corresponding libraries must be loaded.

Pavlov Test script

```

<import resource="classpath:alfresco/webscripts/extension/js/org/bluedolmen/utils/Logger.js">
<import resource="classpath:alfresco/webscripts/extension/js/external/bluedolmen-qunit/src/bluedolmen-qunit-headless.js">
<import resource="classpath:alfresco/webscripts/extension/js/external/bluedolmen-qunit-pavlov.js">
<import resource="classpath:alfresco/webscripts/extension/js/external/bluedolmen-qunit/test/bluedolmen-postload.js">
<import resource="classpath:alfresco/webscripts/extension/js/external/qunit-reporter-junit.js">

<import resource="classpath:alfresco/webscripts/extension/js/external/yaml.min.js">
<import resource="classpath:alfresco/webscripts/extension/js/org/bluedolmen/alfresco/rule/AutoComplete.js">

<import resource="classpath:alfresco/webscripts/extension/js/org/bluedolmen/Base.js">

//$Logger.LEVEL.DEBUG = false;

QUnit.specify.globalApi = true;

QUnit.jUnitReport = function(data) {
    var console = logger;
    if (console) {
        console.log(data.xml);
    }
};

pavlov.specify("AutoComplete Test Set", function() {
    describe("Should be able to update a document with...", function() {

        var doc;

        function fail(msg) {
            $Logger.debug(msg);
            assert(true).equals(false);
        };

        before(function() {
            $Logger.debug("->Before");
            //doc = search.findNode("workspace://SpacesStore/0b0139b3-7cb0-49f2-8429-c21f542f1f08");
            doc = search.findNode("workspace://SpacesStore/35ceacd8-bb9e-45c0-be53-2d9844b54758");

            // Create a dataList item, a doc, a calendarEvent, a post, a page, a link...
        });

        after(function() {
            $Logger.debug("--> After");
        });

        it("must work with the provided rule", function() {
            var updatedProp = $AutoComplete.checkAndUpdate(
                doc,
                "MarchesPublics:ECategoriesDuMarche # cm:name = ${MarchesPublics:AspectMarche_libelleLong} (${MarchesPublics:AspectMarche_libelleCourt})"
            );
            $Logger.debug("UpdatedProp = " + updatedProp);
            assert(doc.properties["cm:name"]).equals(updatedProp);
        });

        it("must work with a rule from bluedolmen/dataLists path in the same site", function() {
            var updatedProp;

            var rule = $AutoComplete.getRewritesFromBlueDolmenDataLists(doc);
            $Logger.log("Main : Rule = " + rule);

            if (undefined != rule && null != rule) {
                updatedProp = $AutoComplete.update(doc, rule);
            }

            $Logger.debug("UpdatedProp = " + updatedProp);
            assert(doc.properties["cm:name"]).equals(updatedProp);
        });
    });
});

QUnit.load();

```

Triple slash '///' are replaced when scripts are copied from my workspace to alfresco webscripts extension directory. We write import statements this way otherwise SIDE doesn't recognise js and methods.

IDE Integration

Launch tests

We wrote a small library that enables us to execute js code by providing it to a webscript. We then simply call it trough the following form:

#!/bin/bash

JS_FILE=$1
ADMIN_PASSWD=admin
HOST=http://localhost:8080
QUERY=/alfresco/service/bluedolmen/javascript/executer
URL=${HOST}${QUERY}

touch ${JS_FILE}; # if not, a cache seems to be present
curl -u admin:${ADMIN_PASSWD} -F "script=@${JS_FILE}" ${URL}

The provided script is then loaded on the alfresco server and played.

We defined external scripts in SIDE, named console1.js, console2.js... which calls this previous script with the corresponding code. It allows us to always use our IDE with usual procedures to write, test and version source code.

Debugging

By looking at Alfresco output into SIDE, we can see logs and analyse variable values. While less interactive than the javascript debugger, this approach presents the advantage to be activated by an administrator who can then follow-up its content to our team to help debugging.

By selectively choosing the log level through the javascript console, it's possible to immediately have more or less information in production mode.

Conclusion

This tutorial showed you how to install your SIDE installation so you can develop and test your server side javascript in your preferred IDE. It allows you to use your toolbox the usual way, especially versioning tools.




blog comments powered by Disqus