Skip to content

Commit

Permalink
Merge branch 'util_scripts' of https://github.com/telefonicaid/PopBox
Browse files Browse the repository at this point in the history
…into util_scripts
  • Loading branch information
Carlos Romero Brox committed Jun 3, 2013
2 parents a3e7345 + aa0e5d2 commit 238f4d9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 39 deletions.
35 changes: 16 additions & 19 deletions lib/groupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,34 +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),
async.map(req.body.groups, getElementsHandler,
function(error, results) {
//[[q,q,q],[q],[q,q]]
var processedResults = [];
for (var j = 0; j < results.length; j++) {
for (var i = 0; i < results[j].length; i++) {
processedResults.push({
id: results[j][i]
});
}

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

var processedResults = [];
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]
});
}
if (req.body.queue) {
req.body.queue = req.body.queue.concat(processedResults);
} else {
req.body.queue = processedResults;
}

next();
next();
});
} else {
next();
Expand Down
76 changes: 56 additions & 20 deletions test/unit/groupsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,37 @@ describe('Groups', function () {

describe('When a message is posted to a group', function () {
var publish;
var groupName = 'group2';
var queues = ['A2', 'B2'];

function checkQueue(transId, queue, done) {
var checkQueue = {
url: 'http://' + HOST + ':' + PORT + '/queue/' + queue + '/pop',
method: 'POST'
}

request(checkQueue, function (error, response, body) {
response.statusCode.should.equal(200);

var parsedBody = JSON.parse(body);
should.exist(parsedBody.data);
parsedBody.data.length.should.equal(1);
parsedBody.data.should.include(publish.json.payload);
parsedBody.transactions.length.should.equal(1);
parsedBody.transactions.should.include(transId);

done();
});
}

beforeEach(function (done) {
var createGroup = {
url: 'http://' + HOST + ':' + PORT + '/group',
method: 'POST',
json: {}
json: {
name: groupName,
queues: queues
}
};

publish = {
Expand All @@ -148,42 +173,53 @@ describe('Groups', function () {
'payload': 'Published message',
'priority': 'H',
'callback': 'http://foo.bar',
'queue': [
],
'groups': [
'group2'
]
}
};

createGroup.json.name = 'group1';
createGroup.json.queues = ['A1', 'B1']
request(createGroup, function (error, response, body) {
createGroup.json.name = 'group2';
createGroup.json.queues = ['A2', 'B2']
request(createGroup, function (error, response, body) {
done();
});
done();
});
});

it('should publish to all the inboxes associated to the group', function (done) {
request(publish, function (error, response, body) {

response.statusCode.should.equal(200);

var checkQueue = {
url: 'http://' + HOST + ':' + PORT + '/queue/B2/pop',
method: 'POST'
var transId = body.data;
var tests = [];

for (var i = 0; i < queues.length; i++) {
tests.push(checkQueue.bind({}, transId, queues[i]));
}

request(checkQueue, function (error, response, body) {
response.statusCode.should.equal(200);
async.parallel(tests, done);

});
});


it('should publish to all the inboxes associated to the group and in the selected queues', function (done) {
var newQueue = 'C2';

publish.json.queue = [ { id: newQueue } ];
request(publish, function (error, response, body) {

response.statusCode.should.equal(200);

var transId = body.data;
var tests = [];

tests.push(checkQueue.bind({}, transId, newQueue));
for (var i = 0; i < queues.length; i++) {
tests.push(checkQueue.bind({}, transId, queues[i]));
}

async.parallel(tests, done);

var parsedBody = JSON.parse(body);
should.exist(parsedBody.data);
parsedBody.data.length.should.equal(1);
done();
});
});
});
});
Expand Down

0 comments on commit 238f4d9

Please sign in to comment.