|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { SavedObjectsClientContract, SavedObjectsFindOptions } from 'opensearch-dashboards/public'; |
| 7 | +import { SearchAPI, SearchAPIDependencies } from './search_api'; |
| 8 | +import { ISearchStart } from 'src/plugins/data/public'; |
| 9 | +import { IUiSettingsClient } from 'opensearch-dashboards/public'; |
| 10 | + |
| 11 | +jest.mock('rxjs', () => ({ |
| 12 | + combineLatest: jest.fn().mockImplementation((obj) => obj), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../../../data/public', () => ({ |
| 16 | + getSearchParamsFromRequest: jest.fn().mockImplementation((obj, _) => obj), |
| 17 | +})); |
| 18 | + |
| 19 | +interface MockSearch { |
| 20 | + params?: Record<string, unknown>; |
| 21 | + dataSourceId?: string; |
| 22 | + pipe: () => {}; |
| 23 | +} |
| 24 | + |
| 25 | +describe('SearchAPI.search', () => { |
| 26 | + // This will only test that searchApiParams were correctly set. As such, every other function can be mocked |
| 27 | + const getSearchAPI = (dataSourceEnabled: boolean) => { |
| 28 | + const savedObjectsClient = {} as SavedObjectsClientContract; |
| 29 | + |
| 30 | + const searchStartMock = {} as ISearchStart; |
| 31 | + searchStartMock.search = jest.fn().mockImplementation((obj, _) => { |
| 32 | + const mockedSearchResults = {} as MockSearch; |
| 33 | + mockedSearchResults.params = obj; |
| 34 | + mockedSearchResults.pipe = jest.fn().mockReturnValue(mockedSearchResults.params); |
| 35 | + return mockedSearchResults; |
| 36 | + }); |
| 37 | + |
| 38 | + const uiSettings = {} as IUiSettingsClient; |
| 39 | + uiSettings.get = jest.fn().mockReturnValue(0); |
| 40 | + uiSettings.get.bind = jest.fn().mockReturnValue(0); |
| 41 | + |
| 42 | + const dependencies = { |
| 43 | + savedObjectsClient, |
| 44 | + dataSourceEnabled, |
| 45 | + search: searchStartMock, |
| 46 | + uiSettings, |
| 47 | + } as SearchAPIDependencies; |
| 48 | + const searchAPI = new SearchAPI(dependencies); |
| 49 | + searchAPI.findDataSourceIdbyName = jest.fn().mockImplementation((name) => { |
| 50 | + if (!dataSourceEnabled) { |
| 51 | + throw new Error(); |
| 52 | + } |
| 53 | + if (name === 'exampleName') { |
| 54 | + return Promise.resolve('some-id'); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + return searchAPI; |
| 59 | + }; |
| 60 | + |
| 61 | + test('If MDS is disabled and there is no datasource, return params without datasource id', async () => { |
| 62 | + const searchAPI = getSearchAPI(false); |
| 63 | + const requests = [{ name: 'example-id' }]; |
| 64 | + const fetchParams = ((await searchAPI.search(requests)) as unknown) as MockSearch[]; |
| 65 | + expect(fetchParams[0].params).toBe(requests[0]); |
| 66 | + expect(fetchParams[0].hasOwnProperty('dataSourceId')).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + test('If MDS is disabled and there is a datasource, it should throw an errorr', () => { |
| 70 | + const searchAPI = getSearchAPI(false); |
| 71 | + const requests = [{ name: 'example-id', data_source_name: 'non-existent-datasource' }]; |
| 72 | + expect(searchAPI.search(requests)).rejects.toThrowError(); |
| 73 | + }); |
| 74 | + |
| 75 | + test('If MDS is enabled and there is no datasource, return params without datasource id', async () => { |
| 76 | + const searchAPI = getSearchAPI(true); |
| 77 | + const requests = [{ name: 'example-id' }]; |
| 78 | + const fetchParams = ((await searchAPI.search(requests)) as unknown) as MockSearch[]; |
| 79 | + expect(fetchParams[0].params).toBe(requests[0]); |
| 80 | + expect(fetchParams[0].hasOwnProperty('dataSourceId')).toBe(false); |
| 81 | + }); |
| 82 | + |
| 83 | + test('If MDS is enabled and there is a datasource, return params with datasource id', async () => { |
| 84 | + const searchAPI = getSearchAPI(true); |
| 85 | + const requests = [{ name: 'example-id', data_source_name: 'exampleName' }]; |
| 86 | + const fetchParams = ((await searchAPI.search(requests)) as unknown) as MockSearch[]; |
| 87 | + expect(fetchParams[0].hasOwnProperty('params')).toBe(true); |
| 88 | + expect(fetchParams[0].dataSourceId).toBe('some-id'); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('SearchAPI.findDataSourceIdbyName', () => { |
| 93 | + const savedObjectsClient = {} as SavedObjectsClientContract; |
| 94 | + savedObjectsClient.find = jest.fn().mockImplementation((query: SavedObjectsFindOptions) => { |
| 95 | + if (query.search === `"uniqueDataSource"`) { |
| 96 | + return Promise.resolve({ |
| 97 | + total: 1, |
| 98 | + savedObjects: [{ id: 'some-datasource-id', attributes: { title: 'uniqueDataSource' } }], |
| 99 | + }); |
| 100 | + } else if (query.search === `"duplicateDataSource"`) { |
| 101 | + return Promise.resolve({ |
| 102 | + total: 2, |
| 103 | + savedObjects: [ |
| 104 | + { id: 'some-datasource-id', attributes: { title: 'duplicateDataSource' } }, |
| 105 | + { id: 'some-other-datasource-id', attributes: { title: 'duplicateDataSource' } }, |
| 106 | + ], |
| 107 | + }); |
| 108 | + } else if (query.search === `"DataSource"`) { |
| 109 | + return Promise.resolve({ |
| 110 | + total: 2, |
| 111 | + savedObjects: [ |
| 112 | + { id: 'some-datasource-id', attributes: { title: 'DataSource' } }, |
| 113 | + { id: 'some-other-datasource-id', attributes: { title: 'DataSource Copy' } }, |
| 114 | + ], |
| 115 | + }); |
| 116 | + } else { |
| 117 | + return Promise.resolve({ |
| 118 | + total: 0, |
| 119 | + savedObjects: [], |
| 120 | + }); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + const getSearchAPI = (dataSourceEnabled: boolean) => { |
| 125 | + const dependencies = { savedObjectsClient, dataSourceEnabled } as SearchAPIDependencies; |
| 126 | + return new SearchAPI(dependencies); |
| 127 | + }; |
| 128 | + |
| 129 | + test('If dataSource is disabled, throw error', () => { |
| 130 | + const searchAPI = getSearchAPI(false); |
| 131 | + expect(searchAPI.findDataSourceIdbyName('nonexistentDataSource')).rejects.toThrowError( |
| 132 | + 'data_source_name cannot be used because data_source.enabled is false' |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + test('If dataSource is enabled but no matching dataSourceName, then throw error', () => { |
| 137 | + const searchAPI = getSearchAPI(true); |
| 138 | + expect(searchAPI.findDataSourceIdbyName('nonexistentDataSource')).rejects.toThrowError( |
| 139 | + 'Expected exactly 1 result for data_source_name "nonexistentDataSource" but got 0 results' |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + test('If dataSource is enabled but multiple dataSourceNames, then throw error', () => { |
| 144 | + const searchAPI = getSearchAPI(true); |
| 145 | + expect(searchAPI.findDataSourceIdbyName('duplicateDataSource')).rejects.toThrowError( |
| 146 | + 'Expected exactly 1 result for data_source_name "duplicateDataSource" but got 2 results' |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + test('If dataSource is enabled but only one dataSourceName, then return id', async () => { |
| 151 | + const searchAPI = getSearchAPI(true); |
| 152 | + expect(await searchAPI.findDataSourceIdbyName('uniqueDataSource')).toBe('some-datasource-id'); |
| 153 | + }); |
| 154 | + |
| 155 | + test('If dataSource is enabled and the dataSourceName is a prefix of another, ensure the prefix is only returned', async () => { |
| 156 | + const searchAPI = getSearchAPI(true); |
| 157 | + expect(await searchAPI.findDataSourceIdbyName('DataSource')).toBe('some-datasource-id'); |
| 158 | + }); |
| 159 | +}); |
0 commit comments