deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [31/31] git commit: Replace xml_response and json_response by xml and json methods on response
Date Wed, 25 Jul 2012 13:56:39 GMT
Replace xml_response and json_response by xml and json methods on response


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/1dff61b3
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/1dff61b3
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/1dff61b3

Branch: refs/heads/master
Commit: 1dff61b3f9fc5d6179451c18b4b10fc5affedc27
Parents: f8a8d3a
Author: David Lutterkort <lutter@redhat.com>
Authored: Fri Jul 20 18:05:43 2012 -0700
Committer: marios <marios@redhat.com>
Committed: Tue Jul 24 12:01:36 2012 +0300

----------------------------------------------------------------------
 tests/deltacloud/base_api_test.rb |   50 +++++++-------
 tests/deltacloud/buckets_test.rb  |  122 ++++++++++++++++----------------
 tests/deltacloud/test_setup.rb    |   18 +++--
 3 files changed, 96 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1dff61b3/tests/deltacloud/base_api_test.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/base_api_test.rb b/tests/deltacloud/base_api_test.rb
index 0eeeb55..a58ea80 100644
--- a/tests/deltacloud/base_api_test.rb
+++ b/tests/deltacloud/base_api_test.rb
@@ -31,14 +31,14 @@ describe "Deltacloud API Entry Point" do
 
   it 'advertise the current driver in API entrypoint' do
     res = get_api :accept => :xml
-    driver = xml_response(res).root[:driver]
+    driver = res.xml.root[:driver]
     driver.wont_be_nil
     DRIVERS.include?(driver).must_equal true
   end
 
   it 'advertise the current API version in API entrypoint' do
     res = get_api :accept => :xml
-    version = xml_response(res).root[:version]
+    version = res.xml.root[:version]
     version.wont_be_nil
     version.must_equal API_VERSION
   end
@@ -55,29 +55,29 @@ describe "Deltacloud API Entry Point" do
 
   it 'advertise collections in API entrypoint' do
     res = get_api :accept => :xml
-    xml_response(res).xpath('//api/link').wont_be_empty
+    res.xml.xpath('//api/link').wont_be_empty
   end
 
   it 'include the :href and :rel attribute for each collection in API entrypoint' do
-    xml_response(get_api(:accept => :xml)).xpath("//api/link").each do |collection|
+    get_api(:accept => :xml).xml.xpath("//api/link").each do |collection|
       collection[:href].wont_be_nil
       collection[:rel].wont_be_nil
     end
   end
 
   it 'uses the absolute URI in the :href attribute for each collection in API entrypoint'
do
-    xml_response(get_api(:accept => :xml)).xpath("//api/link").each do |collection|
+    get_api(:accept => :xml).xml.xpath("//api/link").each do |collection|
       collection[:href].must_match /^http/
     end
   end
 
   it 'advertise features for some collections in API entrypoint' do
-    xml_doc = xml_response(get_api(:accept => :xml))
-    xml_doc.xpath("//api/link/feature").wont_be_empty
+    doc = get_api(:accept => :xml)
+    doc.xml.xpath("//api/link/feature").wont_be_empty
   end
 
   it 'advertise the name of the feature for some collections in API entrypoint' do
-    xml_response(get_api(:accept => :xml)).xpath("//api/link/feature").each do |feature|
+    get_api(:accept => :xml).xml.xpath("//api/link/feature").each do |feature|
       feature[:name].wont_be_nil
     end
   end
@@ -93,19 +93,19 @@ describe "Deltacloud API Entry Point" do
   end
 
   it 'must change the driver when using X-Deltacloud-Driver HTTP header' do
-    res = xml_response(get_api(:driver => 'ec2', :accept=> :xml))
-    res.root[:driver].must_equal 'ec2'
-    res = xml_response(get_api(:driver => 'mock', :accept=> :xml))
-    res.root[:driver].must_equal 'mock'
+    res = get_api(:driver => 'ec2', :accept=> :xml)
+    res.xml.root[:driver].must_equal 'ec2'
+    res = get_api(:driver => 'mock', :accept=> :xml)
+    res.xml.root[:driver].must_equal 'mock'
   end
 
   it 'must change the features when driver is swapped using HTTP headers' do
-    res = xml_response(get_api(:driver => 'ec2', :accept=> :xml))
+    res = get_api(:driver => 'ec2', :accept=> :xml)
     # The 'user_name' feature is not supported currently for the EC2 driver
