2014-11-04 18:05:29 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2014 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require("should");
|
|
|
|
var request = require('supertest');
|
|
|
|
var express = require('express');
|
2015-07-15 23:43:24 +02:00
|
|
|
var bodyParser = require('body-parser');
|
2014-11-04 18:05:29 +01:00
|
|
|
var sinon = require('sinon');
|
|
|
|
var when = require('when');
|
|
|
|
|
|
|
|
var redNodes = require("../../../red/nodes");
|
|
|
|
|
|
|
|
var flows = require("../../../red/api/flows");
|
|
|
|
|
|
|
|
describe("flows api", function() {
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
var app;
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
app = express();
|
2015-07-15 23:43:24 +02:00
|
|
|
app.use(bodyParser.json());
|
2014-11-04 18:05:29 +01:00
|
|
|
app.get("/flows",flows.get);
|
|
|
|
app.post("/flows",flows.post);
|
|
|
|
});
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
it('returns flow', function(done) {
|
|
|
|
var getFlows = sinon.stub(redNodes,'getFlows', function() {
|
|
|
|
return [1,2,3];
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.get('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err,res) {
|
|
|
|
getFlows.restore();
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
res.body.should.be.an.Array.and.have.lengthOf(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
it('sets flows', function(done) {
|
|
|
|
var setFlows = sinon.stub(redNodes,'setFlows', function() {
|
|
|
|
return when.resolve();
|
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(204)
|
|
|
|
.end(function(err,res) {
|
|
|
|
setFlows.restore();
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns error when set fails', function(done) {
|
|
|
|
var setFlows = sinon.stub(redNodes,'setFlows', function() {
|
2015-01-10 23:09:37 +01:00
|
|
|
return when.reject(new Error("expected error"));
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|
|
|
|
request(app)
|
|
|
|
.post('/flows')
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.expect(500)
|
|
|
|
.end(function(err,res) {
|
|
|
|
setFlows.restore();
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-03-30 15:16:04 +02:00
|
|
|
res.body.should.have.property("message","expected error");
|
2014-11-04 18:05:29 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2014-11-04 18:05:29 +01:00
|
|
|
});
|