You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2011/11/22 15:11:02 UTC

[PATCH core] CIMI: Added 'should_properly_serialize_model' helper to RSpec tests

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/spec/cimi/model/machine_admin_spec.rb       |   14 ++------------
 .../spec/cimi/model/machine_configuration_spec.rb  |   12 ++----------
 server/spec/cimi/model/machine_image_spec.rb       |   11 ++---------
 server/spec/cimi/model/machine_spec.rb             |   12 ++----------
 server/spec/cimi/model/machine_template_spec.rb    |   12 ++----------
 server/spec/cimi/model/volume_spec.rb              |   12 ++----------
 server/spec/spec_helper.rb                         |   15 +++++++--------
 7 files changed, 19 insertions(+), 69 deletions(-)

diff --git a/server/spec/cimi/model/machine_admin_spec.rb b/server/spec/cimi/model/machine_admin_spec.rb
index e9cabc4..036a66d 100644
--- a/server/spec/cimi/model/machine_admin_spec.rb
+++ b/server/spec/cimi/model/machine_admin_spec.rb
@@ -21,18 +21,8 @@ describe "MachineAdmin model" do
     @json = IO::read(File::join(DATA_DIR, "machine_admin.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::MachineAdmin.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineAdmin, @xml, @json
   end
 
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::MachineAdmin.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
-  end
-
-
-
 end
diff --git a/server/spec/cimi/model/machine_configuration_spec.rb b/server/spec/cimi/model/machine_configuration_spec.rb
index 454e8c4..2a57269 100644
--- a/server/spec/cimi/model/machine_configuration_spec.rb
+++ b/server/spec/cimi/model/machine_configuration_spec.rb
@@ -20,16 +20,8 @@ describe "MachineConfiguration model" do
     @json = IO::read(File::join(DATA_DIR, "machine_configuration.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::MachineConfiguration.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::MachineConfiguration.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineConfiguration, @xml, @json
   end
 
 end
diff --git a/server/spec/cimi/model/machine_image_spec.rb b/server/spec/cimi/model/machine_image_spec.rb
index d4af9af..f284952 100644
--- a/server/spec/cimi/model/machine_image_spec.rb
+++ b/server/spec/cimi/model/machine_image_spec.rb
@@ -21,16 +21,9 @@ describe "MachineImage model" do
     @json = IO::read(File::join(DATA_DIR, "machine_image.json"))
   end
 
-  it "can be constructed from XML" do
-    img = CIMI::Model::MachineImage.from_xml(@xml)
-    img.should_not be_nil
-    should_serialize_from_xml! img, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineImage, @xml, @json
   end
 
-  it "can be constructed from JSON" do
-    img = CIMI::Model::MachineImage.from_json(@json)
-    img.should_not be_nil
-    should_serialize_from_json! img, @xml, @json
-  end
 
 end
diff --git a/server/spec/cimi/model/machine_spec.rb b/server/spec/cimi/model/machine_spec.rb
index d50b078..59b5d6f 100644
--- a/server/spec/cimi/model/machine_spec.rb
+++ b/server/spec/cimi/model/machine_spec.rb
@@ -20,16 +20,8 @@ describe "Machine model" do
     @json = IO::read(File::join(DATA_DIR, "machine.json"))
   end
 
-  it "can be constructed from XML" do
-    machine = CIMI::Model::Machine.from_xml(@xml)
-    machine.should_not be_nil
-    should_serialize_from_xml! machine, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    machine = CIMI::Model::Machine.from_json(@json)
-    machine.should_not be_nil
-    should_serialize_from_json! machine, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::Machine, @xml, @json
   end
 
 end
diff --git a/server/spec/cimi/model/machine_template_spec.rb b/server/spec/cimi/model/machine_template_spec.rb
index 0c6f50a..3ca2c9a 100644
--- a/server/spec/cimi/model/machine_template_spec.rb
+++ b/server/spec/cimi/model/machine_template_spec.rb
@@ -20,16 +20,8 @@ describe "MachineTemplate model" do
     @json = IO::read(File::join(DATA_DIR, "machine_template.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::MachineTemplate.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::MachineTemplate.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineTemplate, @xml, @json
   end
 
 end
diff --git a/server/spec/cimi/model/volume_spec.rb b/server/spec/cimi/model/volume_spec.rb
index 829dc57..d208e15 100644
--- a/server/spec/cimi/model/volume_spec.rb
+++ b/server/spec/cimi/model/volume_spec.rb
@@ -21,16 +21,8 @@ describe "Volume model" do
     @json = IO::read(File::join(DATA_DIR, "volume.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::Volume.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::Volume.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::Volume, @xml, @json
   end
 
 end
diff --git a/server/spec/spec_helper.rb b/server/spec/spec_helper.rb
index c8b41fc..69d4797 100644
--- a/server/spec/spec_helper.rb
+++ b/server/spec/spec_helper.rb
@@ -90,14 +90,13 @@ class HashCmp
   end
 end
 
-def should_serialize_from_xml!(model, xml, json)
-  model.to_xml.should serialize_to xml, :fmt => :xml
-  model.to_json.should serialize_to json, :fmt => :json
-end
-
-def should_serialize_from_json!(model, xml, json)
-  model.to_xml.should serialize_to xml, :fmt => :xml
-  model.to_json.should serialize_to json, :fmt => :json
+def should_properly_serialize_model(model_class, xml, json)
+  # Roundtrip in same format
+  model_class.from_xml(xml).should serialize_to xml, :fmt => :xml
+  model_class.from_json(json).should serialize_to json, :fmt => :json
+  # Roundtrip crossing format
+  model_class.from_xml(xml).should serialize_to json, :fmt => :json
+  model_class.from_json(json).should serialize_to xml, :fmt => :xml
 end
 
 RSpec::Matchers.define :serialize_to do |exp, opts|
-- 
1.7.4.4