Search in sources :

Example 1 with XmlUtilException

use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.

the class XmlUnitService method initInputTranslator.

/**
 * Initializes {@link #inputTranslator} by two {@link InputTranslator}
 * <ul>
 * <li>One for handle the <code>url</code> prefix</li>
 * <li>One for handle without prefix</li>
 * </ul>
 */
private void initInputTranslator() {
    inputTranslator = new InputTranslatorManager<Document>();
    // Add handling on the "url" prefix, to get URL input
    inputTranslator.addTranslator(new AInputTranslator<Document>("url") {

        @Override
        public Document translate(String input) throws InputTranslatorException {
            try {
                URL urlInput = new URL(InputTranslatorUtil.getValue(input));
                return XmlUtil.fromURL(urlInput);
            } catch (MalformedURLException e) {
                throw new InputTranslatorException(e);
            } catch (XmlUtilException e) {
                throw new InputTranslatorException(e);
            }
        }
    });
    // Add handling for raw XML input
    inputTranslator.addTranslator(new AInputTranslator<Document>(null) {

        @Override
        public Document translate(String input) throws InputTranslatorException {
            try {
                return XmlUtil.fromString(input);
            } catch (XmlUtilException e) {
                throw new InputTranslatorException(e);
            }
        }
    });
}
Also used : MalformedURLException(java.net.MalformedURLException) InputTranslatorException(org.cerberus.service.xmlunit.InputTranslatorException) XmlUtilException(org.cerberus.util.XmlUtilException) Document(org.w3c.dom.Document) URL(java.net.URL)

Example 2 with XmlUtilException

use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.

the class XmlUnitService method getFromXml.

@Override
public String getFromXml(final String xmlToParse, final String xpath) {
    if (xpath == null) {
        LOG.warn("Null argument");
        return DEFAULT_GET_FROM_XML_VALUE;
    }
    try {
        final Document document = StringUtil.isURL(xmlToParse) ? XmlUtil.fromURL(new URL(xmlToParse)) : XmlUtil.fromString(xmlToParse);
        final String result = XmlUtil.evaluateString(document, xpath);
        // Not that in case of multiple values then send the first one
        return result != null && result.length() > 0 ? result : DEFAULT_GET_FROM_XML_VALUE;
    } catch (XmlUtilException e) {
        LOG.warn("Unable to get from xml", e);
    } catch (MalformedURLException e) {
        LOG.warn("Unable to get from xml", e);
    }
    return DEFAULT_GET_FROM_XML_VALUE;
}
Also used : MalformedURLException(java.net.MalformedURLException) XmlUtilException(org.cerberus.util.XmlUtilException) Document(org.w3c.dom.Document) URL(java.net.URL)

Example 3 with XmlUtilException

use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.

the class XmlUnitService method isElementEquals.

@Override
public boolean isElementEquals(String lastSOAPResponse, String xpath, String expectedElement) {
    if (lastSOAPResponse == null || xpath == null || expectedElement == null) {
        LOG.warn("Null argument");
        return false;
    }
    try {
        NodeList candidates = XmlUtil.evaluate(lastSOAPResponse, xpath);
        LOG.debug(candidates.toString());
        for (Document candidate : XmlUtil.fromNodeList(candidates)) {
            if (Differences.fromString(getDifferencesFromXml(XmlUtil.toString(candidate), expectedElement)).isEmpty()) {
                return true;
            }
        }
    } catch (XmlUtilException xue) {
        LOG.warn("Unable to check if element equality", xue);
    } catch (DifferencesException de) {
        LOG.warn("Unable to check if element equality", de);
    }
    return false;
}
Also used : DifferencesException(org.cerberus.service.xmlunit.DifferencesException) NodeList(org.w3c.dom.NodeList) XmlUtilException(org.cerberus.util.XmlUtilException) Document(org.w3c.dom.Document)

Example 4 with XmlUtilException

use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.

the class Differences method toDocument.

/**
 * Returns a {@link Document} representation of this {@link Differences} by following the {@link Differences} format.
 *
 * @return a {@link Document} representation of this {@link Differences}
 * @throws DifferencesException
 *             if an error occurred
 */
public Document toDocument() throws DifferencesException {
    // Creates the result document which will contain difference list
    Document resultDoc = null;
    try {
        resultDoc = XmlUtil.newDocument();
    } catch (XmlUtilException e) {
        throw new DifferencesException(e);
    }
    // Creates the root node
    Element resultRoot = resultDoc.createElement(DIFFERENCES_NODE);
    resultDoc.appendChild(resultRoot);
    // Appends differences to the root node
    for (Difference diff : differences) {
        Element element = resultDoc.createElement(DIFFERENCE_NODE);
        element.appendChild(resultDoc.createTextNode(diff.getDiff()));
        resultRoot.appendChild(element);
    }
    // Returns the result document
    return resultDoc;
}
Also used : Element(org.w3c.dom.Element) XmlUtilException(org.cerberus.util.XmlUtilException) Document(org.w3c.dom.Document)

Example 5 with XmlUtilException

use of org.cerberus.util.XmlUtilException in project cerberus-source by cerberustesting.

the class Differences method mkString.

/**
 * Returns a {@link String} representation of this {@link Differences} by following the {@link Differences} format.
 *
 * <p>
 * If this {@link Differences} is empty then returned the {@link #EMPTY_DIFFERENCES_STRING} value
 * </p>
 *
 * <p>
 * In case of error, then returned <code>null</code>
 * </p>
 */
public String mkString() {
    try {
        Document doc = toDocument();
        XPath path = XPathFactory.newInstance().newXPath();
        NodeList list = (NodeList) path.compile(DIFFERENCE_COMMON_XPATH).evaluate(doc, XPathConstants.NODESET);
        return list.getLength() == 0 ? EMPTY_DIFFERENCES_STRING : XmlUtil.toString(doc);
    } catch (XmlUtilException e) {
        return null;
    } catch (DifferencesException e) {
        return null;
    } catch (XPathExpressionException e) {
        return null;
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) XmlUtilException(org.cerberus.util.XmlUtilException) Document(org.w3c.dom.Document)

Aggregations

XmlUtilException (org.cerberus.util.XmlUtilException)6 Document (org.w3c.dom.Document)6 NodeList (org.w3c.dom.NodeList)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 AppService (org.cerberus.crud.entity.AppService)1 CountryEnvironmentDatabase (org.cerberus.crud.entity.CountryEnvironmentDatabase)1 Parameter (org.cerberus.crud.entity.Parameter)1 IFactoryAppService (org.cerberus.crud.factory.IFactoryAppService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 CerberusEventException (org.cerberus.exception.CerberusEventException)1 CerberusException (org.cerberus.exception.CerberusException)1