-
Notifications
You must be signed in to change notification settings - Fork 0
Response Templating
Most of the fields in your payload would be random data (for the testing purposes, of course). For example, phone numbers, current timestamps, future dates, invoice numbers, order numbers, service request ids, UUIDs
etc need to be generated randomly using the testing tool. With response templating you can keep your scripts clean and without parameter files containing just random data.
You would still need to use your testing tools' APIs to use a seeded data source, for example if you want to use CSVs or .dat files. But if you just need a 10 digit random number, Kafka Service is all you need.
Currently Kafka Service supports all of the String Helpers provided by Handlebar.java (though few of those helpers might not make sense in this context). There are few custom helpers provided by Wiremock which can be used with Kafka Service.
Of course, pull requests are welcome in case any additional helpers are required.
Suppose you want to send following message to Kafka:
{
"OrderCreatedAt": 1616320755763,
"CustomerPhoneNuber": 1234567890
}
But you want to update these values for every message you send. You'd usually use your testing tool to generate random values and current timestamp for you. But you can also format POST body for any of the above APIs as:
{
"message": "{ \"OrderCreatedAt\": {{now format='epoch'}}, \"CustomerPhoneNuber\": {{randomValue length=10 type='NUMERIC'}} }"
}
and you don't need to create parameters in your test tool, Kafka Service will handle that for you.
Following are few examples to help you get a sense of how templating works. Note that these examples have shamelessly been copied from Wiremock's documentation and Handlebar.java documenation:
- capitalizeFirst
Capitalizes the first character of the value. For example:
{{capFirst value}}
If value is "handlebars.java", the output will be "Handlebars.java".
- center
Centers the value in a field of a given width. For example:
{{center value size=19 [pad="char"] }}
If value is "Handlebars.java", the output will be " Handlebars.java ".
- cut
Removes all values of arg from the given string. For example:
{{cut value [" "]}}
If value is "String with spaces", the output will be "Stringwithspaces".
- defaultIfEmpty
If value evaluates to False, uses the given default. Otherwise, uses the value. For example:
{{defaultIfEmpty value ["nothing"] }}
If value is "" (the empty string), the output will be nothing.
- join
Joins an array, iterator or an iterable with a string. For example:
{{join value " // " [prefix=""] [suffix=""]}}
If value is the list ['a', 'b', 'c'], the output will be the string "a // b // c".
- Or:
{{join "a" "b" "c" " // " [prefix=""] [suffix=""]}}
Join the "a", "b", "c", the output will be the string "a // b // c".
- ljust
Left-aligns the value in a field of a given width. Argument: field size For example:
{{ljust value 20 [pad=" "] }}
If value is Handlebars.java, the output will be "Handlebars.java ".
- rjust
Right-aligns the value in a field of a given width. Argument: field size For example:
{{rjust value 20 [pad=" "] }}
If value is Handlebars.java, the output will be " Handlebars.java".
- substring
Returns a new CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1 Argument: start offset end offset For example:
{{substring value 11 }}
If value is Handlebars.java, the output will be "java". or
{{substring value 0 10 }}
If value is Handlebars.java, the output will be "Handlebars".
- lower
Converts a string into all lowercase. For example:
{{lower value}}
If value is 'Still MAD At Yoko', the output will be 'still mad at yoko'.
- upper
Converts a string into all lowercase. For example:
{{upper value}}
If value is 'Hello', the output will be 'HELLO'.
- slugify
Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace. For example:
{{slugify value}}
If value is "Joel is a slug", the output will be "joel-is-a-slug".
- stringFormat
Formats the variable according to the argument, a string formatting specifier. For example:
{{stringFormat string param0 param1 ... paramN}}
If value is "Hello %s" "handlebars.java", the output will be "Hello handlebars.java".
- stripTags
Strips all [X]HTML tags. For example:
{{stripTags value}}
- capitalize
Capitalizes all the whitespace separated words in a String. For example:
{{ capitalize value [fully=false]}}
If value is "my first post", the output will be "My First Post".
- abbreviate
Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence ("..."). Argument: Number of characters to truncate to For example:
{{abbreviate value 13 }}
If value is "Handlebars rocks", the output will be "Handlebars...".
- wordWrap
Wraps words at specified line length. Argument: number of characters at which to wrap the text For example:
{{ wordWrap value 5 }}
If value is Joel is a slug, the output would be:
Joel
is a
slug
- replace
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. For example:
{{ replace value "..." "rocks" }}
If value is "Handlebars ...", the output will be "Handlebars rocks".
- yesno
Maps values for true, false and (optionally) null, to the strings "yes", "no", "maybe". For example:
{{yesno value [yes="yes"] [no="no"] maybe=["maybe"] }}
- dateFormat
Usage:
{{dateFormat date ["format"] [format="format"][tz=timeZone|timeZoneId]}}
Format parameters is one of:
-
"full": full date format. For example: Tuesday, June 19, 2012
-
"long": long date format. For example: June 19, 2012
-
"medium": medium date format. For example: Jun 19, 2012
-
"short": short date format. For example: 6/19/12
-
"pattern": a date pattern.
-
Otherwise, the default formatter will be used. The format option can be specified as a parameter or hash (a.k.a named parameter).
-
numberFormat
Usage:
{{numberFormat number ["format"] [locale=default]}}
Format parameters is one of:
-
"integer": the integer number format
-
"percent": the percent number format
-
"currency": the decimal number format
-
"pattern": a decimal pattern.
-
Otherwise, the default formatter will be used.
More options:
-
groupingUsed: Set whether or not grouping will be used in this format.
-
maximumFractionDigits: Sets the maximum number of digits allowed in the fraction portion of a number.
-
maximumIntegerDigits: Sets the maximum number of digits allowed in the integer portion of a number
-
minimumFractionDigits: Sets the minimum number of digits allowed in the fraction portion of a number
-
minimumIntegerDigits: Sets the minimum number of digits allowed in the integer portion of a number.
-
parseIntegerOnly: Sets whether or not numbers should be parsed as integers only.
-
roundingMode: Sets the RoundingMode used in this NumberFormat.
-
randomValue: Random strings of various kinds can be generated:
{{randomValue length=33 type='ALPHANUMERIC'}}
{{randomValue length=12 type='ALPHANUMERIC' uppercase=true}}
{{randomValue length=55 type='ALPHABETIC'}}
{{randomValue length=27 type='ALPHABETIC' uppercase=true}}
{{randomValue length=10 type='NUMERIC'}}
{{randomValue length=5 type='ALPHANUMERIC_AND_SYMBOLS'}}
{{randomValue type='UUID'}}
- date/parseDate
{{date (parseDate '03/21/2021') offset='-1 days'}}
- now (overwrites 'now' provided by StringHelpers in the documentation above)
{{now}}
{{now offset='3 days'}}
{{now offset='-24 seconds'}}
{{now offset='1 years'}}
{{now offset='10 years' format='yyyy-MM-dd'}}
{{now timezone='Australia/Sydney' format='yyyy-MM-dd HH:mm:ssZ'}}
{{now offset='2 years' format='epoch'}}
{{now offset='2 years' format='unix'}}
- trim
{{trim ' Text with whitespaces '}}
- base64
{{base64 'value to encode'}}
{{base64 'aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1kUXc0dzlXZ1hjUQ==' decode=true}}
- urlEncode
{{urlEncode 'value to encode'}}
{{urlEncode 'value%20to%20decode' decode=true}}
- size
{{size 'abcde'}}
- pickRandom
{{{pickRandom '1' '2' '3'}}}