Unit Testing approach for policies with transformation code

In some of our policies we are doing transformations which are written in Javascript. These transforms for ex. cleanup/fix names of resource properties, apply formatting logic etc.

Is there a way to unit test this transform javascript code. Is there a test harness which takes request or response (for inbound and outbound), execute the Javascript transform logic and write assertions.

Can we make use of Qunit, Mocha or Jasmine for writing these unit tests?

Solved Solved
1 7 777
1 ACCEPTED SOLUTION

Not applicable

Hi Vineet, running unit tests of JSC policies directly the on MP is currently not supported. However, we'll be releasing a jsc Debugger to be executed locally that could serve for that purpose down the road, release date is not available yet though. I'll update this question when it's available.

That being said, we do support functional testing of API responses/transformations with the tools you mentioned above with Apigee Deploy Grunt Plugin, you can find an example of the weather API with tests and assertions with mocha on the response content. Supporting Jasmine or QUnit in Grunt would be just a matter of adding them to Grunt as an additional target and including js files under test folder.

Hope it helps!

View solution in original post

7 REPLIES 7

Not applicable

Hi Vineet, running unit tests of JSC policies directly the on MP is currently not supported. However, we'll be releasing a jsc Debugger to be executed locally that could serve for that purpose down the road, release date is not available yet though. I'll update this question when it's available.

That being said, we do support functional testing of API responses/transformations with the tools you mentioned above with Apigee Deploy Grunt Plugin, you can find an example of the weather API with tests and assertions with mocha on the response content. Supporting Jasmine or QUnit in Grunt would be just a matter of adding them to Grunt as an additional target and including js files under test folder.

Hope it helps!

Thanks @dzuluaga.

The tests and assertions for the weather sample api are end-to-end (integration tests). Is there any example which shows writing "unit test" for JSC policies. I tried to write one. But when I try to run chai/sinon I am getting "ReferenceError: response is not defined". Is there a way to create "test double" (mock) for "response" and other context variables?

/usr/local/bin/node /usr/local/lib/node_modules/grunt-cli/bin/grunt --gruntfile /apigee/grunt/apis/Gruntfile.js --env=test --debug executeTests
Running "executeTests" task
[D] Task source: grunt/tasks/executeTests.js


Running "mochaTest:test" (mochaTest) task
[D] Task source: /apigee/grunt/apis/node_modules/grunt-mocha-test/tasks/mocha-test.js
>> Mocha exploded!
>> ReferenceError: response is not defined
>>     at Object.<anonymous> (/apigee/grunt/apis/apiproxy/resources/jsc/xxx-Response.js:27:18)
>>     at Module._compile (module.js:460:26)
>>     at Object.Module._extensions..js (module.js:478:10)
>>     at Module.load (module.js:355:32)
>>     at Function.Module._load (module.js:310:12)
>>     at Module.require (module.js:365:17)
>>     at require (module.js:384:17)
>>     at Object.<anonymous> (/apigee/grunt/apis/tests/apis-xxx-Response-test.js:2:27)
>>     at Module._compile (module.js:460:26)
>>     at Object.Module._extensions..js (module.js:478:10)
Warning: Task "mochaTest:test" failed. Use --force to continue.


Aborted due to warnings.

Not applicable

I've seen this error when for some reason the signature of the request callback function is incorrect. The signature should be (error, response, body). If you like, send me the mocha test script as an attachment and I can try to spot the error.

As for unit tests with JSC policies, I can investigate it further. @David Allen, you may shed some light on it.

  describe('Check weather in cities', function() {
      async.each(weatherData.simpleWeatherArray() , function(cityData, callback) {
        it('you should be able to get forecast weather for ' + cityData.name + ' from this API Proxy.', function(done) {
           var options = {
                    url: cityData.url, //'https://testmyapi-test.apigee.net/weathergrunt/apigee/forecastrss?w=2502265',
                    headers: {
                      'User-Agent': 'request'
                    }
           }
            request(options, function (error, response, body) {
                expect(body).to.contain(cityData.name) //Sunnyvale, Madrid
                assert.equal(cityData.responseCode, response.statusCode)
                done()
              })
            })
        callback();
    });
  });

@Vineet Bhatia - take a look at Sparrow.js. It's a light weight test framework built on top of Mocha.

Thanks @dzuluaga we are really looking for unit testing of JSC policies. Integration testing with Chaijs works great but we have a lot of transformation code in javascript which needs to be unit tested.

I don't know how to make response.content.asXML, response.content.asJSON, context.getVariable(), etc work from within a unit test. Is there a way to mock these variables? I have been able to create test-double using xml2js npm module instead of using apigee xml2json policy. But thats not something we would prefer to do as there could be differences between how "apigee xml2json policy" and "xml2js npm module" convert xml to json.

Not applicable

Vineet,

When faced with a similar challenge I coded up a framework for debugging and unit testing JSC callouts offline. Drop me an email (dallen@apigee.com) and we can discuss off line. This framework is not generally available, but if interested you could assist with beta testing it.

Regards,

David

@David Allen Sent you email with what I have tried so far. Looking forward to hearing back.