deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@redhat.com
Subject [PATCH 3/3] CIMI tests: revamp to allow same test against XML and JSON output
Date Wed, 29 Aug 2012 23:37:46 GMT
From: David Lutterkort <lutter@redhat.com>

  - use CIMI models to represent data retrieved from server
  - add special Spec class to run each spec twice: once
    using XML, once using JSON
---
 server/lib/cimi/models/schema.rb |    2 +
 tests/cimi/cep_test.rb           |   58 ++++++++-----------------------------
 tests/cimi/test_helper.rb        |   59 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 46 deletions(-)

diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index a0e4637..57a190d 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -14,6 +14,8 @@
 # under the License.
 #
 
+require_relative "../../deltacloud/core_ext"
+
 # The smarts of converting from XML and JSON into internal objects
 class CIMI::Model::Schema
 
diff --git a/tests/cimi/cep_test.rb b/tests/cimi/cep_test.rb
index 8db3986..fcce068 100644
--- a/tests/cimi/cep_test.rb
+++ b/tests/cimi/cep_test.rb
@@ -18,55 +18,21 @@ $:.unshift File.join(File.dirname(__FILE__))
 
 require "test_helper.rb"
 
-describe "CIMI Entry Point" do
-  include CIMI::Test::Methods
-
-  describe "XML form" do
-    # Cache the response for all assertions
-    res = cep(:accept => :xml)
-
-    it "should set the proper content type" do
-      res.headers[:content_type].must_equal "application/xml"
-    end
-
-    it "should use CloudEntryPoint as the XML root" do
-      res.xml.root.name.must_equal "CloudEntryPoint"
-      names = res.xml.xpath("/c:CloudEntryPoint", api.ns).map { |e| e.name }
-      names.must_equal ["CloudEntryPoint"]
-    end
-
-    it "should have an id equal to the CEP URL" do
-      (res.xml/"CloudEntryPoint/id").text.must_equal api.cep_url
-    end
-
-    it "should have a baseURI" do
-      (res.xml/"CloudEntryPoint/baseURI").text.must_be_uri
-    end
-
-    it "should have a name" do
-      (res.xml/"CloudEntryPoint/name").wont_be_empty
-    end
+describe "CIMI Entry Point Behavior" do
+  # 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|
+    cep(:accept => fmt)
   end
 
-  describe "JSON form" do
-    # Cache the response for all assertions
-    res = cep(:accept => :json)
-
-    it "should set the proper content type" do
-      res.headers[:content_type].must_equal "application/json"
-    end
-
-    it "should return JSON if asked to" do
-      res.headers[:content_type].must_equal "application/json"
-      res.json["id"].must_equal api.cep_url
-    end
+  it "should have an id equal to the CEP URL" do
+    subject.id.must_equal api.cep_url
+  end
 
-    it "should have a baseURI" do
-      res.json["baseURI"].must_be_uri
-    end
+  it "should have a baseURI" do
+    subject.base_uri.must_be_uri
+  end
 
-    it "should have a name" do
-      res.json["name"].wont_be_empty
-    end
+  it "should have a name" do
+    subject.name.wont_be_empty
   end
 end
diff --git a/tests/cimi/test_helper.rb b/tests/cimi/test_helper.rb
index f26fa96..8e3ed9f 100644
--- a/tests/cimi/test_helper.rb
+++ b/tests/cimi/test_helper.rb
@@ -19,6 +19,7 @@ require 'require_relative'
 require_relative '../helpers/common.rb'
 
 require 'singleton'
+require_relative "../../server/lib/cimi/models"
 
 # Add CIMI specific config stuff
 module CIMI
@@ -106,3 +107,61 @@ module CIMI::Test::Methods
     base.send(:include, Global)
   end
 end
+
+# Special spec class for 'behavior' tests that need to be run once
+# for XML and once for JSON
+class CIMI::Test::Spec < MiniTest::Spec
+  include CIMI::Test::Methods
+
+  CONTENT_TYPES = { :xml => "application/xml",
+    :json => "application/json" }
+
+  def use_format(fmt)
+    @format = fmt
+    @content_type = CONTENT_TYPES[fmt]
+  end
+
+  def self.it desc = "anonymous", &block
+    block ||= proc { skip "(no tests defined)" }
+
+    CONTENT_TYPES.keys.each do |fmt|
+      super("#{desc} [#{fmt}]") do
+        use_format(fmt)
+        instance_eval &block
+      end
+    end
+  end
+
+  def self.model(name, model_class, opts = {}, &block)
+    define_method name do
+      @_memoized ||= {}
+      @@_cache ||= {}
+      @_memoized.fetch("#{name}_#{@format}") do |k|
+        if opts[:cache]
+          @_memoized[k] = @@_cache.fetch(k) do |k|
+            @@_cache[k] = fetch_model(k, model_class, &block)
+          end
+        else
+          @_memoized[k] = fetch_model(k, model_class, &block)
+        end
+      end
+    end
+  end
+
+  def last_response
+    @@_cache ||= {}
+    @@_cache[:last_response]
+  end
+
+  private
+
+  def fetch_model(k, model_class, &block)
+    response = instance_exec(@format, &block)
+    @@_cache[:last_response] = response
+    assert_equal @content_type, response.headers[:content_type]
+    # FIXME: for XML check that the correct namespace is set
+    model_class.parse(response.body, @content_type)
+  end
+end
+
+MiniTest::Spec.register_spec_type(/Behavior$/, CIMI::Test::Spec)
-- 
1.7.7.6


Mime
View raw message