UNPKG

@ibm-cloud/platform-services

Version:

Node.js client library for IBM Cloud Platform Services

95 lines (77 loc) 2.84 kB
/** * @jest-environment node */ /** * (C) Copyright IBM Corp. 2020. * * 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. */ 'use strict'; const GlobalSearchV2 = require('../dist/global-search/v2'); const { readExternalSources } = require('ibm-cloud-sdk-core'); const authHelper = require('../test/resources/auth-helper.js'); // // This file provides an example of how to use the Global Search service. // // The following configuration properties are assumed to be defined: // // The following configuration properties are assumed to be defined in the external configuration file: // GLOBAL_SEARCH_URL=<service url> // GLOBAL_SEARCH_AUTHTYPE=iam // GLOBAL_SEARCH_APIKEY=<IAM api key> // GLOBAL_SEARCH_AUTH_URL=<IAM token service URL - omit this if using the production environment> // // These configuration properties can be exported as environment variables, or stored // in a configuration file and then: // export IBM_CREDENTIALS_FILE=<name of configuration file> // const configFile = 'global_search.env'; const describe = authHelper.prepareTests(configFile); // Save original console.log and console.warn const originalLog = console.log; const originalWarn = console.warn; // Mocks for console.log and console.warn const consoleLogMock = jest.spyOn(console, 'log'); const consoleWarnMock = jest.spyOn(console, 'warn'); describe('GlobalSearchV2', () => { jest.setTimeout(30000); // begin-common const globalSearchService = GlobalSearchV2.newInstance({}); // end-common const config = readExternalSources(GlobalSearchV2.DEFAULT_SERVICE_NAME); test('search request example', async () => { consoleLogMock.mockImplementation(output => { originalLog(output); }); consoleWarnMock.mockImplementation(output => { originalWarn(output); // when the test fails we need to print out the error message and stop execution right after it expect(true).toBeFalsy(); }); const searchCursor = undefined; originalLog('search() result:'); // begin-search const params = { query: 'GST-sdk-*', fields: ['*'], searchCursor: searchCursor, }; try { const res = await globalSearchService.search(params); console.log(JSON.stringify(res.result, null, 2)); } catch (err) { console.warn(err); } // end-search }); });