Class SlingClient
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.http.client.HttpClient
- Direct Known Subclasses:
IndexingClient,MicrodataClient,OsgiConsoleClient,QueryClient,SlingEmailClient
The Base class for all Integration Test Clients. It provides generic methods to send HTTP requests to a server.
It has methods to perform simple node operations on the server like creating and deleting nodes, etc. on the server using requests.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic classSlingClient.InternalBuilder<T extends SlingClient>Extensible InternalBuilder for SlingClient. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSlingClient(URI url, String user, String password) Handy constructor easy to use in simple tests.SlingClient(org.apache.http.impl.client.CloseableHttpClient http, SlingClientConfig config) Constructor used by Builders and adaptTo(). -
Method Summary
Modifier and TypeMethodDescriptioncreateFolder(String folderName, String folderTitle, String parentPath, int... expectedStatus) Creates a new Folder of type sling:OrderedFolder.createNode(String path, String nodeType) Creates the node specified by a given path with the given node type.
If the given node type isnull, the node will be created with the default type: "sling:OrderedFolder".
If the node already exists, the method will return null, with no errors.
The method ignores trailing slashes so a path like this /a/b/c/// is accepted and will create the c node if the rest of the path exists.createNodeRecursive(String path, String nodeType) Recursively creates all the none existing nodes in the given path using thecreateNode(String, String)method.deletePath(String path, int... expectedStatus) Deletes a sling path (:operation delete)com.fasterxml.jackson.databind.JsonNodeReturns theJsonNodeobject corresponding to a content node.End the impersonation of the current user.booleanChecks whether a path exists or not by making a GET request to that path with thejsonextensioncom.fasterxml.jackson.databind.JsonNodegetJsonNode(String path, int depth) Deprecated.com.fasterxml.jackson.databind.JsonNodegetJsonNode(String path, int depth, long waitMillis, int retryNumber, int... expectedStatus) Deprecated.usewaitExists(String, long, long)anddoGetJson(String, int, int...)insteadprotected StringgetNodeNameFromPath(String path) Extracts the node from pathprotected StringgetParentPath(String path) Extracts the parent path from the given StringgetUser()Returns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).getUUId(com.fasterxml.jackson.databind.JsonNode jsonNode) Get the UUID from a node that was already parsed in aJsonNodeGet the UUID of a repository pathimpersonate(String userId) Impersonate user with the givenuserIdimportContent(String parentPath, String contentType, File contentFile, int... expectedStatus) Create a tree structure underparentPathby providing acontentFilein one of the supported formats: xml, jcr.xml, json, jar, zip.importContent(String parentPath, String contentType, String content, int... expectedStatus) Create a tree structure underparentPathby providing acontentin one of the supported formats: xml, jcr.xml, json, jar, zip.importJson(String parentPath, com.fasterxml.jackson.databind.JsonNode json, int... expectedStatus) Wrapper method overimportContent(String, String, String, int...)for directly importing a json nodeMoves a sling path to a new location (:operation move)setPropertiesString(String nodePath, List<org.apache.http.NameValuePair> properties, int... expectedStatus) Sets multiple String properties on a node in a single requestsetPropertyString(String nodePath, String propName, String propValue, int... expectedStatus) Sets String component property on a node.setPropertyStringArray(String nodePath, String propName, List<String> propValueList, int... expectedStatus) Sets a String[] component property on a node.Uploads a file to the repository.voidwaitExists(String path, long timeout, long delay) Waits until a path exists by making successive GET requests to that path with thejson extensionvoidwaitUntilExists(String path, long waitMillis, int retryCount) Deprecated.usewaitExists(String, long, long)instead.Methods inherited from class org.apache.sling.testing.clients.AbstractSlingClient
adaptTo, addValue, close, doDelete, doGet, doGet, doGet, doHead, doPatch, doPost, doPost, doPut, doRawRequest, doRequest, doStreamGet, doStreamPost, doStreamRequest, execute, execute, execute, execute, execute, execute, execute, execute, getClientId, getConnectionManager, getCookieStore, getCredentialsProvider, getParams, getPassword, getPath, getPath, getUrl, getUrl, getUrl, getValue, getValues, hasValue
-
Field Details
-
DEFAULT_NODE_TYPE
- See Also:
-
CLIENT_CONNECTION_TIMEOUT_PROP
- See Also:
-
SUDO_COOKIE_NAME
- See Also:
-
-
Constructor Details
-
SlingClient
public SlingClient(org.apache.http.impl.client.CloseableHttpClient http, SlingClientConfig config) throws ClientException Constructor used by Builders and adaptTo(). Should never be called directly from the code.- Parameters:
http- the underlying HttpClient to be usedconfig- sling specific configs- Throws:
ClientException- if the client could not be created- See Also:
-
SlingClient
Handy constructor easy to use in simple tests. Creates a client that uses basic authentication.
For constructing clients with complex configurations, use a
SlingClient.InternalBuilderFor constructing clients with the same configuration, but a different class, use
AbstractSlingClient.adaptTo(Class)- Parameters:
url- url of the server (including context path)user- username for basic authenticationpassword- password for basic authentication- Throws:
ClientException- never, kept for uniformity with the other constructors
-
-
Method Details
-
move
public SlingHttpResponse move(String srcPath, String destPath, int... expectedStatus) throws ClientException Moves a sling path to a new location (:operation move)- Parameters:
srcPath- source pathdestPath- destination pathexpectedStatus- list of accepted status codes in response- Returns:
- the response
- Throws:
ClientException- if an error occurs during operation
-
deletePath
Deletes a sling path (:operation delete)- Parameters:
path- path to be deletedexpectedStatus- list of accepted status codes in response- Returns:
- the response
- Throws:
ClientException- if an error occurs during operation
-
createNodeRecursive
Recursively creates all the none existing nodes in the given path using thecreateNode(String, String)method. All the created nodes will have the given node type.- Parameters:
path- the path to use for creating all the none existing nodesnodeType- the node type to use for the created nodes- Returns:
- the response to the creation of the leaf node
- Throws:
ClientException- if one of the nodes can't be created
-
createNode
Creates the node specified by a given path with the given node type.
If the given node type isnull, the node will be created with the default type: "sling:OrderedFolder".
If the node already exists, the method will return null, with no errors.
The method ignores trailing slashes so a path like this /a/b/c/// is accepted and will create the c node if the rest of the path exists.- Parameters:
path- the path to the node to createnodeType- the type of the node to create- Returns:
- the sling HTTP response or null if the path already existed
- Throws:
ClientException- if the node can't be created
-
endImpersonation
End the impersonation of the current user.- Returns:
- the SlingClient
-
exists
Checks whether a path exists or not by making a GET request to that path with the
jsonextension- Parameters:
path- path to be checked- Returns:
- true if GET response returns 200
- Throws:
ClientException- if the request could not be performed
-
getParentPath
Extracts the parent path from the given String- Parameters:
path- string containing the path- Returns:
- the parent path if exists or empty string otherwise
-
getNodeNameFromPath
Extracts the node from path- Parameters:
path- string containing the path- Returns:
- the node without parent path
-
waitUntilExists
@Deprecated public void waitUntilExists(String path, long waitMillis, int retryCount) throws TestingValidationException, InterruptedException Deprecated.usewaitExists(String, long, long)instead.Checks whether a path exists or not by making a GET request to that path with the
json extensionIt polls the server and waits until the path exists
- Parameters:
path- path to be checkedwaitMillis- time to wait between retriesretryCount- number of retries before throwing an exception- Throws:
TestingValidationException- if the path was not foundInterruptedException- to mark this operation as "waiting"
-
waitExists
public void waitExists(String path, long timeout, long delay) throws TimeoutException, InterruptedException Waits until a path exists by making successive GET requests to that path with the
json extensionPolls the server until the path exists or until timeout is reached
- Parameters:
path- path to be checkedtimeout- max total time to wait, in millisecondsdelay- time to wait between checks, in milliseconds- Throws:
TimeoutException- if the path was not found before timeoutInterruptedException- to mark this operation as "waiting", should be rethrown by callers- Since:
- 1.1.0
-
setPropertyString
public SlingHttpResponse setPropertyString(String nodePath, String propName, String propValue, int... expectedStatus) throws ClientException Sets String component property on a node.- Parameters:
nodePath- path to the node to be editedpropName- name of the property to be editedpropValue- value of the property to be editedexpectedStatus- list of expected HTTP Status to be returned, if not set, 200 is assumed.- Returns:
- the response object
- Throws:
ClientException- if something fails during the request/response cycle
-
setPropertyStringArray
public SlingHttpResponse setPropertyStringArray(String nodePath, String propName, List<String> propValueList, int... expectedStatus) throws ClientException Sets a String[] component property on a node.- Parameters:
nodePath- path to the node to be editedpropName- name of the property to be editedpropValueList- List of String valuesexpectedStatus- list of expected HTTP Status to be returned, if not set, 200 is assumed.- Returns:
- the response
- Throws:
ClientException- if something fails during the request/response cycle
-
setPropertiesString
public SlingHttpResponse setPropertiesString(String nodePath, List<org.apache.http.NameValuePair> properties, int... expectedStatus) throws ClientException Sets multiple String properties on a node in a single request- Parameters:
nodePath- path to the node to be editedproperties- list of NameValue pairs with the name and value for each property. String[] properties can be defined by adding multiple time the same property name with different valuesexpectedStatus- list of expected HTTP Status to be returned, if not set, 200 is assumed.- Returns:
- the response
- Throws:
ClientException- if the operation could not be completed
-
getJsonNode
@Deprecated public com.fasterxml.jackson.databind.JsonNode getJsonNode(String path, int depth) throws ClientException, InterruptedException Deprecated.usewaitExists(String, long, long)anddoGetJson(String, int, int...)insteadReturns the JSON content of a node already mapped to aJsonNode.
Waits max 10 seconds for the node to be created.- Parameters:
path- the path to the content nodedepth- the number of levels to go down the tree, -1 for infinity- Returns:
- a
JsonNodemapping to the requested content node. - Throws:
ClientException- if something fails during request/response processingInterruptedException- to mark this operation as "waiting"
-
getJsonNode
@Deprecated public com.fasterxml.jackson.databind.JsonNode getJsonNode(String path, int depth, long waitMillis, int retryNumber, int... expectedStatus) throws ClientException, InterruptedException Deprecated.usewaitExists(String, long, long)anddoGetJson(String, int, int...)insteadReturns JSON format of a content node already mapped to aJsonNode.- Parameters:
path- the path to the content nodedepth- the number of levels to go down the tree, -1 for infinitywaitMillis- how long it should wait between requestsretryNumber- number of retries before throwing an exceptionexpectedStatus- list of allowed HTTP Status to be returned. If not set, http status 200 (OK) is assumed.- Returns:
- a
JsonNodemapping to the requested content node. - Throws:
ClientException- if something fails during request/response cycleInterruptedException- to mark this operation as "waiting"
-
doGetJson
public com.fasterxml.jackson.databind.JsonNode doGetJson(String path, int depth, int... expectedStatus) throws ClientException Returns theJsonNodeobject corresponding to a content node.- Parameters:
path- the path to the content nodedepth- the number of levels to go down the tree, -1 for infinityexpectedStatus- list of allowed HTTP Status to be returned. If not set, 200 (OK) is assumed.- Returns:
- a
JsonNodemapping to the requested content node. - Throws:
ClientException- if the path does not exist or something fails during request/response cycle- Since:
- 1.1.0
-
upload
public SlingHttpResponse upload(File file, String mimeType, String toPath, boolean createFolders, int... expectedStatus) throws ClientException Uploads a file to the repository. It creates a leaf node typednt:file. The intermediary nodes are created with type "sling:OrderedFolder" if parametercreateFoldersis true- Parameters:
file- the file to be uploadedmimeType- the MIME Type of the filetoPath- the complete path of the file in the repository including file namecreateFolders- if true, all non existing parent nodes will be created using node typesling:OrderedFolderexpectedStatus- list of expected HTTP Status to be returned, if not set, 201 is assumed.- Returns:
- the response
- Throws:
ClientException- if something fails during the request/response cycle
-
createFolder
public SlingHttpResponse createFolder(String folderName, String folderTitle, String parentPath, int... expectedStatus) throws ClientException Creates a new Folder of type sling:OrderedFolder. Same as usingNew Folder...in the Site Admin.- Parameters:
folderName- The name of the folder to be used in the URL.folderTitle- Title of the Folder to be set in jcr:titleparentPath- The parent path where the folder gets added.expectedStatus- list of expected HTTP Status to be returned, if not set, 201 is assumed.- Returns:
- the response
- Throws:
ClientException- if something fails during the request/response cycle
-
importContent
public SlingHttpResponse importContent(String parentPath, String contentType, String content, int... expectedStatus) throws ClientException Create a tree structure under
parentPathby providing acontentin one of the supported formats: xml, jcr.xml, json, jar, zip.This is the implementation of
:operation import, as documented in importing-content-structures- Parameters:
parentPath- path where the tree is createdcontentType- format of the contentcontent- string expressing the structure to be created, in the specified formatexpectedStatus- list of expected HTTP Status to be returned, if not set, 201 is assumed- Returns:
- the response
- Throws:
ClientException- if something fails during the request/response cycle
-
importContent
public SlingHttpResponse importContent(String parentPath, String contentType, File contentFile, int... expectedStatus) throws ClientException Create a tree structure under
parentPathby providing acontentFilein one of the supported formats: xml, jcr.xml, json, jar, zip.This is the implementation of
:operation import, as documented in importing-content-structures- Parameters:
parentPath- path where the tree is createdcontentType- format of the contentcontentFile- file containing the structure to be created, in the specified formatexpectedStatus- list of expected HTTP Status to be returned, if not set, 200 is assumed- Returns:
- the response
- Throws:
ClientException- if something fails during the request/response cycle
-
importJson
public SlingHttpResponse importJson(String parentPath, com.fasterxml.jackson.databind.JsonNode json, int... expectedStatus) throws ClientException Wrapper method overimportContent(String, String, String, int...)for directly importing a json node- Parameters:
parentPath- path where the tree is createdjson- json node with the desired structureexpectedStatus- list of expected HTTP Status to be returned, if not set, 201 is assumed- Returns:
- the response
- Throws:
ClientException- if something fails during the request/response cycle
-
getUUID
Get the UUID of a repository path- Parameters:
path- path in repository- Returns:
- uuid as String or null if path does not exist
- Throws:
ClientException- if something fails during request/response cycle
-
getUUId
Get the UUID from a node that was already parsed in aJsonNode- Parameters:
jsonNode-JsonNodeobject of the repository node- Returns:
- UUID as String or null if jsonNode is null or if the UUID was not found
- Throws:
ClientException- if something fails during request/response cycle
-
getUser
Description copied from class:AbstractSlingClientReturns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).- Overrides:
getUserin classAbstractSlingClient- Returns:
- user's name
-
impersonate
Impersonate user with the givenuserIdBy impersonating a user SlingClient can access content from the perspective of that user.
Passing anullwill clear impersonation.- Parameters:
userId- the user to impersonate. Anullvalue clears impersonation- Returns:
- the slingClient with the impersonation applied
-
waitExists(String, long, long)anddoGetJson(String, int, int...)instead