waz-storage 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,282 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'tests/configuration'
4
+ require 'lib/waz-blobs'
5
+
6
+ describe "blobs service behavior" do
7
+ it "should create container" do
8
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
9
+ RestClient::Request.any_instance.expects(:execute)
10
+ service.expects(:generate_request_uri).with("mock-container", {:restype => 'container'}).returns("mock-uri")
11
+ service.expects(:generate_request).with(:put, "mock-uri", {:x_ms_version => '2009-09-19'}, nil).returns(RestClient::Request.new(:method => :put, :url => "http://localhost"))
12
+ service.create_container('mock-container')
13
+ end
14
+
15
+ it "should get container properties" do
16
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
17
+ mock_response = mock()
18
+ mock_response.stubs(:headers).returns(RestClient::AbstractResponse.beautify_headers({"x-ms-meta-Name" => ["customName"]}))
19
+ RestClient::Request.any_instance.expects(:execute).returns(mock_response)
20
+ service.expects(:generate_request_uri).with("mock-container", {:restype => 'container'}).returns("mock-uri")
21
+ service.expects(:generate_request).with(:get, "mock-uri", {:x_ms_version => '2009-09-19'}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
22
+ properties = service.get_container_properties('mock-container')
23
+ properties[:x_ms_meta_name].should == "customName"
24
+ end
25
+
26
+ it "should set container properties" do
27
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
28
+ RestClient::Request.any_instance.expects(:execute)
29
+ service.expects(:generate_request_uri).with("mock-container", {:restype => 'container', :comp => 'metadata'}).returns("mock-uri")
30
+ service.expects(:generate_request).with(:put, "mock-uri", {:x_ms_version => '2009-09-19', :x_ms_meta_Name => "myName"}, nil).returns(RestClient::Request.new(:method => :put, :url => "http://localhost"))
31
+ properties = service.set_container_properties('mock-container', {:x_ms_meta_Name => "myName"})
32
+ end
33
+
34
+ it "should get container acl" do
35
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
36
+ mock_response = mock()
37
+ mock_response.stubs(:headers).returns(RestClient::AbstractResponse.beautify_headers({:x_ms_blob_public_access.to_s => [true]}))
38
+ RestClient::Request.any_instance.expects(:execute).returns(mock_response)
39
+ service.expects(:generate_request_uri).with("mock-container", {:restype => 'container', :comp => 'acl'}).returns("mock-uri")
40
+ service.expects(:generate_request).with(:get, "mock-uri", {:x_ms_version => '2009-09-19'}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
41
+ service.get_container_acl('mock-container').should == true
42
+ end
43
+
44
+ it "should set container acl" do
45
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
46
+ RestClient::Request.any_instance.expects(:execute)
47
+ service.expects(:generate_request_uri).with("mock-container", :restype => 'container', :comp => 'acl').returns("mock-uri")
48
+ service.expects(:generate_request).with(:put, "mock-uri", {:x_ms_version => '2009-09-19', :x_ms_blob_public_access => false}, nil).returns(RestClient::Request.new(:method => :put, :url => "http://localhost"))
49
+ properties = service.set_container_acl('mock-container', false)
50
+ end
51
+
52
+ it "should list containers" do
53
+ response = <<-eos
54
+ <?xml version="1.0" encoding="utf-8"?>
55
+ <EnumerationResults AccountName="http://myaccount.blob.core.windows.net">
56
+ <Containers>
57
+ <Container>
58
+ <Name>mycontainer</Name>
59
+ <Url>http://localhost/mycontainer</Url>
60
+ <LastModified>2009-09-11</LastModified>
61
+ </Container>
62
+ <Container>
63
+ <Name>othercontainer</Name>
64
+ <Url>http://localhost/othercontainer</Url>
65
+ <LastModified>2009-09-11</LastModified>
66
+ </Container>
67
+ </Containers>
68
+ </EnumerationResults>
69
+ eos
70
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
71
+ RestClient::Request.any_instance.expects(:execute).returns(response)
72
+ service.expects(:generate_request_uri).with(nil, {:comp => 'list'}).returns("mock-uri")
73
+ service.expects(:generate_request).with(:get, "mock-uri", {}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
74
+ containers = service.list_containers
75
+ containers[0][:name].should == "mycontainer"
76
+ containers[1][:name].should == "othercontainer"
77
+ end
78
+
79
+ it "should delete container" do
80
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
81
+ RestClient::Request.any_instance.expects(:execute)
82
+ service.expects(:generate_request_uri).with("mock-container", {:restype => 'container'}).returns("mock-uri")
83
+ service.expects(:generate_request).with(:delete, "mock-uri", {:x_ms_version => '2009-09-19'}, nil).returns(RestClient::Request.new(:method => :put, :url => "http://localhost"))
84
+ service.delete_container('mock-container')
85
+ end
86
+
87
+ it "should list blobs" do
88
+ response = <<-eos
89
+ <?xml version="1.0" encoding="utf-8"?>
90
+ <EnumerationResults AccountName="http://myaccount.blob.core.windows.net">
91
+ <Blobs>
92
+ <Blob>
93
+ <Url>http://localhost/container/blob</Url>
94
+ <Name>blob</Name>
95
+ <Properties>
96
+ <Content-Type>text/xml</Content-Type>
97
+ </Properties>
98
+ <Metadata>
99
+ <Name>value</Name>
100
+ </Metadata>
101
+ </Blob>
102
+ <Blob>
103
+ <Url>http://localhost/container/blob2</Url>
104
+ <Name>blob2</Name>
105
+ <Properties>
106
+ <Content-Type>application/x-stream</Content-Type>
107
+ </Properties>
108
+ <Metadata>
109
+ <Name>value</Name>
110
+ </Metadata>
111
+ </Blob>
112
+ </Blobs>
113
+ </EnumerationResults>
114
+ eos
115
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
116
+ RestClient::Request.any_instance.expects(:execute).returns(response)
117
+ service.expects(:generate_request_uri).with("container", {:restype => 'container', :comp => 'list'}).returns("mock-uri")
118
+ service.expects(:generate_request).with(:get, "mock-uri", {:x_ms_version => '2009-09-19'}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
119
+ blobs = service.list_blobs("container")
120
+ blobs[0][:name].should == "blob"
121
+ blobs[1][:name].should == "blob2"
122
+ blobs[0][:url].should == "http://localhost/container/blob"
123
+ blobs[1][:url].should == "http://localhost/container/blob2"
124
+ blobs[0][:content_type].should == "text/xml"
125
+ blobs[1][:content_type].should == "application/x-stream"
126
+ end
127
+
128
+ it "should put blob" do
129
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
130
+ RestClient::Request.any_instance.expects(:execute).returns(nil)
131
+ service.expects(:generate_request_uri).with("container/blob", nil).returns("mock-uri")
132
+ service.expects(:generate_request).with(:put, "mock-uri", {'Content-Type' => 'application/octet-stream', :x_ms_version => "2009-09-19", :x_ms_blob_type => "BlockBlob"}, "payload").returns(RestClient::Request.new(:method => :put, :url => "http://localhost"))
133
+ service.put_blob("container/blob", "payload")
134
+ end
135
+
136
+ it "should get blob" do
137
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
138
+ RestClient::Request.any_instance.expects(:execute).returns("payload")
139
+ service.expects(:generate_request_uri).with("container/blob", {}).returns("mock-uri")
140
+ service.expects(:generate_request).with(:get, "mock-uri", {:x_ms_version => "2009-09-19"}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
141
+ service.get_blob("container/blob").should == "payload"
142
+ end
143
+
144
+ it "should delete blob" do
145
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
146
+ RestClient::Request.any_instance.expects(:execute).returns(nil)
147
+ service.expects(:generate_request_uri).with("container/blob", nil).returns("mock-uri")
148
+ service.expects(:generate_request).with(:delete, "mock-uri", {:x_ms_version => "2009-09-19"}, nil).returns(RestClient::Request.new(:method => :put, :url => "http://localhost"))
149
+ service.delete_blob("container/blob")
150
+ end
151
+
152
+ it "should get blob properties" do
153
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
154
+ response = mock()
155
+ response.stubs(:headers).returns(RestClient::AbstractResponse.beautify_headers({"x-ms-meta-Name" => ["customName"]}))
156
+ RestClient::Request.any_instance.expects(:execute).returns(response)
157
+ service.expects(:generate_request_uri).with("container/blob", {}).returns("mock-uri")
158
+ service.expects(:generate_request).with(:head, "mock-uri", {:x_ms_version => '2009-09-19'}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
159
+ service.get_blob_properties("container/blob").should == response.headers
160
+ end
161
+
162
+ it "should set blob properties" do
163
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
164
+ RestClient::Request.any_instance.expects(:execute).returns(nil)
165
+ service.expects(:generate_request_uri).with("container/blob", :comp => 'properties').returns("mock-uri")
166
+ service.expects(:generate_request).with(:put, "mock-uri", {:x_ms_version => '2009-09-19', :x_ms_meta_Name => "johnny"}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
167
+ service.set_blob_properties("container/blob", {:x_ms_meta_Name => "johnny"})
168
+ end
169
+
170
+ it "should copy blob" do
171
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
172
+ RestClient::Request.any_instance.expects(:execute).returns(nil)
173
+ service.expects(:generate_request_uri).with("container/blob-copy", nil).returns("mock-uri")
174
+ service.expects(:generate_request).with(:put, "mock-uri", {:x_ms_version => "2009-09-19", :x_ms_copy_source => "/mock-account/container/blob"}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
175
+ service.copy_blob("container/blob", "container/blob-copy")
176
+ end
177
+
178
+ it "should put block" do
179
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
180
+ RestClient::Request.any_instance.expects(:execute).returns(nil)
181
+ service.expects(:generate_request_uri).with("container/blob", { :blockid => 'block_id', :comp => 'block'}).returns("mock-uri")
182
+ service.expects(:generate_request).with(:put, "mock-uri", {'Content-Type' => 'application/octet-stream'}, "payload").returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
183
+ service.put_block("container/blob", "block_id", "payload")
184
+ end
185
+
186
+ it "should put block list" do
187
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
188
+ RestClient::Request.any_instance.expects(:execute).returns(nil)
189
+ service.expects(:generate_request_uri).with("container/blob", { :comp => 'blocklist'}).returns("mock-uri")
190
+ service.expects(:generate_request).with(:put, "mock-uri", {'Content-Type' => 'text/plain', :x_ms_version => '2009-09-19'}, '<?xml version="1.0" encoding="utf-8"?><BlockList><Latest>AAAAAA==</Latest><Latest>AQAAAA==</Latest></BlockList>').returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
191
+ service.put_block_list("container/blob", ["AAAAAA==", "AQAAAA=="], "text/plain")
192
+ end
193
+
194
+ it "should list blocks" do
195
+ response = <<-eos
196
+ <?xml version="1.0" encoding="utf-8"?>
197
+ <BlockList>
198
+ <CommittedBlocks>
199
+ <Block>
200
+ <Name>AAAAAA==</Name>
201
+ <Size>1048576</Size>
202
+ </Block>
203
+ </CommittedBlocks>
204
+ <UncommittedBlocks>
205
+ <Block>
206
+ <Name>AQAAAA==</Name>
207
+ <Size>1048576</Size>
208
+ </Block>
209
+ <Block>
210
+ <Name>AgAAAA==</Name>
211
+ <Size>402848</Size>
212
+ </Block>
213
+ </UncommittedBlocks>
214
+ </BlockList>
215
+ eos
216
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
217
+ RestClient::Request.any_instance.expects(:execute).returns(response)
218
+ service.expects(:generate_request_uri).with('container/blob', {:comp => 'blocklist', :blocklisttype => 'all'}).returns("mock-uri")
219
+ service.expects(:generate_request).with(:get, "mock-uri", {:x_ms_version => "2009-04-14"}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
220
+ blocks = service.list_blocks('container/blob')
221
+ blocks.first[:name].should == "AAAAAA=="
222
+ blocks.first[:size].should == "1048576"
223
+ blocks.first[:committed].should == true
224
+ blocks.last[:name].should == "AgAAAA=="
225
+ blocks.last[:size].should == "402848"
226
+ blocks.last[:committed].should == false
227
+ end
228
+
229
+ it "should list with additional parameters" do
230
+ response = <<-eos
231
+ <?xml version="1.0" encoding="utf-8"?>
232
+ <BlockList>
233
+ <CommittedBlocks>
234
+ <Block>
235
+ <Name>AAAAAA==</Name>
236
+ <Size>1048576</Size>
237
+ </Block>
238
+ </CommittedBlocks>
239
+ <UncommittedBlocks>
240
+ <Block>
241
+ <Name>AQAAAA==</Name>
242
+ <Size>1048576</Size>
243
+ </Block>
244
+ <Block>
245
+ <Name>AgAAAA==</Name>
246
+ <Size>402848</Size>
247
+ </Block>
248
+ </UncommittedBlocks>
249
+ </BlockList>
250
+ eos
251
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
252
+ RestClient::Request.any_instance.expects(:execute).returns(response)
253
+ service.expects(:generate_request_uri).with('container/blob', {:comp => 'blocklist', :blocklisttype => 'committed'}).returns("mock-uri")
254
+ service.expects(:generate_request).with(:get, "mock-uri", {:x_ms_version => "2009-04-14"}, nil).returns(RestClient::Request.new(:method => :get, :url => "http://localhost"))
255
+ blocks = service.list_blocks('container/blob', 'COMMITTED')
256
+ blocks.first[:name].should == "AAAAAA=="
257
+ blocks.first[:size].should == "1048576"
258
+ blocks.first[:committed].should == true
259
+ blocks.last[:name].should == "AgAAAA=="
260
+ blocks.last[:size].should == "402848"
261
+ blocks.last[:committed].should == false
262
+ end
263
+
264
+ it "should throw when block list type is nil or doesn't fall into the valid values" do
265
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
266
+ lambda { service.list_blocks('container/blob', 'whatever') }.should raise_error(WAZ::Storage::InvalidParameterValue)
267
+ lambda { service.list_blocks('container/blob', nil) }.should raise_error(WAZ::Storage::InvalidParameterValue)
268
+ end
269
+
270
+ it "should take blob snapshots" do
271
+ service = WAZ::Blobs::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
272
+
273
+ mock_response = mock()
274
+ mock_response.stubs(:headers).returns({:x_ms_snapshot => Time.new.httpdate})
275
+ mock_request = mock()
276
+ mock_request.stubs(:execute).returns(mock_response)
277
+
278
+ service.expects(:generate_request_uri).with("container/blob", {:comp => "snapshot"}).returns("container/blob")
279
+ service.expects(:generate_request).with(:put, "container/blob", {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
280
+ service.snapshot_blob("container/blob").should == mock_response.headers[:x_ms_snapshot]
281
+ end
282
+ end
@@ -0,0 +1,33 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'tests/configuration'
4
+ require 'lib/waz-queues'
5
+
6
+ describe "Message object behavior" do
7
+ it "should delete message" do
8
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
9
+ WAZ::Queues::Service.any_instance.expects(:delete_message).with("queue-10", "message id", "receipt")
10
+ options = {:message_id => "message id", :message_text => "text", :expiration_time => Time.new,
11
+ :insertion_time => Time.new, :pop_receipt => "receipt", :queue_name => "queue-10"}
12
+
13
+ message = WAZ::Queues::Message.new(options)
14
+ message.destroy!
15
+ end
16
+
17
+ it "should throw when trying to delete a message peeked (no pop_receipt)" do
18
+ options = {:message_id => "message id", :message_text => "text", :expiration_time => Time.new,
19
+ :insertion_time => Time.new, :queue_name => "queue-10" }
20
+
21
+ message = WAZ::Queues::Message.new(options)
22
+
23
+ lambda { message.destroy! }.should raise_error(WAZ::Queues::InvalidOperation)
24
+ end
25
+
26
+ it "should respond to dequeue_count property" do
27
+ options = {:message_id => "message id", :message_text => "text", :expiration_time => Time.new,
28
+ :insertion_time => Time.new, :pop_receipt => "receipt", :queue_name => "queue-10", :dequeue_count => 1}
29
+
30
+ message = WAZ::Queues::Message.new(options)
31
+ message.dequeue_count.should == 1
32
+ end
33
+ end
@@ -0,0 +1,206 @@
1
+ # enabling the load of files from root (on RSpec)
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../')
3
+ require 'tests/configuration'
4
+ require 'lib/waz-queues'
5
+
6
+ describe "Queue object behavior" do
7
+ it "should list queues by name" do
8
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
9
+ WAZ::Queues::Service.any_instance.expects(:list_queues).returns([{:name => 'queue1', :url => 'queue1_url'}, {:name => 'queue2', :url => 'queue2_url'}])
10
+ containers = WAZ::Queues::Queue.list
11
+ containers.size.should == 2
12
+ containers.first().name.should == "queue1"
13
+ containers.first().url.should == "queue1_url"
14
+ containers.last().name.should == "queue2"
15
+ end
16
+
17
+ it "should throw when not name provided for the queue" do
18
+ lambda { WAZ::Queues::Queue.new({:foo => "bar"}) }.should raise_error(WAZ::Storage::InvalidOption)
19
+ end
20
+
21
+ it "should throw when not url provided for the queue" do
22
+ lambda { WAZ::Queues::Queue.new({:name => "mock-queue"}) }.should raise_error(WAZ::Storage::InvalidOption)
23
+ end
24
+
25
+ it "should create queue" do
26
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
27
+ WAZ::Queues::Service.any_instance.expects(:create_queue)
28
+ queue = WAZ::Queues::Queue.create('queue1')
29
+ queue.name.should == "queue1"
30
+ queue.url.should == "http://my-account.queue.core.windows.net/queue1"
31
+ end
32
+
33
+ it "should find queue" do
34
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
35
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).returns { }
36
+ queue = WAZ::Queues::Queue.find('queue1')
37
+ queue.name.should == "queue1"
38
+ queue.url.should == "http://my-account.queue.core.windows.net/queue1"
39
+ end
40
+
41
+ it "should return null when the queue isn't found" do
42
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
43
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").raises(RestClient::ResourceNotFound)
44
+ queue = WAZ::Queues::Queue.find('queue1')
45
+ queue.nil?.should == true
46
+ end
47
+
48
+ it "should delete queue" do
49
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
50
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({})
51
+ WAZ::Queues::Service.any_instance.expects(:delete_queue).with("queue1").returns()
52
+ queue = WAZ::Queues::Queue.find('queue1')
53
+ queue.destroy!
54
+ end
55
+
56
+ it "should get queue metadata" do
57
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
58
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({:x_ms_meta_property => "value"}).twice
59
+ queue = WAZ::Queues::Queue.find('queue1')
60
+ queue.metadata[:x_ms_meta_property].should == "value"
61
+ end
62
+
63
+ it "should get queue length" do
64
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
65
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({:x_ms_approximate_messages_count => "2"}).twice
66
+ queue = WAZ::Queues::Queue.find('queue1')
67
+ queue.size.should == 2
68
+ end
69
+
70
+ it "should merge queue metadata new metadata" do
71
+ existing_metadata = {:x_ms_approximate_message_count => 2, :x_ms_request_id => 2, :x_ms_meta_property1 => "value1"}
72
+ valid_metadata = {:x_ms_meta_property1 => "value1"}
73
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
74
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns(existing_metadata).twice
75
+ WAZ::Queues::Service.any_instance.expects(:set_queue_metadata).with(valid_metadata.merge(:x_ms_meta_property2 => "value2"))
76
+ queue = WAZ::Queues::Queue.find('queue1')
77
+ queue.put_properties!({:x_ms_meta_property2 => "value2"})
78
+ end
79
+
80
+ it "should override queue metadata new metadata" do
81
+ existing_metadata = {:x_ms_approximate_message_count => 2, :x_ms_request_id => 2, :x_ms_meta_property1 => "value1"}
82
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
83
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns(existing_metadata)
84
+ WAZ::Queues::Service.any_instance.expects(:set_queue_metadata).with({:x_ms_meta_property2 => "value2"})
85
+ queue = WAZ::Queues::Queue.find('queue1')
86
+ queue.put_properties!({:x_ms_meta_property2 => "value2"}, true)
87
+ end
88
+
89
+ it "should enqueue message on the queue" do
90
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
91
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
92
+ WAZ::Queues::Service.any_instance.expects(:enqueue).with("queue1", "this is my message enqueued", 604800)
93
+ queue = WAZ::Queues::Queue.find('queue1')
94
+ queue.enqueue!("this is my message enqueued")
95
+ end
96
+
97
+ it "should enqueue message on the queue with specific time to live" do
98
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
99
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
100
+ WAZ::Queues::Service.any_instance.expects(:enqueue).with("queue1", "this is my message enqueued", 600)
101
+ queue = WAZ::Queues::Queue.find('queue1')
102
+ queue.enqueue!("this is my message enqueued", 600)
103
+ end
104
+
105
+ it "should peek lock a single message from the queue" do
106
+ expected_message = {:message_id => "message id", :message_text => "text", :expiration_time => Time.new, :insertion_time => Time.new, :pop_receipt => "receipt"}
107
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
108
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
109
+ WAZ::Queues::Service.any_instance.expects(:get_messages).with('queue1', {:num_of_messages => 1}).returns([expected_message])
110
+ queue = WAZ::Queues::Queue.find('queue1')
111
+ message = queue.lock()
112
+ message.queue_name == "queue1"
113
+ message.message_id.should == "message id"
114
+ message.message_text.should == "text"
115
+ message.pop_receipt.should == "receipt"
116
+ end
117
+
118
+ it "should peek lock messages from the queue" do
119
+ expected_messages = [ {:message_id => "message id", :message_text => "text", :expiration_time => Time.new, :insertion_time => Time.new, :pop_receipt => "receipt"},
120
+ {:message_id => "message id2", :message_text => "text-second", :expiration_time => Time.new, :insertion_time => Time.new, :pop_receipt => "receipt"}]
121
+
122
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
123
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
124
+ WAZ::Queues::Service.any_instance.expects(:get_messages).with('queue1', {:num_of_messages => 2}).returns(expected_messages)
125
+ queue = WAZ::Queues::Queue.find('queue1')
126
+ messages = queue.lock(2)
127
+ messages.last().queue_name == "queue1"
128
+ messages.last().message_id.should == "message id2"
129
+ messages.last().message_text.should == "text-second"
130
+ messages.last().pop_receipt.should == "receipt"
131
+ end
132
+
133
+ it "should peek a single message from the queue" do
134
+ expected_message = {:message_id => "message id", :message_text => "text", :expiration_time => Time.new, :insertion_time => Time.new, :pop_receipt => "receipt"}
135
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
136
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
137
+ WAZ::Queues::Service.any_instance.expects(:peek).with('queue1', {:num_of_messages => 1}).returns([expected_message])
138
+ queue = WAZ::Queues::Queue.find('queue1')
139
+ message = queue.peek()
140
+ message.queue_name == "queue1"
141
+ message.message_id.should == "message id"
142
+ message.message_text.should == "text"
143
+ message.pop_receipt.should == "receipt"
144
+ end
145
+
146
+ it "should peek messages from the queue" do
147
+ expected_messages = [{:message_id => "message id", :message_text => "text", :expiration_time => Time.new, :insertion_time => Time.new, :pop_receipt => "receipt"},
148
+ {:message_id => "message id2", :message_text => "text-second", :expiration_time => Time.new, :insertion_time => Time.new, :pop_receipt => "receipt"}]
149
+
150
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
151
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
152
+ WAZ::Queues::Service.any_instance.expects(:peek).with('queue1', {:num_of_messages => 2}).returns(expected_messages)
153
+ queue = WAZ::Queues::Queue.find('queue1')
154
+ messages = queue.peek(2)
155
+ messages.last().queue_name == "queue1"
156
+ messages.last().message_id.should == "message id2"
157
+ messages.last().message_text.should == "text-second"
158
+ messages.last().pop_receipt.should == "receipt"
159
+ end
160
+
161
+ it "should clear queue" do
162
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
163
+ WAZ::Queues::Service.any_instance.expects(:get_queue_metadata).with("queue1").returns({}).once
164
+ WAZ::Queues::Service.any_instance.expects(:clear_queue).with('queue1').returns()
165
+ queue = WAZ::Queues::Queue.find('queue1')
166
+ queue.clear()
167
+ end
168
+
169
+ it "should list queues including metadata when requested" do
170
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
171
+ WAZ::Queues::Service.any_instance.expects(:list_queues).with({:include => 'metadata'}).returns({}).once
172
+ queue = WAZ::Queues::Queue.list(true)
173
+ end
174
+
175
+ it "should raise an exception when queue name starts with - (hypen)" do
176
+ lambda { WAZ::Queues::Queue.create('-queue') }.should raise_error(WAZ::Storage::InvalidParameterValue)
177
+ end
178
+
179
+ it "should raise an exception when queue name ends with - (hypen)" do
180
+ lambda { WAZ::Queues::Queue.create('queue-') }.should raise_error(WAZ::Storage::InvalidParameterValue)
181
+ end
182
+
183
+ it "should raise an exception when queue name is less than 3" do
184
+ lambda { WAZ::Queues::Queue.create('q') }.should raise_error(WAZ::Storage::InvalidParameterValue)
185
+ end
186
+
187
+ it "should raise an exception when queue name is longer than 63" do
188
+ lambda { WAZ::Queues::Queue.create('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa') }.should raise_error(WAZ::Storage::InvalidParameterValue)
189
+ end
190
+
191
+ it "should create the queue if it doesn't exists when calling ensure" do
192
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
193
+ WAZ::Queues::Queue.expects(:create).with("queue1")
194
+ WAZ::Queues::Queue.expects(:find).with("queue1").returns(nil)
195
+
196
+ queue = WAZ::Queues::Queue.ensure("queue1")
197
+ end
198
+
199
+ it "should retrieve the queue if it already exists when calling ensure" do
200
+ WAZ::Storage::Base.stubs(:default_connection).returns({:account_name => "my-account", :access_key => "key"})
201
+ WAZ::Queues::Queue.expects(:create).with("queue1").never
202
+ WAZ::Queues::Queue.expects(:find).with("queue1").returns(mock())
203
+
204
+ queue = WAZ::Queues::Queue.ensure("queue1")
205
+ end
206
+ end