def test_show_image_no_metadata_key(self): serializer = images.ImageTemplate() fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, 'rel': 'self', }, { 'href': self.SERVER_BOOKMARK, 'rel': 'bookmark', }, ], }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } output = serializer.serialize(fixture) root = etree.XML(output) xmlutil.validate_schema(root, 'image') image_dict = fixture['image'] for key in ['name', 'id', 'updated', 'created', 'status']: self.assertEqual(root.get(key), str(image_dict[key])) link_nodes = root.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value) meta_nodes = root.findall('{0}meta'.format(ATOMNS)) self.assertEqual(len(meta_nodes), 0) server_root = root.find('{0}server'.format(NS)) self.assertEqual(server_root.get('id'), image_dict['server']['id']) link_nodes = server_root.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['server']['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value)
def test_show_no_server(self): serializer = images.ImageTemplate() fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'metadata': { 'key1': 'value1', }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } output = serializer.serialize(fixture) root = etree.XML(output) xmlutil.validate_schema(root, 'image') image_dict = fixture['image'] for key in ['name', 'id', 'updated', 'created', 'status']: self.assertEqual(root.get(key), str(image_dict[key])) link_nodes = root.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value) metadata_root = root.find('{0}metadata'.format(NS)) metadata_elems = metadata_root.findall('{0}meta'.format(NS)) self.assertEqual(len(metadata_elems), 1) for i, metadata_elem in enumerate(metadata_elems): (meta_key, meta_value) = image_dict['metadata'].items()[i] self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) server_root = root.find('{0}server'.format(NS)) self.assertEqual(server_root, None)
def test_xml_declaration(self): serializer = images.ImageTemplate() fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'progress': 80, 'server': { 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, 'rel': 'self', }, { 'href': self.SERVER_BOOKMARK, 'rel': 'bookmark', }, ], }, 'metadata': { 'key1': 'value1', }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } output = serializer.serialize(fixture) has_dec = output.startswith("<?xml version='1.0' encoding='UTF-8'?>") self.assertTrue(has_dec)
def setUp(self): super(ImageXMLSerializationTest, self).setUp() self.fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'progress': 80, 'server': { 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, 'rel': 'self', }, { 'href': self.SERVER_BOOKMARK, 'rel': 'bookmark', }, ], }, 'metadata': { 'key1': 'value1', }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } image = self.fixture['image'] image['id'] = '2' image['name'] = 'Image2' image['status'] = 'SAVING' image['links'][0]['href'] = self.IMAGE_HREF % 2 image['links'][1]['href'] = self.IMAGE_BOOKMARK % 2 self.fixture_dict = { 'images': [ self.fixture['image'], image, ], 'images_links': [{ 'rel': 'next', 'href': self.IMAGE_NEXT % (2, 2), }], } self.serializer = images.ImageTemplate()