Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions lib/groupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,31 @@ function getGroup(req, res) {
});
}

function getElementsHandler(db) {
return function (name, callback) {
db.smembers(name, callback);
}
function getElementsHandler(name, callback) {
var db = dbCluster.getTransactionDb(name);
db.smembers(name, callback);
}

function addQueuesFromGroup(req, res, next) {
var db = dbCluster.getTransactionDb(req.params.groupName);

if (req.body && req.body.groups) {
async.map(req.body.groups, getElementsHandler(db), function (error, results) {
if (req.body.queue) {
async.map(req.body.groups, getElementsHandler,
function(error, results) {

if (!req.body.queue) {
req.body.queue = [];
}

var processedResults = [];
for (var i = 0; i < results[0].length; i++) {
processedResults.push({
id: results[0][i]
});
for (var j = 0; j < results.length; j++) {
for (var i = 0; i < results[j].length; i++) {
req.body.queue.push({
id: results[j][i]
});
}
}
req.body.queue = req.body.queue.concat(processedResults);
} else {
req.body.queue = results[0];
}

next();
next();
});
} else {
next();
Expand Down
111 changes: 59 additions & 52 deletions test/unit/bugsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ var utils = require('./../utils.js');

var agent = require('../../.');

describe('Bugs', function() {
describe('Bugs', function () {

before(function(done){
agent.start(done);
});
before(function (done) {
agent.start(done);
});

after(function(done) {
utils.cleanBBDD(function() {
agent.stop(done);
} );
after(function (done) {
utils.cleanBBDD(function () {
agent.stop(done);
});
});

beforeEach(function(done) {
beforeEach(function (done) {
utils.cleanBBDD(done);
});

it('should return empty data (priority is not considered)', function(done) {
it('should return empty data (priority is not considered)', function (done) {

var transID = 'fake';
var modifiedData = {
Expand All @@ -30,40 +30,40 @@ describe('Bugs', function() {

async.series([

function(callback) {
function (callback) {

utils.putTransaction(transID, modifiedData, function(err, response, data) {
utils.putTransaction(transID, modifiedData, function (err, response, data) {

should.not.exist(err);
response.statusCode.should.be.equal(200);
data.data.should.be.equal('empty data');
should.not.exist(err);
response.statusCode.should.be.equal(200);
data.data.should.be.equal('empty data');

callback();
});
},
callback();
});
},

function(callback) {
function (callback) {

utils.getTransState(transID, function(err, response, data) {
utils.getTransState(transID, function (err, response, data) {

should.not.exist(err);
response.statusCode.should.be.equal(200);
should.not.exist(data.data);
should.not.exist(err);
response.statusCode.should.be.equal(200);
should.not.exist(data.data);

callback();
});
}
callback();
});
}
], done);

});

it('should return errors (does not exist [id])', function(done) {
it('should return errors (does not exist [id])', function (done) {

var transID = 'false';

var modifyTrans = function(payload, cb) {
var modifyTrans = function (payload, cb) {

utils.putTransaction(transID, {expirationDate: 2147483645}, function(err, response, data) {
utils.putTransaction(transID, {expirationDate: 2147483645}, function (err, response, data) {

should.not.exist(err);
response.statusCode.should.be.equal(400);
Expand All @@ -73,9 +73,9 @@ describe('Bugs', function() {
});
}

var checkState = function(cb) {
var checkState = function (cb) {

utils.getTransState(transID, function(err, response, data) {
utils.getTransState(transID, function (err, response, data) {

should.not.exist(err);
response.statusCode.should.be.equal(200);
Expand All @@ -86,23 +86,25 @@ describe('Bugs', function() {
}

async.series([
modifyTrans.bind({}, {expirationDate: 2147483645}),
checkState,
modifyTrans.bind({}, {payload: 'hello'}),
checkState
], done);
modifyTrans.bind({}, {expirationDate: 2147483645}),
checkState,
modifyTrans.bind({}, {payload: 'hello'}),
checkState
], done);
});

it('Invalid Content-Type creating a transaction', function(done) {
it('Invalid Content-Type creating a transaction', function (done) {

var trans = utils.createTransaction('Message', 'H', [ { 'id': 'q1' } ]);
var trans = utils.createTransaction('Message', 'H', [
{ 'id': 'q1' }
]);

var heads = {};
var options = { host: config.hostname, port: config.port,
path: '/trans/', method: 'POST', headers: heads};
var transParsed = JSON.stringify(trans);

utils.makeRequest(options, transParsed, function(error, response, data) {
utils.makeRequest(options, transParsed, function (error, response, data) {

response.statusCode.should.be.equal(400);
should.not.exist(error);
Expand All @@ -116,13 +118,16 @@ describe('Bugs', function() {

});

it('Invalid Content-Type modifying a transaction', function(done) {
it('Invalid Content-Type modifying a transaction', function (done) {

var QUEUES = [ { 'id': 'q1' }, { 'id': 'q2' } ];
var trans = utils.createTransaction('Test', 'H', QUEUES, Math.round(new Date().getTime() / 1000 + 2),
var QUEUES = [
{ 'id': 'q1' },
{ 'id': 'q2' }
];
var trans = utils.createTransaction('Test', 'H', QUEUES, Math.round(new Date().getTime() / 1000 + 2),
'http://telefonica.com');

utils.pushTransaction(trans, function(error, response, data) {
utils.pushTransaction(trans, function (error, response, data) {

response.statusCode.should.be.equal(200);
should.not.exist(error);
Expand All @@ -140,7 +145,7 @@ describe('Bugs', function() {
path: '/trans/' + id, method: 'PUT', headers: heads};
var contentModifiedParsed = JSON.stringify(contentModified);

utils.makeRequest(options, contentModifiedParsed, function(error, response, data) {
utils.makeRequest(options, contentModifiedParsed, function (error, response, data) {

response.statusCode.should.be.equal(400);
should.not.exist(error);
Expand All @@ -154,24 +159,26 @@ describe('Bugs', function() {
});
});

it('Queue state in a transaction should be \'Delivered\' when the queue has been popped', function(done) {
it('Queue state in a transaction should be \'Delivered\' when the queue has been popped', function (done) {

var QUEUE_ID = 'q0', MESSAGE = 'MESSAGE 1', transID;
var transaction = utils.createTransaction(MESSAGE, 'H', [ { 'id': QUEUE_ID } ]);
var transaction = utils.createTransaction(MESSAGE, 'H', [
{ 'id': QUEUE_ID }
]);

utils.pushTransaction(transaction, function(error, response, data) {
utils.pushTransaction(transaction, function (error, response, data) {

response.statusCode.should.be.equal(200);
should.not.exist(error);
transID = data.data;


//Check queue state before pop
checkState(transID, 'Pending', function() {
checkState(transID, 'Pending', function () {
//Pop
popQueue(function() {
popQueue(function () {
//Check queue state after pop
checkState(transID, 'Delivered', function() {
checkState(transID, 'Delivered', function () {
done();
});
});
Expand All @@ -180,7 +187,7 @@ describe('Bugs', function() {

function popQueue(cb) {

utils.pop(QUEUE_ID, function(error, response, data) {
utils.pop(QUEUE_ID, function (error, response, data) {

should.not.exist(error);
response.statusCode.should.be.equal(200);
Expand All @@ -197,7 +204,7 @@ describe('Bugs', function() {

function checkState(id, expectedState, cb) {

utils.getTransState(id, expectedState, function(error, response, data) {
utils.getTransState(id, expectedState, function (error, response, data) {

should.not.exist(error);
response.statusCode.should.be.equal(200);
Expand Down
Loading