Search in sources :

Example 11 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor in project sling by apache.

the class ServerSideTestClient method runTests.

public TestResults runTests(String testPackageOrClassName) throws Exception {
    final RemoteTestHttpClient testClient = new RemoteTestHttpClient(serverBaseUrl + "/system/sling/junit", serverUsername, serverPassword, true);
    final TestResults r = new TestResults();
    final Map<String, String> options = new HashMap<String, String>();
    options.put("forceReload", "true");
    final RequestExecutor executor = testClient.runTests(testPackageOrClassName, null, "json", options);
    executor.assertContentType("application/json");
    String content = executor.getContent();
    if (!content.trim().isEmpty()) {
        final JsonArray json = JsonUtil.parseArray(content);
        for (int i = 0; i < json.size(); i++) {
            final JsonObject obj = json.getJsonObject(i);
            if ("test".equals(obj.getString("INFO_TYPE"))) {
                r.testCount++;
                if (obj.containsKey("failure")) {
                    r.failures.add(JsonUtil.toString(obj.get("failure")));
                }
            }
        }
    }
    return r;
}
Also used : JsonArray(javax.json.JsonArray) HashMap(java.util.HashMap) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) JsonObject(javax.json.JsonObject) RemoteTestHttpClient(org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient)

Example 12 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor in project sling by apache.

the class SlingRemoteExecutionRule method invokeRemote.

private void invokeRemote(String remoteUrl, String remoteUsername, String remotePassword, FrameworkMethod method) throws Throwable {
    final String testClassesSelector = method.getMethod().getDeclaringClass().getName();
    final String methodName = method.getMethod().getName();
    final RemoteTestHttpClient testHttpClient = new RemoteTestHttpClient(remoteUrl, remoteUsername, remotePassword, false);
    testHttpClient.setRequestCustomizer(this);
    final RequestExecutor executor = testHttpClient.runTests(testClassesSelector, methodName, "serialized");
    log.debug("Ran test {} method {} at URL {}", new Object[] { testClassesSelector, methodName, remoteUrl });
    final HttpEntity entity = executor.getResponse().getEntity();
    if (entity != null) {
        try {
            final Object o = new ObjectInputStream(entity.getContent()).readObject();
            if (!(o instanceof ExecutionResult)) {
                throw new IllegalStateException("Expected an ExecutionResult, got a " + o.getClass().getName());
            }
            final ExecutionResult result = (ExecutionResult) o;
            if (result.isFailure()) {
                throw result.getException();
            }
        } finally {
            entity.consumeContent();
        }
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) RemoteTestHttpClient(org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

RequestExecutor (org.apache.sling.testing.tools.http.RequestExecutor)12 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)8 StringReader (java.io.StringReader)7 JsonObject (javax.json.JsonObject)7 Test (org.junit.Test)5 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)3 StringBody (org.apache.http.entity.mime.content.StringBody)3 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)3 RemoteTestHttpClient (org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient)3 JsonArray (javax.json.JsonArray)2 Request (org.apache.sling.testing.tools.http.Request)2 ObjectInputStream (java.io.ObjectInputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1