Package org.apache.sling.testing.clients

# Class SlingClient

java.lang.Object
org.apache.sling.testing.clients.AbstractSlingClient
org.apache.sling.testing.clients.SlingClient

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`, `org.apache.http.client.HttpClient`

Direct Known Subclasses:
  `IndexingClient`, `MicrodataClient`, `OsgiConsoleClient`, `QueryClient`, `SlingEmailClient`

@Contract(threading=IMMUTABLE) public class SlingClient extends AbstractSlingClient

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 Classes
Modifier and Type
Class
Description
`static final class`
`SlingClient.Builder`
`static class`
`SlingClient.InternalBuilder<T extends SlingClient>`
Extensible InternalBuilder for SlingClient.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`CLIENT_CONNECTION_TIMEOUT_PROP`
`static final String`
`DEFAULT_NODE_TYPE`
`static final String`
`SUDO_COOKIE_NAME`

## Constructor Summary

Constructors
Constructor
Description
`SlingClient(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 Type
Method
Description
`SlingHttpResponse`
`createFolder(String folderName, String folderTitle, String parentPath, int... expectedStatus)`
Creates a new Folder of type sling:OrderedFolder.
`SlingHttpResponse`
`createNode(String path, String nodeType)`
Creates the node specified by a given path with the given node type.
If the given node type is `null`, 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.
`SlingHttpResponse`
`createNodeRecursive(String path, String nodeType)`
Recursively creates all the none existing nodes in the given path using the `createNode(String, String)` method.
`SlingHttpResponse`
`deletePath(String path, int... expectedStatus)`
Deletes a sling path (:operation delete)
`com.fasterxml.jackson.databind.JsonNode`
`doGetJson(String path, int depth, int... expectedStatus)`
Returns the `JsonNode` object corresponding to a content node.
`SlingClient`
`endImpersonation()`
End the impersonation of the current user.
`boolean`
`exists(String path)`
Checks whether a path exists or not by making a GET request to that path with the `json` extension
`com.fasterxml.jackson.databind.JsonNode`
`getJsonNode(String path, int depth)`
Deprecated.
use `waitExists(String, long, long)` and `doGetJson(String, int, int...)` instead
`com.fasterxml.jackson.databind.JsonNode`
`getJsonNode(String path, int depth, long waitMillis, int retryNumber, int... expectedStatus)`
Deprecated.
use `waitExists(String, long, long)` and `doGetJson(String, int, int...)` instead
`protected String`
`getNodeNameFromPath(String path)`
Extracts the node from path
`protected String`
`getParentPath(String path)`
Extracts the parent path from the given String
`String`
`getUser()`
Returns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).
`String`
`getUUId(com.fasterxml.jackson.databind.JsonNode jsonNode)`
Get the UUID from a node that was already parsed in a `JsonNode`
`String`
`getUUID(String path)`
Get the UUID of a repository path
`SlingClient`
`impersonate(String userId)`
Impersonate user with the given `userId`
`SlingHttpResponse`
`importContent(String parentPath, String contentType, File contentFile, int... expectedStatus)`
Create a tree structure under `parentPath` by providing a `contentFile` in one of the supported formats: xml, jcr.xml, json, jar, zip.
`SlingHttpResponse`
`importContent(String parentPath, String contentType, String content, int... expectedStatus)`
Create a tree structure under `parentPath` by providing a `content` in one of the supported formats: xml, jcr.xml, json, jar, zip.
`SlingHttpResponse`
`importJson(String parentPath, com.fasterxml.jackson.databind.JsonNode json, int... expectedStatus)`
Wrapper method over `importContent(String, String, String, int...)` for directly importing a json node
`SlingHttpResponse`
`move(String srcPath, String destPath, int... expectedStatus)`
Moves a sling path to a new location (:operation move)
`SlingHttpResponse`
`setPropertiesString(String nodePath, List<org.apache.http.NameValuePair> properties, int... expectedStatus)`
Sets multiple String properties on a node in a single request
`SlingHttpResponse`
`setPropertyString(String nodePath, String propName, String propValue, int... expectedStatus)`
Sets String component property on a node.
`SlingHttpResponse`
`setPropertyStringArray(String nodePath, String propName, List<String> propValueList, int... expectedStatus)`
Sets a String[] component property on a node.
`SlingHttpResponse`
`upload(File file, String mimeType, String toPath, boolean createFolders, int... expectedStatus)`
Uploads a file to the repository.
`void`
`waitExists(String path, long timeout, long delay)`
Waits until a path exists by making successive GET requests to that path with the `json extension`
`void`
`waitUntilExists(String path, long waitMillis, int retryCount)`
Deprecated.
use `waitExists(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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### DEFAULT_NODE_TYPE

public static final String DEFAULT_NODE_TYPE

See Also:
  - Constant Field Values

### CLIENT_CONNECTION_TIMEOUT_PROP

public static final String CLIENT_CONNECTION_TIMEOUT_PROP

See Also:
  - Constant Field Values

### SUDO_COOKIE_NAME

public static final String SUDO_COOKIE_NAME

See Also:
  - Constant Field Values

## 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 used
  `config` - sling specific configs
Throws:
  `ClientException` - if the client could not be created
See Also:
  - `AbstractSlingClient(CloseableHttpClient, SlingClientConfig)`

### SlingClient

public SlingClient(URI url, String user, String password) throws ClientException

Handy constructor easy to use in simple tests. Creates a client that uses basic authentication.

For constructing clients with complex configurations, use a `SlingClient.InternalBuilder`

For 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 authentication
  `password` - 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 path
  `destPath` - destination path
  `expectedStatus` - list of accepted status codes in response
Returns:
  the response
Throws:
  `ClientException` - if an error occurs during operation

### deletePath

public SlingHttpResponse deletePath(String path, int... expectedStatus) throws ClientException
Deletes a sling path (:operation delete)

Parameters:
  `path` - path to be deleted
  `expectedStatus` - list of accepted status codes in response
Returns:
  the response
Throws:
  `ClientException` - if an error occurs during operation

### createNodeRecursive

public SlingHttpResponse createNodeRecursive(String path, String nodeType) throws ClientException
Recursively creates all the none existing nodes in the given path using the `createNode(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 nodes
  `nodeType` - 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

public SlingHttpResponse createNode(String path, String nodeType) throws ClientException
Creates the node specified by a given path with the given node type.
If the given node type is `null`, 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 create
  `nodeType` - 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

public SlingClient endImpersonation()
End the impersonation of the current user.

Returns:
  the SlingClient

### exists

public boolean exists(String path) throws ClientException

Checks whether a path exists or not by making a GET request to that path with the `json` extension

Parameters:
  `path` - path to be checked
Returns:
  true if GET response returns 200
Throws:
  `ClientException` - if the request could not be performed

### getParentPath

protected String getParentPath(String path)
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

protected String getNodeNameFromPath(String path)
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.
use `waitExists(String, long, long)` instead.

Checks whether a path exists or not by making a GET request to that path with the `json extension`

It polls the server and waits until the path exists

Parameters:
  `path` - path to be checked
  `waitMillis` - time to wait between retries
  `retryCount` - number of retries before throwing an exception
Throws:
  `TestingValidationException` - if the path was not found
  `InterruptedException` - 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 extension`

Polls the server until the path exists or until timeout is reached

Parameters:
  `path` - path to be checked
  `timeout` - max total time to wait, in milliseconds
  `delay` - time to wait between checks, in milliseconds
Throws:
  `TimeoutException` - if the path was not found before timeout
  `InterruptedException` - 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 edited
  `propName` - name of the property to be edited
  `propValue` - value of the property to be edited
  `expectedStatus` - 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 edited
  `propName` - name of the property to be edited
  `propValueList` - List of String values
  `expectedStatus` - 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 edited
  `properties` - 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 values
  `expectedStatus` - 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.
use `waitExists(String, long, long)` and `doGetJson(String, int, int...)` instead
Returns the JSON content of a node already mapped to a `JsonNode`.
Waits max 10 seconds for the node to be created.

Parameters:
  `path` - the path to the content node
  `depth` - the number of levels to go down the tree, -1 for infinity
Returns:
  a `JsonNode` mapping to the requested content node.
Throws:
  `ClientException` - if something fails during request/response processing
  `InterruptedException` - 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.
use `waitExists(String, long, long)` and `doGetJson(String, int, int...)` instead
Returns JSON format of a content node already mapped to a `JsonNode`.

Parameters:
  `path` - the path to the content node
  `depth` - the number of levels to go down the tree, -1 for infinity
  `waitMillis` - how long it should wait between requests
  `retryNumber` - number of retries before throwing an exception
  `expectedStatus` - list of allowed HTTP Status to be returned. If not set, http status 200 (OK) is assumed.
Returns:
  a `JsonNode` mapping to the requested content node.
Throws:
  `ClientException` - if something fails during request/response cycle
  `InterruptedException` - to mark this operation as "waiting"

### doGetJson

public com.fasterxml.jackson.databind.JsonNode doGetJson(String path, int depth, int... expectedStatus) throws ClientException
Returns the `JsonNode` object corresponding to a content node.

Parameters:
  `path` - the path to the content node
  `depth` - the number of levels to go down the tree, -1 for infinity
  `expectedStatus` - list of allowed HTTP Status to be returned. If not set, 200 (OK) is assumed.
Returns:
  a `JsonNode` mapping 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 typed `nt:file`. The intermediary nodes are created with type "sling:OrderedFolder" if parameter `createFolders` is true

Parameters:
  `file` - the file to be uploaded
  `mimeType` - the MIME Type of the file
  `toPath` - the complete path of the file in the repository including file name
  `createFolders` - if true, all non existing parent nodes will be created using node type `sling:OrderedFolder`
  `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

### createFolder

public SlingHttpResponse createFolder(String folderName, String folderTitle, String parentPath, int... expectedStatus) throws ClientException
Creates a new Folder of type sling:OrderedFolder. Same as using `New 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:title
  `parentPath` - 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 `parentPath` by providing a `content` in 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 created
  `contentType` - format of the content
  `content` - string expressing the structure to be created, in the specified format
  `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, File contentFile, int... expectedStatus) throws ClientException

Create a tree structure under `parentPath` by providing a `contentFile` in 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 created
  `contentType` - format of the content
  `contentFile` - file containing the structure to be created, in the specified format
  `expectedStatus` - 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 over `importContent(String, String, String, int...)` for directly importing a json node

Parameters:
  `parentPath` - path where the tree is created
  `json` - json node with the desired structure
  `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

### getUUID

public String getUUID(String path) throws ClientException
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

public String getUUId(com.fasterxml.jackson.databind.JsonNode jsonNode) throws ClientException
Get the UUID from a node that was already parsed in a `JsonNode`

Parameters:
  `jsonNode` - `JsonNode` object 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

public String getUser()
Description copied from class: `AbstractSlingClient`
Returns the name of the user that will be used to authenticate the requests (by basic auth, if not replaced).

Overrides:
  `getUser` in class `AbstractSlingClient`
Returns:
  user's name

### impersonate

public SlingClient impersonate(String userId)
Impersonate user with the given `userId`

By impersonating a user SlingClient can access content from the perspective of that user.

Passing a `null` will clear impersonation.

Parameters:
  `userId` - the user to impersonate. A `null` value clears impersonation
Returns:
  the slingClient with the impersonation applied