From: Michal Fojtik <mfoj...@redhat.com>
Signed-off-by: Michal fojtik <mfoj...@redhat.com> --- server/lib/cimi/model/machine_image.rb | 23 +++++++++ server/spec/cimi/data/machine_image.json | 13 +++++ server/spec/cimi/data/machine_image.xml | 11 ++++ server/spec/cimi/model/machine_image_spec.rb | 66 ++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 0 deletions(-) create mode 100644 server/lib/cimi/model/machine_image.rb create mode 100644 server/spec/cimi/data/machine_image.json create mode 100644 server/spec/cimi/data/machine_image.xml create mode 100644 server/spec/cimi/model/machine_image_spec.rb diff --git a/server/lib/cimi/model/machine_image.rb b/server/lib/cimi/model/machine_image.rb new file mode 100644 index 0000000..dd3c960 --- /dev/null +++ b/server/lib/cimi/model/machine_image.rb @@ -0,0 +1,23 @@ +# 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. + +class CIMI::Model::MachineImage < CIMI::Model::Base + + scalar :image_location + + array :operations do + scalar :rel, :href + end +end diff --git a/server/spec/cimi/data/machine_image.json b/server/spec/cimi/data/machine_image.json new file mode 100644 index 0000000..e037144 --- /dev/null +++ b/server/spec/cimi/data/machine_image.json @@ -0,0 +1,13 @@ +{ + "uri": "http://cimi.example.org/machine_images/1", + "name": "My First image", + "description": "A image for testing", + "created": "2011-11-14", + "imageLocation": { "href": "nfs://cimi.example.com/images/1.img"} + "properties": [ {"name": "status", "value": "build"}, { "name": "locked", "value": "true" } ] + "operations": [ + { "rel": "edit", + "href": "http://cimi.example.org/machine_images/1/edit" }, + { "rel": "delete", + "href": "http://cimi.example.org/machine_images/1/delete" }] +} diff --git a/server/spec/cimi/data/machine_image.xml b/server/spec/cimi/data/machine_image.xml new file mode 100644 index 0000000..a02bc36 --- /dev/null +++ b/server/spec/cimi/data/machine_image.xml @@ -0,0 +1,11 @@ +<MachineImage xmlns="http://www.dmtf.org/cimi"> + <uri>http://cimi.example.org/machine_image/1</uri> + <name>img1</name> + <description>Machine Image One</description> + <created>2011-11-14</created> + <property name="status">BUILD</property> + <property name="locked">true</property> + <imageLocation href="nfs://cimi.example.com/images/1.img"/> + <operation rel="edit" href="http://cimi.example.org/machine_image/1/edit"/> + <operation rel="delete" href="http://cimi.example.org/machine_image/1/delete"/> +</MachineImage> diff --git a/server/spec/cimi/model/machine_image_spec.rb b/server/spec/cimi/model/machine_image_spec.rb new file mode 100644 index 0000000..6336660 --- /dev/null +++ b/server/spec/cimi/model/machine_image_spec.rb @@ -0,0 +1,66 @@ +# 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. +# + +describe "MachineImage model" do + + before(:all) do + @xml = IO::read(File::join(DATA_DIR, "machine_image.xml")) + @json = IO::read(File::join(DATA_DIR, "machine_template.json")) + end + + it "can be constructed from XML" do + img = CIMI::Model::MachineImage.from_xml(@xml) + img.should_not be_nil + img.created.should == "2011-11-14" + img.name.should == "img1" + img.description.should == "Machine Image One" + img.uri.should == "http://cimi.example.org/machine_image/1" + img.image_location.size == 1 + img.image_location[0]['href'].should == 'nfs://cimi.example.com/images/1.img' + img.operations.any? { |operation| operation.rel == 'edit' }.should be_true + img.operations.any? { |operation| operation.rel == 'delete' }.should be_true + img.operations.each { |operation| operation.href.should =~ /^http:\/\/.*\/(#{operation.rel})$/ } + img.should serialize_to @xml, :fmt => :xml + end + + it "should parse properties correctly in XML" do + img = CIMI::Model::MachineImage.from_xml(@xml) + img.property.any? { |p| p.name == 'status' }.should be_true + img.property.any? { |p| p.name == 'locked' }.should be_true + img.property.size.should == 2 + end + + it "should convert strings in keys to symbols when contructed from XML" do + imgl = CIMI::Model::MachineImage.from_xml(@xml) + imgl.should_not be_nil + imgl.attribute_values.keys.each { |key| key.should be_a(Symbol) } + end + + it "can be constructed from JSON" do + templ = CIMI::Model::MachineTemplate.from_json(@json) + templ.should_not be_nil + templ.created.should == "2011-11-01" + templ.should serialize_to @json, :fmt => :json + end + + it "should parse properties correctly in JSON" do + img = CIMI::Model::MachineImage.from_json(@json) + # TODO: Fix this + # img.property.any? { |p| p.name == 'status' }.should be_true + # img.property.any? { |p| p.name == 'locked' }.should be_true + # img.property.size.should == 2 + end +end -- 1.7.4.4