require 'spec_helper'

describe "DossierEntry", "converting to xml" do

  before do
    @dossier_entry_a = Caren::Dossier::DossierEntry.new :type => 'Dossier::TemperatureDossierEntry',
                                                        :external_person_id => 1,
                                                        :external_id => 1,
                                                        :person_name => 'Andre Foeken',
                                                        :comment => 'Burning up',
                                                        :value => 38.1

    @dossier_entry_b = Caren::Dossier::DossierEntry.new :type => 'Dossier::BMIDossierEntry',
                                                        :external_person_id => 2,
                                                        :external_id => 2,
                                                        :person_name => 'Jan Willem Mooij',
                                                        :comment => 'Heavy weight',
                                                        :value => 38.1
  end

  it "should be able to convert a dossier entry to valid xml" do
    @dossier_entry_a.should convert_to_valid_caren_xml
  end

  it "should be able to convert an array of dossier entries to valid xml" do
    [@dossier_entry_a,@dossier_entry_b].should convert_to_valid_caren_array_xml
  end

end

describe "DossierEntry", "REST methods" do

  before do
    message = File.read("spec/fixtures/caren_dossier_entry.xml")
    messages = File.read("spec/fixtures/caren_dossier_entries.xml")

    entries_url = Caren::Api.session.url_for( Caren::Dossier::DossierEntry.resource_url(1) )
    entry_url = Caren::Api.session.url_for( Caren::Dossier::DossierEntry.resource_url(1,1) )
    timestamp = DateTime.now.to_i

    FakeWeb.register_uri(:get, entries_url, :body => messages, :signature => Caren::Api.session.sign(timestamp,nil,messages), :timestamp => timestamp )
    FakeWeb.register_uri(:get, entry_url, :body => message, :signature => Caren::Api.session.sign(timestamp,nil,message), :timestamp => timestamp )
    FakeWeb.register_uri(:post, entries_url, :status => 201, :signature => Caren::Api.session.sign(timestamp), :timestamp => timestamp )
    FakeWeb.register_uri(:delete, entry_url, :signature => Caren::Api.session.sign(timestamp), :timestamp => timestamp )
  end

  it "should be able to find all dossier entries" do
    messages = Caren::Dossier::DossierEntry.all 1, Caren::Api.session
    messages.should have(2).things
    messages.first.comment.should == "Test"
  end

  it "should be able to find one dossier entry" do
    message = Caren::Dossier::DossierEntry.find 1, 1, Caren::Api.session
    message.comment.should == "Test"
  end

  it "should be able to create a dossier entry" do
    lambda{ Caren::Dossier::DossierEntry.new :type => 'Dossier::ReportDossierEntry',
                                             :external_person_id => 1,
                                             :external_id => 1,
                                             :person_name => 'Andre Foeken',
                                             :comment => 'This guy is a dude' }.should_not raise_error
  end

end
