return param_str.rstrip('&')
-class MetadataDeserializer(wsgi.MetadataXMLDeserializer):
- def deserialize(self, text):
- dom = minidom.parseString(text)
- metadata_node = self.find_first_child_named(dom, "metadata")
- metadata = self.extract_metadata(metadata_node)
- return {'body': {'metadata': metadata}}
-
-
-class MetaItemDeserializer(wsgi.MetadataXMLDeserializer):
- def deserialize(self, text):
- dom = minidom.parseString(text)
- metadata_item = self.extract_metadata(dom)
- return {'body': {'meta': metadata_item}}
-
-
-class MetadataXMLDeserializer(wsgi.XMLDeserializer):
-
- def extract_metadata(self, metadata_node):
- """Marshal the metadata attribute of a parsed request"""
- if metadata_node is None:
- return {}
- metadata = {}
- for meta_node in self.find_children_named(metadata_node, "meta"):
- key = meta_node.getAttribute("key")
- metadata[key] = self.extract_text(meta_node)
- return metadata
-
- def _extract_metadata_container(self, datastring):
- dom = minidom.parseString(datastring)
- metadata_node = self.find_first_child_named(dom, "metadata")
- metadata = self.extract_metadata(metadata_node)
- return {'body': {'metadata': metadata}}
-
- def create(self, datastring):
- return self._extract_metadata_container(datastring)
-
- def update_all(self, datastring):
- return self._extract_metadata_container(datastring)
-
- def update(self, datastring):
- dom = minidom.parseString(datastring)
- metadata_item = self.extract_metadata(dom)
- return {'body': {'meta': metadata_item}}
-
-
-metadata_nsmap = {None: xmlutil.XMLNS_V11}
-
-
-class MetaItemTemplate(xmlutil.TemplateBuilder):
- def construct(self):
- sel = xmlutil.Selector('meta', xmlutil.get_items, 0)
- root = xmlutil.TemplateElement('meta', selector=sel)
- root.set('key', 0)
- root.text = 1
- return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap)
-
-
-class MetadataTemplateElement(xmlutil.TemplateElement):
- def will_render(self, datum):
- return True
-
-
-class MetadataTemplate(xmlutil.TemplateBuilder):
- def construct(self):
- root = MetadataTemplateElement('metadata', selector='metadata')
- elem = xmlutil.SubTemplateElement(root, 'meta',
- selector=xmlutil.get_items)
- elem.set('key', 0)
- elem.text = 1
- return xmlutil.MasterTemplate(root, 1, nsmap=metadata_nsmap)
-
-
def check_snapshots_enabled(f):
@functools.wraps(f)
def inner(*args, **kwargs):
expected = '2'
actual = common.get_version_from_href(fixture)
self.assertEqual(actual, expected)
-
-
-class MetadataXMLDeserializationTest(test.TestCase):
-
- deserializer = common.MetadataXMLDeserializer()
-
- def test_create(self):
- request_body = """
- <metadata xmlns="http://docs.openstack.org/compute/api/v1.1">
- <meta key='123'>asdf</meta>
- <meta key='567'>jkl;</meta>
- </metadata>"""
- output = self.deserializer.deserialize(request_body, 'create')
- expected = {"body": {"metadata": {"123": "asdf", "567": "jkl;"}}}
- self.assertEquals(output, expected)
-
- def test_create_empty(self):
- request_body = """
- <metadata xmlns="http://docs.openstack.org/compute/api/v1.1"/>"""
- output = self.deserializer.deserialize(request_body, 'create')
- expected = {"body": {"metadata": {}}}
- self.assertEquals(output, expected)
-
- def test_update_all(self):
- request_body = """
- <metadata xmlns="http://docs.openstack.org/compute/api/v1.1">
- <meta key='123'>asdf</meta>
- <meta key='567'>jkl;</meta>
- </metadata>"""
- output = self.deserializer.deserialize(request_body, 'update_all')
- expected = {"body": {"metadata": {"123": "asdf", "567": "jkl;"}}}
- self.assertEquals(output, expected)
-
- def test_update(self):
- request_body = """
- <meta xmlns="http://docs.openstack.org/compute/api/v1.1"
- key='123'>asdf</meta>"""
- output = self.deserializer.deserialize(request_body, 'update')
- expected = {"body": {"meta": {"123": "asdf"}}}
- self.assertEquals(output, expected)
-
-
-class MetadataXMLSerializationTest(test.TestCase):
-
- def test_xml_declaration(self):
- serializer = common.MetadataTemplate()
- fixture = {
- 'metadata': {
- 'one': 'two',
- 'three': 'four',
- },
- }
-
- output = serializer.serialize(fixture)
- print output
- has_dec = output.startswith("<?xml version='1.0' encoding='UTF-8'?>")
- self.assertTrue(has_dec)
-
- def test_index(self):
- serializer = common.MetadataTemplate()
- fixture = {
- 'metadata': {
- 'one': 'two',
- 'three': 'four',
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- xmlutil.validate_schema(root, 'metadata')
- metadata_dict = fixture['metadata']
- metadata_elems = root.findall('{0}meta'.format(NS))
- self.assertEqual(len(metadata_elems), 2)
- for i, metadata_elem in enumerate(metadata_elems):
- (meta_key, meta_value) = metadata_dict.items()[i]
- self.assertEqual(str(metadata_elem.get('key')), str(meta_key))
- self.assertEqual(str(metadata_elem.text).strip(), str(meta_value))
-
- def test_index_null(self):
- serializer = common.MetadataTemplate()
- fixture = {
- 'metadata': {
- None: None,
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- xmlutil.validate_schema(root, 'metadata')
- metadata_dict = fixture['metadata']
- metadata_elems = root.findall('{0}meta'.format(NS))
- self.assertEqual(len(metadata_elems), 1)
- for i, metadata_elem in enumerate(metadata_elems):
- (meta_key, meta_value) = metadata_dict.items()[i]
- self.assertEqual(str(metadata_elem.get('key')), str(meta_key))
- self.assertEqual(str(metadata_elem.text).strip(), str(meta_value))
-
- def test_index_unicode(self):
- serializer = common.MetadataTemplate()
- fixture = {
- 'metadata': {
- u'three': u'Jos\xe9',
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- xmlutil.validate_schema(root, 'metadata')
- metadata_dict = fixture['metadata']
- metadata_elems = root.findall('{0}meta'.format(NS))
- self.assertEqual(len(metadata_elems), 1)
- for i, metadata_elem in enumerate(metadata_elems):
- (meta_key, meta_value) = metadata_dict.items()[i]
- self.assertEqual(str(metadata_elem.get('key')), str(meta_key))
- self.assertEqual(metadata_elem.text.strip(), meta_value)
-
- def test_show(self):
- serializer = common.MetaItemTemplate()
- fixture = {
- 'meta': {
- 'one': 'two',
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- meta_dict = fixture['meta']
- (meta_key, meta_value) = meta_dict.items()[0]
- self.assertEqual(str(root.get('key')), str(meta_key))
- self.assertEqual(root.text.strip(), meta_value)
-
- def test_update_all(self):
- serializer = common.MetadataTemplate()
- fixture = {
- 'metadata': {
- 'key6': 'value6',
- 'key4': 'value4',
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- xmlutil.validate_schema(root, 'metadata')
- metadata_dict = fixture['metadata']
- metadata_elems = root.findall('{0}meta'.format(NS))
- self.assertEqual(len(metadata_elems), 2)
- for i, metadata_elem in enumerate(metadata_elems):
- (meta_key, meta_value) = metadata_dict.items()[i]
- self.assertEqual(str(metadata_elem.get('key')), str(meta_key))
- self.assertEqual(str(metadata_elem.text).strip(), str(meta_value))
-
- def test_update_item(self):
- serializer = common.MetaItemTemplate()
- fixture = {
- 'meta': {
- 'one': 'two',
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- meta_dict = fixture['meta']
- (meta_key, meta_value) = meta_dict.items()[0]
- self.assertEqual(str(root.get('key')), str(meta_key))
- self.assertEqual(root.text.strip(), meta_value)
-
- def test_create(self):
- serializer = common.MetadataTemplate()
- fixture = {
- 'metadata': {
- 'key9': 'value9',
- 'key2': 'value2',
- 'key1': 'value1',
- },
- }
- output = serializer.serialize(fixture)
- print output
- root = etree.XML(output)
- xmlutil.validate_schema(root, 'metadata')
- metadata_dict = fixture['metadata']
- metadata_elems = root.findall('{0}meta'.format(NS))
- self.assertEqual(len(metadata_elems), 3)
- for i, metadata_elem in enumerate(metadata_elems):
- (meta_key, meta_value) = metadata_dict.items()[i]
- self.assertEqual(str(metadata_elem.get('key')), str(meta_key))
- self.assertEqual(str(metadata_elem.text).strip(), str(meta_value))
- actual = minidom.parseString(output.replace(" ", ""))
-
- expected = minidom.parseString("""
- <metadata xmlns="http://docs.openstack.org/compute/api/v1.1">
- <meta key="key2">value2</meta>
- <meta key="key9">value9</meta>
- <meta key="key1">value1</meta>
- </metadata>
- """.replace(" ", "").replace("\n", ""))
-
- self.assertEqual(expected.toxml(), actual.toxml())