Skip to content

Stub Configuration, via API

Tamás Kőhegyi edited this page May 22, 2021 · 4 revisions

Since 1.5 version of Java Service API library, it is possible to configure the stub from the code, without actually using any JSON configuration.

Creating a new Stub Configuration by using Service API

The main approach is the following:

//use some imports from wilma-service-api.jar
import com.epam.wilma.service.configuration.stub.WilmaStub;
import com.epam.wilma.service.configuration.stub.WilmaStubBuilder;

//need connection to Wilma Service
//Note: in properties, wilma.host and wilma.port (the internal port) should be specified
private WilmaService wilmaService = new WilmaService(myComponent.getProperties());

//build up a configuration
WilmaStub wilmaStub = new WilmaStubBuilder()
    .forRequestsLike().comingFrom("localhost")
    .willRespondWith().plainTextResponse("blah")
    .build();

//then just upload this to Wilma Service
boolean b =  wilmaService.uploadStubConfiguration(wilmaStub);
//b will be true if upload was success, otherwise false

// Do whatever is necessary, but ensure that the app/component uses the Wilma Service as proxy for the HTTP(S) calls.

//Finally drop the configuration
wilmaService.dropStubConfig(wilmaStub.getGroupName());

When creating the stub configuration, the following possibilities are offered:

new WilmaStubBuilder().forRequestsLike() - Use this, if you would like to build up a dialg descriptor, that contains a request condition and a response descriptor.

new WilmaStubBuilder().forAnyRequest() - Use this, if you don't want to use any dialog descriptor, just interceptors.

If you selected forRequestsLike() you have the following possibilities in order to create the request condition part:

  • .andStart() .... .andEnd() - The outcome of the conditions between them will be connected with arithmetical AND function.

  • .orStart() .... .orEnd() - The outcome of the conditions between them will be connected with arithmetical OR function.

  • .notStart() .... .notEnd() - The outcome of the (single) condition between them will be negated.

  • .condition(String className) - Specify a condition by its name.

  • .negatedCondition(String className) - Specify a condition by its name, and use its negated result.

  • .condition(String className, ConfigurationParameter[] parameters) - Specify a condition by its name, and with its parameters.

  • .negatedCondition(String className) - Specify a condition by its name and with its parameters, and use its negated result.

  • .comingFrom(String hostname) - Utilizes an UrlPatternChecker, with value //hostname.

  • .textInUrl(String text) - Utilizes an UrlPatternChecker, with value text.

  • .withHeader(String headerName, String headerValue) - Utilizes a HeaderPatternChecker, with header name value pairs.

  • .withTextInHeader(String pattern) - Utilizes a HeaderPatternChecker, with pattern value.

  • .withTextInBody(String pattern) - Utilizes a BodyPatternChecker, with pattern value.

To continue with describing the response, call:

  • .willRespondeWith() - This call leads to response descriptor part, where you may define:

  • .plaintextResponse(String text) - Will answer with text/plain content-type and with the text as body of the response.

  • .textFileResponse(String textFileName) - Will answer with text/plain content-type and the response body will come from specified file.

  • .xmlFileResponse(String xmlFileName) - Will answer with application/xml content-type and the response body will come from specified file.

  • .htmlFileResponse(String htmlFileName) - Will answer with text/html content-type and the response body will come from specified file.

  • .jsonFileResponse(String jsonFileName) - Will answer with application/json content-type and the response body will come from specified file.

  • .generatedResponse(String className) - Will call the class to generate a dynamic response.

  • .applyFormatter(String formatterClass) - Apply a response formatter by specifying its class name.

  • .applyFormatter(String formatterClass, ConfigurationParameter[] parameters) - Apply a response formatter by specifying its class name, and using formatter class parameters from parameters.

  • .withStatus(int statusCode) - To set the status response code.

  • .withMimeType(String mimeType) - To set the mime-type (content-type) of the response.

  • .withDelay(int delay) - To set response delay in milliseconds.

When the response part is defined (or the forAnyRequest() call was used), you may add some interceptors too:

  • .addInterceptor(String interceptorname, String interceptorClass) - To add an interceptor with its name and classname.
  • .addInterceptor(String interceptorname, String interceptorClass, ConfigurationParameter[] parameters) - To add an interceptor with its name and classname, with parameters.

And finally call the build() method to have the WilmaStub object in your hand.

The last step is to upload the configuration to Wilma. You can do it by calling: wilmaService.uploadStubConfiguration(wilmaStub.toString())

Identifying an uploaded Stub Configuration

A stub configuration is identified by its groupName. When you call WilmaStubBuilder() call, a generated groupName will be used, that is service-api-<uniqueId>, where the <uniqueId> part is generated by the service api library. With this approach, the name of the stub configuration will be unique enough to use is at a Wilma instance that serves several clients in parallel.

If you call WilmaStubBuilder(String name) method, you may specify the name yourself. In this case it is up to you to use a proper (unique) stub configuration groupName.

You may get information on the generated name by calling String wilmaStub.getGroupName() method.

When the work is finished with the stub configuration, it is necessary to drop it from Wilma, with this command: wilmaService.dropStubConfig(groupName);

Clone this wiki locally