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"));
}
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"));
}
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());
}
}
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;
}
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();
}
Aggregations