# SPDX-License-Identifier: Apache-2.0
#
# The fluent-plugin-opensearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright fluent-plugin-opensearch Contributors. See
# GitHub history for details.
#
# Licensed to Uken Inc. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Uken Inc. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

require_relative '../helper'
require 'fluent/test/driver/output'
require 'fluent/plugin/out_opensearch'

class OpenSearchFallbackSelectorTest < Test::Unit::TestCase
  attr_accessor :index_cmds

  def setup
    Fluent::Test.setup
    @driver = nil
    log = Fluent::Engine.log
    log.out.logs.slice!(0, log.out.logs.length)
  end

  def stub_opensearch(url="http://localhost:9200/_bulk")
    stub_request(:post, url).with do |req|
      @index_cmds = req.body.split("\n").map {|r| JSON.parse(r) }
    end
  end

  def stub_opensearch_info(url="http://localhost:9200/", version="1.2.2")
    body ="{\"version\":{\"number\":\"#{version}\", \"distribution\":\"opensearch\"},\"tagline\":\"The OpenSearch Project: https://opensearch.org/\"}"
    stub_request(:get, url).to_return({:status => 200, :body => body, :headers => { 'Content-Type' => 'json' } })
  end

  def stub_opensearch_info_not_found(url="http://localhost:9200/", version="1.2.2")
    stub_request(:get, url).to_return(:status => [404, "Not Found"])
  end

  def stub_opensearch_info_unavailable(url="http://localhost:9200/", version="1.2.2")
    stub_request(:get, url).to_return(:status => [503, "Service Unavailable"])
  end

  def sample_record(content={})
    {'age' => 26, 'request_id' => '42', 'parent_id' => 'parent', 'routing_id' => 'routing'}.merge(content)
  end

  def driver(conf='')
    @driver ||= Fluent::Test::Driver::Output.new(Fluent::Plugin::OpenSearchOutput) {
      # v0.12's test driver assume format definition. This simulates ObjectBufferedOutput format
      if !defined?(Fluent::Plugin::Output)
        def format(tag, time, record)
          [time, record].to_msgpack
        end
      end
    }.configure(conf)
  end

  def test_fallback_on_info
    stub_opensearch_info_not_found("http://localhost:9202/")
    stub_opensearch_info_unavailable("http://localhost:9201/")
    stub_opensearch_info
    stub_opensearch
    config = %[
      hosts localhost:9202,localhost:9201,localhost:9200
      selector_class_name Fluent::Plugin::OpenSearchFallbackSelector
      @log_level debug
      with_transporter_log true
      reload_connections true
      reload_after 10
      catch_transport_exception_on_retry false # For fallback testing
    ]
    assert_raise(OpenSearch::Transport::Transport::Errors::NotFound) do
      driver(config)
    end
    driver.run(default_tag: 'test') do
      driver.feed(sample_record)
    end
    assert_equal(2, index_cmds.length)
    assert_equal("fluentd", index_cmds.first['index']['_index'])
  end

  # TODO: on feed phase test case
end