-    (res/'api/link/feature').map { |f| f[:name] }.wont_include 'user_name'
-    res = xml_response(get_api(:driver => 'mock', :accept=> :xml))
+    (res.xml/'api/link/feature').map { |f| f[:name] }.wont_include 'user_name'
+    res = get_api(:driver => 'mock', :accept=> :xml)
     # But it's supported in Mock driver
-    (res/'api/link/feature').map { |f| f[:name] }.must_include 'user_name'
+    (res.xml/'api/link/feature').map { |f| f[:name] }.must_include 'user_name'
   end
 
   it 'must re-validate the driver credentials when using "?force_auth" parameter in URL'
do
@@ -119,18 +119,18 @@ describe "Deltacloud API Entry Point" do
   end
 
   it 'must change the API PROVIDER using the /api;provider matrix parameter in URI' do
-    res = xml_response(get(';provider=test1', :public => true))
-    res.root[:provider].wont_be_nil
-    res.root[:provider].must_equal 'test1'
-    res = xml_response(get(';provider=test2', :public => true))
-    res.root[:provider].must_equal 'test2'
+    res = get(';provider=test1', :public => true)
+    res.xml.root[:provider].wont_be_nil
+    res.xml.root[:provider].must_equal 'test1'
+    res = get(';provider=test2', :public => true)
+    res.xml.root[:provider].must_equal 'test2'
   end
 
   it 'must change the API DRIVER using the /api;driver matrix parameter in URI' do
-    res = xml_response(get(';driver=ec2', :public => true))
-    res.root[:driver].must_equal 'ec2'
-    res = xml_response(get(';driver=mock', :public => true))
-    res.root[:driver].must_equal 'mock'
+    res = get(';driver=ec2', :public => true)
+    res.xml.root[:driver].must_equal 'ec2'
+    res = get(';driver=mock', :public => true)
+    res.xml.root[:driver].must_equal 'mock'
   end
 
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1dff61b3/tests/deltacloud/buckets_test.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/buckets_test.rb b/tests/deltacloud/buckets_test.rb
index 6b4b743..728ff71 100644
--- a/tests/deltacloud/buckets_test.rb
+++ b/tests/deltacloud/buckets_test.rb
@@ -38,17 +38,18 @@ def create_a_bucket_and_blob
   unless res.code == 200
     raise Exception.new("Failed to create blob #{blob_name}")
   end
-  return [bucket_name, blob_name]
+  return [ bucket_name, blob_name ]
 end
 
 #make sure we have at least one bucket and blob to test
-bucket, blob = create_a_bucket_and_blob
+created_bucket_and_blob = create_a_bucket_and_blob
 
 features_hash = discover_features
 
 describe 'Deltacloud API buckets collection' do
 
   MiniTest::Unit.after_tests {
+    bucket, blob = created_bucket_and_blob
     # delete the bucket/blob we created for the tests
     res = delete("/buckets/#{bucket}/#{blob}")
     unless res.code == 204
@@ -61,7 +62,7 @@ describe 'Deltacloud API buckets collection' do
   }
 
   it 'must advertise the buckets collection in API entrypoint' do
-    res = xml_response(get("/"))
+    res = get("/").xml
     (res/'api/link[@rel=buckets]').wont_be_empty
   end
 
@@ -80,10 +81,9 @@ describe 'Deltacloud API buckets collection' do
     res = post(BUCKETS, :name=>bucket_name)
     #check response
     res.code.must_equal 201
-    xml_res = xml_response(res)
-    xml_res.xpath("//bucket/name").text.must_equal bucket_name
-    xml_res.xpath("//bucket").size.must_equal 1
-    xml_res.xpath("//bucket")[0][:id].must_equal bucket_name
+    res.xml.xpath("//bucket/name").text.must_equal bucket_name
+    res.xml.xpath("//bucket").size.must_equal 1
+    res.xml.xpath("//bucket")[0][:id].must_equal bucket_name
     #GET bucket
     res = get(BUCKETS+"/"+bucket_name)
     res.code.must_equal 200
@@ -112,81 +112,81 @@ describe 'Deltacloud API buckets collection' do
   end
 
   it 'must have the "buckets" element on top level' do
-    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
-    xml_res.root.name.must_equal 'buckets'
+    res = get(BUCKETS, :accept=>:xml)
+    res.xml.root.name.must_equal 'buckets'
   end
 
   it 'must have some "bucket" elements inside "buckets"' do
-    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
-    (xml_res/'buckets/bucket').wont_be_empty
+    res = get(BUCKETS, :accept=>:xml)
+    (res.xml/'buckets/bucket').wont_be_empty
   end
 
   it 'must provide the :id attribute for each bucket in collection' do
