1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

Add XMLUtils tests

This commit is contained in:
Daniel Molina 2012-04-11 16:56:59 +02:00
parent 57308da948
commit f5ec948338
2 changed files with 111 additions and 0 deletions

View File

@ -101,6 +101,8 @@ module OpenNebula
end
end
# Delete an element from the xml
# xpath::_String_ xpath expression that selects the elemnts to be deleted
def delete_element(xpath)
if NOKOGIRI
@xml.xpath(xpath.to_s).remove
@ -109,6 +111,15 @@ module OpenNebula
end
end
# Add a new element to the xml
# xpath::_String_ xpath xpression where the elemente will be added
# elems::_Hash_ Hash containing the pairs key-value to be included
# Examples:
# add_element('VM', 'NEW_ITEM' => 'NEW_VALUE')
# <VM><NEW_ITEM>NEW_VALUE</NEW_ITEM>...</VM>
#
# add_element('VM/TEMPLATE', 'V1' => {'X1' => 'A1', 'Y2' => 'A2'})
# <VM><TEMPLATE><V1><X1>A1</X1><Y2>A2</Y2>...</TEMPLATE></VM>
def add_element(xpath, elems)
elems.each { |key, value|
if value.instance_of?(Hash)

View File

@ -0,0 +1,100 @@
$: << '..'
require 'rexml/document'
require 'OpenNebula/XMLUtils'
shared_examples "modifying XML" do
it "add a new element '.', 'B2' => 'bdos'" do
@xml_element.add_element('.', 'B2' => 'bdos')
@xml_element.to_xml(true).should eql "<V1>
<A1>auno</A1>
<B2>bdos</B2>
</V1>"
end
it "add a new element '.', 'V2' => {'CC3' => 'cctres'}" do
@xml_element.add_element('.', 'V2' => {'CC3' => 'cctres'})
@xml_element.to_xml(true).should eql "<V1>
<A1>auno</A1>
<B2>bdos</B2>
<V2>
<CC3>cctres</CC3>
</V2>
</V1>"
end
it "add a new element 'V2', 'DD4' => 'ddcuatro'" do
@xml_element.add_element('V2', 'DD4' => 'ddcuatro')
@xml_element.to_xml(true).should eql "<V1>
<A1>auno</A1>
<B2>bdos</B2>
<V2>
<CC3>cctres</CC3>
<DD4>ddcuatro</DD4>
</V2>
</V1>"
end
it "delete an element 'B2'" do
@xml_element.delete_element('B2')
@xml_element.to_xml(true).should eql "<V1>
<A1>auno</A1>
<V2>
<CC3>cctres</CC3>
<DD4>ddcuatro</DD4>
</V2>
</V1>"
end
it "delete an element 'V2/DD4'" do
@xml_element.delete_element('V2/DD4')
@xml_element.to_xml(true).should eql "<V1>
<A1>auno</A1>
<V2>
<CC3>cctres</CC3>
</V2>
</V1>"
end
it "delete an element 'V2'" do
@xml_element.delete_element('V2')
@xml_element.to_xml(true).should eql "<V1>
<A1>auno</A1>
</V1>"
end
end
describe 'NOKOGIRI' do
before :all do
xml = "<V1><A1>auno</A1></V1>"
bxml = OpenNebula::XMLElement.build_xml(xml, "V1")
bxml.class.to_s.should eql('Nokogiri::XML::NodeSet')
@xml_element = OpenNebula::XMLElement.new(bxml)
end
it_behaves_like "modifying XML"
end
describe 'REXML' do
before :all do
OpenNebula::NOKOGIRI = false
xml = "<V1><A1>auno</A1></V1>"
bxml = OpenNebula::XMLElement.build_xml(xml, "V1")
bxml.class.to_s.should eql('REXML::Element')
@xml_element = OpenNebula::XMLElement.new(bxml)
end
it_behaves_like "modifying XML"
end