From e55c8cc78e9219e92c10f40a6efbfa2ff61671d7 Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Wed, 26 Oct 2022 08:42:35 -0400 Subject: [PATCH] fix(Enterprise Usage Reports): regen service to get Pager (#170) * fix(Enterprise Usage Reports): regen service to get Pager This commit contains a re-gen of the service to leverage the new Pagers emitted by the SDK generator. Signed-off-by: Phil Adams --- .secrets.baseline | 50 ++++-- enterprise-usage-reports/v1.ts | 118 ++++++++++++- examples/enterprise-usage-reports.v1.test.js | 22 ++- .../enterprise-usage-reports.v1.test.js | 73 +++++--- test/unit/enterprise-usage-reports.v1.test.js | 167 ++++++++++++++---- 5 files changed, 338 insertions(+), 92 deletions(-) diff --git a/.secrets.baseline b/.secrets.baseline index 2a1d5753..e5a5eae2 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": "package-lock.json|go.sum|^.secrets.baseline$", "lines": null }, - "generated_at": "2022-03-17T11:35:57Z", + "generated_at": "2022-10-26T12:15:59Z", "plugins_used": [ { "name": "AWSKeyDetector" @@ -88,7 +88,7 @@ "hashed_secret": "a2190c299b60e882d9fb33736d5e6ab6ffe42708", "is_secret": false, "is_verified": false, - "line_number": 1004, + "line_number": 1009, "type": "Secret Keyword", "verified_result": null } @@ -98,7 +98,7 @@ "hashed_secret": "aa4ad361672f4c98fd64bf0db80127dd79be59d0", "is_secret": false, "is_verified": false, - "line_number": 271, + "line_number": 305, "type": "Secret Keyword", "verified_result": null }, @@ -106,7 +106,7 @@ "hashed_secret": "0a49d517da94f14c36ac92806c1d15cf95fbca67", "is_secret": false, "is_verified": false, - "line_number": 633, + "line_number": 693, "type": "Secret Keyword", "verified_result": null }, @@ -114,7 +114,7 @@ "hashed_secret": "835e124f126ae02c1c18b3c992a28dde441f5e04", "is_secret": false, "is_verified": false, - "line_number": 1803, + "line_number": 1915, "type": "Secret Keyword", "verified_result": null }, @@ -122,7 +122,7 @@ "hashed_secret": "e058a1c493ad749bd67d368340e9056ed1c2f3ed", "is_secret": false, "is_verified": false, - "line_number": 2469, + "line_number": 2736, "type": "Secret Keyword", "verified_result": null } @@ -132,7 +132,7 @@ "hashed_secret": "c9208613cbf7c65f191e34c0ecc0a24c7e98c471", "is_secret": false, "is_verified": false, - "line_number": 38, + "line_number": 39, "type": "Hex High Entropy String", "verified_result": null } @@ -142,7 +142,7 @@ "hashed_secret": "a2190c299b60e882d9fb33736d5e6ab6ffe42708", "is_secret": false, "is_verified": false, - "line_number": 1285, + "line_number": 1289, "type": "Secret Keyword", "verified_result": null } @@ -198,7 +198,7 @@ "hashed_secret": "f0b0b291c4d22d76db59faaeb94802714dd6d884", "is_secret": false, "is_verified": false, - "line_number": 654, + "line_number": 666, "type": "Hex High Entropy String", "verified_result": null }, @@ -206,7 +206,7 @@ "hashed_secret": "4c41e7a08e8895da0c6066adf33bf389c202b4e2", "is_secret": false, "is_verified": false, - "line_number": 962, + "line_number": 1001, "type": "Hex High Entropy String", "verified_result": null }, @@ -214,17 +214,33 @@ "hashed_secret": "ca8b3e9d1445b3218e3512da63b05c8f26f181e5", "is_secret": false, "is_verified": false, - "line_number": 973, + "line_number": 1012, "type": "Hex High Entropy String", "verified_result": null } ], "test/unit/enterprise-usage-reports.v1.test.js": [ + { + "hashed_secret": "953fd8a5aac905b12bf5d8cff3710d345c209632", + "is_secret": false, + "is_verified": false, + "line_number": 214, + "type": "Hex High Entropy String", + "verified_result": null + }, + { + "hashed_secret": "a0138a9944cb15ff025195706f98f95d9a544eb6", + "is_secret": false, + "is_verified": false, + "line_number": 214, + "type": "Hex High Entropy String", + "verified_result": null + }, { "hashed_secret": "5bfabda9d31f1270745654e91a23ca1ff9b26a95", "is_secret": false, "is_verified": false, - "line_number": 106, + "line_number": 253, "type": "Hex High Entropy String", "verified_result": null }, @@ -232,7 +248,7 @@ "hashed_secret": "d06f0318f9eff64a13844ce2fa66cf9fb37a8cad", "is_secret": false, "is_verified": false, - "line_number": 107, + "line_number": 254, "type": "Hex High Entropy String", "verified_result": null }, @@ -240,7 +256,7 @@ "hashed_secret": "820b2eb95f60d828f82a984ce0321feab7868833", "is_secret": false, "is_verified": false, - "line_number": 108, + "line_number": 255, "type": "Hex High Entropy String", "verified_result": null } @@ -250,7 +266,7 @@ "hashed_secret": "b8473b86d4c2072ca9b08bd28e373e8253e865c4", "is_secret": false, "is_verified": false, - "line_number": 736, + "line_number": 880, "type": "Secret Keyword", "verified_result": null }, @@ -258,13 +274,13 @@ "hashed_secret": "cf4d2385b84329a52ca542285b93d9c4618420df", "is_secret": false, "is_verified": false, - "line_number": 2376, + "line_number": 2862, "type": "Secret Keyword", "verified_result": null } ] }, - "version": "0.13.1+ibm.47.dss", + "version": "0.13.1+ibm.55.dss", "word_list": { "file": null, "hash": null diff --git a/enterprise-usage-reports/v1.ts b/enterprise-usage-reports/v1.ts index 28ecdb05..04cfa12d 100644 --- a/enterprise-usage-reports/v1.ts +++ b/enterprise-usage-reports/v1.ts @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corp. 2020. + * (C) Copyright IBM Corp. 2022. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,22 +15,28 @@ */ /** - * IBM OpenAPI SDK Code Generator Version: 99-SNAPSHOT-629bbb97-20201207-171303 + * IBM OpenAPI SDK Code Generator Version: 3.60.0-13f6e1ba-20221019-164457 */ +/* eslint-disable max-classes-per-file */ +/* eslint-disable no-await-in-loop */ + import * as extend from 'extend'; import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'; import { Authenticator, BaseService, getAuthenticatorFromEnvironment, - getMissingParams, + validateParams, UserOptions, + getQueryParam, } from 'ibm-cloud-sdk-core'; import { getSdkHeaders } from '../lib/common'; /** * Usage reports for IBM Cloud enterprise entities + * + * API Version: 1.0.0-beta.1 */ class EnterpriseUsageReportsV1 extends BaseService { @@ -124,6 +130,22 @@ class EnterpriseUsageReportsV1 extends BaseService { params?: EnterpriseUsageReportsV1.GetResourceUsageReportParams ): Promise> { const _params = { ...params }; + const _requiredParams = []; + const _validParams = [ + 'enterpriseId', + 'accountGroupId', + 'accountId', + 'children', + 'month', + 'billingUnitId', + 'limit', + 'offset', + 'headers', + ]; + const _validationErrors = validateParams(_params, _requiredParams, _validParams); + if (_validationErrors) { + return Promise.reject(_validationErrors); + } const query = { 'enterprise_id': _params.enterpriseId, @@ -181,7 +203,7 @@ namespace EnterpriseUsageReportsV1 { export type Callback = (error: any, response?: Response) => void; /** The body of a service request that returns no response data. */ - export interface Empty {} + export interface EmptyObject {} /** A standard JS object, defined to avoid the limitations of `Object` and `object` */ export interface JsonObject { @@ -333,6 +355,94 @@ namespace EnterpriseUsageReportsV1 { /** Details about all the resources that are included in the aggregated charges. */ resources: ResourceUsage[]; } + + /************************* + * pager classes + ************************/ + + /** + * GetResourceUsageReportPager can be used to simplify the use of getResourceUsageReport(). + */ + export class GetResourceUsageReportPager { + protected _hasNext: boolean; + + protected pageContext: any; + + protected client: EnterpriseUsageReportsV1; + + protected params: EnterpriseUsageReportsV1.GetResourceUsageReportParams; + + /** + * Construct a GetResourceUsageReportPager object. + * + * @param {EnterpriseUsageReportsV1} client - The service client instance used to invoke getResourceUsageReport() + * @param {Object} [params] - The parameters to be passed to getResourceUsageReport() + * @constructor + * @returns {GetResourceUsageReportPager} + */ + constructor( + client: EnterpriseUsageReportsV1, + params?: EnterpriseUsageReportsV1.GetResourceUsageReportParams + ) { + if (params && params.offset) { + throw new Error(`the params.offset field should not be set`); + } + + this._hasNext = true; + this.pageContext = { next: undefined }; + this.client = client; + this.params = JSON.parse(JSON.stringify(params || {})); + } + + /** + * Returns true if there are potentially more results to be retrieved by invoking getNext(). + * @returns {boolean} + */ + public hasNext(): boolean { + return this._hasNext; + } + + /** + * Returns the next page of results by invoking getResourceUsageReport(). + * @returns {Promise} + */ + public async getNext(): Promise { + if (!this.hasNext()) { + throw new Error('No more results available'); + } + + if (this.pageContext.next) { + this.params.offset = this.pageContext.next; + } + const response = await this.client.getResourceUsageReport(this.params); + const { result } = response; + + let next = null; + if (result && result.next) { + if (result.next.href) { + next = getQueryParam(result.next.href, 'offset'); + } + } + this.pageContext.next = next; + if (!this.pageContext.next) { + this._hasNext = false; + } + return result.reports; + } + + /** + * Returns all results by invoking getResourceUsageReport() repeatedly until all pages of results have been retrieved. + * @returns {Promise} + */ + public async getAll(): Promise { + const results: ResourceUsageReport[] = []; + while (this.hasNext()) { + const nextPage = await this.getNext(); + results.push(...nextPage); + } + return results; + } + } } export = EnterpriseUsageReportsV1; diff --git a/examples/enterprise-usage-reports.v1.test.js b/examples/enterprise-usage-reports.v1.test.js index 4e3465b3..84b0578e 100644 --- a/examples/enterprise-usage-reports.v1.test.js +++ b/examples/enterprise-usage-reports.v1.test.js @@ -2,7 +2,7 @@ * @jest-environment node */ /** - * (C) Copyright IBM Corp. 2020. + * (C) Copyright IBM Corp. 2020, 2022. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,7 +56,7 @@ describe('EnterpriseUsageReportsV1', () => { // begin-common - const enterpriseUsageReportsService = EnterpriseUsageReportsV1.newInstance({}); + const enterpriseUsageReportsService = EnterpriseUsageReportsV1.newInstance(); // end-common @@ -65,13 +65,12 @@ describe('EnterpriseUsageReportsV1', () => { let billingMonth = config.billingMonth; test('getResourceUsageReport request example', async () => { - - consoleLogMock.mockImplementation(output => { + consoleLogMock.mockImplementation((output) => { originalLog(output); }); - consoleWarnMock.mockImplementation(output => { + consoleWarnMock.mockImplementation((output) => { + // if an error occurs, display the message and then fail the test originalWarn(output); - // when the test fails we need to print out the error message and stop execution right after it expect(true).toBeFalsy(); }); @@ -81,12 +80,17 @@ describe('EnterpriseUsageReportsV1', () => { const params = { enterpriseId: enterpriseId, month: billingMonth, - limit: 10, }; + const allResults = []; try { - const res = await enterpriseUsageReportsService.getResourceUsageReport(params); - console.log(JSON.stringify(res.result, null, 2)); + const pager = new EnterpriseUsageReportsV1.GetResourceUsageReportPager(enterpriseUsageReportsService, params); + while (pager.hasNext()) { + const nextPage = await pager.getNext(); + expect(nextPage).not.toBeNull(); + allResults.push(...nextPage); + } + console.log(JSON.stringify(allResults, null, 2)); } catch (err) { console.warn(err); } diff --git a/test/integration/enterprise-usage-reports.v1.test.js b/test/integration/enterprise-usage-reports.v1.test.js index 433689a1..0644738e 100644 --- a/test/integration/enterprise-usage-reports.v1.test.js +++ b/test/integration/enterprise-usage-reports.v1.test.js @@ -1,6 +1,6 @@ /* eslint-disable no-console */ /** - * (C) Copyright IBM Corp. 2020. + * (C) Copyright IBM Corp. 2020, 2022. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,7 @@ * limitations under the License. */ -const { readExternalSources } = require('ibm-cloud-sdk-core'); +const { readExternalSources, getQueryParam } = require('ibm-cloud-sdk-core'); const EnterpriseUsageReportsV1 = require('../../dist/enterprise-usage-reports/v1'); const authHelper = require('../resources/auth-helper.js'); @@ -27,19 +27,19 @@ const configFile = 'enterprise_usage_reports.env'; const describe = authHelper.prepareTests(configFile); -let service; -let accountId; -let accountGroupId; -let enterpriseId; -let billingMonth; - describe('EnterpriseUsageReportsV1_integration', () => { + let enterpriseUsageReportsService; + let accountId; + let accountGroupId; + let enterpriseId; + let billingMonth; + test('Init', async () => { - service = EnterpriseUsageReportsV1.newInstance({}); + enterpriseUsageReportsService = EnterpriseUsageReportsV1.newInstance(); const config = readExternalSources(EnterpriseUsageReportsV1.DEFAULT_SERVICE_NAME); - expect(service).not.toBeNull(); + expect(enterpriseUsageReportsService).not.toBeNull(); expect(config).not.toBeNull(); accountId = config.accountId; @@ -67,7 +67,7 @@ describe('EnterpriseUsageReportsV1_integration', () => { offset, }; - const res = await service.getResourceUsageReport(params); + const res = await enterpriseUsageReportsService.getResourceUsageReport(params); expect(res.status).toEqual(200); const { result } = res; @@ -79,7 +79,7 @@ describe('EnterpriseUsageReportsV1_integration', () => { // Determine the offset to use to get the next page. if (result.next) { - offset = getOffsetFromURL(result.next.href); + offset = getQueryParam(result.next.href, 'offset'); } else { offset = null; } @@ -108,7 +108,7 @@ describe('EnterpriseUsageReportsV1_integration', () => { offset, }; - const res = await service.getResourceUsageReport(params); + const res = await enterpriseUsageReportsService.getResourceUsageReport(params); expect(res.status).toEqual(200); const { result } = res; @@ -120,7 +120,7 @@ describe('EnterpriseUsageReportsV1_integration', () => { // Determine the offset to use to get the next page. if (result.next) { - offset = getOffsetFromURL(result.next.href); + offset = getQueryParam(result.next.href, 'offset'); } else { offset = null; } @@ -149,7 +149,7 @@ describe('EnterpriseUsageReportsV1_integration', () => { offset, }; - const res = await service.getResourceUsageReport(params); + const res = await enterpriseUsageReportsService.getResourceUsageReport(params); expect(res.status).toEqual(200); const { result } = res; @@ -161,7 +161,7 @@ describe('EnterpriseUsageReportsV1_integration', () => { // Determine the offset to use to get the next page. if (result.next) { - offset = getOffsetFromURL(result.next.href); + offset = getQueryParam(result.next.href, 'offset'); } else { offset = null; } @@ -176,15 +176,34 @@ describe('EnterpriseUsageReportsV1_integration', () => { // console.log(`getResourceUsageReport() response contained ${numReports} total reports`); expect(numReports).toBeGreaterThan(0); }); -}); -function getOffsetFromURL(urlstring) { - let offset = null; - if (urlstring) { - // We use a bogus "baseurl" in case "urlstring" is a relative url. - // This is fine since we're only trying to retrieve the "offset" query parameter. - const url = new URL(urlstring, 'https://fakehost.com'); - offset = url.searchParams.get('offset'); - } - return offset; -} + test('getResourceUsageReport() via GetResourceUsageReportPager', async () => { + const params = { + accountGroupId, + month: billingMonth, + }; + + const allResults = []; + + // Test getNext(). + let pager = new EnterpriseUsageReportsV1.GetResourceUsageReportPager( + enterpriseUsageReportsService, + params + ); + while (pager.hasNext()) { + const nextPage = await pager.getNext(); + expect(nextPage).not.toBeNull(); + allResults.push(...nextPage); + } + + // Test getAll(). + pager = new EnterpriseUsageReportsV1.GetResourceUsageReportPager( + enterpriseUsageReportsService, + params + ); + const allItems = await pager.getAll(); + expect(allItems).not.toBeNull(); + expect(allItems).toHaveLength(allResults.length); + console.log(`Retrieved a total of ${allResults.length} items(s) with pagination.`); + }); +}); diff --git a/test/unit/enterprise-usage-reports.v1.test.js b/test/unit/enterprise-usage-reports.v1.test.js index 56f51552..21eec3e0 100644 --- a/test/unit/enterprise-usage-reports.v1.test.js +++ b/test/unit/enterprise-usage-reports.v1.test.js @@ -1,5 +1,5 @@ /** - * (C) Copyright IBM Corp. 2020. + * (C) Copyright IBM Corp. 2022. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,13 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -'use strict'; // need to import the whole package to mock getAuthenticatorFromEnvironment const core = require('ibm-cloud-sdk-core'); + const { NoAuthAuthenticator, unitTestUtils } = core; const EnterpriseUsageReportsV1 = require('../../dist/enterprise-usage-reports/v1'); +const nock = require('nock'); + +/* eslint-disable no-await-in-loop */ const { getOptions, @@ -29,27 +32,44 @@ const { checkForSuccessfulExecution, } = unitTestUtils; -const service = { +const enterpriseUsageReportsServiceOptions = { authenticator: new NoAuthAuthenticator(), url: 'https://enterprise.cloud.ibm.com', }; -const enterpriseUsageReportsService = new EnterpriseUsageReportsV1(service); +const enterpriseUsageReportsService = new EnterpriseUsageReportsV1(enterpriseUsageReportsServiceOptions); -// dont actually create a request -const createRequestMock = jest.spyOn(enterpriseUsageReportsService, 'createRequest'); -createRequestMock.mockImplementation(() => Promise.resolve()); +let createRequestMock = null; +function mock_createRequest() { + if (!createRequestMock) { + createRequestMock = jest.spyOn(enterpriseUsageReportsService, 'createRequest'); + createRequestMock.mockImplementation(() => Promise.resolve()); + } +} +function unmock_createRequest() { + if (createRequestMock) { + createRequestMock.mockRestore(); + createRequestMock = null; + } +} // dont actually construct an authenticator const getAuthenticatorMock = jest.spyOn(core, 'getAuthenticatorFromEnvironment'); getAuthenticatorMock.mockImplementation(() => new NoAuthAuthenticator()); -afterEach(() => { - createRequestMock.mockClear(); - getAuthenticatorMock.mockClear(); -}); - describe('EnterpriseUsageReportsV1', () => { + + beforeEach(() => { + mock_createRequest(); + }); + + afterEach(() => { + if (createRequestMock) { + createRequestMock.mockClear(); + } + getAuthenticatorMock.mockClear(); + }); + describe('the newInstance method', () => { test('should use defaults when options not provided', () => { const testInstance = EnterpriseUsageReportsV1.newInstance(); @@ -77,6 +97,7 @@ describe('EnterpriseUsageReportsV1', () => { expect(testInstance).toBeInstanceOf(EnterpriseUsageReportsV1); }); }); + describe('the constructor', () => { test('use user-given service url', () => { const options = { @@ -99,9 +120,10 @@ describe('EnterpriseUsageReportsV1', () => { expect(testInstance.baseOptions.serviceUrl).toBe(EnterpriseUsageReportsV1.DEFAULT_SERVICE_URL); }); }); + describe('getResourceUsageReport', () => { describe('positive tests', () => { - test('should pass the right params to createRequest', () => { + function __getResourceUsageReportTest() { // Construct the params object for operation getResourceUsageReport const enterpriseId = 'abc12340d4bf4e36b0423d209b286f24'; const accountGroupId = 'def456a237b94b9a9238ef024e204c9f'; @@ -111,18 +133,18 @@ describe('EnterpriseUsageReportsV1', () => { const billingUnitId = 'testString'; const limit = 10; const offset = 'testString'; - const params = { - enterpriseId: enterpriseId, - accountGroupId: accountGroupId, - accountId: accountId, - children: children, - month: month, - billingUnitId: billingUnitId, - limit: limit, - offset: offset, + const getResourceUsageReportParams = { + enterpriseId, + accountGroupId, + accountId, + children, + month, + billingUnitId, + limit, + offset, }; - const getResourceUsageReportResult = enterpriseUsageReportsService.getResourceUsageReport(params); + const getResourceUsageReportResult = enterpriseUsageReportsService.getResourceUsageReport(getResourceUsageReportParams); // all methods should return a Promise expectToBePromise(getResourceUsageReportResult); @@ -130,34 +152,49 @@ describe('EnterpriseUsageReportsV1', () => { // assert that create request was called expect(createRequestMock).toHaveBeenCalledTimes(1); - const options = getOptions(createRequestMock); + const mockRequestOptions = getOptions(createRequestMock); - checkUrlAndMethod(options, '/v1/resource-usage-reports', 'GET'); + checkUrlAndMethod(mockRequestOptions, '/v1/resource-usage-reports', 'GET'); const expectedAccept = 'application/json'; const expectedContentType = undefined; checkMediaHeaders(createRequestMock, expectedAccept, expectedContentType); - expect(options.qs['enterprise_id']).toEqual(enterpriseId); - expect(options.qs['account_group_id']).toEqual(accountGroupId); - expect(options.qs['account_id']).toEqual(accountId); - expect(options.qs['children']).toEqual(children); - expect(options.qs['month']).toEqual(month); - expect(options.qs['billing_unit_id']).toEqual(billingUnitId); - expect(options.qs['limit']).toEqual(limit); - expect(options.qs['offset']).toEqual(offset); + expect(mockRequestOptions.qs.enterprise_id).toEqual(enterpriseId); + expect(mockRequestOptions.qs.account_group_id).toEqual(accountGroupId); + expect(mockRequestOptions.qs.account_id).toEqual(accountId); + expect(mockRequestOptions.qs.children).toEqual(children); + expect(mockRequestOptions.qs.month).toEqual(month); + expect(mockRequestOptions.qs.billing_unit_id).toEqual(billingUnitId); + expect(mockRequestOptions.qs.limit).toEqual(limit); + expect(mockRequestOptions.qs.offset).toEqual(offset); + } + + test('should pass the right params to createRequest with enable and disable retries', () => { + // baseline test + __getResourceUsageReportTest(); + + // enable retries and test again + createRequestMock.mockClear(); + enterpriseUsageReportsService.enableRetries(); + __getResourceUsageReportTest(); + + // disable retries and test again + createRequestMock.mockClear(); + enterpriseUsageReportsService.disableRetries(); + __getResourceUsageReportTest(); }); test('should prioritize user-given headers', () => { // parameters const userAccept = 'fake/accept'; const userContentType = 'fake/contentType'; - const params = { + const getResourceUsageReportParams = { headers: { Accept: userAccept, 'Content-Type': userContentType, }, }; - enterpriseUsageReportsService.getResourceUsageReport(params); + enterpriseUsageReportsService.getResourceUsageReport(getResourceUsageReportParams); checkMediaHeaders(createRequestMock, userAccept, userContentType); }); @@ -167,5 +204,65 @@ describe('EnterpriseUsageReportsV1', () => { checkForSuccessfulExecution(createRequestMock); }); }); + + describe('GetResourceUsageReportPager tests', () => { + const serviceUrl = enterpriseUsageReportsServiceOptions.url; + const path = '/v1/resource-usage-reports'; + const mockPagerResponse1 = + '{"next":{"href":"https://myhost.com/somePath?offset=1"},"reports":[{"entity_id":"de129b787b86403db7d3a14be2ae5f76","entity_type":"enterprise","entity_crn":"crn:v1:bluemix:public:enterprise::a/e9a57260546c4b4aa9ebfa316a82e56e::enterprise:de129b787b86403db7d3a14be2ae5f76","entity_name":"Platform-Services","billing_unit_id":"65719a07280a4022a9efa2f6ff4c3369","billing_unit_crn":"crn:v1:bluemix:public:billing::a/3f99f8accbc848ea96f3c61a0ae22c44::billing-unit:65719a07280a4022a9efa2f6ff4c3369","billing_unit_name":"Operations","country_code":"USA","currency_code":"USD","month":"2017-08","billable_cost":13,"non_billable_cost":17,"billable_rated_cost":19,"non_billable_rated_cost":23,"resources":[{"resource_id":"resource_id","billable_cost":13,"billable_rated_cost":19,"non_billable_cost":17,"non_billable_rated_cost":23,"plans":[{"plan_id":"plan_id","pricing_region":"pricing_region","pricing_plan_id":"pricing_plan_id","billable":true,"cost":4,"rated_cost":10,"usage":[{"metric":"UP-TIME","unit":"HOURS","quantity":711.11,"rateable_quantity":700,"cost":123.45,"rated_cost":130,"price":[{"anyKey":"anyValue"}]}]}]}]}],"total_count":2,"limit":1}'; + const mockPagerResponse2 = + '{"reports":[{"entity_id":"de129b787b86403db7d3a14be2ae5f76","entity_type":"enterprise","entity_crn":"crn:v1:bluemix:public:enterprise::a/e9a57260546c4b4aa9ebfa316a82e56e::enterprise:de129b787b86403db7d3a14be2ae5f76","entity_name":"Platform-Services","billing_unit_id":"65719a07280a4022a9efa2f6ff4c3369","billing_unit_crn":"crn:v1:bluemix:public:billing::a/3f99f8accbc848ea96f3c61a0ae22c44::billing-unit:65719a07280a4022a9efa2f6ff4c3369","billing_unit_name":"Operations","country_code":"USA","currency_code":"USD","month":"2017-08","billable_cost":13,"non_billable_cost":17,"billable_rated_cost":19,"non_billable_rated_cost":23,"resources":[{"resource_id":"resource_id","billable_cost":13,"billable_rated_cost":19,"non_billable_cost":17,"non_billable_rated_cost":23,"plans":[{"plan_id":"plan_id","pricing_region":"pricing_region","pricing_plan_id":"pricing_plan_id","billable":true,"cost":4,"rated_cost":10,"usage":[{"metric":"UP-TIME","unit":"HOURS","quantity":711.11,"rateable_quantity":700,"cost":123.45,"rated_cost":130,"price":[{"anyKey":"anyValue"}]}]}]}]}],"total_count":2,"limit":1}'; + + beforeEach(() => { + unmock_createRequest(); + const scope = nock(serviceUrl) + .get(uri => uri.includes(path)) + .reply(200, mockPagerResponse1) + .get(uri => uri.includes(path)) + .reply(200, mockPagerResponse2); + }); + + afterEach(() => { + nock.cleanAll(); + mock_createRequest(); + }); + + test('getNext()', async () => { + const params = { + enterpriseId: 'abc12340d4bf4e36b0423d209b286f24', + accountGroupId: 'def456a237b94b9a9238ef024e204c9f', + accountId: '987abcba31834216b8c726a7dd9eb8d6', + children: true, + month: '2019-06', + billingUnitId: 'testString', + limit: 10, + }; + const allResults = []; + const pager = new EnterpriseUsageReportsV1.GetResourceUsageReportPager(enterpriseUsageReportsService, params); + while (pager.hasNext()) { + const nextPage = await pager.getNext(); + expect(nextPage).not.toBeNull(); + allResults.push(...nextPage); + } + expect(allResults).not.toBeNull(); + expect(allResults).toHaveLength(2); + }); + + test('getAll()', async () => { + const params = { + enterpriseId: 'abc12340d4bf4e36b0423d209b286f24', + accountGroupId: 'def456a237b94b9a9238ef024e204c9f', + accountId: '987abcba31834216b8c726a7dd9eb8d6', + children: true, + month: '2019-06', + billingUnitId: 'testString', + limit: 10, + }; + const pager = new EnterpriseUsageReportsV1.GetResourceUsageReportPager(enterpriseUsageReportsService, params); + const allResults = await pager.getAll(); + expect(allResults).not.toBeNull(); + expect(allResults).toHaveLength(2); + }); + }); }); });