Skip to content

adamyork/wiremock-velocity-transformer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wiremock-velocity-transformer

transformer used to render velocity templates for stubbed responses.

To use within a custom Wiremock Java implementation

  • Include wiremock-velocity-transformer in your project
  • Requires jdk 8+

Maven

<dependency>
  <groupId>com.github.adamyork</groupId>
  <artifactId>wiremock-velocity-transformer</artifactId>
  <version>2.0</version>
</dependency>

Gradle

dependencies {
    compile group: "com.github.adamyork", name: "wiremock-velocity-transformer", version: "2.0"
}
new WireMockServer(wireMockConfig().extensions("com.github.adamyork.wiremock.transformer.VelocityResponseTransformer",));

or

new WireMockServer(wireMockConfig().extensions(VelocityResponseTransformer.class));

or

new WireMockServer(wireMockConfig().extensions(new VelocityResponseTransformer()));

To use in conjunction with Wiremock Standalone Jar

java -cp "wiremock-standalone-2.32.2.jar;wiremock-velocity-transformer-standalone-2.3.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions com.github.adamyork.wiremock.transformer.VelocityResponseTransformer

Unix

java -cp "wiremock-standalone-2.32.2.jar:wiremock-velocity-transformer-standalone-2.3.jar" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --verbose --extensions com.github.adamyork.wiremock.transformer.VelocityResponseTransformer

Notes on velocity templates

  • all Generic Velocity tools are available to the Velocity context
  • request bodies are available via the context
  • query parameters via the context.
  • if multiple query parameters are found with the same name, they will referenced by a postfix; and index start at 0. see example below

Below is an example of a template, assuming a uri of "/resource?startDate=2018-02-01&endDate=2018-02-28&product-code=10&product-code=j1j1j1"

{
    "requestAbsoluteUrl" : "$requestAbsoluteUrl",
    "requestMethod" : "$requestMethod",
    "requestHeaderHost" : "$requestHeaderHost",
    "requestHeaderUserAgent" : "$requestHeaderUserAgent",
    "requestHeaderAcceptAccept" : "$requestHeaderAccept",
    "requestHeaderAcceptLanguage" : "$requestHeaderAcceptLanguage",
    "requestHeaderAcceptEncoding" : "$requestHeaderAcceptEncoding",
    "requestHeaderConnection" : "$requestHeaderConnection",
    "requestBody" : $requestBody,
    "requestBodySomeKeyValue" : "$requestBody.someKey",
    #if($requestAbsoluteUrl == 'http://localhost:8089/my/resource')
    "customProp" : "customValue",
    "customProp2" : "customValue2",
    #else
    "customProp" : "customValue",
    #end
    "date" : "$date.getMonth()",
    "math" : "$math.floor(2.5)",
    "startDate" : "$startDate",
    "endDate" : "$endDate",
    "productCode0" : "$productcode0",
    "productCode1" : "$productcode1"
}