Commands

Command operations that can be used on Code expressions


So far, the following operations and attributes are available to usage on Code Expressions:

Attributes

Description Availability Usage Sample
The received request Always available "${request.queryParams}"
The declared global variables Available for usage on all Scenario's fields "${globalVariables.myVariable}"
The declared variables Available for usage on Callbacks, Commands and Response "${variables.myVariable}"
The declared response Available for usage on Callbacks and Commands "${response.body}"

IMPORTANT: There is a special case regarding attributes that are Maps, like request.queryParams, globalVariables, variables, etc. In this case if the map contains a key that have the same name of a Map method, like size(), it should be used this way:


Lambda

Copycat has a limited support for Java Lambda and requires a particular syntax for it to work. The following table contains the supported methods:

Function Syntax Usage Sample
java.util.function.Predicate.class predicate "${values.stream().filter(predicate((v)-> {v == variables.intValue}))}"
java.util.function.Function.class func "${values.stream().map(func((x)-> { x + x; }))}"
java.util.function.Consumer.class consumer "${values.stream().peek(consumer((x)-> {x.toString()}))}"
java.util.function.BinaryOperator.class binaryOperator "${values.stream().reduce(0, binaryOperator((a, b) -> { a + b }))}"
java.util.Comparator.class comparator "${values.stream().min(comparator((c1, c2) -> {c1 > c2 ? 1 : 0}))}"

Data Storage

Description Signature Usage Sample
Checks if the given collection have an entry with the given id boolean exists(String collectionName, Object id) "${db.exists('users', 1}"
Gets the value stored for the given collection/id Object get(String collectionName, Object id) "${db.get('users',1}"
Gets the fist element of the given collection that matches the given filters Object getBy(String collectionName, Map fieldFilters) "${db.getBy('users', {'username': request.body.username})}"
List all elements of the given collection List<?> list(String collectionName) "${db.list('users'}"
List all elements of the given collection that matches the given filter List<?> listBy(String collectionName, Map fieldFilters) "${db.listBy('users', request.queryParams}"
Saves the given object on the given collection using the next numerical sequential id. The document will be deleted after 30 days save(String collectionName, Object value) "${db.save('users', request.body}"
Saves the given object on the given collection using the given id. The document will be deleted after 30 days save(String collectionName, Object id, Object value) "${db.save('users', response.body.id, response.body}"
Saves the given object on the given collection using the given id. The document will be deleted after the informed seconds. A ttlSeconds <= 0 or null means that the document will never expire save(String collectionName, Object id, Object value, Long ttlSeconds) "${db.save('users', response.body.id, response.body, 15}"
Updates the given object changing only the attributes that were informed on the "value" parameter. Works like a Patch. If you want to replace the value, use the save method. The TTL will not be changed Object update(String collectionName, Object id, Object value) "${db.update('users', response.body.id, response.body}"
Updates the given object changing only the attributes that were informed on the "value" parameter. Works like a Patch. If you want to replace the value, use the save method. The TTL will be updated with the new value Object update(String collectionName, Object id, Object value, String ttlSeconds) "${db.update('users', response.body.id, response.body, 15}"
Updates the given object changing only the expiration date. A ttlSeconds <= 0 or null means that the document will never expire Object updateTTL(String collectionName, Object id, Long ttlSeconds) "${db.updateTTL('users', response.body.id, 15}"
Permanently deletes the entry on the given collection delete(String collectionName, Object id) "${db.delete('users', request.pathParams.idUser)}"

Id Generation

Description Signature Usage Sample
Gets the next id for the given collection Long seqId(String collectionName) "${id.seqId('users')}"
Generates an uuid with dashes String uuid() "${id.uuid()}"
Generates an uuid without dashes String uuidWithoutDashes() "${id.uuidWithoutDashes()}"
Generates an random numeric id with the given max length long numericId(int length) "${id.numericId(8)}"
Generates an random string id with the given max length String stringId(int length) "${id.stringId(8)}"

Format

Description Signature Usage Sample
Formats a String String string(String format, Object... params) "${format.string('username %s is not available',request.body.username)}"
Formats a string filling it with chars on the left side until meets the specified length String stringLeftPad(String format, int length, String padStr, Object... args) "${format.stringLeftPad('Number with fixed length: %s', 5, '0', request.body.amount)}"
Formats a string filling it with chars on the right side until meets the specified length String stringRightPad(String format, int length, String padStr, Object... args) "${format.stringRightPad('Number with fixed length: %s', 5, '0', request.body.amount)}"
Formats a string like 1000 into 10,00 String money(String value, String separator) "${format.money(request.body.amount, ',')}"
Returns the the time of the server formatted as the specified on format param. It uses DateTimeFormatter.ofPattern(format) internally String now(String format) "${format.now('MMdd')}"
Returns the the time of the server, adjusted to the given zone, formatted as the specified on format param. It uses DateTimeFormatter.ofPattern(format) internally String now(String format) "${format.now('MMdd', 'America/Sao_Paulo')}"