-    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept=>:xml)
+    (res.xml/'buckets/bucket').each do |r|
       r[:id].wont_be_nil
     end
   end
 
   it 'must include the :href attribute for each "bucket" element in collection' do
-    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept=>:xml)
+    (res.xml/'buckets/bucket').each do |r|
       r[:href].wont_be_nil
     end
   end
 
   it 'must use the absolute URL in each :href attribute' do
-    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept=>:xml)
+    (res.xml/'buckets/bucket').each do |r|
       r[:href].must_match /^http/
     end
   end
 
   it 'must have the URL ending with the :id of the bucket' do
-    xml_res = xml_response(get(BUCKETS, :accept=>:xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept=>:xml)
+    (res.xml/'buckets/bucket').each do |r|
       r[:href].must_match /#{r[:id]}$/
     end
   end
 
   it 'must have the "name" element defined for each bucket in collection' do
-    xml_res = xml_response(get(BUCKETS, :accept => :xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept => :xml)
+    (res.xml/'buckets/bucket').each do |r|
       (r/'name').wont_be_nil
       (r/'name').wont_be_empty
     end
   end
 
   it 'must have the "size" element defined for each bucket in collection' do
-    xml_res = xml_response(get(BUCKETS, :accept => :xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept => :xml)
+    (res.xml/'buckets/bucket').each do |r|
       (r/'size').wont_be_nil
       (r/'size').wont_be_empty
     end
   end
 
   it 'must return 200 OK when following the URL in bucket element' do
-    xml_res = xml_response(get(BUCKETS, :accept => :xml))
-    (xml_res/'buckets/bucket').each do |r|
+    res = get(BUCKETS, :accept => :xml)
+    (res.xml/'buckets/bucket').each do |r|
       bucket_res = get r[:href]
       bucket_res.code.must_equal 200
     end
   end
 
   it 'must have the "name" element for the bucket and it should match with the one in collection'
do
-    xml_res = xml_response(get(BUCKETS, :accept => :xml))
-    (xml_res/'buckets/bucket').each do |r|
-      bucket_xml = xml_response(get(BUCKETS+"/#{r[:id]}", :accept=>:xml))
-      (bucket_xml/'name').wont_be_empty
-      (bucket_xml/'name').first.text.must_equal((r/'name').first.text)
+    res = get(BUCKETS, :accept => :xml)
+    (res.xml/'buckets/bucket').each do |r|
+      bucket = get(BUCKETS+"/#{r[:id]}", :accept=>:xml)
+      (bucket.xml/'name').wont_be_empty
+      (bucket.xml/'name').first.text.must_equal((r/'name').first.text)
     end
   end
 
   it 'all "blob" elements for the bucket should match the ones in collection' do
-    xml_res = xml_response(get(BUCKETS, :accept => :xml))
-    (xml_res/'buckets/bucket').each do |r|
-      bucket_xml = xml_response(get(BUCKETS+"/#{r[:id]}", :accept=>:xml))
-      (bucket_xml/'bucket/blob').each do |b|
+    res = get(BUCKETS, :accept => :xml)
+    (res.xml/'buckets/bucket').each do |r|
+      bucket = get(BUCKETS+"/#{r[:id]}", :accept=>:xml)
+      (bucket.xml/'bucket/blob').each do |b|
         b[:id].wont_be_nil
         b[:href].wont_be_nil
         b[:href].must_match /^http/
@@ -196,33 +196,33 @@ describe 'Deltacloud API buckets collection' do
   end
 
   it 'must allow to get all blobs details and the details should be set correctly' do
-    xml_res = xml_response(get(BUCKETS, :accept => :xml))
-    (xml_res/'buckets/bucket').each do |r|
-      bucket_xml = xml_response(get(BUCKETS+"/#{r[:id]}", :accept=>:xml))
-      (bucket_xml/'bucket/blob').each do |b|
-        blob_xml = xml_response(get(BUCKETS+"/#{r[:id]}/#{b[:id]}", :accept=>:xml))
-        blob_xml.root.name.must_equal 'blob'
-        blob_xml.root[:id].must_equal b[:id]
-        (blob_xml/'bucket').wont_be_empty
-        (blob_xml/'bucket').size.must_equal 1
-        (blob_xml/'bucket').first.text.wont_be_nil
-        (blob_xml/'bucket').first.text.must_equal r[:id]
-        (blob_xml/'content_length').wont_be_empty
-        (blob_xml/'content_length').size.must_equal 1
-        (blob_xml/'content_length').first.text.must_match /^(\d+)$/
-        (blob_xml/'content_type').wont_be_empty
-        (blob_xml/'content_type').size.must_equal 1
-        (blob_xml/'content_type').first.text.wont_be_nil
-        (blob_xml/'last_modified').wont_be_empty
-        (blob_xml/'last_modified').size.must_equal 1
-        (blob_xml/'last_modified').first.text.wont_be_empty
-        (blob_xml/'content').wont_be_empty
-        (blob_xml/'content').size.must_equal 1
-        (blob_xml/'content').first[:rel].wont_be_nil
-        (blob_xml/'content').first[:rel].must_equal 'blob_content'
-        (blob_xml/'content').first[:href].wont_be_nil
-        (blob_xml/'content').first[:href].must_match /^http/
-        (blob_xml/'content').first[:href].must_match /\/content$/
+    res = get(BUCKETS, :accept => :xml)
+    (res.xml/'buckets/bucket').each do |r|
+      bucket = get(BUCKETS+"/#{r[:id]}", :accept=>:xml)
+      (bucket.xml/'bucket/blob').each do |b|
+        blob = get(BUCKETS+"/#{r[:id]}/#{b[:id]}", :accept=>:xml)
+        blob.xml.root.name.must_equal 'blob'
+        blob.xml.root[:id].must_equal b[:id]
+        (blob.xml/'bucket').wont_be_empty
+        (blob.xml/'bucket').size.must_equal 1
+        (blob.xml/'bucket').first.text.wont_be_nil
+        (blob.xml/'bucket').first.text.must_equal r[:id]
+        (blob.xml/'content_length').wont_be_empty
+        (blob.xml/'content_length').size.must_equal 1
+        (blob.xml/'content_length').first.text.must_match /^(\d+)$/
+        (blob.xml/'content_type').wont_be_empty
+        (blob.xml/'content_type').size.must_equal 1
+        (blob.xml/'content_type').first.text.wont_be_nil
+        (blob.xml/'last_modified').wont_be_empty
+        (blob.xml/'last_modified').size.must_equal 1
+        (blob.xml/'last_modified').first.text.wont_be_empty
+        (blob.xml/'content').wont_be_empty
+        (blob.xml/'content').size.must_equal 1
+        (blob.xml/'content').first[:rel].wont_be_nil
+        (blob.xml/'content').first[:rel].must_equal 'blob_content'
+        (blob.xml/'content').first[:href].wont_be_nil
+        (blob.xml/'content').first[:href].must_match /^http/
+        (blob.xml/'content').first[:href].must_match /\/content$/
       end
     end
   end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/1dff61b3/tests/deltacloud/test_setup.rb
----------------------------------------------------------------------
diff --git a/tests/deltacloud/test_setup.rb b/tests/deltacloud/test_setup.rb
index 64d3cf6..22b37ae 100644
--- a/tests/deltacloud/test_setup.rb
+++ b/tests/deltacloud/test_setup.rb
@@ -40,12 +40,14 @@ end
 API_VERSION = Nokogiri::XML(RestClient.get API_URL).root[:version]
 #SETUP
 
-def xml_response(xml)
-  Nokogiri::XML(xml)
-end
+module RestClient::Response
+  def xml
+    @xml ||= Nokogiri::XML(body)
+  end
 
-def json_response(json)
-  JSON.parse(json)
+  def json
+    @json ||= JSON.parse(body)
+  end
 end
 
 # Make a GET request for +path+ and return the +RestClient::Response+. The
@@ -127,7 +129,7 @@ TEST_FILES =  { :images             => "images_test.rb",
 }
 #gets the list of collections from the server running at API_URL and translates those into
file names accoring to TEST_FILES
 def deltacloud_test_file_names
-  driver_collections = xml_response(RestClient.get API_URL, {:accept=>:xml}).xpath("//api/link").inject([]){|res,
current| res<<current[:rel].to_sym ;res}
+  driver_collections = (RestClient.get API_URL, {:accept=>:xml}).xml.xpath("//api/link").inject([]){|res,
current| res<<current[:rel].to_sym ;res}
   driver_collections.inject([]){|res, current| res << "deltacloud/#{TEST_FILES[current]}"
if TEST_FILES[current] ;res}
 end
 
@@ -137,8 +139,8 @@ def random_name
 end
 
 def discover_features
-  res = xml_response(get("/"))
-  features_hash = res.xpath("//api/link").inject({}) do |result, collection|
+  res = get("/")
+  features_hash = res.xml.xpath("//api/link").inject({}) do |result, collection|
     result.merge!({collection[:rel] => []})
     collection.children.inject([]){|features, current_child| result[collection[:rel]] <<
current_child[:name] if current_child.name == "feature"}
     result


Mime
View raw message