public final class TestRestUtils extends Object
TestRestUtils is a collection of REST-based utility
methods for use in unit and integration testing scenarios.| Modifier and Type | Method and Description |
|---|---|
static List<org.apache.http.NameValuePair> |
createDateQuery(String paramName,
LocalDate date)
Creates the
GET query for local date based upon configuration. |
static org.apache.http.client.utils.URIBuilder |
createGetUrl(String path)
Construct an instance from the REST path which must be a valid URI.
|
static javax.json.JsonObjectBuilder |
createJson()
Creates
JsonObjectBuilder to easy creation JSON objects and serialization into string. |
static org.springframework.data.domain.Pageable |
createPage(int page,
int size)
Creates
Pageable object based upon configuration. |
static List<org.apache.http.NameValuePair> |
createPagePair(int page,
int size)
Creates the
GET pageable query based upon configuration. |
static String |
createPageQuery(int page,
int size)
Formats the
GET query based upon configuration. |
static List<org.apache.http.NameValuePair> |
createSortPair(org.springframework.data.domain.Sort sort)
Creates the
GET sortable query based upon configuration. |
static <T> T |
jsonObject(org.springframework.test.web.servlet.MvcResult mockResult,
Class<T> valueType)
Method to deserialize JSON content from given JSON content String.
|
static <T> T |
jsonObject(String content,
Class<T> valueType)
Method to deserialize JSON content from given JSON content String.
|
static String |
toUrl(org.apache.http.client.utils.URIBuilder builder)
Creates URL string based upon
URIBuilder which is URL decoded in UTF8 character encoding. |
public static final String UTF8
public static String createPageQuery(int page, int size)
GET query based upon configuration.
createPageQuery(1,20) → p=1&s=20
page - the page numbersize - the size of pagepublic static List<org.apache.http.NameValuePair> createPagePair(int page, int size)
GET pageable query based upon configuration.
createPagePair(1,20) → p=1&s=20
page - the page numbersize - the size of pageNameValuePairpublic static List<org.apache.http.NameValuePair> createSortPair(org.springframework.data.domain.Sort sort)
GET sortable query based upon configuration. With this method it is necessary to use
URLDecoder#decode(String, String) to decode URL or use toUrl(URIBuilder).
createSortPair(new Sort(new Sort.Order(Sort.Direction.DESC, "dateFrom"))) → sort=name&name,dir=desc
sort - the sort instrumentNameValuePairURLDecoder.decode(String, String),
toUrl(URIBuilder)public static List<org.apache.http.NameValuePair> createDateQuery(String paramName, LocalDate date)
GET query for local date based upon configuration.
createDateQuery("dateFrom", LocalDate.now()) → dateFrom=2015-03-09
paramName - the name of attribute/parameterdate - the local date valueNameValuePair that represents query parameter for date represented
by paramName attributepublic static org.springframework.data.domain.Pageable createPage(int page,
int size)
Pageable object based upon configuration.page - the number of pagesize - the size of pagePageablepublic static javax.json.JsonObjectBuilder createJson()
JsonObjectBuilder to easy creation JSON objects and serialization into string.
final String content = createJson()
.add("p", 1)
.add("size", 20)
.add("fulltext", "value")
.build().toString();
JsonObjectBuilderpublic static org.apache.http.client.utils.URIBuilder createGetUrl(String path)
final String context = createGetUrl("/rpc/orders")
.addParameters(createPagePair(1, 20))
.addParameter("fulltext", "value")
.build().toString();
Result: /rpc/orders?p=1&s=20&fulltext=value&createdFrom=2015-06-20path - a valid REST URI path in string formpublic static String toUrl(org.apache.http.client.utils.URIBuilder builder) throws Exception
URIBuilder which is URL decoded in UTF8 character encoding.builder - the URI builderException - occurs if URI is wrongURLDecoder.decode(String, String),
UTF8public static <T> T jsonObject(org.springframework.test.web.servlet.MvcResult mockResult,
Class<T> valueType)
T - JSON object typemockResult - as result of mock that contains response as JSON stringvalueType - as type of object that represents JSON stringjsonObject(String, Class)public static <T> T jsonObject(String content, Class<T> valueType)
T - JSON object typecontent - as JSON stringvalueType - as type of object that represents JSON stringjsonObject(MvcResult, Class)Copyright © 2018 Pivotal Software, Inc.. All rights reserved.