import * as protos from '../protos/protos' ; import * as assert from 'assert' ; import * as sinon from 'sinon' ; import { SinonStub } from 'sinon' ; import { describe , it , beforeEach , afterEach } from 'mocha' ; import * as regioninstancesModule from '../src' ; import { GoogleAuth , protobuf } from 'google-gax' ; function generateSampleMessage < T extends object > ( instance ) { const filledObject = ( instance . constructor as typeof protobuf . Message ) . toObject ( instance as protobuf . Message < T > , { defaults : true } ) ; return ( instance . constructor as typeof protobuf . Message ) . fromObject ( filledObject ) as T ; } function stubSimpleCall < ResponseType > ( response ? , error ? ) { return error ? sinon . stub ( ) . rejects ( error ) : sinon . stub ( ) . resolves ( [ response ] ) ; } function stubSimpleCallWithCallback < ResponseType > ( response ? , error ? ) { return error ? sinon . stub ( ) . callsArgWith ( 2 , error ) : sinon . stub ( ) . callsArgWith ( 2 , null , response ) ; } describe ( 'v1.RegionInstancesClient' , ( ) => { let googleAuth ; beforeEach ( ( ) => { googleAuth = { getClient : sinon . stub ( ) . resolves ( { getRequestHeaders : sinon . stub ( ) . resolves ( { Authorization : 'Bearer SOME_TOKEN' } ) , } ) , } as unknown as GoogleAuth ; } ) ; afterEach ( ( ) => { sinon . restore ( ) ; } ) ; it ( 'has servicePath' , ( ) => { const servicePath = regioninstancesModule . v1 . RegionInstancesClient . servicePath ; assert ( servicePath ) ; } ) ; it ( 'has apiEndpoint' , ( ) => { const apiEndpoint = regioninstancesModule . v1 . RegionInstancesClient . apiEndpoint ; assert ( apiEndpoint ) ; } ) ; it ( 'has port' , ( ) => { const port = regioninstancesModule . v1 . RegionInstancesClient . port ; assert ( port ) ; assert ( typeof port === 'number' ) ; } ) ; it ( 'should create a client with no option' , ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( ) ; assert ( client ) ; } ) ; it ( 'should create a client with gRPC fallback' , ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( { fallback : true , } ) ; assert ( client ) ; } ) ; it ( 'has initialize method and supports deferred initialization' , async ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; assert . strictEqual ( client . regionInstancesStub , undefined ) ; await client . initialize ( ) ; assert ( client . regionInstancesStub ) ; } ) ; it ( 'has close method' , ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; client . close ( ) ; } ) ; it ( 'has getProjectId method' , async ( ) => { const fakeProjectId = 'fake-project-id' ; const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; client . auth . getProjectId = sinon . stub ( ) . resolves ( fakeProjectId ) ; const result = await client . getProjectId ( ) ; assert . strictEqual ( result , fakeProjectId ) ; assert ( ( client . auth . getProjectId as SinonStub ) . calledWithExactly ( ) ) ; } ) ; it ( 'has getProjectId method with callback' , async ( ) => { const fakeProjectId = 'fake-project-id' ; const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; client . auth . getProjectId = sinon . stub ( ) . callsArgWith ( 0 , null , fakeProjectId ) ; const promise = new Promise ( ( resolve , reject ) => { client . getProjectId ( ( err ? , projectId ? ) => { if ( err ) { reject ( err ) ; } else { resolve ( projectId ) ; } } ) ; } ) ; const result = await promise ; assert . strictEqual ( result , fakeProjectId ) ; } ) ; describe ( 'bulkInsert' , ( ) => { it ( 'invokes bulkInsert without error' , async ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; client . initialize ( ) ; const request = generateSampleMessage ( new protos . google . cloud . compute . v1 . BulkInsertRegionInstanceRequest ( ) ) ; request . project = '' ; const expectedHeaderRequestParams = 'project=' ; const expectedOptions = { otherArgs : { headers : { 'x-goog-request-params' : expectedHeaderRequestParams , } , } , } ; const expectedResponse = generateSampleMessage ( new protos . google . cloud . compute . v1 . Operation ( ) ) ; client . innerApiCalls . bulkInsert = stubSimpleCall ( expectedResponse ) ; const [ response ] = await client . bulkInsert ( request ) ; assert . deepStrictEqual ( response . latestResponse , expectedResponse ) ; assert ( ( client . innerApiCalls . bulkInsert as SinonStub ) . getCall ( 0 ) . calledWith ( request , expectedOptions , undefined ) ) ; } ) ; it ( 'invokes bulkInsert without error using callback' , async ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; client . initialize ( ) ; const request = generateSampleMessage ( new protos . google . cloud . compute . v1 . BulkInsertRegionInstanceRequest ( ) ) ; request . project = '' ; const expectedHeaderRequestParams = 'project=' ; const expectedOptions = { otherArgs : { headers : { 'x-goog-request-params' : expectedHeaderRequestParams , } , } , } ; const expectedResponse = generateSampleMessage ( new protos . google . cloud . compute . v1 . Operation ( ) ) ; client . innerApiCalls . bulkInsert = stubSimpleCallWithCallback ( expectedResponse ) ; const promise = new Promise ( ( resolve , reject ) => { client . bulkInsert ( request , ( err ? , result ? ) => { if ( err ) { reject ( err ) ; } else { resolve ( result ) ; } } ) ; } ) ; const response = await promise ; assert . deepStrictEqual ( response , expectedResponse ) ; assert ( ( client . innerApiCalls . bulkInsert as SinonStub ) . getCall ( 0 ) . calledWith ( request , expectedOptions ) ) ; } ) ; it ( 'invokes bulkInsert with error' , async ( ) => { const client = new regioninstancesModule . v1 . RegionInstancesClient ( { auth : googleAuth , projectId : 'bogus' , } ) ; client . initialize ( ) ; const request = generateSampleMessage ( new protos . google . cloud . compute . v1 . BulkInsertRegionInstanceRequest ( ) ) ; request . project = '' ; const expectedHeaderRequestParams = 'project=' ; const expectedOptions = { otherArgs : { headers : { 'x-goog-request-params' : expectedHeaderRequestParams , } , } , } ; const expectedError = new Error ( 'expected' ) ; client . innerApiCalls . bulkInsert = stubSimpleCall ( undefined , expectedError ) ; await assert . rejects ( client . bulkInsert ( request ) , expectedError ) ; assert ( ( client . innerApiCalls . bulkInsert as SinonStub ) . getCall ( 0 ) . calledWith ( request , expectedOptions , undefined ) ) ; } ) ; } ) ; } ) ;