artrest 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +20 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +31 -0
  7. data/artrest.gemspec +30 -0
  8. data/bin/artrest +194 -0
  9. data/lib/artrest.rb +81 -0
  10. data/lib/artrest/build.rb +31 -0
  11. data/lib/artrest/buildnumber.rb +26 -0
  12. data/lib/artrest/builds.rb +46 -0
  13. data/lib/artrest/dir_entry.rb +115 -0
  14. data/lib/artrest/repositories.rb +61 -0
  15. data/lib/artrest/repository.rb +42 -0
  16. data/lib/artrest/resource.rb +366 -0
  17. data/lib/artrest/resources.rb +54 -0
  18. data/lib/artrest/system.rb +74 -0
  19. data/lib/artrest/system_general_configuration.rb +45 -0
  20. data/lib/artrest/version.rb +3 -0
  21. data/spec/artrest/build_spec.rb +62 -0
  22. data/spec/artrest/buildnumber_spec.rb +45 -0
  23. data/spec/artrest/builds_spec.rb +69 -0
  24. data/spec/artrest/folder_spec.rb +88 -0
  25. data/spec/artrest/repositories_spec.rb +47 -0
  26. data/spec/artrest/repository_spec.rb +63 -0
  27. data/spec/artrest/resource_spec.rb +385 -0
  28. data/spec/artrest/resources_spec.rb +66 -0
  29. data/spec/artrest/system_general_configuration_spec.rb +72 -0
  30. data/spec/artrest/system_spec.rb +50 -0
  31. data/spec/artrest_spec.rb +30 -0
  32. data/spec/fixtures/build/build_response_correct.txt +7 -0
  33. data/spec/fixtures/buildnumber/buildnumber_25_response.txt +7 -0
  34. data/spec/fixtures/builds/build_api_response_correct.txt +7 -0
  35. data/spec/fixtures/folder/pom_file_response_1.txt +7 -0
  36. data/spec/fixtures/folder/sub_folder_response.txt +7 -0
  37. data/spec/fixtures/folder/top_folder_response.txt +7 -0
  38. data/spec/fixtures/repositories/libs_snapshot_local_folder_response.txt +7 -0
  39. data/spec/fixtures/repositories/repositories_response.txt +7 -0
  40. data/spec/fixtures/repository/libs_snapshot_local_response.txt +7 -0
  41. data/spec/fixtures/resource/artifact_response.txt +7 -0
  42. data/spec/fixtures/resource/build_response.txt +7 -0
  43. data/spec/fixtures/resource/buildnumber_response.txt +7 -0
  44. data/spec/fixtures/resource/builds_response.txt +7 -0
  45. data/spec/fixtures/resource/folder_response.txt +7 -0
  46. data/spec/fixtures/resource/json_response.txt +7 -0
  47. data/spec/fixtures/resource/repositories_response.txt +7 -0
  48. data/spec/fixtures/resource/repository_not_found_response.txt +7 -0
  49. data/spec/fixtures/resource/repository_response.txt +7 -0
  50. data/spec/fixtures/resource/repository_unauthorized_response.txt +8 -0
  51. data/spec/fixtures/resource/string_response.txt +188 -0
  52. data/spec/fixtures/resource/sub_response.txt +188 -0
  53. data/spec/fixtures/resource/system_general_configuration_response.txt +654 -0
  54. data/spec/fixtures/resource/system_response.txt +188 -0
  55. data/spec/fixtures/resources/resources_response.txt +7 -0
  56. data/spec/fixtures/system/200_OK_ping_response.txt +7 -0
  57. data/spec/fixtures/system/general_configuration_response.txt +654 -0
  58. data/spec/fixtures/system/system_response.txt +188 -0
  59. data/spec/spec.opts +5 -0
  60. data/spec/spec_helper.rb +38 -0
  61. metadata +269 -0
