Search in sources :

Example 46 with Closure

use of groovy.lang.Closure in project mdw-designer by CenturyLinkCloud.

the class GroovyTestCaseScript method xpath.

/**
 * Matches according to MDW XPath.
 */
public Closure<Boolean> xpath(final String condition) throws TestException {
    return new Closure<Boolean>(this, this) {

        @Override
        public Boolean call(Object request) {
            try {
                XmlPath xpath = new XmlPath(condition);
                String v = xpath.evaluate(TestDataFilter.parseRequest(request.toString()));
                return v != null;
            } catch (MbengException ex) {
                getTestCaseRun().log.println("Failed to parse request as XML/JSON. Stub response: " + AdapterActivity.MAKE_ACTUAL_CALL);
                return false;
            }
        }
    };
}
Also used : XmlPath(com.qwest.mbeng.XmlPath) MbengException(com.qwest.mbeng.MbengException) Closure(groovy.lang.Closure) JSONObject(org.json.JSONObject)

Example 47 with Closure

use of groovy.lang.Closure in project mdw-designer by CenturyLinkCloud.

the class GroovyTestCaseScript method gpath.

/**
 * Matches according to GPath.
 */
public Closure<Boolean> gpath(final String condition) throws TestException {
    return new Closure<Boolean>(this, this) {

        @Override
        public Boolean call(Object request) {
            try {
                GPathResult gpathRequest = new XmlSlurper().parseText(request.toString());
                Binding binding = getBinding();
                binding.setVariable("request", gpathRequest);
                return (Boolean) new GroovyShell(binding).evaluate(condition);
            } catch (Exception ex) {
                getTestCaseRun().log.println("Failed to parse request as XML/JSON. Stub response: " + AdapterActivity.MAKE_ACTUAL_CALL);
                return false;
            }
        }
    };
}
Also used : Binding(groovy.lang.Binding) Closure(groovy.lang.Closure) XmlSlurper(groovy.util.XmlSlurper) JSONObject(org.json.JSONObject) GPathResult(groovy.util.slurpersupport.GPathResult) GroovyShell(groovy.lang.GroovyShell) IOException(java.io.IOException) MbengException(com.qwest.mbeng.MbengException)

Example 48 with Closure

use of groovy.lang.Closure in project rest-assured by rest-assured.

the class EncoderRegistry method encodeXML.

/**
 * Encode the content as XML.  The argument may be either an object whose
 * <code>toString</code> produces valid markup, or a Closure which will be
 * interpreted as a builder definition.
 *
 * @param xml data that defines the XML structure
 * @return an {@link HttpEntity} encapsulating this request data
 * @throws UnsupportedEncodingException
 */
public HttpEntity encodeXML(Object contentType, Object xml) throws UnsupportedEncodingException {
    String contentTypeAsString = contentTypeToString(contentType);
    if (xml instanceof Closure) {
        StreamingMarkupBuilder smb = new StreamingMarkupBuilder();
        xml = smb.bind(xml);
    } else if (xml instanceof File) {
        xml = toString((File) xml, contentTypeAsString);
    }
    return createEntity(contentTypeAsString, xml);
}
Also used : StreamingMarkupBuilder(groovy.xml.StreamingMarkupBuilder) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) FileReader.readToString(io.restassured.internal.support.FileReader.readToString) GString(groovy.lang.GString)

Example 49 with Closure

use of groovy.lang.Closure in project rest-assured by rest-assured.

the class EncoderRegistry method encodeText.

/**
 * Default handler used for a plain text content-type.  Acceptable argument
 * types are:
 * <ul>
 * <li>Closure</li>
 * <li>Writable</li>
 * <li>Reader</li>
 * </ul>
 * For Closure argument, a {@link PrintWriter} is passed as the single
 * argument to the closure.  Any data sent to the writer from the
 * closure will be sent to the request content body.
 *
 * @param data
 * @return an {@link HttpEntity} encapsulating this request data
 * @throws IOException
 */
public HttpEntity encodeText(Object contentType, Object data) throws IOException {
    String contentTypeAsString = contentTypeToString(contentType);
    if (data instanceof Closure) {
        StringWriter out = new StringWriter();
        PrintWriter writer = new PrintWriter(out);
        ((Closure) data).call(writer);
        writer.close();
        out.flush();
        data = out;
    } else if (data instanceof Writable) {
        StringWriter out = new StringWriter();
        ((Writable) data).writeTo(out);
        out.flush();
        data = out;
    } else if (data instanceof Reader && !(data instanceof BufferedReader)) {
        data = new BufferedReader((Reader) data);
    } else if (data instanceof File) {
        data = toString((File) data, contentTypeAsString);
    }
    if (data instanceof BufferedReader) {
        StringWriter out = new StringWriter();
        DefaultGroovyMethods.leftShift(out, (BufferedReader) data);
        data = out;
    }
    // if data is a String, we are already covered.
    return createEntity(contentTypeAsString, data);
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) Writable(groovy.lang.Writable) FileReader.readToString(io.restassured.internal.support.FileReader.readToString) GString(groovy.lang.GString)

Example 50 with Closure

use of groovy.lang.Closure in project rest-assured by rest-assured.

the class EncoderRegistry method buildDefaultEncoderMap.

/**
 * Returns a map of default encoders.  Override this method to change
 * what encoders are registered by default.  You can of course call
 * <code>super.buildDefaultEncoderMap()</code> and then add or remove
 * from that result as well.
 */
protected Map<String, Closure> buildDefaultEncoderMap() {
    Map<String, Closure> encoders = new HashMap<String, Closure>();
    encoders.put(ContentType.BINARY.toString(), new MethodClosure(this, "encodeStream"));
    encoders.put(ContentType.TEXT.toString(), new MethodClosure(this, "encodeText"));
    encoders.put(ContentType.URLENC.toString(), new MethodClosure(this, "encodeForm"));
    Closure encClosure = new MethodClosure(this, "encodeXML");
    for (String ct : ContentType.XML.getContentTypeStrings()) encoders.put(ct, encClosure);
    encoders.put(ContentType.HTML.toString(), encClosure);
    encClosure = new MethodClosure(this, "encodeJSON");
    for (String ct : ContentType.JSON.getContentTypeStrings()) encoders.put(ct, encClosure);
    return encoders;
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) FileReader.readToString(io.restassured.internal.support.FileReader.readToString) GString(groovy.lang.GString) MethodClosure(org.codehaus.groovy.runtime.MethodClosure)

Aggregations

Closure (groovy.lang.Closure)251 Map (java.util.Map)55 HashMap (java.util.HashMap)38 ArrayList (java.util.ArrayList)37 GroovyObject (groovy.lang.GroovyObject)33 List (java.util.List)33 Binding (groovy.lang.Binding)22 GString (groovy.lang.GString)21 LinkedHashMap (java.util.LinkedHashMap)20 LinkedList (java.util.LinkedList)19 Collection (java.util.Collection)17 GroovyShell (groovy.lang.GroovyShell)14 Test (org.junit.Test)14 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)13 FileType (groovy.io.FileType)12 FileVisitResult (groovy.io.FileVisitResult)12 File (java.io.File)12 Iterator (java.util.Iterator)10 GroovyBugError (org.codehaus.groovy.GroovyBugError)10 IOException (java.io.IOException)9