You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2012/11/09 20:49:39 UTC

[8/9] git commit: CIMI tests: infer the correct model class from the response

CIMI tests: infer the correct model class from the response

CIMI responses carry enough information for us to figure out what model
class should be used to parse the response. That makes it unnecessary to
list the expected model class explicitly


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

Branch: refs/heads/master
Commit: dab4197d07c751b6d09b90079b97d36946de3f0f
Parents: 433fa81
Author: David Lutterkort <lu...@redhat.com>
Authored: Thu Nov 8 17:11:30 2012 -0800
Committer: David Lutterkort <lu...@redhat.com>
Committed: Fri Nov 9 11:48:42 2012 -0800

----------------------------------------------------------------------
 tests/cimi/cep_test.rb                |    2 +-
 tests/cimi/machine_collection_test.rb |    4 +-
 tests/cimi/machine_test.rb            |    2 +-
 tests/cimi/network_collection_test.rb |    4 +-
 tests/cimi/test_helper.rb             |   46 ++++++++++++++++++++-------
 5 files changed, 40 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/dab4197d/tests/cimi/cep_test.rb
----------------------------------------------------------------------
diff --git a/tests/cimi/cep_test.rb b/tests/cimi/cep_test.rb
index 76ee963..364d9e5 100644
--- a/tests/cimi/cep_test.rb
+++ b/tests/cimi/cep_test.rb
@@ -34,7 +34,7 @@ class CloundEntryPointBehavior < CIMI::Test::Spec
   RESOURCE_URI = "http://schemas.dmtf.org/cimi/1/CloudEntryPoint"
 
   # We'd like to call this :cep, but there's already a method by that name
-  model :subject, CIMI::Model::CloudEntryPoint, :cache => true do |fmt|
+  model :subject, :cache => true do |fmt|
     cep(:accept => fmt)
   end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/dab4197d/tests/cimi/machine_collection_test.rb
----------------------------------------------------------------------
diff --git a/tests/cimi/machine_collection_test.rb b/tests/cimi/machine_collection_test.rb
index c6b5b7b..4e487fe 100644
--- a/tests/cimi/machine_collection_test.rb
+++ b/tests/cimi/machine_collection_test.rb
@@ -22,12 +22,12 @@ class MachineCollectionBehavior < CIMI::Test::Spec
   RESOURCE_URI =
     "http://schemas.dmtf.org/cimi/1/MachineCollection"
 
-  model :machines, CIMI::Model::MachineCollection do |fmt|
+  model :machines do |fmt|
     mcoll_uri = cep(:accept => :json).json["machines"]["href"]
     get(mcoll_uri, :accept => fmt)
   end
 
-  check_collection :machines, CIMI::Model::Machine
+  check_collection :machines
 
   it "should have the correct resourceURI", :only => :json do
     machines.wont_be_nil      # Make sure we talk to the server

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/dab4197d/tests/cimi/machine_test.rb
----------------------------------------------------------------------
diff --git a/tests/cimi/machine_test.rb b/tests/cimi/machine_test.rb
index fd76ee5..ec263a6 100644
--- a/tests/cimi/machine_test.rb
+++ b/tests/cimi/machine_test.rb
@@ -22,7 +22,7 @@ class MachineBehavior < CIMI::Test::Spec
 
   RESOURCE_URI = "http://schemas.dmtf.org/cimi/1/Machine"
 
-  model :machine, CIMI::Model::Machine do |fmt|
+  model :machine do |fmt|
     mcoll_uri = cep(:accept => :json).json["machines"]["href"]
     mcoll = get(mcoll_uri, :accept => :json).json
     m_url = mcoll["machines"][0]["id"]

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/dab4197d/tests/cimi/network_collection_test.rb
----------------------------------------------------------------------
diff --git a/tests/cimi/network_collection_test.rb b/tests/cimi/network_collection_test.rb
index cd01583..bd2774a 100644
--- a/tests/cimi/network_collection_test.rb
+++ b/tests/cimi/network_collection_test.rb
@@ -22,10 +22,10 @@ class NetworkCollectionBehavior < CIMI::Test::Spec
 
   need_collection :networks
 
