REST Test Harness
The CASL for REST services (CaslRest
) provides a framework to test REST
services. The framework is based on an object-oriented design concept to facilitate the process of
building, transmitting, and verifying REST services.
The REST Test Harness
The CASL for REST services is the primary test harness that is used for testing REST services with an API that is auto-generated. It provides the exhaustive API of all existing REST services in WebSphere Commerce and is based on the Apache HttpClient library. The REST framework is used primarily as a quick and easy way to run test setup and cleanup. There is nothing to prevent its use during test scenarios.
- Developers use Java objects (builders) that represent each REST service, where the URL and input parameters are built in for you.
- A wrapper class is provided. The wrapper represents the responses of an HTTP request along with convenience methods to fetch data easily.
- The response resource (payload) is modeled by Java objects with all the getter methods to parse the response data.
- Automatic caching of
WCToken
,WCTrustedToken
, andWC_USERACTIVITY
cookies for authentication.
Request builders
Each builder class corresponds to a single REST resource handler API. The builder class encapsulates all of its relevant inputs and configurations along with providing setter methods to populate its values to facilitate building the request. These classes follow the Builder Design Pattern and provide the advantage of not requiring knowledge of the request format beforehand.
All required inputs for a REST service API are made as arguments into the builder's constructor.
If the service supports both HTTP and HTTPS, then the builder has both setter methods withProtocolAsHTTP() and withProtocolAsHTTPS(). Otherwise, it defaults to the required protocol.
For query parameters that can be specified multiple times, such as associationType=UPSELL&associationType=X-SELL, the builder contains methods to add and remove the parameter values, such as addAssociationType(String) and removeAssociationType(String).
- The REST handler service's corresponding URL.
- The optional and non-optional URL and query parameter
- The response model class type, returned by the service in a subclass called ResponseTypes.
- All of the possible search profile inputs for search REST services in an enum called RestSearchProfiles.
- The method addCustomParameter() to add custom query parameters if they are not included in the builder.
- The method withContentTypeHeader() to set the content-type in the header of the request.
- The method asResourceRequest() to change the context URL to retrieve resources in store preview context.
- The method withJasonTextContent() and withXmlTextContent() to set the body of a PUT or POST request.
FindProductsBySearchTermBuilder builder =
f_searchBuilderFactory.createFindProductsBySearchTermBuilder("78036", "*")
.withSearchSource(SearchSource.QUICK_SEARCH)
.withSearchProfile(FindProductsBySearchTermBuilder.RestSearchProfiles.IBM_findProductsBySearchTerm)
.withPageNumber("1")
.withSearchTerm("orange");
RestResponse response = session.sendRequest(byPartNumber);
Productsbysearchterm searchResponse =
response.createResponse(FindProductsBySearchTermBuilder.ResponseTypes.getProductsbysearchtermType());
for (ProductsbysearchtermCatalogEntryView catEntry : searchResponse.getCatalogEntryView())
{
if(catEntry.getPartNumber().equals(“Expected_PartNumber”))
{
return;
}fail();
}
Response wrapper
The RestResponse class is a wrapper class that abstracts the raw Apache HttpResponse object and gives convenience methods for parsing the response. RestResponse also gives the option of returning the actual HttpResponse for more exceptional verification cases. The object that is returned by the session object when a request is sent.
Response model types
The response models are generated plain old Java objects (POJOs) that model the format of the response that is returned by the REST service handler API. Developers can retrieve data from the response payload by calling the getter methods on the response instance instead of parsing through the respond JSON by using strings. Inside each builder class that returns a response, there is a subclass that is called ResponseTypes that contains the return response class type. The response type can be retrieved by using the createResponse(Class<T> responseType method from the RestResponse wrapper instance.
public static class ResponseTypes {
public static Class<Cart> getCartType(){
return Cart.class;
}
}
Cart cartUpdateResponse = response.createResponse(GetCartBuilder.ResponseTypes.getCartType());
Assert.assertEquals(cartUpdateResponse.getorderItem().get(0).getpartNumber(), "GFR033_330101");
Factories
Two abstract factory classes exist which are used to create builder objects. The SearchRestBuilderFactory is used for creating builders for the Search REST services that were created in WebSphere Commerce version 7. If you want to run a search, use the SearchRestBuilderFactory, as the current REST services correspond to the search feature on the Aurora store. The BodRestBuilderFactory is used for creating builders for all services on the WebSphere Commerce Server side.
RestSession
The RestSession object is used to send a request to the server by passing in
a builder object and returning a response wrapper. The session caches the WCToken
,
the WCTrustedToken
, from the REST logon response, and the
WC_USERACTIVITY
cookie from the web logon. The session automatically populates
subsequent requests until the user ends the logon session by logging out.