Skip to content

Commit

Permalink
add try catch and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed Jul 9, 2024
1 parent 1956444 commit 641b9b1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
const globalConfig = await this.globalConfig$.pipe(first()).toPromise();
const isPermissionControlEnabled = globalConfig.savedObjects.permission.enabled === true;

this.client = new WorkspaceClient(core);
this.client = new WorkspaceClient(core, this.logger);

await this.client.setup(core);

Expand Down
13 changes: 13 additions & 0 deletions src/plugins/workspace/server/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,17 @@ describe('workspace utils', () => {
dataSources[0]
);
});

it('should clear default data source if there is no new data source', async () => {
const savedObjectsClient = savedObjectsClientMock.create();
const uiSettings = uiSettingsServiceMock.createStartContract();
const uiSettingsClient = uiSettings.asScopedToClient(savedObjectsClient);
const dataSources: string[] = [];
uiSettingsClient.get = jest.fn().mockResolvedValue('');
await checkAndSetDefaultDataSource(uiSettingsClient, dataSources, true);
expect(uiSettingsClient.set).toHaveBeenCalledWith(
DEFAULT_DATA_SOURCE_UI_SETTINGS_ID,
undefined
);
});
});
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export const getDataSourcesList = (client: SavedObjectsClientContract, workspace
export const checkAndSetDefaultDataSource = async (
uiSettingsClient: IUiSettingsClient,
dataSources: string[],
isNeededCheck: boolean
needCheck: boolean
) => {
if (dataSources?.length > 0) {
if (!isNeededCheck) {
if (!needCheck) {
// Create# Will set first data source as default data source.
await uiSettingsClient.set(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, dataSources[0]);
} else {
Expand Down
22 changes: 14 additions & 8 deletions src/plugins/workspace/server/workspace_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

import { WorkspaceClient } from './workspace_client';

import { coreMock, httpServerMock, uiSettingsServiceMock } from '../../../core/server/mocks';
import {
coreMock,
httpServerMock,
uiSettingsServiceMock,
loggingSystemMock,
} from '../../../core/server/mocks';
import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../data_source/common';
import { SavedObjectsServiceStart, SavedObjectsClientContract } from '../../../core/server';
import { IRequestDetail } from './types';
Expand All @@ -15,6 +20,7 @@ const coreSetup = coreMock.createSetup();
const mockWorkspaceId = 'workspace_id';
const mockWorkspaceName = 'workspace_name';
const mockCheckAndSetDefaultDataSource = jest.fn();
const logger = loggingSystemMock.create().get();

jest.mock('./utils', () => ({
generateRandomId: () => mockWorkspaceId,
Expand Down Expand Up @@ -62,7 +68,7 @@ describe('#WorkspaceClient', () => {
} as unknown) as IRequestDetail;

it('create# should not call addToWorkspaces if no data sources passed', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);

Expand All @@ -75,7 +81,7 @@ describe('#WorkspaceClient', () => {
});

it('create# should call addToWorkspaces with passed data sources normally', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);

Expand All @@ -91,7 +97,7 @@ describe('#WorkspaceClient', () => {
});

it('create# should call set default data source after creating', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);
client?.setUiSettings(uiSettings);
Expand All @@ -108,7 +114,7 @@ describe('#WorkspaceClient', () => {
});

it('update# should not call addToWorkspaces if no new data sources added', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);

Expand All @@ -122,7 +128,7 @@ describe('#WorkspaceClient', () => {
});

it('update# should call deleteFromWorkspaces if there is data source to be removed', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);

Expand All @@ -140,7 +146,7 @@ describe('#WorkspaceClient', () => {
]);
});
it('update# should calculate data sources to be added and to be removed', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);

Expand All @@ -159,7 +165,7 @@ describe('#WorkspaceClient', () => {
});

it('update# should call set default data source with check after updating', async () => {
const client = new WorkspaceClient(coreSetup);
const client = new WorkspaceClient(coreSetup, logger);
await client.setup(coreSetup);
client?.setSavedObjects(savedObjects);
client?.setUiSettings(uiSettings);
Expand Down
20 changes: 14 additions & 6 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SavedObjectsServiceStart,
UiSettingsServiceStart,
WORKSPACE_TYPE,
Logger,
} from '../../../core/server';
import { updateWorkspaceState, getWorkspaceState } from '../../../core/server/utils';
import {
Expand All @@ -37,11 +38,13 @@ const DUPLICATE_WORKSPACE_NAME_ERROR = i18n.translate('workspace.duplicate.name.

export class WorkspaceClient implements IWorkspaceClientImpl {
private setupDep: CoreSetup;
private logger: Logger;
private savedObjects?: SavedObjectsServiceStart;
private uiSettings?: UiSettingsServiceStart;

constructor(core: CoreSetup) {
constructor(core: CoreSetup, logger: Logger) {
this.setupDep = core;
this.logger = logger;
}

private getScopedClientWithoutPermission(
Expand Down Expand Up @@ -133,11 +136,16 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
});
// Set first data source as default after creating workspace
const uiSettingsClient = this.uiSettings.asScopedToClient(client);
await checkAndSetDefaultDataSource(uiSettingsClient, dataSources, false);
// Reset workspace state
updateWorkspaceState(requestDetail.request, {
requestWorkspaceId: rawState.requestWorkspaceId,
});
try {
await checkAndSetDefaultDataSource(uiSettingsClient, dataSources, false);
} catch (e) {
this.logger.error('Set default data source error');
} finally {
// Reset workspace state
updateWorkspaceState(requestDetail.request, {
requestWorkspaceId: rawState.requestWorkspaceId,
});
}
}

return {
Expand Down

0 comments on commit 641b9b1

Please sign in to comment.