Skip to content

Commit

Permalink
Corrected issue with filling a collection of complex types
Browse files Browse the repository at this point in the history
  • Loading branch information
visoft committed Jun 16, 2013
1 parent 76a57b1 commit 1a3b8b9
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/ruby_odata/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def extract_partial(doc)
next_links = doc.xpath('//atom:link[@rel="next"]', @ds_namespaces)
@has_partial = next_links.any?
if @has_partial
uri = Addressable::URI.parse(next_links[0]['href'])
uri = Addressable::URI.parse(next_links[0]['href'])
uri.query_values = uri.query_values.merge @additional_params unless @additional_params.empty?
@next_uri = uri.to_s
end
Expand Down Expand Up @@ -638,21 +638,39 @@ def build_batch_operation(operation, changeset_num)
def complex_type_to_class(complex_type_xml)
type = Helpers.get_namespaced_attribute(complex_type_xml, 'type', 'm')

is_collection = false
# Extract the class name in case this is a Collection
if type =~ /\(([^)]*)\)/m
type = $~[1]
is_collection = true
collection = []
end

klass_name = qualify_class_name(type.split('.')[-1])
klass = @classes[klass_name].new

# Fill in the properties
if is_collection
# extract the elements from the collection
elements = complex_type_xml.xpath(".//d:element", @namespaces)
elements.each do |e|
element = @classes[klass_name].new
fill_complex_type_properties(e, element)
collection << element
end
return collection
else
klass = @classes[klass_name].new
# Fill in the properties
fill_complex_type_properties(complex_type_xml, klass)
return klass
end
end

# Helper method for complex_type_to_class
def fill_complex_type_properties(complex_type_xml, klass)
properties = complex_type_xml.xpath(".//*")
properties.each do |prop|
klass.send "#{prop.name}=", parse_value(prop)
end

return klass
end

