import { addBasePath } from '../helpers' ; import { registerRestoreRoutes } from './restore' ; import { RouterMock , routeDependencies , RequestMock } from '../../test/helpers' ; describe ( '[Snapshot and Restore API Routes] Restore' , ( ) => { const mockEsShard = { type : 'SNAPSHOT' , source : { } , target : { } , index : { size : { } , files : { } } , } ; const router = new RouterMock ( ) ; beforeAll ( ( ) => { registerRestoreRoutes ( { ... routeDependencies , router , } ) ; } ) ; /**
     * ES APIs used by these endpoints
     */ const indicesRecoveryFn = router . getMockApiFn ( 'indices.recovery' ) ; const restoreSnapshotFn = router . getMockApiFn ( 'snapshot.restore' ) ; describe ( 'Restore snapshot' , ( ) => { const mockRequest = { method : 'post' , path : addBasePath ( 'restore/{repository}/{snapshot}' ) , params : { repository : 'foo' , snapshot : 'snapshot-1' , } , body : { } , } ; it ( 'should return successful response from ES' , async ( ) => { const mockEsResponse = { acknowledged : true } ; restoreSnapshotFn . mockResolvedValue ( { body : mockEsResponse } ) ; await expect ( router . runRequest ( mockRequest ) ) . resolves . toEqual ( { body : mockEsResponse , } ) ; } ) ; it ( 'should throw if ES error' , async ( ) => { restoreSnapshotFn . mockRejectedValue ( new Error ( ) ) ; await expect ( router . runRequest ( mockRequest ) ) . rejects . toThrowError ( ) ; } ) ; } ) ; describe ( 'getAllHandler()' , ( ) => { const mockRequest = { method : 'get' , path : addBasePath ( 'restores' ) , } ; it ( 'should arrify and filter restore shards returned from ES' , async ( ) => { const mockEsResponse = { fooIndex : { shards : [ mockEsShard ] , } , barIndex : { shards : [ mockEsShard , mockEsShard ] , } , testIndex : { shards : [ { ... mockEsShard , type : 'EMPTY_STORE' , } , ] , } , } ; indicesRecoveryFn . mockResolvedValue ( { body : mockEsResponse } ) ; const expectedResponse = [ { index : 'fooIndex' , shards : [ { } ] , isComplete : false , latestActivityTimeInMillis : 0 , } , { index : 'barIndex' , shards : [ { } , { } ] , isComplete : false , latestActivityTimeInMillis : 0 , } , ] ; await expect ( router . runRequest ( mockRequest ) ) . resolves . toEqual ( { body : expectedResponse , } ) ; } ) ; it ( 'should return empty array if no repositories returned from ES' , async ( ) => { const mockEsResponse = { } ; indicesRecoveryFn . mockResolvedValue ( { body : mockEsResponse } ) ; const expectedResponse = [ ] ; await expect ( router . runRequest ( mockRequest ) ) . resolves . toEqual ( { body : expectedResponse , } ) ; } ) ; it ( 'should throw if ES error' , async ( ) => { indicesRecoveryFn . mockRejectedValue ( new Error ( ) ) ; await expect ( router . runRequest ( mockRequest ) ) . rejects . toThrowError ( ) ; } ) ; } ) ; } ) ;