require 'spec_helper'
require 'rabbitmq/http/client'

describe GovukSeedCrawler do
  let(:options) {{
      :exchange => "govuk_seed_crawler_integration",
      :topic => "#",
      :site_root => "https://www.gov.uk/",
  }}
  let(:amqp_vhost) { "/" }
  let(:rabbitmq_mgmt_client) { RabbitMQ::HTTP::Client.new("http://guest:guest@127.0.0.1:15672") }
  let(:probable_min_number_of_urls) { 1000 }

  subject { GovukSeedCrawler::Seeder::seed(options) }

  before(:each) do
    rabbitmq_mgmt_client.declare_exchange(amqp_vhost, options[:exchange], { :type => "topic" })
  end

  after(:each) do
    rabbitmq_mgmt_client.delete_exchange(amqp_vhost, options[:exchange])
  end

  it "publishes URLs it finds to an AMQP topic exchange" do
    subject

    sleep(1) # give the management API time to recognise the messages we've just published
    exchange_info = rabbitmq_mgmt_client.exchange_info(amqp_vhost, options[:exchange])
    number_of_messages_on_exchange = exchange_info["message_stats"]["publish_in"]
    expect(number_of_messages_on_exchange).to be > probable_min_number_of_urls
  end
end

require 'spec_helper'
require 'rabbitmq/http/client'

describe GovukSeedCrawler do
  let(:options) {{
      :amqp_exchange => "govuk_seed_crawler_integration",
      :amqp_topic => "#",
      :site_root => "https://www.gov.uk/",
  }}
  let(:amqp_vhost) { "/" }
  let(:rabbitmq_mgmt_client) { RabbitMQ::HTTP::Client.new("http://guest:guest@127.0.0.1:15672") }
  let(:probable_min_number_of_urls) { 1000 }

  subject { GovukSeedCrawler::Seeder::seed(options) }

  before(:each) do
    rabbitmq_mgmt_client.declare_exchange(amqp_vhost, options[:amqp_exchange], { :type => "topic" })
  end

  after(:each) do
    rabbitmq_mgmt_client.delete_exchange(amqp_vhost, options[:amqp_exchange])
  end

  it "publishes URLs it finds to an AMQP topic exchange" do
    subject

    sleep(1) # give the management API time to recognise the messages we've just published
    exchange_info = rabbitmq_mgmt_client.exchange_info(amqp_vhost, options[:amqp_exchange])
    number_of_messages_on_exchange = exchange_info["message_stats"]["publish_in"]
    expect(number_of_messages_on_exchange).to be > probable_min_number_of_urls
  end
end

Fix AMQP argument error in integration tests
Fix AMQP argument error in integration tests

The `exchange` and `topic` arguments to GovukSeedCrawler::Seeder::seed
should be called `amqp_exchange` and `amqp_topic`. Otherwise we raise an
error in GovukSeedCrawler::UrlPublisher::publish_urls that the exchange (and
later the topic) are not set:

    1) GovukSeedCrawler publishes URLs it finds to an AMQP topic exchange
       Failure/Error: subject { GovukSeedCrawler::Seeder::seed(options) }
       RuntimeError:
         Exchange not defined
       # ./lib/govuk_seed_crawler/url_publisher.rb:12:in `publish_urls'
       # ./lib/govuk_seed_crawler/seeder.rb:11:in `seed'
       # ./spec/integration/govuk_seed_crawler_spec.rb:14:in `block (2 levels) in <top (required)>'
       # ./spec/integration/govuk_seed_crawler_spec.rb:25:in `block (2 levels) in <top (required)>'
