base = File.dirname(__FILE__) require base + '/../test_setup' require 'set' context 'S33r bucket listing' do setup do xml_file = File.join(base, '../files/bucket_listing.xml') @with_bucket_listing_xml = File.open(xml_file) { |f| f.read } xml_file2 = File.join(base, '../files/bucket_listing2.xml') @with_bucket_listing_xml2 = File.open(xml_file2) { |f| f.read } xml_file3 = File.join(base, '../files/bucket_listing3.xml') @with_empty_bucket_listing_xml = File.open(xml_file3) { |f| f.read } xml_file4 = File.join(base, '../files/bucket_listing_broken.xml') @with_broken_bucket_listing_xml = File.open(xml_file4) { |f| f.read } @bucket_listing = BucketListing.new(@with_bucket_listing_xml) @bucket_properties = %w(name prefix marker max_keys is_truncated) @bucket_property_setters = @bucket_properties.map { |prop| prop + "=" } end specify 'can only be created if bucket listing XML supplied' do lambda { BucketListing.new }.should.raise ArgumentError end specify 'cannot be created from invalid XML' do lambda { BucketListing.new(nil) }.should.raise S33rException::InvalidBucketListing end specify 'should recover gracefully from broken bucket listing XML' do lambda { BucketListing.new(@with_broken_bucket_listing_xml) }.should.raise S33rException::InvalidBucketListing end specify 'should cope if bucket is empty (i.e. no elements)' do @bucket_listing.set_listing_xml(@with_empty_bucket_listing_xml) end specify 'can have the bucket listing XML reset' do @bucket_listing.should.respond_to :set_listing_xml end specify 'should present bucket metadata as typed properties' do @bucket_listing.name.should.equal('testingtesting') @bucket_listing.prefix.should.equal('') @bucket_listing.marker.should.equal('') @bucket_listing.max_keys.should.equal(1000) @bucket_listing.is_truncated.should.equal(false) @bucket_listing.delimiter.should.be nil end specify 'when listing XML is reset, should update all properties correctly' do @bucket_listing.set_listing_xml(@with_bucket_listing_xml2) @bucket_listing.name.should.equal('testing2') @bucket_listing.prefix.should.equal('/home/ell/') @bucket_listing.marker.should.equal('') @bucket_listing.max_keys.should.equal(100) @bucket_listing.delimiter.should.equal('/') @bucket_listing.is_truncated.should.equal(false) end specify 'should provide private setters for metadata' do # all private methods methods = @bucket_listing.private_methods.to_set # names of the setters setters = @bucket_property_setters # all methods should be a superset of the private setters methods.superset?(setters.to_set).should.equal true end specify 'should store resources ( elements) in a hash' do @bucket_listing.contents.size.should_be 10 first_obj = @bucket_listing.contents['/home/ell/dir1/four.txt'] first_obj.should_be_instance_of S3Object end specify 'should enable access to metadata for a resource by its key' do obj = @bucket_listing['/home/ell/dir1/four.txt'] obj.should_be_instance_of S3Object obj.etag.should_equal '24ce59274b89287b3960c184153ac24b' end specify 'should be able to build a full representation given full object XML from GET on resource key' do todo end specify 'should provide easy access to elements as a hash' do todo end end context 'S3 object' do setup do @s3_object_xml = File.open(File.join(base, '../files/s3_object.xml')).read @s3obj = S3Object.new @s3obj.set_from_xml_string(@s3_object_xml) end specify 'can be initialised from XML fragment with correct data types' do @s3obj.key.should.equal '/home/ell/dir1/four.txt' d = @s3obj.last_modified [d.year, d.month, d.day, d.hour, d.min, d.sec].should.equal [2006, 8, 19, 22, 53, 29] @s3obj.etag.should.equal '24ce59274b89287b3960c184153ac24b' @s3obj.size.should.equal 14 end specify 'should treat the owner as an object in his/her own right' do [@s3obj.owner.id, @s3obj.owner.display_name].should.equal \ ['56efddfead5aa65da942f156fb2b294f44d78fd932d701331edc5fba19620fd4', 'elliotsmith3'] @s3obj.owner.should_be_instance_of S3User end specify 'can be associated with a NamedBucket' do todo end specify 'can be saved by proxing through the NamedBucket it is associated with' do todo end specify 'cannot be saved unless associated with a NamedBucket' do todo end end