Skip to content

Commit

Permalink
[ML] Update test for DFA action_delete, rename deleteDataFrameAnalyti…
Browse files Browse the repository at this point in the history
…csJobSchema, and clean up
  • Loading branch information
qn895 committed May 25, 2020
1 parent e5e618f commit ba82079
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ import { render } from '@testing-library/react';
import * as CheckPrivilige from '../../../../../capabilities/check_capabilities';

import { DeleteAction } from './action_delete';

import { coreMock as mockCoreServices } from '../../../../../../../../../../src/core/public/mocks';
import mockAnalyticsListItem from './__mocks__/analytics_list_item.json';

jest.mock('../../../../../capabilities/check_capabilities', () => ({
checkPermission: jest.fn(() => false),
createPermissionFailureMessage: jest.fn(),
}));

jest.mock('../../../../../../application/util/dependency_cache', () => ({
getToastNotifications: () => ({ addSuccess: jest.fn(), addDanger: jest.fn() }),
}));

jest.mock('../../../../../contexts/kibana', () => ({
useMlKibana: () => ({
services: mockCoreServices.createStart(),
}),
}));

describe('DeleteAction', () => {
test('When canDeleteDataFrameAnalytics permission is false, button should be disabled.', () => {
const { getByTestId } = render(<DeleteAction item={mockAnalyticsListItem} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const DeleteAction: FC<DeleteActionProps> = ({ item }) => {
const [indexPatternExists, setIndexPatternExists] = useState<boolean>(false);

const { savedObjects, notifications } = useMlKibana().services;
const { toasts } = notifications;
const savedObjectsClient = savedObjects.client;

const indexName = item.config.dest.index;
Expand All @@ -66,11 +65,14 @@ export const DeleteAction: FC<DeleteActionProps> = ({ item }) => {
setIndexPatternExists(true);
}
} catch (error) {
const { toasts } = notifications;

toasts.addDanger(
i18n.translate(
'xpack.ml.dataframe.analyticsList.errorWithCheckingIfIndexPatternExistsNotificationErrorMessage',
{
defaultMessage: 'An error occurred checking if index pattern ${indexPattern} exists',
defaultMessage:
'An error occurred checking if index pattern ${indexPattern} exists: {error}',
values: { indexPattern: indexName, error: JSON.stringify(error) },
}
)
Expand All @@ -84,11 +86,13 @@ export const DeleteAction: FC<DeleteActionProps> = ({ item }) => {
setUserCanDeleteIndex(true);
}
} catch (error) {
const { toasts } = notifications;
toasts.addDanger(
i18n.translate(
'xpack.ml.dataframe.analyticsList.errorWithCheckingIfUserCanDeleteIndexNotificationErrorMessage',
{
defaultMessage: 'An error occurred checking if user can delete ${destinationIndex}',
defaultMessage:
'An error occurred checking if user can delete ${destinationIndex} exists: {error}',
values: { destinationIndex: indexName, error: JSON.stringify(error) },
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,22 @@ export class IndexPatternHandler {
constructor(private savedObjectsClient: SavedObjectsClientContract) {}
// returns a id based on an index pattern name
async getIndexPatternId(indexName: string) {
try {
const response = await this.savedObjectsClient.find<IIndexPattern>({
type: 'index-pattern',
perPage: 10,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
});
const response = await this.savedObjectsClient.find<IIndexPattern>({
type: 'index-pattern',
perPage: 10,
search: `"${indexName}"`,
searchFields: ['title'],
fields: ['title'],
});

const ip = response.saved_objects.find(
obj => obj.attributes.title.toLowerCase() === indexName.toLowerCase()
);
const ip = response.saved_objects.find(
obj => obj.attributes.title.toLowerCase() === indexName.toLowerCase()
);

return ip !== undefined ? ip.id : undefined;
} catch (error) {
throw error;
}
return ip?.id;
}

async deleteIndexPatternById(indexId: string) {
try {
return await this.savedObjectsClient.delete('index-pattern', indexId);
} catch (error) {
throw error;
}
return await this.savedObjectsClient.delete('index-pattern', indexId);
}
}
29 changes: 4 additions & 25 deletions x-pack/plugins/ml/server/routes/data_frame_analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { RequestHandlerContext } from 'kibana/server';
import { i18n } from '@kbn/i18n';
import { wrapError } from '../client/error_wrapper';
import { analyticsAuditMessagesProvider } from '../models/data_frame_analytics/analytics_audit_messages';
import { RouteInitialization } from '../types';
Expand All @@ -15,7 +14,7 @@ import {
dataAnalyticsExplainSchema,
analyticsIdSchema,
stopsDataFrameAnalyticsJobQuerySchema,
analyticsIdIndexSchema,
deleteDataFrameAnalyticsJobSchema,
} from './schemas/data_analytics_schema';
import { IndexPatternHandler } from '../models/data_frame_analytics/index_patterns';

Expand Down Expand Up @@ -316,7 +315,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
path: '/api/ml/data_frame/analytics/{analyticsId}',
validate: {
params: analyticsIdSchema,
query: analyticsIdIndexSchema,
query: deleteDataFrameAnalyticsJobSchema,
},
options: {
tags: ['access:ml:canDeleteDataFrameAnalytics'],
Expand Down Expand Up @@ -357,17 +356,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
deleteTargetIndexAcknowledged = true;
} catch (deleteIndexError) {
errorsEncountered.push({
msg: i18n.translate(
'xpack.ml.dataframe.analyticsList.errorDeleteIndexNotificationErrorMessage',
{
defaultMessage:
'An error occurred deleting destination index {destinationIndex}: {error}',
values: {
destinationIndex,
error: JSON.stringify(deleteIndexError),
},
}
),
msg: `An error occurred deleting destination index ${destinationIndex}`,
});
}
} else {
Expand All @@ -385,17 +374,7 @@ export function dataFrameAnalyticsRoutes({ router, mlLicense }: RouteInitializat
deleteIndexPatternAcknowledged = true;
} catch (deleteIndexPatternError) {
errorsEncountered.push({
msg: i18n.translate(
'xpack.ml.dataframe.analyticsList.errorDeleteIndexPatternNotificationErrorMessage',
{
defaultMessage:
'An error occurred deleting index pattern {destinationIndex}: {error}',
values: {
destinationIndex,
error: JSON.stringify(deleteIndexPatternError),
},
}
),
msg: `An error occurred deleting index pattern ${destinationIndex}`,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const analyticsIdSchema = schema.object({
analyticsId: schema.string(),
});

export const analyticsIdIndexSchema = schema.object({
export const deleteDataFrameAnalyticsJobSchema = schema.object({
/**
* Analytics Destination Index
*/
Expand Down

0 comments on commit ba82079

Please sign in to comment.