/*
 * 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 org.elasticsearch.gradle.Version

apply plugin: 'elasticsearch.java'

dependencies {
  api project(":test:yaml-rest-runner")
}

// We don't currently have any tests for this because they are test utilities.
tasks.named("test").configure { enabled = false }

subprojects {
  if (project.name.startsWith('core-with-')) {
    apply plugin: 'elasticsearch.legacy-yaml-rest-test'

    dependencies {
      yamlRestTestImplementation project(":x-pack:qa:runtime-fields")
      restTestConfig project(path: ':modules:aggregations', configuration: 'restTests')
    }

    restResources {
      restApi {
        include 'capabilities', '_common', 'bulk', 'count', 'cluster', 'index', 'indices', 'field_caps', 'msearch',
          'search', 'async_search', 'graph', '*_point_in_time', 'put_script', 'scripts_painless_execute'
      }
      restTests {
        includeCore '*'
        includeXpack 'async_search', 'graph'
      }
    }

    testClusters.matching { it.name == "yamlRestTest" }.configureEach {
      testDistribution = 'DEFAULT'
      setting 'xpack.license.self_generated.type', 'trial'
      setting 'xpack.security.enabled', 'false'

      requiresFeature 'es.index_mode_feature_flag_registered', Version.fromString("8.0.0")
      requiresFeature 'es.sub_objects_auto_feature_flag_enabled', Version.fromString("8.16.0")
    }

    tasks.named("yamlRestTest").configure {
      def suites = [
        'async_search',
        'search',
        'aggregations',
        'search.highlight',
        'search.inner_hits',
        'search_shards',
        'suggest',
      ]
      if (project.name.equals('core-with-mapped')) {
        suites += [
          // These two don't support runtime fields on the request. Should they?
          'field_caps',
          'graph',
          // The search request tests don't know how to support msearch for now
          'msearch',
        ]
      }
      systemProperty 'tests.rest.suite', suites.join(',')
      systemProperty 'tests.rest.blacklist',
        [
          /////// TO FIX ///////
          'aggregations/range/Date range', //source only date field should also emit values for numbers, it expects strings only
          'search/115_multiple_field_collapsing/two levels fields collapsing', // Field collapsing on a runtime field does not work
          'search/111_field_collapsing_with_max_score/*', // Field collapsing on a runtime field does not work
          'search/112_field_collapsing_with_rescore/*', // Field collapsing on a runtime field does not work
          'field_caps/30_index_filter/Field caps with index filter', // We don't support filtering field caps on runtime fields. What should we do?
          'search/350_point_in_time/point-in-time with index filter', // We don't support filtering pit on runtime fields.
          'aggregations/filters_bucket/cache busting', // runtime keyword does not support split_queries_on_whitespace
          'search/140_pre_filter_search_shards/pre_filter_shard_size with shards that have no hit',
          //completion suggester does not return options when the context field is a geo_point runtime field
          'suggest/30_context/Multi contexts should work',

          //there is something wrong when using dotted document syntax here, passes in main yaml tests
          'search/330_fetch_fields/Test nested field inside object structure',

          /////// TO FIX ///////

          /////// NOT SUPPORTED ///////
          'search.highlight/40_keyword_ignore/Plain Highligher should skip highlighting ignored keyword values', // The plain highlighter is incompatible with the prefix queries that we make for runtime fields, use unified highlighter instead.
          'aggregations/rare_terms/*', // Requires an index and we won't have it
          'aggregations/terms_disable_opt/*', // Optimization requires an index and runtime fields don't have one
          // Runtime fields don't support sub-fields
          'aggregations/histogram/*',
          'suggest/50_completion_with_multi_fields/Search by suggestion on geofield-hash on sub field should work',
          // Runtime fields don't have global ords
          'analytics/nested_top_metrics_sort/*',
          'aggregations/terms/string profiler via global ordinals filters implementation',
          'aggregations/terms/string profiler via global ordinals native implementation',
          'aggregations/terms/Global ordinals are loaded with the global_ordinals execution hint',
          'aggregations/cardinality_metric/profiler string',
          'aggregations/cardinality_metric/profiler string segment ords hint',
          'aggregations/cardinality_metric/profiler string global ords hint',
          'aggregations/cardinality_metric/profiler string direct hint',
          'aggregations/cardinality_metric/profiler string save memory hint',
          'aggregations/cardinality_metric/profiler string save time hint',
          'aggregations/composite_sorted/*',
          // timeseries dimensions can't be runtime fields
          'aggregations/time_series/*',
          // The error messages are different
          'search/330_fetch_fields/error includes field name',
          'search/330_fetch_fields/error includes glob pattern',
          // we need a @timestamp field to be defined in index mapping
          'search/380_sort_segments_on_timestamp/*',
          'field_caps/40_time_series/*',
          // Synthetic source needs doc values that runtime fields tests disable
          'search/400_synthetic_source/*',
          'search.highlight/50_synthetic_source/*',
          'aggregations/top_hits/synthetic _source',
          // Runtime fields produce a slightly different result for multi valued matrix_stats yaml tests:
          // (this appears to always have been the case. muted when matrix_stats was moved from its own
          // module to aggregations module)
          'aggregations/matrix_stats_multi_value_field/Multi value field Min',
          'aggregations/matrix_stats_multi_value_field/Multi value field Max',
          'aggregations/matrix_stats_multi_value_field/Partially unmapped',
          'aggregations/matrix_stats_multi_value_field/Partially unmapped with missing defaults',
          /////// NOT SUPPORTED ///////
        ].join(',')
    }
  }
}
