require "integration_test_helper"

class ElasticsearchClosingTest < IntegrationTest

  def setup
    stub_elasticsearch_settings
    enable_test_index_connections
    try_remove_test_index
  end

  def teardown
    clean_test_indexes
  end

  # When the index is closed, we don't particularly mind which exception gets
  # raised, so long as it's a client error (4xx) of some kind
  def restclient_4xx_errors
    RestClient::Exceptions::EXCEPTIONS_MAP.select { |code, exception|
      (400..499).include? code
    }.values
  end

  def test_should_fail_to_insert_or_get_when_index_closed
    create_test_index

    index = search_server.index_group(@default_index_name).current
    index.close
    assert_raises *restclient_4xx_errors do
      index.add([sample_document])
    end

    assert_raises *restclient_4xx_errors do
      index.get("/foobang")
    end
  end
end

require "integration_test_helper"

class ElasticsearchClosingTest < IntegrationTest

  def setup
    stub_elasticsearch_settings
    enable_test_index_connections
    try_remove_test_index
  end

  def teardown
    clean_test_indexes
  end

  # When the index is closed, we don't particularly mind which exception gets
  # raised, so long as it's a client error (4xx) of some kind
  def restclient_4xx_errors
    RestClient::Exceptions::EXCEPTIONS_MAP.select { |code, exception|
      (400..499).include? code
    }.values
  end

  def test_should_fail_to_insert_or_get_when_index_closed
    create_test_index

    index = search_server.index_group(@default_index_name).current
    index.close
    assert_raises *(restclient_4xx_errors + [Elasticsearch::BulkIndexFailure]) do
      index.add([sample_document])
    end

    assert_raises *restclient_4xx_errors do
      index.get("/foobang")
    end
  end
end

Allow for BulkIndexFailure error in closing test
Allow for BulkIndexFailure error in closing test

With elasticsearch 1.4.3+, when we try and do a bulk write to a closed
index, we get a "success" response code, but with error reports for the
individual documents which fail (rather than the 0.90.x behaviour of
returning an HTTP 4xx error).  Update the test of writing to a closed
index to expect this as one of the possible responses.

This should work with both elasticsearch 1.4.3+ or 0.90.  It fails with
1.4.2, because that version has a bug which causes an internal
NullPointerException, and returns an HTTP 500 status code.
