/**
 *
 * Requires the database and tables setup in config/environments/test.js to exist
 * Ensure the user is present in the pgbouncer auth file too
 * TODO: Add OAuth tests.
 *
 * To run this test, ensure that cartodb_test_user_1_db metadata exists
 * in Redis for the vizzuality.cartodb.com domain
 *
 * SELECT 5
 * HSET rails:users:vizzuality id 1
 * HSET rails:users:vizzuality database_name cartodb_test_user_1_db
 *
 */
require('../../helper');

var server = require('../../../app/server')();
var assert = require('../../support/assert');
var redisUtils = require('../../support/redis_utils');
var querystring = require('querystring');
var metadataBackend = require('cartodb-redis')({ pool: redisUtils.getPool() });
var batchFactory = require('../../../batch');

describe('Use case 7: cancel a job with quotes', function() {
    var batch = batchFactory(metadataBackend, redisUtils.getPool());

    before(function (done) {
        batch.start();
        batch.on('ready', done);
    });

    after(function (done) {
        batch.stop();
        redisUtils.clean('batch:*', done);
    });

    var runningJob = {};

    it('Step 1, should create job with quotes', function (done) {
        assert.response(server, {
            url: '/api/v2/sql/job?api_key=1234',
            headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
            method: 'POST',
            data: querystring.stringify({
                query: "SELECT name FROM untitle_table_4 WHERE name = 'Hawai'; select pg_sleep(3)"
            })
        }, {
            status: 201
        }, function (err, res) {
            runningJob = JSON.parse(res.body);
            done();
        });
    });

    it('Step 2, job should be running', function (done){
        var interval = setInterval(function () {
            assert.response(server, {
                url: '/api/v2/sql/job/' + runningJob.job_id + '?api_key=1234',
                headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
                method: 'GET'
            }, {
                status: 200
            }, function(err, res) {
                var job = JSON.parse(res.body);
                if (job.status === "running") {
                    clearInterval(interval);
                    done();
                }  else if (job.status === "done" || job.status === "failed" || job.status === "cancelled") {
                    clearInterval(interval);
                    done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be running'));
                }
            });
        }, 50);
    });

    it('Step 3, running job should be cancelled', function (done){
        assert.response(server, {
            url: '/api/v2/sql/job/' + runningJob.job_id + '?api_key=1234',
            headers: { 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' },
            method: 'DELETE'
        }, {
            status: 200
        }, function(err, res) {
            var cancelledJob = JSON.parse(res.body);
            assert.equal(cancelledJob.status, "cancelled");
            done();
        });
    });
});

@@ -1,503 +1,29 @@
-/**%0A *%0A * Requires the database and tables setup in config/environments/test.js to exist%0A * Ensure the user is present in the pgbouncer auth file too%0A * TODO: Add OAuth tests.%0A *%0A * To run this test, ensure that cartodb_test_user_1_db metadata exists%0A * in Redis for the vizzuality.cartodb.com domain%0A *%0A * SELECT 5%0A * HSET rails:users:vizzuality id 1%0A * HSET rails:users:vizzuality database_name cartodb_test_user_1_db%0A *%0A */%0Arequire('../../helper');%0A%0Avar server = require('../../../app/server')();
+require('../../helper');%0A
 %0Avar
@@ -69,25 +69,24 @@
 ');%0Avar 
-redisUtil
+JobStatu
 s = requ
@@ -100,246 +100,118 @@
 /../
-support/redis_utils');%0Avar querystring = require('querystring');%0Avar metadataBackend = require('cartodb-redis')(%7B pool: redisUtils.getPool() %7D);%0Avar batchFactory = require('../../../batch');%0A%0Adescribe('Use case 7: cancel a job with quotes
+../batch/job_status');%0Avar BatchTestClient = require('../../support/batch-test-client');%0A%0Adescribe('Use case 7
 ', f
@@ -221,83 +221,14 @@
 tion
+ 
 () %7B%0A
-    var batch = batchFactory(metadataBackend, redisUtils.getPool());%0A%0A
     
@@ -234,38 +234,33 @@
  before(function
- (done
+(
 ) %7B%0A        batc
@@ -259,53 +259,51 @@
     
-batch.start();%0A        batch.on('ready', done
+this.batchTestClient = new BatchTestClient(
 );%0A 
@@ -320,33 +320,32 @@
   after(function
- 
 (done) %7B%0A       
@@ -349,435 +349,128 @@
     
-batch.stop();%0A        redisUtils.clean('batch:*', done);%0A    %7D);%0A%0A    var runningJob = %7B%7D;%0A%0A    it('Step 1, should create job with quotes', function (done) %7B%0A        assert.response(server, %7B%0A            url: '/api/v2/sql/job?api_key=1234',%0A            headers: %7B 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' %7D,%0A            method: 'POST',%0A            data: querystring.stringify(%7B%0A    
+this.batchTestClient.drain(done);%0A    %7D);%0A%0A    it('cancel a job with quotes', function (done) %7B%0A        var payload = %7B%0A
     
@@ -572,61 +572,58 @@
     
-   
- %7D)%0A        %7D, %7B%0A            status: 201%0A        %7D
+%7D;%0A%0A        this.batchTestClient.createJob(payload
 , fu
@@ -632,1568 +632,597 @@
 tion
- 
 (err, 
-res) %7B%0A            runningJob = JSON.parse(res.body);%0A            done();%0A        %7D);%0A    %7D);%0A%0A    it('Step 2, job should be running', function (done)%7B%0A        var interval = setInterval(function () %7B%0A            assert.response(server, %7B%0A                url: '/api/v2/sql/job/' + runningJob.job_id + '?api_key=1234',%0A                headers: %7B 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' %7D,%0A                method: 'GET'%0A            %7D, %7B%0A                status: 200%0A            %7D, function(err, res) %7B%0A                var job = JSON.parse(res.body);%0A                if (job.status === %22running%22) %7B%0A                    clearInterval(interval);%0A                    done();%0A                %7D  else if (job.status === %22done%22 %7C%7C job.status === %22failed%22 %7C%7C job.status === %22cancelled%22) %7B%0A                    clearInterval(interval);%0A                    done(new Error('Job ' + job.job_id + ' is ' + job.status + ', expected to be running'));%0A                %7D%0A            %7D);%0A        %7D, 50);%0A    %7D);%0A%0A    it('Step 3, running job should be cancelled', function (done)%7B%0A        assert.response(server, %7B%0A            url: '/api/v2/sql/job/' + runningJob.job_id + '?api_key=1234',%0A            headers: %7B 'host': 'vizzuality.cartodb.com', 'Content-Type': 'application/x-www-form-urlencoded' %7D,%0A            method: 'DELETE'%0A        %7D, %7B%0A            status: 200%0A        %7D, function(err, res) %7B%0A            var cancelledJob = JSON.parse(res.body);%0A            assert.equal(cancelledJob.status, %22cancelled%22);%0A            done(
+jobResult) %7B%0A            if (err) %7B%0A                return done(err);%0A            %7D%0A%0A            jobResult.getStatus(JobStatus.RUNNING, function (err, job) %7B%0A                if (err) %7B%0A                    return done(err);%0A                %7D%0A%0A                assert.equal(job.status, JobStatus.RUNNING);%0A%0A                jobResult.cancel(function (err, job) %7B%0A                    if (err) %7B%0A                        return done(err);%0A                    %7D%0A%0A                    assert.equal(job.status, JobStatus.CANCELLED);%0A                    done();%0A                %7D);%0A            %7D
 );%0A 

Use batch test client for use case 7