@@ -0,0 +1,47 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Repositories do
4
+
5
+ before(:each) do
6
+ @repos_url = "#{ARTIFACTORY_URL}/api/repositories"
7
+ @artrepos = ArtRest::Repositories.new(@repos_url, OPTIONS)
8
+ register_stub_request('./repositories/repositories_response.txt', "api/repositories")
9
+ end
10
+
11
+ describe "::get" do
12
+ it "should return an ArtRest::Repositories instance" do
13
+ repositories = ArtRest::Repositories.get(ARTIFACTORY_URL, OPTIONS)
14
+ repositories.should be_an_instance_of ArtRest::Repositories
15
+ end
16
+ end
17
+
18
+ describe "#each" do
19
+ it "should iterate over all repositories" do
20
+ expected_number_of_repos = 24
21
+ actual_number_of_repos = 0
22
+ @artrepos.each do |repo|
23
+ actual_number_of_repos += 1
24
+ end
25
+ actual_number_of_repos.should equal expected_number_of_repos
26
+ end
27
+
28
+ it "should should return each repo as an ArtRest:Repository instance" do
29
+ @artrepos.each do |repo|
30
+ repo.should be_an_instance_of ArtRest::Repository
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#repository" do
36
+
37
+ before(:each) do
38
+ @repo_name = 'libs-snapshot-local'
39
+ register_stub_request('./repositories/libs_snapshot_local_folder_response.txt', "api/storage/#{@repo_name}/")
40
+ end
41
+
42
+ it "should return requested repository as an ArtRest::Repository instance" do
43
+ repo = @artrepos.repository(@repo_name)
44
+ repo.should be_an_instance_of ArtRest::Repository
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,63 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Repository do
4
+
5
+ before(:each) do
6
+ @repo_name = 'libs-snapshot-local'
7
+ @repo_url = "#{ARTIFACTORY_URL}/api/storage/#{@repo_name}"
8
+ @artrepo = ArtRest::Repository.new(@repo_url, OPTIONS)
9
+ register_stub_request('./repository/libs_snapshot_local_response.txt', "api/storage/#{@repo_name}")
10
+ end
11
+
12
+ describe "#each" do
13
+ it "should iterate over all folders" do
14
+ expected_number_of_folders = 1
15
+ actual_number_of_folders = 0
16
+ @artrepo.each do |folder|
17
+ actual_number_of_folders += 1
18
+ end
19
+ actual_number_of_folders.should equal expected_number_of_folders
20
+ end
21
+
22
+ it "should return each folder as an instance of ArtRest::Folder" do
23
+ @artrepo.each do |folder|
24
+ folder.should be_an_instance_of ArtRest::Folder
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "[all resource attributes]" do
30
+
31
+ before(:each) do
32
+ @resource_attributes = [:path,
33
+ :lastUpdated,
34
+ :repo,
35
+ :uri,
36
+ :modifiedBy,
37
+ :created,
38
+ :createdBy,
39
+ :lastModified,
40
+ :metadataUri,
41
+ :children]
42
+ end
43
+
44
+ context "when no block given" do
45
+ it "should return a non-nil value" do
46
+ @resource_attributes.each do |attr|
47
+ value = @artrepo.send(attr)
48
+ value.should_not be_nil
49
+ end
50
+ end
51
+ end
52
+
53
+ context "when block given" do
54
+ it "should yield a non-nil value to that block" do
55
+ @resource_attributes.each do |attr|
56
+ @artrepo.send(attr) do |value|
57
+ value.should_not be_nil
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,385 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe ArtRest::Resource do
4
+
5
+ describe "::create" do
6
+ before(:each) do
7
+ @system_url = "#{ARTIFACTORY_URL}/api/system"
8
+ register_stub_request('./resource/system_response.txt', "api/system")
9
+
10
+ @system_config_url = "#{ARTIFACTORY_URL}/api/system/configuration"
11
+ register_stub_request('./resource/system_general_configuration_response.txt', "api/system/configuration")
12
+
13
+ @builds_url = "#{ARTIFACTORY_URL}/api/build"
14
+ register_stub_request('./resource/builds_response.txt', "api/build")
15
+
16
+ @build_url = "#{ARTIFACTORY_URL}/api/build/vnet.sms.common.shell"
17
+ register_stub_request('./resource/build_response.txt', "api/build/vnet.sms.common.shell")
18
+
19
+ @buildnumber_url = "#{ARTIFACTORY_URL}/api/build/vnet.sms.common.shell/25"
20
+ register_stub_request('./resource/buildnumber_response.txt', "api/build/vnet.sms.common.shell/25")
21
+
22
+ @repositories_url = "#{ARTIFACTORY_URL}/api/repositories"
23
+ register_stub_request('./resource/repositories_response.txt', "api/repositories")
24
+
25
+ @repository_url = "#{ARTIFACTORY_URL}/api/storage/libs-snapshot-local"
26
+ register_stub_request('./resource/repository_response.txt', "api/storage/libs-snapshot-local")
27
+
28
+ @folder_url = "#{ARTIFACTORY_URL}/api/storage/ext-snapshot-local/vnet/sms/infrastructure/rpm-elasticsearch/1.0.0-SNAPSHOT"
29
+ register_stub_request('./resource/folder_response.txt',
30
+ 'api/storage/ext-snapshot-local/vnet/sms/infrastructure/rpm-elasticsearch/1.0.0-SNAPSHOT')
31
+
32
+ @artifact_url = "#{ARTIFACTORY_URL}/api/storage/ext-snapshot-local/vnet/sms/infrastructure/rpm-elasticsearch/1.0.0-SNAPSHOT//rpm-elasticsearch-1.0.0-20120411.155423-1.pom"
33
+ register_stub_request('./resource/artifact_response.txt',
34
+ 'api/storage/ext-snapshot-local/vnet/sms/infrastructure/rpm-elasticsearch/1.0.0-SNAPSHOT//rpm-elasticsearch-1.0.0-20120411.155423-1.pom')
35
+ end
36
+
37
+ context "given a system resource URL" do
38
+ it "should create an ArtRest::System instance" do
39
+ instance = ArtRest::Resource.create("#{@system_url}?foo=bar", OPTIONS)
40
+ instance.should be_an_instance_of ArtRest::System
41
+ end
42
+ end
43
+
44
+ context "given a system general configuration resource URL" do
45
+ it "should create an ArtRest::System::GeneralConfiguration instance" do
46
+ instance = ArtRest::Resource.create("#{@system_config_url}?foo=bar", OPTIONS)
47
+ instance.should be_an_instance_of ArtRest::System::GeneralConfiguration
48
+ end
49
+ end
50
+
51
+ context "given a builds resource URL" do
52
+ it "should create an ArtRest::Builds instance" do
53
+ instance = ArtRest::Resource.create("#{@builds_url}?foo=bar", OPTIONS)
54
+ instance.should be_an_instance_of ArtRest::Builds
55
+ end
56
+ end
57
+
58
+ context "given a build resource URL" do
59
+ it "should create an ArtRest::Build instance" do
60
+ instance = ArtRest::Resource.create("#{@build_url}?foo=bar", OPTIONS)
61
+ instance.should be_an_instance_of ArtRest::Build
62
+ end
63
+ end
64
+
65
+ context "given a buildnumber resource URL" do
66
+ it "should create an ArtRest::Buildnumber instance" do
67
+ instance = ArtRest::Resource.create("#{@buildnumber_url}?foo=bar", OPTIONS)
68
+ instance.should be_an_instance_of ArtRest::Buildnumber
69
+ end
70
+ end
71
+
72
+ context "given a repositories resource URL" do
73
+ it "should create an ArtRest::Repositories instance" do
74
+ instance = ArtRest::Resource.create("#{@repositories_url}?foo=bar", OPTIONS)
75
+ instance.should be_an_instance_of ArtRest::Repositories
76
+ end
77
+ end
78
+
79
+ context "given a repository resource URL" do
80
+ it "should create an ArtRest::Repository instance" do
81
+ instance = ArtRest::Resource.create("#{@repository_url}?foo=bar", OPTIONS)
82
+ instance.should be_an_instance_of ArtRest::Repository
83
+ end
84
+ end
85
+
86
+ context "given a folder resource URL" do
87
+ it "should create an ArtRest::Folder instance" do
88
+ instance = ArtRest::Resource.create("#{@folder_url}?foo=bar", OPTIONS)
89
+ instance.should be_an_instance_of ArtRest::Folder
90
+ end
91
+ end
92
+
93
+ context "given an artifact resource URL" do
94
+ it "should create an ArtRest::Artifact instance" do
95
+ instance = ArtRest::Resource.create("#{@artifact_url}?foo=bar", OPTIONS)
96
+ instance.should be_an_instance_of ArtRest::Artifact
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "#base_url" do
102
+ it "should correctly expose the 'base_url' property" do
103
+ resource = ArtRest::Resource.new("#{ARTIFACTORY_URL}/api/resource", OPTIONS)
104
+ base_url = resource.base_url
105
+ base_url.should_not be_nil
106
+ base_url.should eq ARTIFACTORY_URL
107
+ end
108
+ end
109
+
110
+ describe "#content" do
111
+ before(:each) do
112
+ @string_url = "#{ARTIFACTORY_URL}/api/string"
113
+ @string_res = StringResourceSample.new(@string_url, OPTIONS)
114
+ register_stub_request('./resource/string_response.txt', "api/string")
115
+
116
+ @json_url = "#{ARTIFACTORY_URL}/api/json"
117
+ @json_res = JsonResourceSample.new(@json_url, OPTIONS)
118
+ register_stub_request('./resource/json_response.txt', "api/json")
119
+ end
120
+
121
+ context "when dealing with plain text content" do
122
+ it "should return content as string" do
123
+ content = @string_res.content
124
+ content.should be_an_instance_of String
125
+ end
126
+ end
127
+
128
+ context "when dealing with json content" do
129
+ it "should return content as ruby hash" do
130
+ content = @json_res.content
131
+ content.should be_an_instance_of Hash
132
+ end
133
+ end
134
+
135
+ context "when passing in a block" do
136
+ it "should pass content on to a given block" do
137
+ @string_res.content do |content|
138
+ content.should be_an_instance_of String
139
+ end
140
+ end
141
+
142
+ it "should return self" do
143
+ return_value = @string_res.content do |content|
144
+ # Intentionally left blank
145
+ end
146
+ return_value.should be @string_res
147
+ end
148
+ end
149
+
150
+ context "given a non-existing resource URL" do
151
+ before(:each) do
152
+ @non_existing_res_url = "#{ARTIFACTORY_URL}/api/does-not-exist"
153
+ @non_existing_res = JsonResourceSample.new(@non_existing_res_url, OPTIONS)
154
+ register_stub_request('./resource/repository_not_found_response.txt', "api/does-not-exist")
155
+ end
156
+
157
+ it "should raise RestClient::ResourceNotFound" do
158
+ expect { @non_existing_res.content }.to raise_error RestClient::ResourceNotFound
159
+ end
160
+ end
161
+
162
+ context "given invalid credentials" do
163
+ before(:each) do
164
+ @unauthorized_res_url = "#{ARTIFACTORY_URL}/api/unauthorized"
165
+ @invalid_options = OPTIONS.merge({ :password => 'wrong' })
166
+ @unauthorized_res = JsonResourceSample.new(@unauthorized_res_url, @invalid_options)
167
+ register_stub_request('./resource/repository_unauthorized_response.txt', "api/unauthorized", "wrong")
168
+ end
169
+
170
+ it "should raise RestClient::Unauthorized" do
171
+ expect { @unauthorized_res.content }.to raise_error RestClient::Unauthorized
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "#content=" do
177
+ before(:each) do
178
+ @string_url = "#{ARTIFACTORY_URL}/api/string"
179
+ @string_res = StringResourceSample.new(@string_url, OPTIONS)
180
+ register_stub_request('./resource/string_response.txt', "api/string")
181
+
182
+ @json_url = "#{ARTIFACTORY_URL}/api/json"
183
+ @json_res = JsonResourceSample.new(@json_url, OPTIONS)
184
+ register_stub_request('./resource/json_response.txt', "api/json")
185
+ end
186
+
187
+ context "when dealing with a string resource" do
188
+ it "should accept a string as is" do
189
+ new_content = "NEW"
190
+ @string_res.content = new_content
191
+ @string_res.content.should == new_content
192
+ end
193
+
194
+ it "should convert a hash into a string" do
195
+ new_content = { :content => "NEW" }
196
+ @string_res.content = new_content
197
+ @string_res.content.should == new_content.to_s
198
+ end
199
+ end
200
+
201
+ context "when dealing with a json resource" do
202
+ it "should accept a hash as is" do
203
+ new_content = { :content => "NEW" }
204
+ @json_res.content = new_content
205
+ @json_res.content.should == new_content
206
+ end
207
+
208
+ it "should parse a json hash into a ruby hash" do
209
+ new_content = "{ \"json\": 1 }"
210
+ @json_res.content = new_content
211
+ @json_res.content.should == JSON.parse(new_content)
212
+ end
213
+
214
+ it "should reject content that is neither a ruby hash nor a json hash with an ArgumentError" do
215
+ new_content = MIME::Types['text/plain']
216
+ expect { @json_res.content = new_content }.to raise_error ArgumentError
217
+ end
218
+ end
219
+ end
220
+
221
+ describe "#content!" do
222
+ before(:each) do
223
+ @string_url = "#{ARTIFACTORY_URL}/api/string"
224
+ @string_res = StringResourceSample.new(@string_url, OPTIONS)
225
+ register_stub_request('./resource/string_response.txt', "api/string")
226
+ end
227
+
228
+ context "when passing in a block" do
229
+ it "should pass content on to a given block" do
230
+ @string_res.content! do |content|
231
+ content.should be_an_instance_of String
232
+ end
233
+ end
234
+
235
+ it "should return self" do
236
+ return_value = @string_res.content! do |content|
237
+ # Intentionally left blank
238
+ end
239
+ return_value.should be @string_res
240
+ end
241
+ end
242
+
243
+ context "when passing in a block returning a value" do
244
+ it "should accept that value as our resource's new content" do
245
+ new_value = "NEW"
246
+ @string_res.content! do |content|
247
+ new_value
248
+ end
249
+ @string_res.content.should == new_value
250
+ end
251
+ end
252
+
253
+ context "given a non-existing resource URL" do
254
+ before(:each) do
255
+ @non_existing_res_url = "#{ARTIFACTORY_URL}/api/does-not-exist"
256
+ @non_existing_res = JsonResourceSample.new(@non_existing_res_url, OPTIONS)
257
+ register_stub_request('./resource/repository_not_found_response.txt', "api/does-not-exist")
258
+ end
259
+
260
+ it "should raise RestClient::ResourceNotFound" do
261
+ expect { @non_existing_res.content! { |content| } }.to raise_error RestClient::ResourceNotFound
262
+ end
263
+ end
264
+
265
+ context "given invalid credentials" do
266
+ before(:each) do
267
+ @unauthorized_res_url = "#{ARTIFACTORY_URL}/api/unauthorized"
268
+ @invalid_options = OPTIONS.merge({ :password => 'wrong' })
269
+ @unauthorized_res = JsonResourceSample.new(@unauthorized_res_url, @invalid_options)
270
+ register_stub_request('./resource/repository_unauthorized_response.txt', "api/unauthorized", "wrong")
271
+ end
272
+
273
+ it "should raise RestClient::Unauthorized" do
274
+ expect { @unauthorized_res.content! { |content| } }.to raise_error RestClient::Unauthorized
275
+ end
276
+ end
277
+ end
278
+
279
+ describe "#unparsed_content" do
280
+ before(:each) do
281
+ class ArtRest::Resource
282
+ public :unparsed_content
283
+ end
284
+ @string_url = "#{ARTIFACTORY_URL}/api/string"
285
+ @string_res = StringResourceSample.new(@string_url, OPTIONS)
286
+ register_stub_request('./resource/string_response.txt', "api/string")
287
+
288
+ @json_url = "#{ARTIFACTORY_URL}/api/json"
289
+ @json_res = JsonResourceSample.new(@json_url, OPTIONS)
290
+ register_stub_request('./resource/json_response.txt', "api/json")
291
+ end
292
+
293
+ after(:each) do
294
+ class ArtRest::Resource
295
+ protected :unparsed_content
296
+ end
297
+ end
298
+
299
+ context "when dealing with plain text content" do
300
+ it "should return content as string" do
301
+ unparsed_content = @string_res.unparsed_content
302
+ unparsed_content.should be_an_instance_of String
303
+ end
304
+ end
305
+
306
+ context "when dealing with json content" do
307
+ it "should return content as string " do
308
+ unparsed_content = @json_res.unparsed_content
309
+ unparsed_content.should be_an_instance_of String
310
+ end
311
+ end
312
+
313
+ context "given a non-existing resource URL" do
314
+ before(:each) do
315
+ @non_existing_res_url = "#{ARTIFACTORY_URL}/api/does-not-exist"
316
+ @non_existing_res = JsonResourceSample.new(@non_existing_res_url, OPTIONS)
317
+ register_stub_request('./resource/repository_not_found_response.txt', "api/does-not-exist")
318
+ end
319
+
320
+ it "should raise RestClient::ResourceNotFound" do
321
+ expect { @non_existing_res.unparsed_content }.to raise_error RestClient::ResourceNotFound
322
+ end
323
+ end
324
+
325
+ context "given invalid credentials" do
326
+ before(:each) do
327
+ @unauthorized_res_url = "#{ARTIFACTORY_URL}/api/unauthorized"
328
+ @invalid_options = OPTIONS.merge({ :password => 'wrong' })
329
+ @unauthorized_res = JsonResourceSample.new(@unauthorized_res_url, @invalid_options)
330
+ register_stub_request('./resource/repository_unauthorized_response.txt', "api/unauthorized", "wrong")
331
+ end
332
+
333
+ it "should raise RestClient::Unauthorized" do
334
+ expect { @unauthorized_res.unparsed_content }.to raise_error RestClient::Unauthorized
335
+ end
336
+ end
337
+ end
338
+
339
+ describe "#[]" do
340
+ before(:each) do
341
+ @json_url = "#{ARTIFACTORY_URL}/api/json"
342
+ @json_res = JsonResourceSample.new(@json_url, OPTIONS)
343
+ register_stub_request('./resource/sub_response.txt', "api/json/sub")
344
+ end
345
+
346
+ it "should return sub resource of appropriate type" do
347
+ sub_resource = @json_res['/sub']
348
+ sub_resource.should_not be_nil
349
+ sub_resource.should be_an_instance_of SubResourceSample
350
+ end
351
+ end
352
+ end
353
+
354
+ class StringResourceSample < ArtRest::Resource
355
+
356
+ self.mime_type = MIME::Types['text/plain']
357
+
358
+ class << self
359
+ def matches_path(path, options)
360
+ path =~ /^\/api\/string$/
361
+ end
362
+ end
363
+ end
364
+
365
+ class JsonResourceSample < ArtRest::Resource
366
+
367
+ self.mime_type = MIME::Types['application/json']
368
+
369
+ class << self
370
+ def matches_path(path, options)
371
+ path =~ /^\/api\/json$/
372
+ end
373
+ end
374
+ end
375
+
376
+ class SubResourceSample < ArtRest::Resource
377
+
378
+ self.mime_type = MIME::Types['text/plain']
379
+
380
+ class << self
381
+ def matches_path(path, options)
382
+ path =~ /^\/api\/json\/sub.*/
383
+ end
384
+ end
385
+ end