Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An abstract class for creating integration tests to use the Sling Remote Testing Tools to test AEM / Adobe CQ5 projects.
package com.sixdimensions.wcm.cq.it;
import java.io.IOException;
import org.apache.sling.testing.tools.http.RequestExecutor;
import org.apache.sling.testing.tools.sling.SlingClient;
import org.apache.sling.testing.tools.sling.SlingTestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AbstractIntegrationTest extends SlingTestBase {
/**
* The SLF4J Logger
*/
private static final Logger log = LoggerFactory.getLogger(AbstractIntegrationTest.class);
protected String appsBase;
protected String contentBase;
/**
* The SlingClient can be used to interact with the repository when it is
* started. By retrieving the information for the Server URL, username and
* password, the Sling instance will be automatically started.
*/
protected SlingClient slingClient = new SlingClient(
this.getServerBaseUrl(), this.getServerUsername(),
this.getServerPassword()) {
};
public RequestExecutor initTest(final String name) throws IOException
{
log.info("Deleting existing test app and content");
deleteIfExists(appsBase);
deleteIfExists(contentBase);
log.info("Creating testing component...");
createFolders(this.slingClient, appsBase + "/" + name);
this.slingClient.upload(
appsBase + "/" + name + "/" + name + ".jsp",
getClass().getClassLoader().getResourceAsStream(name + ".jsp"),
-1,
true);
log.info(this.getRequestExecutor().execute(
this.getRequestBuilder().buildGetRequest(appsBase + "/" + name + ".3.json").withCredentials("admin", "admin")).assertStatus(200).getContent());
log.info("Creating testing content...");
this.slingClient.createNode(
contentBase + "/" + name,
"jcr:primaryType", "nt:unstructured", "sling:resourceType",
appsBase + "/" + name);
log.info(this.getRequestExecutor().execute(
this.getRequestBuilder().buildGetRequest(contentBase + "/" + name + ".json").withCredentials("admin", "admin")).assertStatus(200).getContent());
final RequestExecutor exec = this.getRequestExecutor().execute(
this.getRequestBuilder().buildGetRequest(contentBase + "/" + name + ".html").withCredentials("admin", "admin"));
log.info("Content: " + exec.getContent());
exec.assertStatus(200);
return exec;
}
/**
* Creates the parent folders of the specified path.
*
* @param slingClient
* the sling client to use
* @param path
* the path to create
* @throws IOException
*/
public static final void createFolders(SlingClient slingClient, String path)
throws IOException {
String currentPath = "";
for (String part : path.split("/")) {
if (part == null || part.trim().length() == 0) {
continue;
}
currentPath += "/" + part;
if (!slingClient.exists(currentPath)) {
slingClient.mkdir(currentPath);
}
}
}
/**
* Deletes the content at the specified path if any exists.
*
* @param slingClient
* the sling client to use
* @param path
* the path to create
* @throws IOException
*/
public static final void deleteIfExists(SlingClient slingClient, String path) throws IOException {
if (slingClient.exists(path)) {
slingClient.delete(path);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.