Search in sources :

Example 1 with RequestExecutor

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

the class ValidationServiceIT method testValidRequestModel1.

@Test
public void testValidRequestModel1() throws IOException, JsonException {
    final String url = String.format("http://localhost:%s", httpPort());
    final RequestBuilder requestBuilder = new RequestBuilder(url);
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
    entity.addPart("field1", new StringBody("HELLOWORLD"));
    entity.addPart("field2", new StringBody("30.01.1988"));
    entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
    RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
    String content = re.getContent();
    JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
    assertTrue(jsonResponse.getBoolean("valid"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 2 with RequestExecutor

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

the class ValidationServiceIT method testInvalidRequestModel1.

@Test
public void testInvalidRequestModel1() throws IOException, JsonException {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
    entity.addPart("field1", new StringBody("Hello World"));
    entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
    final String url = String.format("http://localhost:%s", httpPort());
    RequestBuilder requestBuilder = new RequestBuilder(url);
    RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
    String content = re.getContent();
    JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
    assertFalse(jsonResponse.getBoolean("valid"));
    JsonObject failure = jsonResponse.getJsonArray("failures").getJsonObject(0);
    assertEquals("Property does not match the pattern \"^\\p{Upper}+$\".", failure.getString("message"));
    assertEquals("field1", failure.getString("location"));
    assertEquals(10, failure.getInt("severity"));
    failure = jsonResponse.getJsonArray("failures").getJsonObject(1);
    assertEquals("Missing required property with name \"field2\".", failure.getString("message"));
    // location is empty as the property is not found (property name is part of the message rather)
    assertEquals("", failure.getString("location"));
    assertEquals(0, failure.getInt("severity"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 3 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor 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 4 with RequestExecutor

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

the class RemoteTestHttpClient method runTests.

public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension, Map<String, String> requestOptions) throws ClientProtocolException, IOException {
    final RequestBuilder builder = new RequestBuilder(junitServletUrl);
    // Optionally let the client to consume the response entity
    final RequestExecutor executor = new RequestExecutor(new DefaultHttpClient()) {

        @Override
        protected void consumeEntity() throws ParseException, IOException {
            if (consumeContent) {
                super.consumeEntity();
            }
        }
    };
    // Build path for POST request to execute the tests
    // Test classes selector
    subpath = new StringBuilder();
    if (!junitServletUrl.endsWith(SLASH)) {
        subpath.append(SLASH);
    }
    subpath.append(testClassesSelector);
    // Test method selector
    if (testMethodSelector != null && testMethodSelector.length() > 0) {
        subpath.append("/");
        subpath.append(testMethodSelector);
    }
    // Extension
    if (!extension.startsWith(DOT)) {
        subpath.append(DOT);
    }
    subpath.append(extension);
    // Request options if any
    final List<NameValuePair> opt = new ArrayList<NameValuePair>();
    if (requestOptions != null) {
        for (Map.Entry<String, String> e : requestOptions.entrySet()) {
            opt.add(new BasicNameValuePair(e.getKey(), e.getValue()));
        }
    }
    log.info("Executing test remotely, path={} JUnit servlet URL={}", subpath, junitServletUrl);
    final Request r = builder.buildPostRequest(subpath.toString()).withCredentials(username, password).withCustomizer(requestCustomizer).withEntity(new UrlEncodedFormEntity(opt));
    executor.execute(r).assertStatus(200);
    return executor;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) ArrayList(java.util.ArrayList) Request(org.apache.sling.testing.tools.http.Request) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 5 with RequestExecutor

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

the class U method getBundleData.

/** Get JSON bundle data from webconsole */
static JsonObject getBundleData(CrankstartSetup C, DefaultHttpClient client, String symbolicName) throws ClientProtocolException, IOException, JsonException {
    final RequestBuilder b = new RequestBuilder(C.getBaseUrl());
    final RequestExecutor e = new RequestExecutor(client);
    return Json.createReader(new StringReader((e.execute(b.buildGetRequest("/system/console/bundles/" + symbolicName + ".json").withCredentials(U.ADMIN, U.ADMIN))).assertStatus(200).getContent())).readObject();
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader)

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