Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
kristenTian committed Jun 14, 2023
1 parent 3da8bcd commit c479637
Show file tree
Hide file tree
Showing 11 changed files with 409 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { i18n } from '@osd/i18n';
import { getSavedObjects } from './saved_objects';
import { fieldMappings } from './field_mappings';
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types';
import { getSavedObjectsWithDataSource } from '../util';
import { getSavedObjectsWithDataSource, appendDataSourceId } from '../util';

const ecommerceName = i18n.translate('home.sampleData.ecommerceSpecTitle', {
defaultMessage: 'Sample eCommerce orders',
Expand All @@ -44,6 +44,7 @@ const ecommerceDescription = i18n.translate('home.sampleData.ecommerceSpecDescri
const initialAppLinks = [] as AppLinkSchema[];

const DEFAULT_INDEX = 'ff959d40-b880-11e8-a6d9-e546fe2bba5f';
const DASHBOARD_ID = '722b74f0-b882-11e8-a6d9-e546fe2bba5f';

export const ecommerceSpecProvider = function (): SampleDatasetSchema {
return {
Expand All @@ -52,10 +53,9 @@ export const ecommerceSpecProvider = function (): SampleDatasetSchema {
description: ecommerceDescription,
previewImagePath: '/plugins/home/assets/sample_data_resources/ecommerce/dashboard.png',
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/ecommerce/dashboard_dark.png',
overviewDashboard: '722b74f0-b882-11e8-a6d9-e546fe2bba5f',
overviewDashboard: appendDataSourceId(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: (dataSourceId?: string) =>
dataSourceId ? `${dataSourceId}_` + DEFAULT_INDEX : DEFAULT_INDEX,
defaultIndex: appendDataSourceId(DEFAULT_INDEX),
savedObjects: (dataSourceId?: string, dataSourceTitle?: string) =>
getSavedObjectsWithDataSource(getSavedObjects(), dataSourceId, dataSourceTitle),
dataIndices: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { i18n } from '@osd/i18n';
import { getSavedObjects } from './saved_objects';
import { fieldMappings } from './field_mappings';
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types';
import { getSavedObjectsWithDataSource } from '../util';
import { getSavedObjectsWithDataSource, appendDataSourceId } from '../util';

const flightsName = i18n.translate('home.sampleData.flightsSpecTitle', {
defaultMessage: 'Sample flight data',
Expand All @@ -44,6 +44,7 @@ const flightsDescription = i18n.translate('home.sampleData.flightsSpecDescriptio
const initialAppLinks = [] as AppLinkSchema[];

const DEFAULT_INDEX = 'd3d7af60-4c81-11e8-b3d7-01146121b73d';
const DASHBOARD_ID = '7adfa750-4c81-11e8-b3d7-01146121b73d';

export const flightsSpecProvider = function (): SampleDatasetSchema {
return {
Expand All @@ -52,10 +53,9 @@ export const flightsSpecProvider = function (): SampleDatasetSchema {
description: flightsDescription,
previewImagePath: '/plugins/home/assets/sample_data_resources/flights/dashboard.png',
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/flights/dashboard_dark.png',
overviewDashboard: '7adfa750-4c81-11e8-b3d7-01146121b73d',
overviewDashboard: appendDataSourceId(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: (dataSourceId?: string) =>
dataSourceId ? `${dataSourceId}_` + DEFAULT_INDEX : DEFAULT_INDEX,
defaultIndex: appendDataSourceId(DEFAULT_INDEX),
savedObjects: (dataSourceId?: string, dataSourceTitle?: string) =>
getSavedObjectsWithDataSource(getSavedObjects(), dataSourceId, dataSourceTitle),
dataIndices: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { i18n } from '@osd/i18n';
import { getSavedObjects } from './saved_objects';
import { fieldMappings } from './field_mappings';
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types';
import { getSavedObjectsWithDataSource } from '../util';
import { appendDataSourceId, getSavedObjectsWithDataSource } from '../util';

const logsName = i18n.translate('home.sampleData.logsSpecTitle', {
defaultMessage: 'Sample web logs',
Expand All @@ -44,6 +44,7 @@ const logsDescription = i18n.translate('home.sampleData.logsSpecDescription', {
const initialAppLinks = [] as AppLinkSchema[];

const DEFAULT_INDEX = '90943e30-9a47-11e8-b64d-95841ca0b247';
const DASHBOARD_ID = 'edf84fe0-e1a0-11e7-b6d5-4dc382ef7f5b';

export const logsSpecProvider = function (): SampleDatasetSchema {
return {
Expand All @@ -52,10 +53,9 @@ export const logsSpecProvider = function (): SampleDatasetSchema {
description: logsDescription,
previewImagePath: '/plugins/home/assets/sample_data_resources/logs/dashboard.png',
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/logs/dashboard_dark.png',
overviewDashboard: 'edf84fe0-e1a0-11e7-b6d5-4dc382ef7f5b',
overviewDashboard: appendDataSourceId(DASHBOARD_ID),
appLinks: initialAppLinks,
defaultIndex: (dataSourceId?: string) =>
dataSourceId ? `${dataSourceId}_` + DEFAULT_INDEX : DEFAULT_INDEX,
defaultIndex: appendDataSourceId(DEFAULT_INDEX),
savedObjects: (dataSourceId?: string, dataSourceTitle?: string) =>
getSavedObjectsWithDataSource(getSavedObjects(), dataSourceId, dataSourceTitle),
dataIndices: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import { SavedObject } from 'opensearch-dashboards/server';

export const appendDataSourceId = (id: string) => {
return (dataSourceId?: string) => (dataSourceId ? `${dataSourceId}_` + id : id);
};

export const getSavedObjectsWithDataSource = (
saveObjectList: SavedObject[],
dataSourceId?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface SampleDatasetSchema<T = unknown> {
darkPreviewImagePath: string;

// saved object id of main dashboard for sample data set
overviewDashboard: string;
overviewDashboard: (dataSourceId?: string) => string;
appLinks: AppLinkSchema[];

// saved object id of default index-pattern for sample data set
Expand Down
160 changes: 160 additions & 0 deletions src/plugins/home/server/services/sample_data/routes/install.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { CoreSetup, RequestHandlerContext } from 'src/core/server';
import { coreMock, httpServerMock } from '../../../../../../core/server/mocks';
import { flightsSpecProvider } from '../data_sets';
import { SampleDatasetSchema } from '../lib/sample_dataset_registry_types';
import { createInstallRoute } from './install';

const flightsSampleDataset = flightsSpecProvider();

const sampleDatasets: SampleDatasetSchema[] = [flightsSampleDataset];

describe('sample data install route', () => {
let mockCoreSetup: MockedKeys<CoreSetup>;
let mockLogger;
let mockUsageTracker;

beforeEach(() => {
mockCoreSetup = coreMock.createSetup();
mockLogger = {
warn: jest.fn(),
};

mockUsageTracker = {
addInstall: jest.fn(),
addUninstall: jest.fn(),
};
});

it('handler calls expected api with the given request', async () => {
const mockClient = jest.fn().mockResolvedValue(true);

const mockSOClientGetResponse = {
saved_objects: [
{
type: 'dashboard',
id: '12345',
namespaces: ['default'],
attributes: { title: 'dashboard' },
},
],
};
const mockSOClient = { bulkCreate: jest.fn().mockResolvedValue(mockSOClientGetResponse) };

const mockContext = {
core: {
opensearch: {
legacy: {
client: { callAsCurrentUser: mockClient },
},
},
savedObjects: { client: mockSOClient },
},
};
const mockBody = { id: 'flights' };
const mockQuery = {};
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest({
params: mockBody,
query: mockQuery,
});
const mockResponse = httpServerMock.createResponseFactory();

createInstallRoute(
mockCoreSetup.http.createRouter(),
sampleDatasets,
mockLogger,
mockUsageTracker
);

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const handler = mockRouter.post.mock.calls[0][1];

await handler((mockContext as unknown) as RequestHandlerContext, mockRequest, mockResponse);

expect(mockClient.mock.calls[1][1].body.settings).toMatchObject({
index: { number_of_shards: 1, auto_expand_replicas: '0-1' },
});

// expect(mockClient).toBeCalledTimes(2);
expect(mockResponse.ok).toBeCalled();
expect(mockResponse.ok.mock.calls[0][0]).toMatchObject({
body: {
opensearchIndicesCreated: { opensearch_dashboards_sample_data_flights: 13059 },
opensearchDashboardsSavedObjectsLoaded: 20,
},
});
});

it('handler calls expected api with the given request with data source', async () => {
const mockDataSourceId = 'dataSource';

const mockClient = jest.fn().mockResolvedValue(true);

const mockSOClientGetResponse = {
saved_objects: [
{
type: 'dashboard',
id: '12345',
namespaces: ['default'],
attributes: { title: 'dashboard' },
},
],
};
const mockSOClient = {
bulkCreate: jest.fn().mockResolvedValue(mockSOClientGetResponse),
get: jest.fn().mockResolvedValue(mockSOClientGetResponse),
};

const mockContext = {
dataSource: {
opensearch: {
legacy: {
getClient: (id) => {
return {
callAPI: mockClient,
};
},
},
},
},
core: {
savedObjects: { client: mockSOClient },
},
};
const mockBody = { id: 'flights' };
const mockQuery = { data_source_id: mockDataSourceId };
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest({
params: mockBody,
query: mockQuery,
});
const mockResponse = httpServerMock.createResponseFactory();

createInstallRoute(
mockCoreSetup.http.createRouter(),
sampleDatasets,
mockLogger,
mockUsageTracker
);

const mockRouter = mockCoreSetup.http.createRouter.mock.results[0].value;
const handler = mockRouter.post.mock.calls[0][1];

await handler((mockContext as unknown) as RequestHandlerContext, mockRequest, mockResponse);

expect(mockClient.mock.calls[1][1].body.settings).toMatchObject({
index: { number_of_shards: 1 },
});

expect(mockResponse.ok).toBeCalled();
expect(mockResponse.ok.mock.calls[0][0]).toMatchObject({
body: {
opensearchIndicesCreated: { opensearch_dashboards_sample_data_flights: 13059 },
opensearchDashboardsSavedObjectsLoaded: 20,
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/

import { schema } from '@osd/config-schema';
import { IRouter, Logger } from 'src/core/server';
import { IRouter, LegacyCallAPIOptions, Logger } from 'src/core/server';
import { SampleDatasetSchema } from '../lib/sample_dataset_registry_types';
import { createIndexName } from '../lib/create_index_name';
import {
Expand All @@ -44,7 +44,11 @@ const insertDataIntoIndex = (
dataIndexConfig: any,
index: string,
nowReference: string,
caller: (endpoint: string, clientParams?: Record<string, any>, options?: any) => Promise<any>,
caller: (
endpoint: string,
clientParams?: Record<string, any>,
options?: LegacyCallAPIOptions
) => Promise<any>,
logger: Logger
) => {
function updateTimestamps(doc: any) {
Expand Down
Loading

0 comments on commit c479637

Please sign in to comment.