-  model :networks, CIMI::Model::NetworkCollection do |fmt|
+  model :networks do |fmt|
     coll_uri = cep(:accept => :json).json["networks"]["href"]
     get(coll_uri, :accept => fmt)
   end
 
-  check_collection :networks, CIMI::Model::Network
+  check_collection :networks
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/dab4197d/tests/cimi/test_helper.rb
----------------------------------------------------------------------
diff --git a/tests/cimi/test_helper.rb b/tests/cimi/test_helper.rb
index 3d35aaa..824a5e5 100644
--- a/tests/cimi/test_helper.rb
+++ b/tests/cimi/test_helper.rb
@@ -88,6 +88,25 @@ module CIMI::Test::Methods
       RestClient.get absolute_url(path), headers(params)
     end
 
+    # Find the model class that can process the body of the HTTP response
+    # +resp+
+    def model_class(resp)
+      resource = nil
+      ct = resp.headers[:content_type]
+      if ct == "application/json"
+        resource = resp.json["resourceURI"].split("/").last
+      elsif ct == "application/xml"
+        if resp.xml.root.name == "Collection"
+          resource = resp.xml.root["resourceURI"].split("/").last
+        else
+          resource = resp.xml.root.name
+        end
+      else
+        raise "Unexpected content type #{response.content_type}"
+      end
+      CIMI::Model::const_get(resource)
+    end
+
     private
     def absolute_url(path)
       if path.start_with?("http")
@@ -124,9 +143,8 @@ module CIMI::Test::Methods
     end
 
     # Perform basic collection checks; +model_name+ is the name of the
-    # method returning the collection model; +member_class+ is the class
-    # for the model of individual entries
-    def check_collection(model_name, member_class)
+    # method returning the collection model
+    def check_collection(model_name)
       it "must have the \"id\" and \"count\" attributes" do
         coll = self.send(model_name)
         coll.count.wont_be_nil
@@ -137,7 +155,7 @@ module CIMI::Test::Methods
       it "must have a valid id and name for each member" do
         self.send(model_name).entries.each do |entry|
           entry.id.must_be_uri
-          member = fetch(entry.id, member_class)
+          member = fetch(entry.id)
           member.id.must_equal entry.id
           member.name.must_equal entry.name
         end
@@ -165,9 +183,9 @@ class CIMI::Test::Spec < MiniTest::Spec
     @content_type = CONTENT_TYPES[fmt]
   end
 
-  def fetch(uri, model_class)
-    resp = retrieve(uri, model_class) { |fmt| get(uri, :accept => fmt) }
-    model_class.parse(resp.body, @content_type)
+  def fetch(uri)
+    resp = retrieve(uri) { |fmt| get(uri, :accept => fmt) }
+    parse(resp)
   end
 
   def self.it desc = "anonymous", opts = {}, &block
@@ -188,22 +206,22 @@ class CIMI::Test::Spec < MiniTest::Spec
     end
   end
 
-  def self.model(name, model_class, opts = {}, &block)
+  def self.model(name, opts = {}, &block)
     define_method name do
       @_memoized ||= {}
       @@_cache ||= {}
       resp = @_memoized.fetch("#{name}_#{@format}") do |k|
         if opts[:cache]
           @_memoized[k] = @@_cache.fetch(k) do |k|
-            @@_cache[k] = retrieve(k, model_class, &block)
+            @@_cache[k] = retrieve(k, &block)
           end
         else
-          @_memoized[k] = retrieve(k, model_class, &block)
+          @_memoized[k] = retrieve(k, &block)
         end
       end
       @@_cache[:last_response] ||= {}
       @@_cache[:last_response][@format] = resp
-      model_class.parse(resp.body, @content_type)
+      parse(resp)
     end
   end
 
@@ -215,7 +233,11 @@ class CIMI::Test::Spec < MiniTest::Spec
 
   private
 
-  def retrieve(k, model_class, &block)
+  def parse(response)
+    model_class(response).parse(response.body, @content_type)
+  end
+
+  def retrieve(k, &block)
     response = instance_exec(@format, &block)
     assert_equal @content_type, response.headers[:content_type]
     # FIXME: for XML check that the correct namespace is set