deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@redhat.com
Subject [PATCH 4/4] CIMI: ensure that JSON responses have a proper resourceURI
Date Mon, 05 Nov 2012 19:24:45 GMT
From: David Lutterkort <lutter@redhat.com>

---
 server/lib/cimi/models/base.rb        |   10 +++++++-
 tests/cimi/cep_test.rb                |    7 ++++++
 tests/cimi/machine_collection_test.rb |   34 +++++++++++++++++++++++++++++++
 tests/cimi/machine_test.rb            |   36 +++++++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+), 2 deletions(-)
 create mode 100644 tests/cimi/machine_collection_test.rb
 create mode 100644 tests/cimi/machine_test.rb

diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index fe6016d..d217d4e 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -219,13 +219,19 @@ class CIMI::Model::Base
     self.name.split("::").last
   end
 
+  def self.resource_uri
+    CMWG_NAMESPACE + "/" + self.name.split("::").last
+  end
+
   def self.to_json(model)
-    JSON::unparse(@schema.to_json(model))
+    json = @schema.to_json(model)
+    json[:resourceURI] = resource_uri
+    JSON::unparse(json)
   end
 
   def self.to_xml(model)
     xml = @schema.to_xml(model)
-    xml["xmlns"] = "http://schemas.dmtf.org/cimi/1"
+    xml["xmlns"] = CMWG_NAMESPACE
     XmlSimple.xml_out(xml, :root_name => xml_tag_name)
   end
 
diff --git a/tests/cimi/cep_test.rb b/tests/cimi/cep_test.rb
index 4864ada..76ee963 100644
--- a/tests/cimi/cep_test.rb
+++ b/tests/cimi/cep_test.rb
@@ -31,6 +31,8 @@ class CloundEntryPointBehavior < CIMI::Test::Spec
             "jobs", "meters", "meterTemplates", "meterConfigs",
             "eventLogs", "eventLogTemplates" ]
 
+  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|
     cep(:accept => fmt)
@@ -48,6 +50,11 @@ class CloundEntryPointBehavior < CIMI::Test::Spec
     subject.name.wont_be_empty
   end
 
+  it "should have the correct resourceURI", :only => :json do
+    subject.wont_be_nil     # Make sure we talk to the server
+    last_response.json["resourceURI"].must_equal RESOURCE_URI
+  end
+
   it "should have root collections" do
     ROOTS.each do |root|
       r = root.underscore.to_sym
diff --git a/tests/cimi/machine_collection_test.rb b/tests/cimi/machine_collection_test.rb
new file mode 100644
index 0000000..c390b5b
--- /dev/null
+++ b/tests/cimi/machine_collection_test.rb
@@ -0,0 +1,34 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+$:.unshift File.join(File.dirname(__FILE__))
+
+require "test_helper.rb"
+
+class MachineCollectionBehavior < CIMI::Test::Spec
+  RESOURCE_URI =
+    "http://schemas.dmtf.org/cimi/1/MachineCollection"
+
+  model :machines, CIMI::Model::MachineCollection do |fmt|
+    mcoll_uri = cep(:accept => :json).json["machines"]["href"]
+    get(mcoll_uri, :accept => :json)
+  end
+
+  it "should have the correct resourceURI", :only => :json do
+    machines.wont_be_nil      # Make sure we talk to the server
+    last_response.json["resourceURI"].must_equal RESOURCE_URI
+  end
+end
diff --git a/tests/cimi/machine_test.rb b/tests/cimi/machine_test.rb
new file mode 100644
index 0000000..fd76ee5
--- /dev/null
+++ b/tests/cimi/machine_test.rb
@@ -0,0 +1,36 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+$:.unshift File.join(File.dirname(__FILE__))
+
+require "test_helper.rb"
+
+class MachineBehavior < CIMI::Test::Spec
+
+  RESOURCE_URI = "http://schemas.dmtf.org/cimi/1/Machine"
+
+  model :machine, CIMI::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"]
+    get m_url, :accept => fmt
+  end
+
+  it "should have the correct resourceURI", :only => :json do
+    machine.wont_be_nil      # Make sure we talk to the server
+    last_response.json["resourceURI"].must_equal RESOURCE_URI
+  end
+end
-- 
1.7.7.6


Mime
View raw message