Search in sources :

Example 1 with RemoteTestHttpClient

use of org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient in project sling by apache.

the class SlingRemoteTestRunner method maybeExecuteTests.

private void maybeExecuteTests() throws Exception {
    if (testHttpClient != null) {
        // Tests already ran
        return;
    }
    testHttpClient = new RemoteTestHttpClient(testParameters.getJunitServletUrl(), this.username, this.password, true);
    // Let the parameters class customize the request if desired 
    if (testParameters instanceof RequestCustomizer) {
        testHttpClient.setRequestCustomizer((RequestCustomizer) testParameters);
    }
    // Run tests remotely and get response
    final RequestExecutor executor = testHttpClient.runTests(testParameters.getTestClassesSelector(), testParameters.getTestMethodSelector(), "json");
    executor.assertContentType("application/json");
    final JsonArray json = Json.createReader(new StringReader(JsonTicksConverter.tickToDoubleQuote(executor.getContent()))).readArray();
    // based on this vlaue
    for (int i = 0; i < json.size(); i++) {
        final JsonObject obj = json.getJsonObject(i);
        if (obj.containsKey("INFO_TYPE") && "test".equals(obj.getString("INFO_TYPE"))) {
            children.add(new SlingRemoteTest(testClass, obj));
        }
    }
    log.info("Server-side tests executed as {} at {} with path {}", new Object[] { this.username, testParameters.getJunitServletUrl(), testHttpClient.getTestExecutionPath() });
    // Optionally check that number of tests is as expected
    if (testParameters instanceof SlingTestsCountChecker) {
        ((SlingTestsCountChecker) testParameters).checkNumberOfTests(children.size());
    }
}
Also used : JsonArray(javax.json.JsonArray) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) RemoteTestHttpClient(org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient) RequestCustomizer(org.apache.sling.testing.tools.http.RequestCustomizer)

Example 2 with RemoteTestHttpClient

use of org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient 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 3 with RemoteTestHttpClient

use of org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient 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

RemoteTestHttpClient (org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient)3 RequestExecutor (org.apache.sling.testing.tools.http.RequestExecutor)3 JsonArray (javax.json.JsonArray)2 JsonObject (javax.json.JsonObject)2 ObjectInputStream (java.io.ObjectInputStream)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 HttpEntity (org.apache.http.HttpEntity)1 RequestCustomizer (org.apache.sling.testing.tools.http.RequestCustomizer)1