sparqcode-waz-storage 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gitignore +7 -0
  2. data/CHANGELOG.rdoc +72 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +36 -0
  5. data/LICENSE +19 -0
  6. data/README.rdoc +299 -0
  7. data/lib/waz-blobs.rb +5 -0
  8. data/lib/waz-queues.rb +6 -0
  9. data/lib/waz-storage.rb +39 -0
  10. data/lib/waz-tables.rb +5 -0
  11. data/lib/waz/blobs/blob_object.rb +121 -0
  12. data/lib/waz/blobs/container.rb +160 -0
  13. data/lib/waz/blobs/exceptions.rb +11 -0
  14. data/lib/waz/blobs/service.rb +156 -0
  15. data/lib/waz/queues/exceptions.rb +29 -0
  16. data/lib/waz/queues/message.rb +65 -0
  17. data/lib/waz/queues/queue.rb +165 -0
  18. data/lib/waz/queues/service.rb +106 -0
  19. data/lib/waz/storage/base.rb +70 -0
  20. data/lib/waz/storage/core_service.rb +122 -0
  21. data/lib/waz/storage/exceptions.rb +33 -0
  22. data/lib/waz/storage/validation_rules.rb +26 -0
  23. data/lib/waz/storage/version.rb +11 -0
  24. data/lib/waz/tables/edm_type_helper.rb +45 -0
  25. data/lib/waz/tables/exceptions.rb +45 -0
  26. data/lib/waz/tables/service.rb +178 -0
  27. data/lib/waz/tables/table.rb +75 -0
  28. data/lib/waz/tables/table_array.rb +11 -0
  29. data/rakefile +23 -0
  30. data/tests/configuration.rb +14 -0
  31. data/tests/waz/blobs/blob_object_test.rb +80 -0
  32. data/tests/waz/blobs/container_test.rb +162 -0
  33. data/tests/waz/blobs/service_test.rb +282 -0
  34. data/tests/waz/queues/message_test.rb +33 -0
  35. data/tests/waz/queues/queue_test.rb +206 -0
  36. data/tests/waz/queues/service_test.rb +299 -0
  37. data/tests/waz/storage/base_tests.rb +81 -0
  38. data/tests/waz/storage/shared_key_core_service_test.rb +142 -0
  39. data/tests/waz/tables/service_test.rb +614 -0
  40. data/tests/waz/tables/table_test.rb +98 -0
  41. data/waz-storage.gemspec +29 -0
  42. metadata +187 -0
