Skip to content

Commit dc5f859

Browse files
committed
refactor(tone-analyzer): v4 changes
1 parent 8cb2db2 commit dc5f859

File tree

3 files changed

+46
-101
lines changed

3 files changed

+46
-101
lines changed

test/integration/tone_analyzer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const TWENTY_SECONDS = 20000;
1111
describe('tone_analyzer_integration', function() {
1212
jest.setTimeout(TWENTY_SECONDS);
1313

14-
auth.tone_analyzer.version = '2016-06-19';
14+
auth.tone_analyzer.version = '2019-03-27';
1515
const tone_analyzer = new ToneAnalyzerV3(auth.tone_analyzer);
1616

1717
it('tone()', function(done) {

test/unit/tone-analyzer.v3.test.js

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
'use strict';
22

3-
const ToneAnalyzerV3 = require('../../tone-analyzer/v3-generated');
43
const helper = require('ibm-cloud-sdk-core');
4+
const ToneAnalyzerV3 = require('../../tone-analyzer/v3-generated');
55
const utils = require('../resources/unitTestUtils');
66

7-
const missingParamsError = utils.missingParamsError;
8-
const missingParamsSuccess = utils.missingParamsSuccess;
7+
const getOptions = utils.getOptions;
98
const checkUrlAndMethod = utils.checkUrlAndMethod;
109
const checkCallback = utils.checkCallback;
1110
const checkMediaHeaders = utils.checkMediaHeaders;
12-
const checkUserHeader = utils.checkUserHeader;
11+
const missingParamsSuccess = utils.missingParamsSuccess;
12+
const missingParamsError = utils.missingParamsError;
1313
const checkForEmptyObject = utils.checkForEmptyObject;
1414
const checkRequiredParamsHandling = utils.checkRequiredParamsHandling;
15-
const getOptions = utils.getOptions;
16-
const expectToBePromise = utils.expectToBePromise;
15+
const checkUserHeader = utils.checkUserHeader;
16+
const noop = () => {};
1717

1818
const service = {
1919
username: 'batman',
2020
password: 'bruce-wayne',
21-
url: 'https://gateway.watsonplatform.net/tone-analyzer/api',
21+
url: 'https://gateway.watsonplatform.net/tone-analyzer/api/tone-analyzer/api',
2222
version: '2018-10-18',
2323
};
2424

25-
const tone_analyzer = new ToneAnalyzerV3(service);
26-
const createRequestMock = jest.spyOn(tone_analyzer, 'createRequest');
25+
const toneAnalyzer = new ToneAnalyzerV3(service);
26+
const createRequestMock = jest.spyOn(toneAnalyzer, 'createRequest');
2727
const missingParamsMock = jest.spyOn(helper, 'getMissingParams');
28-
const noop = () => {};
28+
29+
// dont actually create a request
30+
createRequestMock.mockImplementation(noop);
2931

3032
afterEach(() => {
3133
createRequestMock.mockReset();
@@ -40,22 +42,22 @@ describe('tone', () => {
4042
test('should pass the right params to createRequest', () => {
4143
// parameters
4244
const tone_input = 'fake_tone_input';
43-
const content_type = 'fake_content_type';
4445
const sentences = 'fake_sentences';
4546
const tones = 'fake_tones';
4647
const content_language = 'fake_content_language';
4748
const accept_language = 'fake_accept_language';
49+
const content_type = 'fake_content_type';
4850
const params = {
4951
tone_input,
50-
content_type,
5152
sentences,
5253
tones,
5354
content_language,
5455
accept_language,
56+
content_type,
5557
};
5658

5759
// invoke method
58-
tone_analyzer.tone(params, noop);
60+
toneAnalyzer.tone(params);
5961

6062
// assert that create request was called
6163
expect(createRequestMock).toHaveBeenCalledTimes(1);
@@ -67,11 +69,10 @@ describe('tone', () => {
6769
const expectedAccept = 'application/json';
6870
const expectedContentType = content_type;
6971
checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType);
70-
checkUserHeader(createRequestMock, 'Content-Type', content_type);
7172
checkUserHeader(createRequestMock, 'Content-Language', content_language);
7273
checkUserHeader(createRequestMock, 'Accept-Language', accept_language);
74+
checkUserHeader(createRequestMock, 'Content-Type', content_type);
7375
expect(options.body).toEqual(tone_input);
74-
expect(options.json).toEqual(content_type === 'application/json');
7576
expect(options.qs['sentences']).toEqual(sentences);
7677
expect(options.qs['tones']).toEqual(tones);
7778
});
@@ -89,32 +90,18 @@ describe('tone', () => {
8990
},
9091
};
9192

92-
tone_analyzer.tone(params, noop);
93+
toneAnalyzer.tone(params);
9394
checkMediaHeaders(createRequestMock, accept, contentType);
9495
});
95-
96-
test('should return a promise when no callback is given', () => {
97-
// parameters
98-
const tone_input = 'fake_tone_input';
99-
const params = {
100-
tone_input,
101-
};
102-
103-
// invoke method
104-
const tonePromise = tone_analyzer.tone(params);
105-
expectToBePromise(tonePromise);
106-
107-
// assert that create request was called
108-
expect(createRequestMock).toHaveBeenCalledTimes(1);
109-
});
11096
});
97+
11198
describe('negative tests', () => {
11299
beforeAll(() => {
113100
missingParamsMock.mockReturnValue(missingParamsError);
114101
});
115102

116103
test('should convert a `null` value for `params` to an empty object', done => {
117-
tone_analyzer.tone(null, () => {
104+
toneAnalyzer.tone(null, () => {
118105
checkForEmptyObject(missingParamsMock);
119106
done();
120107
});
@@ -124,26 +111,14 @@ describe('tone', () => {
124111
// required parameters for this method
125112
const requiredParams = ['tone_input'];
126113

127-
tone_analyzer.tone({}, err => {
128-
checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock);
129-
done();
130-
});
131-
});
132-
133-
test('should reject promise when required params are not given', done => {
134-
// required parameters for this method
135-
const requiredParams = ['tone_input'];
136-
137-
const tonePromise = tone_analyzer.tone();
138-
expectToBePromise(tonePromise);
139-
140-
tonePromise.catch(err => {
114+
toneAnalyzer.tone({}, err => {
141115
checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock);
142116
done();
143117
});
144118
});
145119
});
146120
});
121+
147122
describe('toneChat', () => {
148123
describe('positive tests', () => {
149124
beforeAll(() => {
@@ -161,7 +136,7 @@ describe('toneChat', () => {
161136
};
162137

163138
// invoke method
164-
tone_analyzer.toneChat(params, noop);
139+
toneAnalyzer.toneChat(params);
165140

166141
// assert that create request was called
167142
expect(createRequestMock).toHaveBeenCalledTimes(1);
@@ -176,7 +151,6 @@ describe('toneChat', () => {
176151
checkUserHeader(createRequestMock, 'Content-Language', content_language);
177152
checkUserHeader(createRequestMock, 'Accept-Language', accept_language);
178153
expect(options.body['utterances']).toEqual(utterances);
179-
expect(options.json).toEqual(true);
180154
});
181155

182156
test('should prioritize user-given headers', () => {
@@ -192,32 +166,18 @@ describe('toneChat', () => {
192166
},
193167
};
194168

195-
tone_analyzer.toneChat(params, noop);
169+
toneAnalyzer.toneChat(params);
196170
checkMediaHeaders(createRequestMock, accept, contentType);
197171
});
198-
199-
test('should return a promise when no callback is given', () => {
200-
// parameters
201-
const utterances = 'fake_utterances';
202-
const params = {
203-
utterances,
204-
};
205-
206-
// invoke method
207-
const toneChatPromise = tone_analyzer.toneChat(params);
208-
expectToBePromise(toneChatPromise);
209-
210-
// assert that create request was called
211-
expect(createRequestMock).toHaveBeenCalledTimes(1);
212-
});
213172
});
173+
214174
describe('negative tests', () => {
215175
beforeAll(() => {
216176
missingParamsMock.mockReturnValue(missingParamsError);
217177
});
218178

219179
test('should convert a `null` value for `params` to an empty object', done => {
220-
tone_analyzer.toneChat(null, () => {
180+
toneAnalyzer.toneChat(null, () => {
221181
checkForEmptyObject(missingParamsMock);
222182
done();
223183
});
@@ -227,20 +187,7 @@ describe('toneChat', () => {
227187
// required parameters for this method
228188
const requiredParams = ['utterances'];
229189

230-
tone_analyzer.toneChat({}, err => {
231-
checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock);
232-
done();
233-
});
234-
});
235-
236-
test('should reject promise when required params are not given', done => {
237-
// required parameters for this method
238-
const requiredParams = ['utterances'];
239-
240-
const toneChatPromise = tone_analyzer.toneChat();
241-
expectToBePromise(toneChatPromise);
242-
243-
toneChatPromise.catch(err => {
190+
toneAnalyzer.toneChat({}, err => {
244191
checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock);
245192
done();
246193
});

tone-analyzer/v3-generated.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { AxiosResponse } from 'axios';
1818
import * as extend from 'extend';
19-
import { BaseService, getMissingParams} from 'ibm-cloud-sdk-core';
19+
import { BaseService, getMissingParams } from 'ibm-cloud-sdk-core';
2020
import { getSdkHeaders } from '../lib/common';
2121

2222
/**
@@ -83,8 +83,6 @@ class ToneAnalyzerV3 extends BaseService {
8383
* @param {Object} params - The parameters to send to the service.
8484
* @param {ToneInput|string} params.tone_input - JSON, plain text, or HTML input that contains the content to be
8585
* analyzed. For JSON input, provide an object of type `ToneInput`.
86-
* @param {string} [params.content_type] - The type of the input. A character encoding can be specified by including a
87-
* `charset` parameter. For example, 'text/plain;charset=utf-8'.
8886
* @param {boolean} [params.sentences] - Indicates whether the service is to return an analysis of each individual
8987
* sentence in addition to its analysis of the full document. If `true` (the default), the service returns results for
9088
* each sentence.
@@ -103,9 +101,11 @@ class ToneAnalyzerV3 extends BaseService {
103101
* @param {string} [params.accept_language] - The desired language of the response. For two-character arguments,
104102
* regional variants are treated as their parent language; for example, `en-US` is interpreted as `en`. You can use
105103
* different languages for **Content-Language** and **Accept-Language**.
104+
* @param {string} [params.content_type] - The type of the input. A character encoding can be specified by including a
105+
* `charset` parameter. For example, 'text/plain;charset=utf-8'.
106106
* @param {Object} [params.headers] - Custom request headers
107107
* @param {Function} [callback] - The callback that handles the response.
108-
* @returns {NodeJS.ReadableStream|void}
108+
* @returns {Promise<any>|void}
109109
*/
110110
public tone(params: ToneAnalyzerV3.ToneParams, callback?: ToneAnalyzerV3.Callback<ToneAnalyzerV3.ToneAnalysis>): Promise<any> | void {
111111
const _params = extend({}, params);
@@ -132,21 +132,20 @@ class ToneAnalyzerV3 extends BaseService {
132132
};
133133

134134
const sdkHeaders = getSdkHeaders('tone_analyzer', 'v3', 'tone');
135-
135+
136136
const parameters = {
137137
options: {
138138
url: '/v3/tone',
139139
method: 'POST',
140-
json: (_params.content_type === 'application/json'),
141140
body,
142141
qs: query,
143142
},
144143
defaultOptions: extend(true, {}, this._options, {
145144
headers: extend(true, sdkHeaders, {
146145
'Accept': 'application/json',
147-
'Content-Type': _params.content_type,
148146
'Content-Language': _params.content_language,
149-
'Accept-Language': _params.accept_language
147+
'Accept-Language': _params.accept_language,
148+
'Content-Type': _params.content_type
150149
}, _params.headers),
151150
}),
152151
};
@@ -184,7 +183,7 @@ class ToneAnalyzerV3 extends BaseService {
184183
* different languages for **Content-Language** and **Accept-Language**.
185184
* @param {Object} [params.headers] - Custom request headers
186185
* @param {Function} [callback] - The callback that handles the response.
187-
* @returns {NodeJS.ReadableStream|void}
186+
* @returns {Promise<any>|void}
188187
*/
189188
public toneChat(params: ToneAnalyzerV3.ToneChatParams, callback?: ToneAnalyzerV3.Callback<ToneAnalyzerV3.UtteranceAnalyses>): Promise<any> | void {
190189
const _params = extend({}, params);
@@ -209,12 +208,11 @@ class ToneAnalyzerV3 extends BaseService {
209208
};
210209

211210
const sdkHeaders = getSdkHeaders('tone_analyzer', 'v3', 'toneChat');
212-
211+
213212
const parameters = {
214213
options: {
215214
url: '/v3/tone_chat',
216215
method: 'POST',
217-
json: true,
218216
body,
219217
},
220218
defaultOptions: extend(true, {}, this._options, {
@@ -268,29 +266,23 @@ namespace ToneAnalyzerV3 {
268266
export interface ToneParams {
269267
/** JSON, plain text, or HTML input that contains the content to be analyzed. For JSON input, provide an object of type `ToneInput`. */
270268
tone_input: ToneInput|string;
271-
/** The type of the input. A character encoding can be specified by including a `charset` parameter. For example, 'text/plain;charset=utf-8'. */
272-
content_type?: ToneConstants.ContentType | string;
273269
/** Indicates whether the service is to return an analysis of each individual sentence in addition to its analysis of the full document. If `true` (the default), the service returns results for each sentence. */
274270
sentences?: boolean;
275271
/** **`2017-09-21`:** Deprecated. The service continues to accept the parameter for backward-compatibility, but the parameter no longer affects the response. **`2016-05-19`:** A comma-separated list of tones for which the service is to return its analysis of the input; the indicated tones apply both to the full document and to individual sentences of the document. You can specify one or more of the valid values. Omit the parameter to request results for all three tones. */
276-
tones?: string[];
272+
tones?: ToneConstants.Tones[] | string[];
277273
/** The language of the input text for the request: English or French. Regional variants are treated as their parent language; for example, `en-US` is interpreted as `en`. The input content must match the specified language. Do not submit content that contains both languages. You can use different languages for **Content-Language** and **Accept-Language**. * **`2017-09-21`:** Accepts `en` or `fr`. * **`2016-05-19`:** Accepts only `en`. */
278274
content_language?: ToneConstants.ContentLanguage | string;
279275
/** The desired language of the response. For two-character arguments, regional variants are treated as their parent language; for example, `en-US` is interpreted as `en`. You can use different languages for **Content-Language** and **Accept-Language**. */
280276
accept_language?: ToneConstants.AcceptLanguage | string;
277+
/** The type of the input. A character encoding can be specified by including a `charset` parameter. For example, 'text/plain;charset=utf-8'. */
278+
content_type?: ToneConstants.ContentType | string;
281279
headers?: Object;
282280
return_response?: boolean;
283281
}
284282

285283
/** Constants for the `tone` operation. */
286284
export namespace ToneConstants {
287-
/** The type of the input. A character encoding can be specified by including a `charset` parameter. For example, 'text/plain;charset=utf-8'. */
288-
export enum ContentType {
289-
APPLICATION_JSON = 'application/json',
290-
TEXT_PLAIN = 'text/plain',
291-
TEXT_HTML = 'text/html',
292-
}
293-
/** Tones */
285+
/** **`2017-09-21`:** Deprecated. The service continues to accept the parameter for backward-compatibility, but the parameter no longer affects the response. **`2016-05-19`:** A comma-separated list of tones for which the service is to return its analysis of the input; the indicated tones apply both to the full document and to individual sentences of the document. You can specify one or more of the valid values. Omit the parameter to request results for all three tones. */
294286
export enum Tones {
295287
EMOTION = 'emotion',
296288
LANGUAGE = 'language',
@@ -315,6 +307,12 @@ namespace ToneAnalyzerV3 {
315307
ZH_CN = 'zh-cn',
316308
ZH_TW = 'zh-tw',
317309
}
310+
/** The type of the input. A character encoding can be specified by including a `charset` parameter. For example, 'text/plain;charset=utf-8'. */
311+
export enum ContentType {
312+
APPLICATION_JSON = 'application/json',
313+
TEXT_PLAIN = 'text/plain',
314+
TEXT_HTML = 'text/html',
315+
}
318316
}
319317

320318
/** Parameters for the `toneChat` operation. */

0 commit comments

Comments
 (0)