A string value can be seen as an array of characters and if we want to transform our string value to an array we can use the toArray
function in the dw::util::Coercions
module. Once we have transformed our string to an array we can use all functions that work on arrays. The nice thing about DataWeave is that some functions that work on arrays already have an overloaded version that accepts a string value. Then we don’t have to explicitly use the toArray
function, but we can simply use our original value when we invoke the function.
Continue reading →
We can turn a URL string value into an object with fields that contain information about the URL using the parseURI
function. This function is part of the code dw::core::URL
module. The returned object is of type URI
and has fields like host
, path
, query
, scheme
and more.
Continue reading →
In DataWeave we can use expressions in strings that will be evaluated and inserted into the string value. This is called string interpolation. The expression must be enclosed in parentheses where the first parenthesis is prefixed with a dollar sign: $(<expression>)
. The expression must return a string value or can be automatically coerced into a string value in order for it to work. The expression can also be a variable.
Continue reading →
In DataWeave we can calculate the average for an array of numbers with the avg
function. If we want to calculate the average for other types in an array or object if we first transform the source object to an array of numbers.
Continue reading →
To get a v4 UUID in DataWeave we must use the function uuid
from the dw::Core
module. The function simply returns a value that is created using random numbers. So each time we invoke the function we will get a different value.
Continue reading →
DataWeave has a standard function to join array elements into a string value: joinBy
in the dw::Core
module. The joinBy
function takes an array as first argument and a string value that is used as separator. The array should have string values or values that can be coerced into string values.
Continue reading →
If we want to wrap a string value with another string value we can use the wrapWith
and wrapIfMissing
functions defined in the dw::core::Strings
module. The first argument of these functions is the string we want to wrap and the second argument is the string value that wraps the first argument. The function wrapIfMissing
will only apply the wrapper string if it was not already applied.
To remove a wrapped character from a wrapped string we can use the unwrap
function. The first argument is the string value that is already wrapped and the second argument the character we want to use for unwrapping. The second argument can only be a single character, but we can repeatedly invoke the unwrap
function to remove multiple wrap characters.
Continue reading →
DataWeave has several function to find the highest or lowest value in an array. The items in the array need to be of the same type and implement the Comparable
interface. To find the highest value we can use the max
function. The function returns the maximum value based on the type of the item in the array. If we want to find the highest value based on other criteria we can use the maxBy
function. This function takes a lambda as last argument where we can return a Comparable
value that is used to determine the highest value. To find the lowest value in an array we have similar functions: min
and minBy
.
Continue reading →
DataWeave 2.4 introduced the reverse
function in the Strings
module. With this function we reverse the string value that we pass in as argument. The result is the reversed string.
Continue reading →
The Strings
module in DataWeave has some very useful functions to work with string values. For example to repeat a string value we can use the repeat
function. The first argument is the string value we want to repeat and the second argument the number of times the value must be repeated.
Continue reading →
Testing classes that work with date calculations based on the current date and time (now) can be difficult. First of all we must make sure our class under test accepts a java.time.Clock
instance. This allows us to provide a specific Clock
instance in our tests where we can define for example a fixed value, so our tests don’t break when the actual date and time changes. But this can still not be enough for classes that will behave different based on the value returned for now. The Clock
instances in Java are immutable, so it is not possible to change the date or time for a Clock
instance.
In Spock 2.0 we can use the new MutableClock
class in our specifications to have a Clock
that can be used to go forward or backward in time on the same Clock
instance. We can create a MutableClock
and pass it to the class under test. We can test the class with the initial date and time of the Clock
object, then change the date and time for the clock and test the class again without having to create a new instance of the class under test. This is handy in situations like a queue implementation, where a message delivery date could be used to see if messages need to be delivered or not. By changing the date and time of the clock that is passed to the queue implementation we can write specifications that can check the functionality of the queue instance.
Continue reading →
In Spock we can also get a hold on the arguments that are passed to method call of a mock and we can write assertions to check the parameters for certain conditions.
When we create a mock in Spock and invoke a method on the mock the arguments are matched using the equals() implementation of the argument type. If they are not equal Spock will tell us by showing a message that there are too few invocations of the method call. Let’s show this with an example. First we create some classes we want to test:
Continue reading →