diff --git a/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts b/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts index 2a641066a9b55..e115986b2f092 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts @@ -5,7 +5,6 @@ */ import expect from '@kbn/expect'; -import { allEventsExistInCalendar } from './helpers'; import { FtrProviderContext } from '../../../ftr_provider_context'; import { USER } from '../../../../functional/services/ml/security_common'; import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common'; @@ -55,7 +54,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body).to.have.length(testCalendars.length); expect(body[0].events).to.have.length(testEvents.length); - expect(allEventsExistInCalendar(testEvents, body[0])).to.eql(true); + ml.api.assertAllEventsExistInCalendar(testEvents, body[0]); }); it('should fetch all calendars for user with view permission', async () => { @@ -67,7 +66,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body).to.have.length(testCalendars.length); expect(body[0].events).to.have.length(testEvents.length); - expect(allEventsExistInCalendar(testEvents, body[0])).to.eql(true); + ml.api.assertAllEventsExistInCalendar(testEvents, body[0]); }); it('should not fetch calendars for unauthorized user', async () => { @@ -107,7 +106,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body.job_ids).to.eql(testCalendar.job_ids); expect(body.description).to.eql(testCalendar.description); expect(body.events).to.have.length(testEvents.length); - expect(allEventsExistInCalendar(testEvents, body)).to.eql(true); + ml.api.assertAllEventsExistInCalendar(testEvents, body); }); it('should fetch calendar & associated events by id for user with view permission', async () => { @@ -120,7 +119,7 @@ export default ({ getService }: FtrProviderContext) => { expect(body.job_ids).to.eql(testCalendar.job_ids); expect(body.description).to.eql(testCalendar.description); expect(body.events).to.have.length(testEvents.length); - expect(allEventsExistInCalendar(testEvents, body)).to.eql(true); + ml.api.assertAllEventsExistInCalendar(testEvents, body); }); it('should not fetch calendars for unauthorized user', async () => { diff --git a/x-pack/test/api_integration/apis/ml/calendars/helpers.ts b/x-pack/test/api_integration/apis/ml/calendars/helpers.ts index a90a90bb12333..5d143d9b451f2 100644 --- a/x-pack/test/api_integration/apis/ml/calendars/helpers.ts +++ b/x-pack/test/api_integration/apis/ml/calendars/helpers.ts @@ -6,7 +6,7 @@ import { Calendar, CalendarEvent } from '../../../../../plugins/ml/server/models/calendar'; -export const allEventsExistInCalendar = ( +export const assertAllEventsExistInCalendar = ( eventsToCheck: CalendarEvent[], calendar: Calendar ): boolean => { diff --git a/x-pack/test/functional/services/ml/api.ts b/x-pack/test/functional/services/ml/api.ts index 686c5fd701422..9dfec3a17dec0 100644 --- a/x-pack/test/functional/services/ml/api.ts +++ b/x-pack/test/functional/services/ml/api.ts @@ -11,7 +11,6 @@ import { FtrProviderContext } from '../../ftr_provider_context'; import { DATAFEED_STATE, JOB_STATE } from '../../../../plugins/ml/common/constants/states'; import { DATA_FRAME_TASK_STATE } from '../../../../plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/common'; import { Datafeed, Job } from '../../../../plugins/ml/common/types/anomaly_detection_jobs'; -import { allEventsExistInCalendar } from '../../../api_integration/apis/ml/calendars/helpers'; export type MlApi = ProvidedType; export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { @@ -374,6 +373,36 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { return await esSupertest.get(`/_ml/calendars/${calendarId}/events`).expect(expectedCode); }, + assertAllEventsExistInCalendar: ( + eventsToCheck: CalendarEvent[], + calendar: Calendar + ): boolean => { + const updatedCalendarEvents = calendar.events as CalendarEvent[]; + let allEventsAreUpdated = true; + for (const eventToCheck of eventsToCheck) { + // if at least one of the events that we need to check is not in the updated events + // no need to continue + if ( + updatedCalendarEvents.findIndex( + (updatedEvent) => + updatedEvent.description === eventToCheck.description && + updatedEvent.start_time === eventToCheck.start_time && + updatedEvent.end_time === eventToCheck.end_time + ) < 0 + ) { + allEventsAreUpdated = false; + break; + } + } + expect(allEventsAreUpdated).to.eql( + true, + `Expected calendar ${calendar.calendar_id} to contain events ${JSON.stringify( + eventsToCheck + )}` + ); + return true; + }, + async waitForEventsToExistInCalendar( calendarId: string, eventsToCheck: CalendarEvent[], @@ -383,7 +412,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) { // validate if calendar events have been updated with the requested events const { body } = await this.getCalendarEvents(calendarId, 200); - if (allEventsExistInCalendar(eventsToCheck, body)) { + if (this.assertAllEventsExistInCalendar(eventsToCheck, body)) { return true; } else { throw new Error(