class OpenStackParser {

    public List<Disk> parseAttachedStoragesResponse (String response) 
                                                        throws OCCIException {
    	List<Disk> ret = null;
    	Disk disk;
        Link link;
        SAXBuilder builder = new SAXBuilder();
        builder.setValidation(false);
        builder.setIgnoringElementContentWhitespace(true);
        Document xmldoc;
        
        log.info("[OpenStackParser] Parsing attached storages response...");
        try {
            xmldoc = builder.build(new StringReader(response));
            XPath xpath = XPath.newInstance("//volumeAttachment/@volumeId");
            List volumeIdList = xpath.selectNodes(xmldoc);
            
            xpath = XPath.newInstance("//volumeAttachment/@device");
            List deviceList = xpath.selectNodes(xmldoc);
            
            if (volumeIdList.size() == deviceList.size()) {
                ret = new ArrayList<Disk>();
                
                for (int i = 0 ; i < volumeIdList.size() ; i++) {
                	disk = new Disk();
                	link = new Link();
                    link.setHref(((Attribute)volumeIdList.get(i)).getValue());

                    disk.setStorage(link);
                    disk.setTarget(((Attribute)deviceList.get(i)).getValue());
                    
                    ret.add(disk);
                }
            } else {
                OCCIException exception = new OCCIException();
                exception.setMessage(MALFORMEDRESPONSE);
                log.error(MALFORMEDRESPONSE);
                throw exception;
            }
        } catch (JDOMException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        log.debug("[OpenStackParser] Attached storages response parsed...");
        
        return ret;
    }

}

class OpenStackParserTest {

    private static String AUTHORIZATIONRESPONSE;
    private static String NETWORKCOLLECTIONRESPONSE;
    private static String NETWORKCOLLECTIONMALFORMEDRESPONSE;
    private static String NETWORKRESPONSE;
    private static String NETWORKMALFORMEDRESPONSE;
    private static String SUBNETCOLLECTIONRESPONSE;
    private static String SUBNETCOLLECTIONMALFORMEDRESPONSE;
    private static String SUBNETRESPONSE;
    private static String SUBNETMALFORMEDRESPONSE;
    private static String STORAGECOLLECTIONRESPONSE;
    private static String STORAGECOLLECTIONMALFORMEDRESPONSE;
    private static String STORAGERESPONSE;
    private static String IMAGECOLLECTIONRESPONSE;
    private static String IMAGECOLLECTIONMALFORMEDRESPONSE;
    private static String IMAGERESPONSE;
    private static String IMAGEMALFORMEDRESPONSE;
    private static String STORAGEMALFORMEDRESPONSE;
    private static String COMPUTECOLLECTIONRESPONSE;
    private static String COMPUTECOLLECTIONMALFORMEDRESPONSE;
    private static String COMPUTERESPONSE;
    private static String COMPUTEMALFORMEDRESPONSE;
    private static String VOLUMEATTACHMENTRESPONSE;
    private static String VOLUMEATTACHMENTMALFORMEDRESPONSE;
    private static String VIRTUALINTERFACESRESPONSE;
    private static String VIRTUALINTERFACESMALFORMEDRESPONSE;
    private static String INSTANCETYPECOLLECTIONRESPONSE;
    private static String INSTANCETYPECOLLECTIONMALFORMEDRESPONSE;
    private static String INSTANCETYPERESPONSE;
    private static String INSTANCETYPEMALFORMEDRESPONSE;
    private static String COMPUTEQUOTARESPONSE;
    private static String COMPUTEQUOTAMALFORMEDRESPONSE;
    private static String STORAGEQUOTARESPONSE;
    private static String STORAGEQUOTAMALFORMEDRESPONSE;

    @Test
    public void testParseAttachedStoragesResponse() {

        List<Compute.Disk> results;
        OpenStackParser parser = new OpenStackParser();
        try {
            results =
                parser.parseAttachedStoragesResponse(VOLUMEATTACHMENTRESPONSE);
            
            assertEquals(2, results.size());
            assertEquals("a26887c6-c47b-4654-abb5-dfadf7d3f803",
            	results.get(0).getStorage().getHref());
            assertEquals("a26887c6-c47b-4654-abb5-dfadf7d3f804",
            	results.get(1).getStorage().getHref());
            assertEquals("/dev/sdd", results.get(0).getTarget());
            assertEquals("/dev/sdc", results.get(1).getTarget());
            
            results =
                parser.parseAttachedStoragesResponse(
                		VOLUMEATTACHMENTMALFORMEDRESPONSE);
            fail();
        } catch (OCCIException e) {
            assertTrue(true);
        }
    }

}