def test_xml_to_dict(self): test_xml = \ """<VmHost> <id>VH1</id> <name>host1</name> <note>some other note</note> <processorCoresCount>6</processorCoresCount> <properties> <name>prop1</name> <value>value1</value> </properties> <properties> <name>prop2</name> <value>value2</value> </properties> <os> <osType>LINUX</osType> <osSubType>UBUNTU</osSubType> <osVersion>11.0</osVersion> </os> <model>model1</model> <virtualizationType>KVM</virtualizationType> </VmHost>""" expected_host_json = \ '{"virtualizationType": "KVM", "name": "host1", \ "processorCoresCount": "6", "id": "VH1", "note": "some other note", \ "model": "model1", "os": {"osType": "LINUX", "osSubType": "UBUNTU", \ "osVersion": "11.0"}, "properties": [{"name": "prop1", "value": "value1"}, \ {"name": "prop2", "value": "value2"}]}' test_dict = util.xml_to_dict(test_xml) json_str = json.dumps(test_dict) print json_str self.assertEquals(json_str, expected_host_json)
def test_xml_to_dict_with_collections(self): input_xml = '<Vm><outer><a><storage><id>33</id>\ <name>ESA</name></storage></a></outer>\ <storagevolume xmlns:atom="http://www.w3.org/2005/Atom" id="88">\ <atom:link href="http://localhost/v2.0/storage/88" rel="self"/>\ <atom:link href="http://localhost/storage/88" rel="bookmark"/>\ </storagevolume>\ <storagevolume xmlns:atom="http://www.w3.org/2005/Atom" id="89">\ <atom:link href="http://localhost/v2.0/storage/89" rel="self"/>\ <atom:link href="http://localhost/storage/89" rel="bookmark"/>\ </storagevolume>\ <person xmlns:atom="http://www.w3.org/2005/Atom" \ name="testname" type="human" id="89">\ <atom:link href="http://localhost/v2.0/people/89" \ rel="self"/><atom:link href="http://localhost/people/89" rel="bookmark"/>\ </person></Vm>' expected_json = '{"person": {"links": [{"href": \ "http://localhost/v2.0/people/89", "rel": "self"}, {"href": \ "http://localhost/people/89", "rel": "bookmark"}]}, \ "outer": {"a": {"storage": {"id": "33", "name": "ESA"}}}, \ "storagevolumes": [{"id": "88", "links": [{"href": \ "http://localhost/v2.0/storage/88", "rel": "self"}, {"href": \ "http://localhost/storage/88", "rel": "bookmark"}]}, \ {"id": "89", "links": [{"href": "http://localhost/v2.0/storage/89", \ "rel": "self"}, {"href": "http://localhost/storage/89", \ "rel": "bookmark"}]}]}' self.assertEquals( json.dumps(util.xml_to_dict(input_xml)), expected_json)
def test_xml_to_dict_with_collections(self): input_xml = '<Vm><outer><a><storage><id>33</id><name>ESA</name></storage></a></outer>\ <storagevolume xmlns:atom="http://www.w3.org/2005/Atom" id="88">\ <atom:link href="http://localhost/v2.0/storage/88" rel="self"/><atom:link href="http://localhost/storage/88" rel="bookmark"/>\ </storagevolume>\ <storagevolume xmlns:atom="http://www.w3.org/2005/Atom" id="89">\ <atom:link href="http://localhost/v2.0/storage/89" rel="self"/><atom:link href="http://localhost/storage/89" rel="bookmark"/>\ </storagevolume>\ <person xmlns:atom="http://www.w3.org/2005/Atom" name="testname" type="human" id="89">\ <atom:link href="http://localhost/v2.0/people/89" rel="self"/><atom:link href="http://localhost/people/89" rel="bookmark"/>\ </person></Vm>' expected_json = '{"person": {"links": [{"href": "http://localhost/v2.0/people/89", "rel": "self"}, {"href": "http://localhost/people/89", "rel": "bookmark"}]}, "outer": {"a": {"storage": {"id": "33", "name": "ESA"}}}, "storagevolumes": [{"id": "88", "links": [{"href": "http://localhost/v2.0/storage/88", "rel": "self"}, {"href": "http://localhost/storage/88", "rel": "bookmark"}]}, {"id": "89", "links": [{"href": "http://localhost/v2.0/storage/89", "rel": "self"}, {"href": "http://localhost/storage/89", "rel": "bookmark"}]}]}' self.assertEquals(json.dumps(util.xml_to_dict(input_xml)), expected_json)
def test_xml_with_no_children_to_dict(self): xml_str = '<tag></tag>' test_dict = util.xml_to_dict(xml_str) self.assertEquals(test_dict, None)