forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.tradeoff_analytics.v1.js
95 lines (77 loc) · 2.85 KB
/
test.tradeoff_analytics.v1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
var assert = require('assert');
var watson = require('../lib/index');
var nock = require('nock');
var extend = require('extend');
describe('tradeoff_analytics', function() {
var noop = function() {};
var service_request = require('./resources/dilemma_problem');
var events_request = require('./resources/ta_events');
var service_response = {};
var service_path = '/v1/dilemmas';
var events_path = '/v1/events';
var service = {
username: 'batman',
password: 'bruce-wayne',
url: 'http://ibm.com:80',
version: 'v1'
};
before(function() {
nock.disableNetConnect();
nock(service.url)
.persist()
.post(service_path, service_request)
.reply(200, service_response);
nock(service.url)
.persist()
.post(events_path, events_request)
.reply(200);
});
after(function() {
nock.cleanAll();
});
var tradeoff_analytics = watson.tradeoff_analytics(service);
var missingParameter = function(err) {
assert.ok((err instanceof Error) && /required parameters/.test(err));
};
it('should check no parameters provided', function() {
tradeoff_analytics.dilemmas({}, missingParameter);
tradeoff_analytics.dilemmas(null, missingParameter);
tradeoff_analytics.dilemmas(undefined, missingParameter);
tradeoff_analytics.events({}, missingParameter);
tradeoff_analytics.events(null, missingParameter);
tradeoff_analytics.events(undefined, missingParameter);
});
it('should generate a valid payload', function() {
var params = extend({}, service_request);
params.metadata_header = 'test_header_content';
var req = tradeoff_analytics.dilemmas(params, noop);
var body = new Buffer(req.body).toString('ascii');
assert.equal(req.uri.href, service.url + service_path);
assert.equal(body, JSON.stringify(service_request));
assert.notEqual(body, JSON.stringify(params));
assert.equal(req.headers['x-watson-metadata'], params.metadata_header);
assert.equal(req.method, 'POST');
});
it('should forward the events correctly', function() {
var params = extend({}, events_request);
params.metadata_header = 'test_header_content';
var req = tradeoff_analytics.events(params, noop);
var body = new Buffer(req.body).toString('ascii');
assert.equal(req.uri.href, service.url + events_path);
assert.equal(body, JSON.stringify(events_request));
assert.notEqual(body, JSON.stringify(params));
assert.equal(req.headers['x-watson-metadata'], params.metadata_header);
assert.equal(req.method, 'POST');
});
it('should format the response', function(done) {
tradeoff_analytics.dilemmas(service_request, function(err, response) {
if (err)
done(err);
else {
assert.equal(JSON.stringify(response), JSON.stringify(service_response));
done();
}
});
});
});