require 'spec_helper'
# Dummy storage_Object to represent a file

class DummyXattr
  attr_accessor :name
  attr_accessor :value
  def initialize(name)
    self.name = name
  end
end

class DummyXattrEnumerator
  include ::Enumerable

  attr_accessor :xattrs
  def initialize
    self.xattrs = []
    10.times do |i|
      xattr = DummyXattr.new("key#{i}")
      xattr.value = i
      xattrs << xattr
    end
  end

  def each
    return enum_for(:each) unless block_given?

    xattrs.each do |x|
      yield x
    end
  end
end

class DummyStorage
  extend CephStorage::StorageObject
  include CephStorage::StorageObject

  attr_accessor :contents

  def write_file(contents)
    self.contents = contents
  end

  def read_file
    contents
  end

  def xattr(name)
    DummyXattr.new(name)
  end

  def xattr_enumerator
    puts 'call xattr_enumerator'
    DummyXattrEnumerator.new
  end

  def destroy
    self.contents = nil
  end
end
describe CephStorage::StorageObject do
  before do
    @storage_object1 = DummyStorage.new
    @storage_object2 = DummyStorage.new
  end
  subject { @storage_object1 }

  it 'should respond to move' do
    expect(subject).to respond_to :move
  end

  it 'should respond to copy' do
    expect(subject).to respond_to :copy
  end

  it 'should respond to copy_xattrs' do
    expect(subject).to respond_to :copy_xattrs
  end

  describe 'copying file' do
    it 'should read from storage_object1 and write to storage_object2' do
      expect(@storage_object1).to receive(:read_file)
      expect(@storage_object2).to receive(:write_file)
      expect(@storage_object1).to receive(:copy_xattrs)
      @storage_object1.copy(@storage_object2)
    end
  end

  describe 'moving file' do
    it 'should read from object1, write object2, delete object1' do
      expect(@storage_object1).to receive(:read_file)
      expect(@storage_object2).to receive(:write_file)
      expect(@storage_object1).to receive(:copy_xattrs)
      expect(@storage_object1).to receive(:destroy)
      @storage_object1.move(@storage_object2)
    end
  end
end