# Field Converters
Expand Down
61 changes: 61 additions & 0 deletions spec/fixtures/ms_system_center/hardware_profiles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xml:base="https://srv-spf-98.bs.unsi.no:8090/SC2012/VMM/Microsoft.Management.Odata.svc/">
<id>https://srv-spf-98.bs.unsi.no:8090/SC2012/VMM/Microsoft.Management.Odata.svc/HardwareProfiles</id>
<title type="text">HardwareProfiles</title>
<updated>2013-06-16T10:01:46Z</updated>
<link rel="self" title="HardwareProfiles" href="HardwareProfiles"/>
<entry>
<id>https://srv-spf-98.bs.unsi.no:8090/SC2012/VMM/Microsoft.Management.Odata.svc/HardwareProfiles(ID=guid'8967da8e-eaf0-4414-b1ab-0b8d7a848af7',StampId=guid'a68f9b8b-f8af-46b2-8636-255248c9b5ab')</id>
<category term="VMM.HardwareProfile" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<link rel="edit" title="HardwareProfile" href="HardwareProfiles(ID=guid'8967da8e-eaf0-4414-b1ab-0b8d7a848af7',StampId=guid'a68f9b8b-f8af-46b2-8636-255248c9b5ab')"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VirtualNetworkAdapters" type="application/atom+xml;type=feed" title="VirtualNetworkAdapters" href="HardwareProfiles(ID=guid'8967da8e-eaf0-4414-b1ab-0b8d7a848af7',StampId=guid'a68f9b8b-f8af-46b2-8636-255248c9b5ab')/VirtualNetworkAdapters"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VirtualSCSIAdapters" type="application/atom+xml;type=feed" title="VirtualSCSIAdapters" href="HardwareProfiles(ID=guid'8967da8e-eaf0-4414-b1ab-0b8d7a848af7',StampId=guid'a68f9b8b-f8af-46b2-8636-255248c9b5ab')/VirtualSCSIAdapters"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VirtualDVDDrives" type="application/atom+xml;type=feed" title="VirtualDVDDrives" href="HardwareProfiles(ID=guid'8967da8e-eaf0-4414-b1ab-0b8d7a848af7',StampId=guid'a68f9b8b-f8af-46b2-8636-255248c9b5ab')/VirtualDVDDrives"/>
<title/>
<updated>2013-06-16T10:01:46Z</updated>
<author>
<name/>
</author>
<content type="application/xml">
<m:properties>
<d:StampId m:type="Edm.Guid">a68f9b8b-f8af-46b2-8636-255248c9b5ab</d:StampId>
<d:ID m:type="Edm.Guid">8967da8e-eaf0-4414-b1ab-0b8d7a848af7</d:ID>
<d:AccessedTime m:null="true"/>
<d:AddedTime m:type="Edm.DateTime">2013-06-04T17:28:29.597+02:00</d:AddedTime>
<d:CPUCount m:type="Edm.Byte">2</d:CPUCount>
<d:CPUMax m:type="Edm.Int32">100</d:CPUMax>
<d:CPUReserve m:type="Edm.Int32">0</d:CPUReserve>
<d:CPUTypeId m:null="true"/>
<d:CreationTime m:null="true"/>
<d:DiskIO m:type="Edm.Int32">0</d:DiskIO>
<d:ExpectedCPUUtilization m:type="Edm.Int32">20</d:ExpectedCPUUtilization>
<d:Enabled m:type="Edm.Boolean">true</d:Enabled>
<d:LimitCPUForMigration m:type="Edm.Boolean">false</d:LimitCPUForMigration>
<d:LimitCPUFunctionality m:type="Edm.Boolean">false</d:LimitCPUFunctionality>
<d:Memory m:type="Edm.Int32">3500</d:Memory>
<d:ModifiedTime m:type="Edm.DateTime">2013-06-04T17:41:30.287+02:00</d:ModifiedTime>
<d:Name>[Hardware] - Linux Medium VM</d:Name>
<d:NetworkUtilization m:type="Edm.Int32">0</d:NetworkUtilization>
<d:Owner m:type="VMM.UserAndRole">
<d:UserName>foo\bar</d:UserName>
<d:RoleName>Administrator</d:RoleName>
<d:RoleID m:type="Edm.Guid">75700cd5-893e-4f68-ada7-50ef4668acc6</d:RoleID>
</d:Owner>
<d:GrantedToList m:type="Collection(VMM.UserAndRole)">
<d:element>
<d:UserName m:null="true"/>
<d:RoleName>Important Tenant</d:RoleName>
<d:RoleID m:type="Edm.Guid">ee41a0fc-fa48-4ed6-8403-f42a45529003</d:RoleID>
</d:element>
</d:GrantedToList>
<d:RelativeWeight m:type="Edm.Int32">100</d:RelativeWeight>
<d:ShareSCSIBus m:type="Edm.Boolean">false</d:ShareSCSIBus>
<d:TotalVHDCapacity m:null="true"/>
<d:UndoDisksEnabled m:type="Edm.Boolean">false</d:UndoDisksEnabled>
<d:Accessibility>Public</d:Accessibility>
<d:Description/>
<d:NumLockEnabled m:type="Edm.Boolean">false</d:NumLockEnabled>
</m:properties>
</content>
</entry>
</feed>
21 changes: 21 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@ module OData
stub_request(:get, "http://blabla:@test.com/test.svc/VirtualMachines").
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).
to_return(:status => 200, :body => File.new(File.expand_path("../fixtures/ms_system_center/virtual_machines.xml", __FILE__)), :headers => {})

stub_request(:get, "http://blabla:@test.com/test.svc/HardwareProfiles?$filter=Memory%20eq%203500").
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).
to_return(:status => 200, :body => File.new(File.expand_path("../fixtures/ms_system_center/hardware_profiles.xml", __FILE__)), :headers => {})
end

it "should successfully return a virtual machine" do
Expand All @@ -780,6 +784,23 @@ module OData
results = svc.execute
results.first.should be_a_kind_of(VMM::VirtualMachine)
end

it "should successfully return a hardware profile for results that include a collection of complex types" do
svc = OData::Service.new "http://test.com/test.svc/", { :username => "blabla", :password=> "", :verify_ssl => false, :namespace => "VMM" }
svc.HardwareProfiles.filter("Memory eq 3500")
results = svc.execute
results.first.should be_a_kind_of(VMM::HardwareProfile)
end
it "should successfully return a collection of complex types" do
svc = OData::Service.new "http://test.com/test.svc/", { :username => "blabla", :password=> "", :verify_ssl => false, :namespace => "VMM" }
svc.HardwareProfiles.filter("Memory eq 3500")
results = svc.execute
granted_list = results.first.GrantedToList
granted_list.should be_a_kind_of(Array)
granted_list.first.should be_a_kind_of(VMM::UserAndRole)
granted_list.first.RoleName.should == "Important Tenant"

end
end
end

Expand Down

0 comments on commit 1a3b8b9

Please sign in to comment.