Package org.kiwiproject.test.jaxrs
Class JaxrsTestHelper
- java.lang.Object
-
- org.kiwiproject.test.jaxrs.JaxrsTestHelper
-
public class JaxrsTestHelper extends Object
Helper class for testing JAX-RS code.
-
-
Field Summary
Fields Modifier and Type Field Description static int
UNPROCESSABLE_ENTITY_STATUS
Status code for (non-standard) 422 Unprocessable Entity responses.
-
Constructor Summary
Constructors Constructor Description JaxrsTestHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
assertAcceptedResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 202 Accepted response.static void
assertBadRequest(javax.ws.rs.core.Response response)
Asserts the specifiedresponse
has a 400 Bad Request status.static void
assertClientErrorResponseFamily(javax.ws.rs.core.Response response)
Asserts thatresponse
has a 4xx status code.static void
assertConflict(javax.ws.rs.core.Response response)
Asserts the specifiedresponse
has a 409 Conflict status.static void
assertCreatedResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 201 Created response.static void
assertCreatedResponseWithLocation(javax.ws.rs.core.Response response, String expectedLocation)
Asserts thatresponse
is a 201 Created response with theLocation
header set toexpectedLocation
.static void
assertCreatedResponseWithLocation(javax.ws.rs.core.Response response, String basePath, Object id)
Asserts thatresponse
is a 201 Created response with theLocation
header set to the value ofbasePath
concatenated withid
.static void
assertCreatedResponseWithLocationEndingWith(javax.ws.rs.core.Response response, String locationEndingWith)
Asserts thatresponse
is a 201 Created response with theLocation
header having a location path ending withlocationEndingWith
.static void
assertCustomHeaderFirstValue(javax.ws.rs.core.Response response, String headerName, Object expectedValue)
Asserts that the response has a custom header namedheaderName
and whose first value isexpectedValue
.static <T> void
assertEqualResponseEntity(javax.ws.rs.core.Response response, T expectedEntity)
Asserts that the response contains an entity usingResponse.getEntity()
that is equal to theexpectedEntity
using logical equality, e.g.static void
assertForbiddenResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 403 Forbidden response.static void
assertInternalServerErrorResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 500 Internal Server Error response.static void
assertJsonResponse(javax.ws.rs.core.Response response)
Asserts the actual response type ofresponse
isapplication/json
.static Map<String,Object>
assertMapResponseEntity(javax.ws.rs.core.Response response)
Asserts that the response contains an entity usingResponse.getEntity()
that is aMap
and specifically one whose keys are Strings and values are Objects.static void
assertNoContentResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 204 No Content response.static <T> T
assertNonNullResponseEntity(javax.ws.rs.core.Response response, Class<T> expectedClass)
Asserts that the response contains an entity usingResponse.getEntity()
whose class isexpectedClass
.static void
assertNotFoundResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 404 Not Found response.static void
assertOkResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 200 OK response.static void
assertPlainTextResponse(javax.ws.rs.core.Response response)
Asserts the actual response type ofresponse
istext/plain
.static <T> T
assertResponseEntity(javax.ws.rs.core.Response response, Class<T> expectedClass, T expectedEntity)
Asserts that the response contains an entity usingResponse.getEntity()
whose class isexpectedClass
and which is the same instance as theexpectedEntity
.static <T> void
assertResponseEntity(javax.ws.rs.core.Response response, T expectedEntity)
Asserts that the response contains an entity usingResponse.getEntity()
that is the same instance as theexpectedEntity
.static void
assertResponseFamily(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status.Family expectedFamily)
Asserts thatresponse
is in the expectedResponse.Status.Family
.static void
assertResponseStatusCode(javax.ws.rs.core.Response response, int expectedStatusCode)
Asserts the actual response status code isexpectedStatusCode
.static void
assertResponseStatusCode(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status expectedStatus)
Asserts the actual response expectedStatus code isexpectedStatus
.static void
assertResponseType(javax.ws.rs.core.Response response, String expectedContentType)
Asserts the actual response type ofresponse
isexpectedContentType
.static void
assertServerErrorResponseFamily(javax.ws.rs.core.Response response)
Asserts thatresponse
has a 5xx status code.static void
assertSuccessfulResponseFamily(javax.ws.rs.core.Response response)
Asserts thatresponse
has a 2xx status code.static void
assertUnauthorizedResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 401 Unauthorized response.static void
assertUnprocessableEntity(javax.ws.rs.core.Response response)
Asserts the specifiedresponse
has a 422 Unprocessable Entity status.static void
assertXmlResponse(javax.ws.rs.core.Response response)
Asserts the actual response type ofresponse
isapplication/xml
.static javax.ws.rs.core.UriInfo
createMockUriInfoWithRequestUriBuilder(String basePath)
Creates a Mockito mock {Kink javax.ws.rs.core.UriInfo} object with the specified base path, and which returns aUriBuilder
when theUriInfo.getRequestUriBuilder()
method is called.
-
-
-
Field Detail
-
UNPROCESSABLE_ENTITY_STATUS
public static final int UNPROCESSABLE_ENTITY_STATUS
Status code for (non-standard) 422 Unprocessable Entity responses.Because it is defined as a WebDAV extension, the JAX-RS
Response.Status
does not include it as an enum constant, which is...unfortunate.- See Also:
- 422 Unprocessable Entity, Constant Field Values
-
-
Method Detail
-
createMockUriInfoWithRequestUriBuilder
public static javax.ws.rs.core.UriInfo createMockUriInfoWithRequestUriBuilder(String basePath)
Creates a Mockito mock {Kink javax.ws.rs.core.UriInfo} object with the specified base path, and which returns aUriBuilder
when theUriInfo.getRequestUriBuilder()
method is called.You are obviously free to record additional expectations as well.
- Parameters:
basePath
- the base path- Returns:
- a mock
UriInfo
-
assertJsonResponse
public static void assertJsonResponse(javax.ws.rs.core.Response response)
Asserts the actual response type ofresponse
isapplication/json
.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response type is not the expected value
-
assertXmlResponse
public static void assertXmlResponse(javax.ws.rs.core.Response response)
Asserts the actual response type ofresponse
isapplication/xml
.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response type is not the expected value
-
assertPlainTextResponse
public static void assertPlainTextResponse(javax.ws.rs.core.Response response)
Asserts the actual response type ofresponse
istext/plain
.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response type is not the expected value
-
assertResponseType
public static void assertResponseType(javax.ws.rs.core.Response response, String expectedContentType)
Asserts the actual response type ofresponse
isexpectedContentType
.- Parameters:
response
- the response to checkexpectedContentType
- expected content type- Throws:
AssertionError
- if the response type is not the expected value
-
assertResponseStatusCode
public static void assertResponseStatusCode(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status expectedStatus)
Asserts the actual response expectedStatus code isexpectedStatus
.- Parameters:
response
- the response to checkexpectedStatus
- expected response status- Throws:
AssertionError
- if the response expectedStatus code is not the expected value
-
assertResponseStatusCode
public static void assertResponseStatusCode(javax.ws.rs.core.Response response, int expectedStatusCode)
Asserts the actual response status code isexpectedStatusCode
.- Parameters:
response
- the response to checkexpectedStatusCode
- the expected status code- Throws:
AssertionError
- if the response status code is not the expected value
-
assertBadRequest
public static void assertBadRequest(javax.ws.rs.core.Response response)
Asserts the specifiedresponse
has a 400 Bad Request status.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response status is not 400 Bad Request
-
assertConflict
public static void assertConflict(javax.ws.rs.core.Response response)
Asserts the specifiedresponse
has a 409 Conflict status.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response status is not 409 Conflict
-
assertUnprocessableEntity
public static void assertUnprocessableEntity(javax.ws.rs.core.Response response)
Asserts the specifiedresponse
has a 422 Unprocessable Entity status.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response is not a 422 Unprocessable Entity
-
assertCreatedResponse
public static void assertCreatedResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 201 Created response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response is not a 201 Created
-
assertCreatedResponseWithLocation
public static void assertCreatedResponseWithLocation(javax.ws.rs.core.Response response, String expectedLocation)
Asserts thatresponse
is a 201 Created response with theLocation
header set toexpectedLocation
.- Parameters:
response
- the response to checkexpectedLocation
- the expected value of theLocation
header- Throws:
AssertionError
- if the response isn't a 201 or if the location header is incorrect
-
assertCreatedResponseWithLocationEndingWith
public static void assertCreatedResponseWithLocationEndingWith(javax.ws.rs.core.Response response, String locationEndingWith)
Asserts thatresponse
is a 201 Created response with theLocation
header having a location path ending withlocationEndingWith
.Use this method when you only have a relative location path (e.g. "/object/42") instead of
assertCreatedResponseWithLocation(Response, String)
, which requires a location to match exactly: i.e. "http://localhost:4949/object/42"- Parameters:
response
- the response to checklocationEndingWith
- the expected substring at the end of theLocation
header- Throws:
AssertionError
- if the response isn't a 201 or if the location header is incorrect
-
assertCreatedResponseWithLocation
public static void assertCreatedResponseWithLocation(javax.ws.rs.core.Response response, String basePath, Object id)
Asserts thatresponse
is a 201 Created response with theLocation
header set to the value ofbasePath
concatenated withid
.- Parameters:
response
- the response to checkbasePath
- the base path, e.g. "/users" (do not add a trailing slash)id
- the identifier, e.g. "42" (do not add a leading slash)- Throws:
AssertionError
- if the response isn't a 201 or if the location header is incorrect
-
assertOkResponse
public static void assertOkResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 200 OK response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 200
-
assertAcceptedResponse
public static void assertAcceptedResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 202 Accepted response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 202
-
assertNoContentResponse
public static void assertNoContentResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 204 No Content response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 204
-
assertUnauthorizedResponse
public static void assertUnauthorizedResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 401 Unauthorized response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 401
-
assertForbiddenResponse
public static void assertForbiddenResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 403 Forbidden response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 403
-
assertNotFoundResponse
public static void assertNotFoundResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 404 Not Found response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 404
-
assertInternalServerErrorResponse
public static void assertInternalServerErrorResponse(javax.ws.rs.core.Response response)
Asserts thatresponse
is a 500 Internal Server Error response.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 500
-
assertSuccessfulResponseFamily
public static void assertSuccessfulResponseFamily(javax.ws.rs.core.Response response)
Asserts thatresponse
has a 2xx status code.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 2xx
-
assertClientErrorResponseFamily
public static void assertClientErrorResponseFamily(javax.ws.rs.core.Response response)
Asserts thatresponse
has a 4xx status code.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 4xx
-
assertServerErrorResponseFamily
public static void assertServerErrorResponseFamily(javax.ws.rs.core.Response response)
Asserts thatresponse
has a 5xx status code.- Parameters:
response
- the response to check- Throws:
AssertionError
- if the response isn't a 5xx
-
assertResponseFamily
public static void assertResponseFamily(javax.ws.rs.core.Response response, javax.ws.rs.core.Response.Status.Family expectedFamily)
Asserts thatresponse
is in the expectedResponse.Status.Family
.- Parameters:
response
- the response to checkexpectedFamily
- the expectedResponse.Status.Family
of the given response- Throws:
AssertionError
- if the response isn't in the expected family
-
assertCustomHeaderFirstValue
public static void assertCustomHeaderFirstValue(javax.ws.rs.core.Response response, String headerName, Object expectedValue)
Asserts that the response has a custom header namedheaderName
and whose first value isexpectedValue
.- Parameters:
response
- the response to checkheaderName
- the name of the header to checkexpectedValue
- the expected value of the header- Throws:
AssertionError
- if the response doesn't have a header namedheaderName
or if the value doesn't match
-
assertNonNullResponseEntity
public static <T> T assertNonNullResponseEntity(javax.ws.rs.core.Response response, Class<T> expectedClass)
Asserts that the response contains an entity usingResponse.getEntity()
whose class isexpectedClass
.- Type Parameters:
T
- the entity type- Parameters:
response
- the response to checkexpectedClass
- the expected entity type- Returns:
- the response entity, cast as a
T
- Throws:
AssertionError
- if the response entity doesn't exist or isn't the expected type- See Also:
Response.getEntity()
-
assertResponseEntity
public static <T> void assertResponseEntity(javax.ws.rs.core.Response response, T expectedEntity)
Asserts that the response contains an entity usingResponse.getEntity()
that is the same instance as theexpectedEntity
.- Type Parameters:
T
- the entity type- Parameters:
response
- the response to checkexpectedEntity
- the expected response entity- Throws:
AssertionError
- if the if the response entity doesn't exist or isn't the expected object- See Also:
Response.getEntity()
-
assertResponseEntity
public static <T> T assertResponseEntity(javax.ws.rs.core.Response response, Class<T> expectedClass, T expectedEntity)
Asserts that the response contains an entity usingResponse.getEntity()
whose class isexpectedClass
and which is the same instance as theexpectedEntity
.- Type Parameters:
T
- the response entity type- Parameters:
response
- the response to checkexpectedClass
- the expected entity typeexpectedEntity
- the expected entity- Returns:
- the same entity assuming the assertion passed
- Throws:
AssertionError
- if the response entity doesn't exist or isn't the expected class/object- See Also:
Response.getEntity()
-
assertEqualResponseEntity
public static <T> void assertEqualResponseEntity(javax.ws.rs.core.Response response, T expectedEntity)
Asserts that the response contains an entity usingResponse.getEntity()
that is equal to theexpectedEntity
using logical equality, e.g. usingObject.equals(Object)
.- Type Parameters:
T
- the entity type- Parameters:
response
- the response to checkexpectedEntity
- the expected entity- Throws:
AssertionError
- if the response entity doesn't exist or isn't equal to the expected object- See Also:
Response.getEntity()
-
assertMapResponseEntity
public static Map<String,Object> assertMapResponseEntity(javax.ws.rs.core.Response response)
Asserts that the response contains an entity usingResponse.getEntity()
that is aMap
and specifically one whose keys are Strings and values are Objects.- Parameters:
response
- the response to check- Returns:
- the response entity cast to a map
- Throws:
AssertionError
- if the response doesn't have an entity, or is not a map- Implementation Note:
- This method performs an unchecked cast of the map entity to
Map<String, Object>
after verifying the entity is aMap
instance. Unexpected results and/or exceptions may occur if the map does not contain only String keys.
-
-