diff --git a/natural-language-classifier/v1-generated.ts b/natural-language-classifier/v1-generated.ts index 70f927adb3..62ca596e21 100644 --- a/natural-language-classifier/v1-generated.ts +++ b/natural-language-classifier/v1-generated.ts @@ -16,8 +16,9 @@ import { AxiosResponse } from 'axios'; import * as extend from 'extend'; -import { BaseService, FileObject, getMissingParams } from 'ibm-cloud-sdk-core'; +import { BaseService, getMissingParams } from 'ibm-cloud-sdk-core'; import { getSdkHeaders } from '../lib/common'; +import { FileObject } from 'ibm-cloud-sdk-core'; /** * IBM Watson™ Natural Language Classifier uses machine learning algorithms to return the top matching predefined classes for short text input. You create and train a classifier to connect predefined classes to example texts so that the service can apply those classes to new inputs. @@ -64,7 +65,7 @@ class NaturalLanguageClassifierV1 extends BaseService { * @param {string} params.text - The submitted phrase. The maximum length is 2048 characters. * @param {Object} [params.headers] - Custom request headers * @param {Function} [callback] - The callback that handles the response. - * @returns {NodeJS.ReadableStream|void} + * @returns {Promise|void} */ public classify(params: NaturalLanguageClassifierV1.ClassifyParams, callback?: NaturalLanguageClassifierV1.Callback): Promise | void { const _params = extend({}, params); @@ -93,12 +94,11 @@ class NaturalLanguageClassifierV1 extends BaseService { }; const sdkHeaders = getSdkHeaders('natural_language_classifier', 'v1', 'classify'); - + const parameters = { options: { url: '/v1/classifiers/{classifier_id}/classify', method: 'POST', - json: true, body, path, }, @@ -126,7 +126,7 @@ class NaturalLanguageClassifierV1 extends BaseService { * @param {ClassifyInput[]} params.collection - The submitted phrases. * @param {Object} [params.headers] - Custom request headers * @param {Function} [callback] - The callback that handles the response. - * @returns {NodeJS.ReadableStream|void} + * @returns {Promise|void} */ public classifyCollection(params: NaturalLanguageClassifierV1.ClassifyCollectionParams, callback?: NaturalLanguageClassifierV1.Callback): Promise | void { const _params = extend({}, params); @@ -155,12 +155,11 @@ class NaturalLanguageClassifierV1 extends BaseService { }; const sdkHeaders = getSdkHeaders('natural_language_classifier', 'v1', 'classifyCollection'); - + const parameters = { options: { url: '/v1/classifiers/{classifier_id}/classify_collection', method: 'POST', - json: true, body, path, }, @@ -194,11 +193,9 @@ class NaturalLanguageClassifierV1 extends BaseService { * @param {NodeJS.ReadableStream|FileObject|Buffer} params.training_data - Training data in CSV format. Each text * value must have at least one class. The data can include up to 3,000 classes and 20,000 records. For details, see * [Data preparation](https://cloud.ibm.com/docs/services/natural-language-classifier/using-your-data.html). - * @param {string} [params.metadata_filename] - The filename for training_metadata. - * @param {string} [params.training_data_filename] - The filename for training_data. * @param {Object} [params.headers] - Custom request headers * @param {Function} [callback] - The callback that handles the response. - * @returns {NodeJS.ReadableStream|void} + * @returns {Promise|void} */ public createClassifier(params: NaturalLanguageClassifierV1.CreateClassifierParams, callback?: NaturalLanguageClassifierV1.Callback): Promise | void { const _params = extend({}, params); @@ -217,22 +214,19 @@ class NaturalLanguageClassifierV1 extends BaseService { if (missingParams) { return _callback(missingParams); } - const formData = { 'training_metadata': { data: _params.metadata, - filename: _params.metadata_filename, contentType: 'application/json' }, 'training_data': { data: _params.training_data, - filename: _params.training_data_filename, contentType: 'text/csv' } }; const sdkHeaders = getSdkHeaders('natural_language_classifier', 'v1', 'createClassifier'); - + const parameters = { options: { url: '/v1/classifiers', @@ -257,7 +251,7 @@ class NaturalLanguageClassifierV1 extends BaseService { * @param {string} params.classifier_id - Classifier ID to delete. * @param {Object} [params.headers] - Custom request headers * @param {Function} [callback] - The callback that handles the response. - * @returns {NodeJS.ReadableStream|void} + * @returns {Promise|void} */ public deleteClassifier(params: NaturalLanguageClassifierV1.DeleteClassifierParams, callback?: NaturalLanguageClassifierV1.Callback): Promise | void { const _params = extend({}, params); @@ -282,7 +276,7 @@ class NaturalLanguageClassifierV1 extends BaseService { }; const sdkHeaders = getSdkHeaders('natural_language_classifier', 'v1', 'deleteClassifier'); - + const parameters = { options: { url: '/v1/classifiers/{classifier_id}', @@ -292,7 +286,6 @@ class NaturalLanguageClassifierV1 extends BaseService { defaultOptions: extend(true, {}, this._options, { headers: extend(true, sdkHeaders, { 'Accept': 'application/json', - 'Content-Type': 'application/json', }, _params.headers), }), }; @@ -309,7 +302,7 @@ class NaturalLanguageClassifierV1 extends BaseService { * @param {string} params.classifier_id - Classifier ID to query. * @param {Object} [params.headers] - Custom request headers * @param {Function} [callback] - The callback that handles the response. - * @returns {NodeJS.ReadableStream|void} + * @returns {Promise|void} */ public getClassifier(params: NaturalLanguageClassifierV1.GetClassifierParams, callback?: NaturalLanguageClassifierV1.Callback): Promise | void { const _params = extend({}, params); @@ -334,7 +327,7 @@ class NaturalLanguageClassifierV1 extends BaseService { }; const sdkHeaders = getSdkHeaders('natural_language_classifier', 'v1', 'getClassifier'); - + const parameters = { options: { url: '/v1/classifiers/{classifier_id}', @@ -344,7 +337,6 @@ class NaturalLanguageClassifierV1 extends BaseService { defaultOptions: extend(true, {}, this._options, { headers: extend(true, sdkHeaders, { 'Accept': 'application/json', - 'Content-Type': 'application/json', }, _params.headers), }), }; @@ -360,7 +352,7 @@ class NaturalLanguageClassifierV1 extends BaseService { * @param {Object} [params] - The parameters to send to the service. * @param {Object} [params.headers] - Custom request headers * @param {Function} [callback] - The callback that handles the response. - * @returns {NodeJS.ReadableStream|void} + * @returns {Promise|void} */ public listClassifiers(params?: NaturalLanguageClassifierV1.ListClassifiersParams, callback?: NaturalLanguageClassifierV1.Callback): Promise | void { const _params = (typeof params === 'function' && !callback) ? {} : extend({}, params); @@ -375,7 +367,7 @@ class NaturalLanguageClassifierV1 extends BaseService { } const sdkHeaders = getSdkHeaders('natural_language_classifier', 'v1', 'listClassifiers'); - + const parameters = { options: { url: '/v1/classifiers', @@ -384,7 +376,6 @@ class NaturalLanguageClassifierV1 extends BaseService { defaultOptions: extend(true, {}, this._options, { headers: extend(true, sdkHeaders, { 'Accept': 'application/json', - 'Content-Type': 'application/json', }, _params.headers), }), }; @@ -451,10 +442,6 @@ namespace NaturalLanguageClassifierV1 { metadata: NodeJS.ReadableStream|FileObject|Buffer; /** Training data in CSV format. Each text value must have at least one class. The data can include up to 3,000 classes and 20,000 records. For details, see [Data preparation](https://cloud.ibm.com/docs/services/natural-language-classifier/using-your-data.html). */ training_data: NodeJS.ReadableStream|FileObject|Buffer; - /** The filename for training_metadata. */ - metadata_filename?: string; - /** The filename for training_data. */ - training_data_filename?: string; headers?: Object; return_response?: boolean; } diff --git a/test/unit/natural-language-classifier.v1.test.js b/test/unit/natural-language-classifier.v1.test.js index 18f54c6b91..ce6555dab6 100644 --- a/test/unit/natural-language-classifier.v1.test.js +++ b/test/unit/natural-language-classifier.v1.test.js @@ -1,31 +1,35 @@ 'use strict'; -const NaturalLanguageClassifierV1 = require('../../natural-language-classifier/v1-generated'); const helper = require('ibm-cloud-sdk-core'); +const NaturalLanguageClassifierV1 = require('../../natural-language-classifier/v1-generated'); const utils = require('../resources/unitTestUtils'); -const missingParamsError = utils.missingParamsError; -const missingParamsSuccess = utils.missingParamsSuccess; +const getOptions = utils.getOptions; const checkUrlAndMethod = utils.checkUrlAndMethod; const checkCallback = utils.checkCallback; const checkMediaHeaders = utils.checkMediaHeaders; -const checkDefaultSuccessArgs = utils.checkDefaultSuccessArgs; +const missingParamsSuccess = utils.missingParamsSuccess; +const missingParamsError = utils.missingParamsError; const checkForEmptyObject = utils.checkForEmptyObject; const checkRequiredParamsHandling = utils.checkRequiredParamsHandling; -const getOptions = utils.getOptions; -const expectToBePromise = utils.expectToBePromise; + +const checkDefaultSuccessArgs = utils.checkDefaultSuccessArgs; +const noop = () => {}; const service = { username: 'batman', password: 'bruce-wayne', - url: 'https://gateway.watsonplatform.net/natural-language-classifier/api', + url: + 'https://gateway.watsonplatform.net/natural-language-classifier/api/natural-language-classifier/api', version: '2018-10-18', }; -const natural_language_classifier = new NaturalLanguageClassifierV1(service); -const createRequestMock = jest.spyOn(natural_language_classifier, 'createRequest'); +const naturalLanguageClassifier = new NaturalLanguageClassifierV1(service); +const createRequestMock = jest.spyOn(naturalLanguageClassifier, 'createRequest'); const missingParamsMock = jest.spyOn(helper, 'getMissingParams'); -const noop = () => {}; + +// dont actually create a request +createRequestMock.mockImplementation(noop); afterEach(() => { createRequestMock.mockReset(); @@ -47,7 +51,7 @@ describe('classify', () => { }; // invoke method - natural_language_classifier.classify(params, noop); + naturalLanguageClassifier.classify(params); // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); @@ -60,7 +64,6 @@ describe('classify', () => { const expectedContentType = 'application/json'; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); expect(options.body['text']).toEqual(text); - expect(options.json).toEqual(true); expect(options.path['classifier_id']).toEqual(classifier_id); }); @@ -79,34 +82,18 @@ describe('classify', () => { }, }; - natural_language_classifier.classify(params, noop); + naturalLanguageClassifier.classify(params); checkMediaHeaders(createRequestMock, accept, contentType); }); - - test('should return a promise when no callback is given', () => { - // parameters - const classifier_id = 'fake_classifier_id'; - const text = 'fake_text'; - const params = { - classifier_id, - text, - }; - - // invoke method - const classifyPromise = natural_language_classifier.classify(params); - expectToBePromise(classifyPromise); - - // assert that create request was called - expect(createRequestMock).toHaveBeenCalledTimes(1); - }); }); + describe('negative tests', () => { beforeAll(() => { missingParamsMock.mockReturnValue(missingParamsError); }); test('should convert a `null` value for `params` to an empty object', done => { - natural_language_classifier.classify(null, () => { + naturalLanguageClassifier.classify(null, () => { checkForEmptyObject(missingParamsMock); done(); }); @@ -116,26 +103,14 @@ describe('classify', () => { // required parameters for this method const requiredParams = ['classifier_id', 'text']; - natural_language_classifier.classify({}, err => { - checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); - done(); - }); - }); - - test('should reject promise when required params are not given', done => { - // required parameters for this method - const requiredParams = ['classifier_id', 'text']; - - const classifyPromise = natural_language_classifier.classify(); - expectToBePromise(classifyPromise); - - classifyPromise.catch(err => { + naturalLanguageClassifier.classify({}, err => { checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); done(); }); }); }); }); + describe('classifyCollection', () => { describe('positive tests', () => { beforeAll(() => { @@ -151,7 +126,7 @@ describe('classifyCollection', () => { }; // invoke method - natural_language_classifier.classifyCollection(params, noop); + naturalLanguageClassifier.classifyCollection(params); // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); @@ -164,7 +139,6 @@ describe('classifyCollection', () => { const expectedContentType = 'application/json'; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); expect(options.body['collection']).toEqual(collection); - expect(options.json).toEqual(true); expect(options.path['classifier_id']).toEqual(classifier_id); }); @@ -183,34 +157,18 @@ describe('classifyCollection', () => { }, }; - natural_language_classifier.classifyCollection(params, noop); + naturalLanguageClassifier.classifyCollection(params); checkMediaHeaders(createRequestMock, accept, contentType); }); - - test('should return a promise when no callback is given', () => { - // parameters - const classifier_id = 'fake_classifier_id'; - const collection = 'fake_collection'; - const params = { - classifier_id, - collection, - }; - - // invoke method - const classifyCollectionPromise = natural_language_classifier.classifyCollection(params); - expectToBePromise(classifyCollectionPromise); - - // assert that create request was called - expect(createRequestMock).toHaveBeenCalledTimes(1); - }); }); + describe('negative tests', () => { beforeAll(() => { missingParamsMock.mockReturnValue(missingParamsError); }); test('should convert a `null` value for `params` to an empty object', done => { - natural_language_classifier.classifyCollection(null, () => { + naturalLanguageClassifier.classifyCollection(null, () => { checkForEmptyObject(missingParamsMock); done(); }); @@ -220,26 +178,14 @@ describe('classifyCollection', () => { // required parameters for this method const requiredParams = ['classifier_id', 'collection']; - natural_language_classifier.classifyCollection({}, err => { - checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); - done(); - }); - }); - - test('should reject promise when required params are not given', done => { - // required parameters for this method - const requiredParams = ['classifier_id', 'collection']; - - const classifyCollectionPromise = natural_language_classifier.classifyCollection(); - expectToBePromise(classifyCollectionPromise); - - classifyCollectionPromise.catch(err => { + naturalLanguageClassifier.classifyCollection({}, err => { checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); done(); }); }); }); }); + describe('createClassifier', () => { describe('positive tests', () => { beforeAll(() => { @@ -249,17 +195,13 @@ describe('createClassifier', () => { // parameters const metadata = 'fake_metadata'; const training_data = 'fake_training_data'; - const metadata_filename = 'fake_metadata_filename'; - const training_data_filename = 'fake_training_data_filename'; const params = { metadata, training_data, - metadata_filename, - training_data_filename, }; // invoke method - natural_language_classifier.createClassifier(params, noop); + naturalLanguageClassifier.createClassifier(params); // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); @@ -272,10 +214,8 @@ describe('createClassifier', () => { const expectedContentType = 'multipart/form-data'; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); expect(options.formData['training_metadata'].data).toEqual(metadata); - expect(options.formData['training_metadata'].filename).toEqual(metadata_filename); expect(options.formData['training_metadata'].contentType).toEqual('application/json'); expect(options.formData['training_data'].data).toEqual(training_data); - expect(options.formData['training_data'].filename).toEqual(training_data_filename); expect(options.formData['training_data'].contentType).toEqual('text/csv'); }); @@ -294,34 +234,18 @@ describe('createClassifier', () => { }, }; - natural_language_classifier.createClassifier(params, noop); + naturalLanguageClassifier.createClassifier(params); checkMediaHeaders(createRequestMock, accept, contentType); }); - - test('should return a promise when no callback is given', () => { - // parameters - const metadata = 'fake_metadata'; - const training_data = 'fake_training_data'; - const params = { - metadata, - training_data, - }; - - // invoke method - const createClassifierPromise = natural_language_classifier.createClassifier(params); - expectToBePromise(createClassifierPromise); - - // assert that create request was called - expect(createRequestMock).toHaveBeenCalledTimes(1); - }); }); + describe('negative tests', () => { beforeAll(() => { missingParamsMock.mockReturnValue(missingParamsError); }); test('should convert a `null` value for `params` to an empty object', done => { - natural_language_classifier.createClassifier(null, () => { + naturalLanguageClassifier.createClassifier(null, () => { checkForEmptyObject(missingParamsMock); done(); }); @@ -331,26 +255,14 @@ describe('createClassifier', () => { // required parameters for this method const requiredParams = ['metadata', 'training_data']; - natural_language_classifier.createClassifier({}, err => { - checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); - done(); - }); - }); - - test('should reject promise when required params are not given', done => { - // required parameters for this method - const requiredParams = ['metadata', 'training_data']; - - const createClassifierPromise = natural_language_classifier.createClassifier(); - expectToBePromise(createClassifierPromise); - - createClassifierPromise.catch(err => { + naturalLanguageClassifier.createClassifier({}, err => { checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); done(); }); }); }); }); + describe('deleteClassifier', () => { describe('positive tests', () => { beforeAll(() => { @@ -364,7 +276,7 @@ describe('deleteClassifier', () => { }; // invoke method - natural_language_classifier.deleteClassifier(params, noop); + naturalLanguageClassifier.deleteClassifier(params); // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); @@ -374,7 +286,7 @@ describe('deleteClassifier', () => { checkUrlAndMethod(options, '/v1/classifiers/{classifier_id}', 'DELETE'); checkCallback(createRequestMock); const expectedAccept = 'application/json'; - const expectedContentType = 'application/json'; + const expectedContentType = undefined; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); expect(options.path['classifier_id']).toEqual(classifier_id); }); @@ -392,32 +304,18 @@ describe('deleteClassifier', () => { }, }; - natural_language_classifier.deleteClassifier(params, noop); + naturalLanguageClassifier.deleteClassifier(params); checkMediaHeaders(createRequestMock, accept, contentType); }); - - test('should return a promise when no callback is given', () => { - // parameters - const classifier_id = 'fake_classifier_id'; - const params = { - classifier_id, - }; - - // invoke method - const deleteClassifierPromise = natural_language_classifier.deleteClassifier(params); - expectToBePromise(deleteClassifierPromise); - - // assert that create request was called - expect(createRequestMock).toHaveBeenCalledTimes(1); - }); }); + describe('negative tests', () => { beforeAll(() => { missingParamsMock.mockReturnValue(missingParamsError); }); test('should convert a `null` value for `params` to an empty object', done => { - natural_language_classifier.deleteClassifier(null, () => { + naturalLanguageClassifier.deleteClassifier(null, () => { checkForEmptyObject(missingParamsMock); done(); }); @@ -427,26 +325,14 @@ describe('deleteClassifier', () => { // required parameters for this method const requiredParams = ['classifier_id']; - natural_language_classifier.deleteClassifier({}, err => { - checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); - done(); - }); - }); - - test('should reject promise when required params are not given', done => { - // required parameters for this method - const requiredParams = ['classifier_id']; - - const deleteClassifierPromise = natural_language_classifier.deleteClassifier(); - expectToBePromise(deleteClassifierPromise); - - deleteClassifierPromise.catch(err => { + naturalLanguageClassifier.deleteClassifier({}, err => { checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); done(); }); }); }); }); + describe('getClassifier', () => { describe('positive tests', () => { beforeAll(() => { @@ -460,7 +346,7 @@ describe('getClassifier', () => { }; // invoke method - natural_language_classifier.getClassifier(params, noop); + naturalLanguageClassifier.getClassifier(params); // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); @@ -470,7 +356,7 @@ describe('getClassifier', () => { checkUrlAndMethod(options, '/v1/classifiers/{classifier_id}', 'GET'); checkCallback(createRequestMock); const expectedAccept = 'application/json'; - const expectedContentType = 'application/json'; + const expectedContentType = undefined; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); expect(options.path['classifier_id']).toEqual(classifier_id); }); @@ -488,32 +374,18 @@ describe('getClassifier', () => { }, }; - natural_language_classifier.getClassifier(params, noop); + naturalLanguageClassifier.getClassifier(params); checkMediaHeaders(createRequestMock, accept, contentType); }); - - test('should return a promise when no callback is given', () => { - // parameters - const classifier_id = 'fake_classifier_id'; - const params = { - classifier_id, - }; - - // invoke method - const getClassifierPromise = natural_language_classifier.getClassifier(params); - expectToBePromise(getClassifierPromise); - - // assert that create request was called - expect(createRequestMock).toHaveBeenCalledTimes(1); - }); }); + describe('negative tests', () => { beforeAll(() => { missingParamsMock.mockReturnValue(missingParamsError); }); test('should convert a `null` value for `params` to an empty object', done => { - natural_language_classifier.getClassifier(null, () => { + naturalLanguageClassifier.getClassifier(null, () => { checkForEmptyObject(missingParamsMock); done(); }); @@ -523,26 +395,14 @@ describe('getClassifier', () => { // required parameters for this method const requiredParams = ['classifier_id']; - natural_language_classifier.getClassifier({}, err => { - checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); - done(); - }); - }); - - test('should reject promise when required params are not given', done => { - // required parameters for this method - const requiredParams = ['classifier_id']; - - const getClassifierPromise = natural_language_classifier.getClassifier(); - expectToBePromise(getClassifierPromise); - - getClassifierPromise.catch(err => { + naturalLanguageClassifier.getClassifier({}, err => { checkRequiredParamsHandling(requiredParams, err, missingParamsMock, createRequestMock); done(); }); }); }); }); + describe('listClassifiers', () => { describe('positive tests', () => { beforeAll(() => { @@ -553,7 +413,7 @@ describe('listClassifiers', () => { const params = {}; // invoke method - natural_language_classifier.listClassifiers(params, noop); + naturalLanguageClassifier.listClassifiers(params); // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); @@ -563,7 +423,7 @@ describe('listClassifiers', () => { checkUrlAndMethod(options, '/v1/classifiers', 'GET'); checkCallback(createRequestMock); const expectedAccept = 'application/json'; - const expectedContentType = 'application/json'; + const expectedContentType = undefined; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); }); @@ -578,30 +438,18 @@ describe('listClassifiers', () => { }, }; - natural_language_classifier.listClassifiers(params, noop); + naturalLanguageClassifier.listClassifiers(params); checkMediaHeaders(createRequestMock, accept, contentType); }); - - test('should return a promise when no callback is given', () => { - // parameters - const params = {}; - - // invoke method - const listClassifiersPromise = natural_language_classifier.listClassifiers(params); - expectToBePromise(listClassifiersPromise); - - // assert that create request was called - expect(createRequestMock).toHaveBeenCalledTimes(1); - }); test('should not have any problems when no parameters are passed in', () => { // invoke the method - natural_language_classifier.listClassifiers({}, noop); + naturalLanguageClassifier.listClassifiers(); checkDefaultSuccessArgs(createRequestMock); }); test('should use argument as callback function if only one is passed in', () => { // invoke the method - natural_language_classifier.listClassifiers(noop); + naturalLanguageClassifier.listClassifiers(noop); checkDefaultSuccessArgs(createRequestMock); }); });