@@ -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
@@ -0,0 +1,299 @@
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 "Windows Azure Queues API service" do
7
+ it "should list queues" do
8
+ # setup mocks
9
+ expected_url = "http://my_account.queue.core.windows.net/?comp=list"
10
+ expected_response = <<-eos
11
+ <?xml version="1.0" encoding="utf-8"?>
12
+ <EnumerationResults AccountName="http://myaccount.queue.core.windows.net">
13
+ <Queues>
14
+ <Queue>
15
+ <Name>mock-queue</Name>
16
+ <Url>http://myaccount.queue.core.windows.net/mock-queue</Url>
17
+ </Queue>
18
+ </Queues>
19
+ </EnumerationResults>
20
+ eos
21
+
22
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
23
+ mock_request.expects(:execute).returns(expected_response)
24
+
25
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
26
+ # setup mocha expectations
27
+ service.expects(:generate_request_uri).with(nil, :comp => 'list').returns(expected_url)
28
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
29
+
30
+ queues = service.list_queues()
31
+ queues.first()[:name].should == "mock-queue"
32
+ queues.first()[:url].should == "http://myaccount.queue.core.windows.net/mock-queue"
33
+ end
34
+
35
+ it "should create queue" do
36
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue"
37
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
38
+ mock_request.stubs(:execute)
39
+
40
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
41
+ # setup mocha expectations
42
+ service.expects(:generate_request_uri).with("mock-queue", nil).returns(expected_url)
43
+ service.expects(:generate_request).with(:put, expected_url, {:x_ms_meta_priority => "high-importance", :x_ms_version => '2009-09-19'}, nil).returns(mock_request)
44
+
45
+ service.create_queue("mock-queue", {:x_ms_meta_priority => "high-importance"})
46
+ end
47
+
48
+ it "should delete queue" do
49
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue"
50
+ mock_response = mock()
51
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
52
+ mock_request.stubs(:execute).returns(mock_response)
53
+
54
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
55
+ # setup mocha expectations
56
+ service.expects(:generate_request_uri).with("mock-queue", {}).returns(expected_url)
57
+ service.expects(:generate_request).with(:delete, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
58
+ service.delete_queue('mock-queue')
59
+ end
60
+
61
+ it "should get queue metadata" do
62
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue?comp=metadata"
63
+ mock_response = mock()
64
+ mock_response.stubs(:headers).returns({ :x_ms_request_id => "fake-id" })
65
+ mock_request = RestClient::Request.new(:method => :head, :url => expected_url)
66
+ mock_request.stubs(:execute).returns(mock_response)
67
+
68
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
69
+ # setup mocha expectations
70
+ service.expects(:generate_request_uri).with("mock-queue", {:comp => 'metadata'}).returns(expected_url)
71
+ service.expects(:generate_request).with(:head, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
72
+ service.get_queue_metadata('mock-queue').should == mock_response.headers
73
+ end
74
+
75
+ it "should set queue metadata" do
76
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue"
77
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
78
+ mock_request.stubs(:execute)
79
+
80
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
81
+ # setup mocha expectations
82
+ service.expects(:generate_request_uri).with("mock-queue", :comp => 'metadata').returns(expected_url)
83
+ service.expects(:generate_request).with(:put, expected_url, {:x_ms_version => '2009-09-19', :x_ms_meta_priority => "high-importance"}, nil).returns(mock_request)
84
+
85
+ service.set_queue_metadata("mock-queue", {:x_ms_meta_priority => "high-importance"})
86
+ end
87
+
88
+ it "should enqueue message" do
89
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages?messagettl=604800"
90
+ mock_request = RestClient::Request.new(:method => :post, :url => expected_url)
91
+ mock_request.stubs(:execute)
92
+
93
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
94
+ # setup mocha expectations
95
+ payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?><QueueMessage><MessageText>this is the message payload</MessageText></QueueMessage>"
96
+ service.expects(:generate_request_uri).with("mock-queue/messages", { :messagettl => 604800 }).returns(expected_url)
97
+ service.expects(:generate_request).with(:post, expected_url, { 'Content-Type' => "application/xml", :x_ms_version => '2009-09-19'}, payload).returns(mock_request)
98
+
99
+ service.enqueue("mock-queue", "this is the message payload")
100
+ end
101
+
102
+ it "should get messages from queue" do
103
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
104
+ expected_response = <<-eos
105
+ <?xml version="1.0" encoding="utf-8"?>
106
+ <QueueMessagesList>
107
+ <QueueMessage>
108
+ <MessageId>message_id</MessageId>
109
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
110
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
111
+ <PopReceipt>receipt</PopReceipt>
112
+ <TimeNextVisible>Tue, 23 Sep 2008 05:29:20 GMT</TimeNextVisible>
113
+ <MessageText>text</MessageText>
114
+ </QueueMessage>
115
+ </QueueMessagesList>
116
+ eos
117
+
118
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
119
+ mock_request.stubs(:execute).returns(expected_response)
120
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
121
+
122
+ # setup mocha expectations
123
+ service.expects(:generate_request_uri).with("mock-queue/messages", {}).returns(expected_url)
124
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
125
+
126
+ messages = service.get_messages("mock-queue")
127
+ messages.first()[:message_id].should == "message_id"
128
+ messages.first()[:pop_receipt].should == "receipt"
129
+ messages.first()[:message_text].should == "text"
130
+ messages.first()[:insertion_time].nil?.should == false
131
+ messages.first()[:expiration_time].nil?.should == false
132
+ messages.first()[:time_next_visible].nil?.should == false
133
+ end
134
+
135
+ it "should get messages from queue specifiying additional parameters" do
136
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
137
+ expected_response = <<-eos
138
+ <?xml version="1.0" encoding="utf-8"?>
139
+ <QueueMessagesList>
140
+ <QueueMessage>
141
+ <MessageId>message_id</MessageId>
142
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
143
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
144
+ <PopReceipt>receipt</PopReceipt>
145
+ <TimeNextVisible>Tue, 23 Sep 2008 05:29:20 GMT</TimeNextVisible>
146
+ <MessageText>text</MessageText>
147
+ </QueueMessage>
148
+ </QueueMessagesList>
149
+ eos
150
+
151
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
152
+ mock_request.stubs(:execute).returns(expected_response)
153
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
154
+
155
+ # setup mocha expectations
156
+ service.expects(:generate_request_uri).with("mock-queue/messages", {:num_of_messages => 2, :visibility_timeout => 3}).returns(expected_url)
157
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
158
+
159
+ messages = service.get_messages("mock-queue", :num_of_messages => 2, :visibility_timeout => 3)
160
+ messages.first()[:message_id].should == "message_id"
161
+ messages.first()[:pop_receipt].should == "receipt"
162
+ messages.first()[:message_text].should == "text"
163
+ messages.first()[:insertion_time].nil?.should == false
164
+ messages.first()[:expiration_time].nil?.should == false
165
+ messages.first()[:time_next_visible].nil?.should == false
166
+ end
167
+
168
+ it "should raise exception when :num_of_messages is out of range (1 - 32)" do
169
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
170
+ lambda{ service.get_messages("mock-queue", :num_of_messages => 0) }.should raise_error(WAZ::Queues::OptionOutOfRange)
171
+ lambda{ service.get_messages("mock-queue", :num_of_messages => 33) }.should raise_error(WAZ::Queues::OptionOutOfRange)
172
+ end
173
+
174
+ it "should raise exception when :visibility_timeout is out of range (1 - 7200)" do
175
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
176
+ lambda{ service.get_messages("mock-queue", :visibility_timeout => 0) }.should raise_error(WAZ::Queues::OptionOutOfRange)
177
+ lambda{ service.get_messages("mock-queue", :visibility_timeout => 7201) }.should raise_error(WAZ::Queues::OptionOutOfRange)
178
+ end
179
+
180
+ it "should peek message from queue" do
181
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
182
+ expected_response = <<-eos
183
+ <?xml version="1.0" encoding="utf-8"?>
184
+ <QueueMessagesList>
185
+ <QueueMessage>
186
+ <MessageId>message_id</MessageId>
187
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
188
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
189
+ <MessageText>text</MessageText>
190
+ <DequeueCount>5</DequeueCount>
191
+ </QueueMessage>
192
+ </QueueMessagesList>
193
+ eos
194
+
195
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
196
+ mock_request.stubs(:execute).returns(expected_response)
197
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
198
+
199
+ # setup mocha expectations
200
+ service.expects(:generate_request_uri).with("mock-queue/messages", {:peek_only => true}).returns(expected_url)
201
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
202
+
203
+ messages = service.peek("mock-queue")
204
+ messages.first()[:message_id].should == "message_id"
205
+ messages.first()[:message_text].should == "text"
206
+ messages.first()[:insertion_time].nil?.should == false
207
+ messages.first()[:expiration_time].nil?.should == false
208
+ messages.first()[:dequeue_count].should == 5
209
+ end
210
+
211
+ it "should peek messages from queue when :num_of_messages is specified" do
212
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
213
+ expected_response = <<-eos
214
+ <?xml version="1.0" encoding="utf-8"?>
215
+ <QueueMessagesList>
216
+ <QueueMessage>
217
+ <MessageId>message_id</MessageId>
218
+ <InsertionTime>Mon, 22 Sep 2008 23:29:20 GMT</InsertionTime>
219
+ <ExpirationTime>Mon, 29 Sep 2008 23:29:20 GMT</ExpirationTime>
220
+ <MessageText>text</MessageText>
221
+ <DequeueCount>5</DequeueCount>
222
+ </QueueMessage>
223
+ </QueueMessagesList>
224
+ eos
225
+
226
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
227
+ mock_request.stubs(:execute).returns(expected_response)
228
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
229
+
230
+ # setup mocha expectations
231
+ service.expects(:generate_request_uri).with("mock-queue/messages", {:peek_only => true, :num_of_messages => 32}).returns(expected_url)
232
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => "2009-09-19"}, nil).returns(mock_request)
233
+
234
+ messages = service.peek("mock-queue", {:num_of_messages => 32})
235
+ messages.first()[:message_id].should == "message_id"
236
+ messages.first()[:message_text].should == "text"
237
+ messages.first()[:insertion_time].nil?.should == false
238
+ messages.first()[:expiration_time].nil?.should == false
239
+ messages.first()[:dequeue_count].should == 5
240
+ end
241
+
242
+ it "should delete message" do
243
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages/message_id?popreceipt=pop_receipt"
244
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
245
+ mock_request.stubs(:execute)
246
+
247
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
248
+ # setup mocha expectations
249
+ service.expects(:generate_request_uri).with("mock-queue/messages/message_id", { :pop_receipt => "pop_receipt" }).returns(expected_url)
250
+ service.expects(:generate_request).with(:delete, expected_url, {}, nil).returns(mock_request)
251
+
252
+ service.delete_message("mock-queue", "message_id", "pop_receipt")
253
+ end
254
+
255
+ it "should clear queue" do
256
+ expected_url = "http://myaccount.queue.core.windows.net/mock-queue/messages"
257
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
258
+ mock_request.stubs(:execute)
259
+
260
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
261
+ # setup mocha expectations
262
+ service.expects(:generate_request_uri).with("mock-queue/messages", {}).returns(expected_url)
263
+ service.expects(:generate_request).with(:delete, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
264
+
265
+ service.clear_queue("mock-queue")
266
+ end
267
+
268
+ it "should list queues with metadata" do
269
+ # setup mocks
270
+ expected_url = "http://my_account.queue.core.windows.net/?comp=list&include=metadata"
271
+ expected_response = <<-eos
272
+ <?xml version="1.0" encoding="utf-8"?>
273
+ <EnumerationResults AccountName="http://myaccount.queue.core.windows.net">
274
+ <Queues>
275
+ <Queue>
276
+ <Name>mock-queue</Name>
277
+ <Url>http://myaccount.queue.core.windows.net/mock-queue</Url>
278
+ <Metadata>
279
+ <x-ms-name>Custom Queue</x-ms-name>
280
+ </Metadata>
281
+ </Queue>
282
+ </Queues>
283
+ </EnumerationResults>
284
+ eos
285
+
286
+ mock_request = RestClient::Request.new(:method => :get, :url => expected_url)
287
+ mock_request.expects(:execute).returns(expected_response)
288
+
289
+ service = WAZ::Queues::Service.new(:account_name => "mock-account", :access_key => "mock-key", :type_of_service => "queue", :use_ssl => true, :base_url => "localhost")
290
+ # setup mocha expectations
291
+ service.expects(:generate_request_uri).with(nil, {:comp => 'list', :include => 'metadata'}).returns(expected_url)
292
+ service.expects(:generate_request).with(:get, expected_url, {:x_ms_version => '2009-09-19'}, nil).returns(mock_request)
293
+
294
+ queues = service.list_queues({:include => 'metadata'})
295
+ queues.first()[:name].should == "mock-queue"
296
+ queues.first()[:url].should == "http://myaccount.queue.core.windows.net/mock-queue"
297
+ queues.first()[:metadata][:x_ms_name] = 'Custom Queue'
298
+ end
299
+ end