# encoding: UTF-8

# Copyright 2011-2014 innoQ Deutschland GmbH
#
# Licensed 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 File.join(File.expand_path(File.dirname(__FILE__)), '../test_helper')
require 'webmock/minitest'

class ReverseMatchJobTest < ActiveSupport::TestCase
  include ReverseMatchErrors

  setup do
    @achievement_hobbies = Concept::Skos::Base.new.tap do |c|
      RdfApi.devour c, 'skos:prefLabel', '"Achievement hobbies"@en'
      c.publish
      c.save
    end

    @airsoft = Concept::Skos::Base.new(origin: 'airsoft').tap do |c|
      RdfApi.devour c, 'skos:prefLabel', '"Airsoft"@en'
      c.publish
      c.save
    end

    @reverse_match_service = Services::ReverseMatchService.new('try.iqvoc.com', 'http://')

    body = {links: [
      { rel: 'self', href: 'http://0.0.0.0:3000/en/concepts/airsoft', method: 'get' },
      { rel: 'add_match',href: 'http://0.0.0.0:3000/airsoft/add_match', method: 'patch' },
      { rel: 'remove_match', href: 'http://0.0.0.0:3000/airsoft/remove_match', method: 'patch' }
    ]}.to_json

    stub_request(:get, "http://try.iqvoc.com/").
      with(
        headers: {
          'Accept'=>'application/json',
          'User-Agent'=>"Faraday v#{Faraday::VERSION}"
        }).
      to_return(status: 200, body: body, headers: {})

    @worker = Delayed::Worker.new
    DatabaseCleaner.start
  end

  teardown do
    DatabaseCleaner.clean
  end

  test 'successfull job' do
    status, body = status_and_body(:mapping_added)
    stub_request(:patch, 'http://0.0.0.0:3000/airsoft/add_match?match_class=match_skos_broadmatch&uri=http://try.iqvoc.com/airsoft')
      .with(headers: {
        'Accept' => '*/*',
        'Content-Type' => 'application/json',
        'Referer' => 'http://try.iqvoc.com/',
        'User-Agent'=>"Faraday v#{Faraday::VERSION}"
      })
      .to_return(status: status, body: body.to_json, headers: {})

    job = @reverse_match_service.build_job(:add_match, @airsoft, 'http://try.iqvoc.com', 'Match::Skos::BroadMatch')
    @reverse_match_service.add(job)

    job = Delayed::Job.last
    @worker.run(job)

    assert_equal 0, @airsoft.jobs.size
  end

  test 'job timeout' do
    status, body = status_and_body(:mapping_added)
    stub_request(:patch, 'http://0.0.0.0:3000/airsoft/add_match?match_class=match_skos_broadmatch&uri=http://try.iqvoc.com/airsoft')
      .to_timeout

    job = @reverse_match_service.build_job(:add_match, @airsoft, 'http://try.iqvoc.com', 'Match::Skos::BroadMatch')
    @reverse_match_service.add(job)

    job = Delayed::Job.last
    @worker.run(job)

    assert_equal 1, @airsoft.jobs.size

    assert_equal 'timeout_error', job.error_message
  end

  test 'failed connection' do
    status, body = status_and_body(:mapping_added)
    stub_request(:patch, 'http://0.0.0.0:3000/airsoft/add_match?match_class=match_skos_broadmatch&uri=http://try.iqvoc.com/airsoft')
      .to_raise(Errno::ECONNREFUSED)

    job = @reverse_match_service.build_job(:add_match, @airsoft, 'http://try.iqvoc.com', 'Match::Skos::BroadMatch')
    @reverse_match_service.add(job)

    job = Delayed::Job.last
    @worker.run(job)

    assert_equal 1, @airsoft.jobs.size

    assert_equal 'connection_failed', job.error_message
  end

  test 'unknown resource' do
    status, body = status_and_body(:mapping_added)
    stub_request(:patch, 'http://0.0.0.0:3000/airsoft/add_match?match_class=match_skos_broadmatch&uri=http://try.iqvoc.com/airsoft')
      .to_return(status: 404)

    job = @reverse_match_service.build_job(:add_match, @airsoft, 'http://try.iqvoc.com', 'Match::Skos::BroadMatch')
    @reverse_match_service.add(job)

    job = Delayed::Job.last
    @worker.run(job)

    assert_equal 1, @airsoft.jobs.size

    assert_equal 'resource_not_found', job.error_message
  end

  test 'unknown match class' do
    status, body = status_and_body(:unknown_match)
    stub_request(:patch, 'http://0.0.0.0:3000/airsoft/add_match?match_class=match_skos_broadmatch&uri=http://try.iqvoc.com/airsoft')
      .with(headers: {
        'Accept' => '*/*',
        'Content-Type' => 'application/json',
        'Referer' => 'http://try.iqvoc.com/',
        'User-Agent'=>'Faraday v1.10.3'
      })
      .to_return(status: status, body: body.to_json, headers: {})

    job = @reverse_match_service.build_job(:add_match, @airsoft, 'http://try.iqvoc.com', 'Match::Skos::BroadMatch')
    @reverse_match_service.add(job)

    job = Delayed::Job.last
    @worker.run(job)

    assert_equal 1, @airsoft.jobs.size

    assert_equal 'unknown_match', job.error_message
  end
end
