---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
---
Test valid job config:
- do:
    ml.validate:
      body: "{\n  \"analysis_config\": {\n    \"bucket_span\": \"1h\",\n    \"detectors\"\
        : [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\"\
        : \"airline\"}]\n  },\n  \"data_description\": {\n    \"format\": \"xcontent\"\
        ,\n    \"time_field\": \"time\",\n    \"time_format\": \"yyyy-MM-dd HH:mm:ssX\"\
        \n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    acknowledged: true
---
Test invalid job config:
- do:
    catch: "/.data_description. failed to parse field .format./"
    ml.validate:
      body: "{\n  \"analysis_config\": {\n    \"bucket_span\": \"1h\",\n    \"detectors\"\
        : [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\"\
        : \"airline\"}]\n  },\n  \"data_description\": {\n    \"format\": \"wrong\"\
        ,\n    \"time_field\": \"time\",\n    \"time_format\": \"yyyy-MM-dd HH:mm:ssX\"\
        \n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test valid job config with job ID:
- do:
    ml.validate:
      body: "{\n  \"job_id\": \"validate-job-config-with-job-id\",\n  \"analysis_config\"\
        : {\n    \"bucket_span\": \"1h\",\n    \"detectors\": [{\"function\": \"metric\"\
        , \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n  },\n\
        \  \"data_description\": {\n    \"format\": \"xcontent\",\n    \"time_field\"\
        : \"time\",\n    \"time_format\": \"yyyy-MM-dd HH:mm:ssX\"\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    acknowledged: true
---
Test job config that is invalid only because of the job ID:
- do:
    catch: "/Invalid job_id; '_' can contain lowercase alphanumeric \\(a-z and 0-9\\\
      ), hyphens or underscores; must start and end with alphanumeric/"
    ml.validate:
      body: "{\n  \"job_id\": \"_\",\n  \"analysis_config\": {\n    \"bucket_span\"\
        : \"1h\",\n    \"detectors\": [{\"function\": \"metric\", \"field_name\":\
        \ \"responsetime\", \"by_field_name\": \"airline\"}]\n  },\n  \"data_description\"\
        : {\n    \"format\": \"xcontent\",\n    \"time_field\": \"time\",\n    \"\
        time_format\": \"yyyy-MM-dd HH:mm:ssX\"\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test job config is invalid because model snapshot id set:
- do:
    catch: "/x_content_parse_exception/"
    ml.validate:
      body: "{\n  \"model_snapshot_id\": \"wont-create-with-this-setting\",\n  \"\
        analysis_config\" : {\n      \"bucket_span\": \"1h\",\n      \"detectors\"\
        \ :[{\"function\":\"metric\",\"field_name\":\"responsetime\",\"by_field_name\"\
        :\"airline\"}]\n  },\n  \"data_description\" : {\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test job config with duplicate detector configurations:
- do:
    catch: "/illegal_argument_exception.*Duplicate detectors are not allowed/"
    ml.validate:
      body: "{\n  \"analysis_config\": {\n      \"bucket_span\": \"1h\",\n      \"\
        detectors\": [\n          {\"function\":\"max\", \"field_name\":\"responsetime\"\
        },\n          {\"function\":\"max\", \"field_name\":\"responsetime\"}\n  \
        \    ]\n  },\n  \"data_description\": {\n      \"time_field\": \"@timestamp\"\
        \n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "/illegal_argument_exception.*Duplicate detectors are not allowed/"
    ml.validate:
      body: "{\n  \"analysis_config\": {\n      \"bucket_span\": \"1h\",\n      \"\
        detectors\": [\n          {\"function\":\"max\", \"field_name\":\"responsetime\"\
        , \"detector_index\": 0},\n          {\"function\":\"max\", \"field_name\"\
        :\"responsetime\", \"detector_index\": 1}\n      ]\n  },\n  \"data_description\"\
        : {\n      \"time_field\": \"@timestamp\"\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
