Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { render } from '@testing-library/react';
import React from 'react';
import { TestProviders } from '../../../../common/mock';
import { useAlertPrevalenceFromProcessTree } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import { useAlertPrevalenceFromProcessTree } from '../../shared/hooks/use_alert_prevalence_from_process_tree';
import { useTimelineDataFilters } from '../../../../timelines/containers/use_timeline_data_filters';
import { mockContextValue } from '../../shared/mocks/mock_context';
import { mockDataFormattedForFieldBrowser } from '../../shared/mocks/mock_data_formatted_for_field_browser';
Expand All @@ -17,7 +17,7 @@ import { AnalyzerPreview } from './analyzer_preview';
import { ANALYZER_PREVIEW_TEST_ID } from './test_ids';
import * as mock from '../mocks/mock_analyzer_data';

jest.mock('../../../../common/containers/alerts/use_alert_prevalence_from_process_tree', () => ({
jest.mock('../../shared/hooks/use_alert_prevalence_from_process_tree', () => ({
useAlertPrevalenceFromProcessTree: jest.fn(),
}));
const mockUseAlertPrevalenceFromProcessTree = useAlertPrevalenceFromProcessTree as jest.Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { ANALYZER_PREVIEW_TEST_ID, ANALYZER_PREVIEW_LOADING_TEST_ID } from './te
import { getTreeNodes } from '../utils/analyzer_helpers';
import { ANCESTOR_ID, RULE_INDICES } from '../../shared/constants/field_names';
import { useDocumentDetailsContext } from '../../shared/context';
import { useAlertPrevalenceFromProcessTree } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import type { StatsNode } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import { useAlertPrevalenceFromProcessTree } from '../../shared/hooks/use_alert_prevalence_from_process_tree';
import type { StatsNode } from '../../shared/hooks/use_alert_prevalence_from_process_tree';
import { isActiveTimeline } from '../../../../helpers';
import { getField } from '../../shared/utils';
import { useTimelineDataFilters } from '../../../../timelines/containers/use_timeline_data_filters';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { mockContextValue } from '../../shared/mocks/mock_context';
import { AnalyzerPreviewContainer } from './analyzer_preview_container';
import { useIsInvestigateInResolverActionEnabled } from '../../../../detections/components/alerts_table/timeline_actions/investigate_in_resolver';
import { ANALYZER_PREVIEW_TEST_ID } from './test_ids';
import { useAlertPrevalenceFromProcessTree } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import { useAlertPrevalenceFromProcessTree } from '../../shared/hooks/use_alert_prevalence_from_process_tree';
import * as mock from '../mocks/mock_analyzer_data';
import {
EXPANDABLE_PANEL_CONTENT_TEST_ID,
Expand All @@ -28,7 +28,7 @@ import { useInvestigateInTimeline } from '../../../../detections/components/aler
jest.mock(
'../../../../detections/components/alerts_table/timeline_actions/investigate_in_resolver'
);
jest.mock('../../../../common/containers/alerts/use_alert_prevalence_from_process_tree');
jest.mock('../../shared/hooks/use_alert_prevalence_from_process_tree');
jest.mock(
'../../../../detections/components/alerts_table/timeline_actions/use_investigate_in_timeline'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import { usePrevalence } from '../../shared/hooks/use_prevalence';
import { mockGetFieldsData } from '../../shared/mocks/mock_get_fields_data';
import { mockDataFormattedForFieldBrowser } from '../../shared/mocks/mock_data_formatted_for_field_browser';
import { InsightsSection } from './insights_section';
import { useAlertPrevalence } from '../../../../common/containers/alerts/use_alert_prevalence';
import { useAlertPrevalence } from '../../shared/hooks/use_alert_prevalence';
import { useRiskScore } from '../../../../entity_analytics/api/hooks/use_risk_score';
import { useExpandSection } from '../hooks/use_expand_section';
import { useTimelineDataFilters } from '../../../../timelines/containers/use_timeline_data_filters';
import { useTourContext } from '../../../../common/components/guided_onboarding_tour';

jest.mock('../../../../common/containers/alerts/use_alert_prevalence');
jest.mock('../../shared/hooks/use_alert_prevalence');

const mockDispatch = jest.fn();
jest.mock('react-redux', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import { VisualizationsSection } from './visualizations_section';
import { mockContextValue } from '../../shared/mocks/mock_context';
import { mockDataFormattedForFieldBrowser } from '../../shared/mocks/mock_data_formatted_for_field_browser';
import { DocumentDetailsContext } from '../../shared/context';
import { useAlertPrevalenceFromProcessTree } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import { useAlertPrevalenceFromProcessTree } from '../../shared/hooks/use_alert_prevalence_from_process_tree';
import { useTimelineDataFilters } from '../../../../timelines/containers/use_timeline_data_filters';
import { TestProvider } from '@kbn/expandable-flyout/src/test/provider';
import { useExpandSection } from '../hooks/use_expand_section';
import { useInvestigateInTimeline } from '../../../../detections/components/alerts_table/timeline_actions/use_investigate_in_timeline';
import { useIsInvestigateInResolverActionEnabled } from '../../../../detections/components/alerts_table/timeline_actions/investigate_in_resolver';

jest.mock('../hooks/use_expand_section');
jest.mock('../../../../common/containers/alerts/use_alert_prevalence_from_process_tree', () => ({
jest.mock('../../shared/hooks/use_alert_prevalence_from_process_tree', () => ({
useAlertPrevalenceFromProcessTree: jest.fn(),
}));
const mockUseAlertPrevalenceFromProcessTree = useAlertPrevalenceFromProcessTree as jest.Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react';
import { EuiToken } from '@elastic/eui';
import type { Node } from '@elastic/eui/src/components/tree_view/tree_view';
import type { StatsNode } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import type { StatsNode } from '../../shared/hooks/use_alert_prevalence_from_process_tree';

export const mockStatsNode: StatsNode = {
id: '70e19mhyda',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import React from 'react';
import type { Node } from '@elastic/eui/src/components/tree_view/tree_view';
import { EuiToken } from '@elastic/eui';
import type { StatsNode } from '../../../../common/containers/alerts/use_alert_prevalence_from_process_tree';
import type { StatsNode } from '../../shared/hooks/use_alert_prevalence_from_process_tree';

/**
* Helper function to recursively create ancestor tree nodes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { RenderHookResult } from '@testing-library/react-hooks';
import { renderHook } from '@testing-library/react-hooks';
import { useQuery } from '@tanstack/react-query';
import type {
UseAlertDocumentAnalyzerSchemaParams,
UseAlertDocumentAnalyzerSchemaResult,
} from './use_alert_document_analyzer_schema';
import { useAlertDocumentAnalyzerSchema } from './use_alert_document_analyzer_schema';
import { useHttp } from '../../../../common/lib/kibana';

jest.mock('../../../../common/lib/kibana');
jest.mock('@tanstack/react-query');

describe('useAlertPrevalenceFromProcessTree', () => {
let hookResult: RenderHookResult<
UseAlertDocumentAnalyzerSchemaParams,
UseAlertDocumentAnalyzerSchemaResult
>;

beforeEach(() => {
(useHttp as jest.Mock).mockReturnValue({
get: jest.fn(),
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('should return all properties when loading', () => {
(useQuery as jest.Mock).mockReturnValue({
isLoading: true,
data: [],
});

hookResult = renderHook(() =>
useAlertDocumentAnalyzerSchema({
documentId: 'documentId',
indices: [],
})
);

expect(hookResult.result.current.loading).toEqual(true);
expect(hookResult.result.current.error).toEqual(false);
expect(hookResult.result.current.id).toEqual(null);
expect(hookResult.result.current.schema).toEqual(null);
expect(hookResult.result.current.agentId).toEqual(null);
});

it('should return all properties with data', () => {
(useQuery as jest.Mock).mockReturnValue({
isLoading: false,
data: [
{
schema: {},
id: 'id',
agentId: 'agentId',
},
],
});

hookResult = renderHook(() =>
useAlertDocumentAnalyzerSchema({
documentId: 'documentId',
indices: [],
})
);

expect(hookResult.result.current.loading).toEqual(false);
expect(hookResult.result.current.error).toEqual(false);
expect(hookResult.result.current.id).toEqual('id');
expect(hookResult.result.current.schema).toEqual({});
expect(hookResult.result.current.agentId).toEqual('agentId');
});

it('should return error when no data', () => {
(useQuery as jest.Mock).mockReturnValue({
isLoading: false,
data: [],
});

hookResult = renderHook(() =>
useAlertDocumentAnalyzerSchema({
documentId: 'documentId',
indices: [],
})
);

expect(hookResult.result.current.loading).toEqual(false);
expect(hookResult.result.current.error).toEqual(true);
expect(hookResult.result.current.id).toEqual(null);
expect(hookResult.result.current.schema).toEqual(null);
expect(hookResult.result.current.agentId).toEqual(null);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useQuery } from '@tanstack/react-query';
import { useHttp } from '../../../../common/lib/kibana';

interface EntityResponse {
id: string;
name: string;
schema: object;
agentId: string;
}

export interface UseAlertDocumentAnalyzerSchemaParams {
/**
* The document ID of the alert to analyze
*/
documentId: string;
/**
* The indices to search for alerts
*/
indices: string[];
}

export interface UseAlertDocumentAnalyzerSchemaResult {
/**
* True if the request is still loading
*/
loading: boolean;
/**
* True if there was an error
*/
error: boolean;
/**
* The id returned by the API
*/
id: string | null;
/**
* The schema returned by the API
*/
schema: object | null;
/**
* The agent ID value returned byt the API
*/
agentId: string | null;
}

export function useAlertDocumentAnalyzerSchema({
documentId,
indices,
}: UseAlertDocumentAnalyzerSchemaParams): UseAlertDocumentAnalyzerSchemaResult {
const http = useHttp();

const query = useQuery<EntityResponse[]>(['getAlertPrevalenceSchema', documentId], () => {
return http.get<EntityResponse[]>(`/api/endpoint/resolver/entity`, {
query: {
_id: documentId,
indices,
},
});
});

if (query.isLoading) {
return {
loading: true,
error: false,
id: null,
schema: null,
agentId: null,
};
} else if (query.data && query.data.length > 0) {
const {
data: [{ schema, id, agentId }],
} = query;
return {
loading: false,
error: false,
id,
schema,
agentId,
};
} else {
return {
loading: false,
error: true,
id: null,
schema: null,
agentId: null,
};
}
}
Loading