Introduction
Sometimes you need to provide a snippet or a small code that does something and give the opportunity to modify it on the fly, or allow users to customize it to fit their needs. In the case of Java, Groovy is a good starting point for this. As in the following example, a small code is created in Groovy with the possibility of inserting it into a Java Code or calling it from JMeter, Jenkins, etc.
NOTE: Add Groovy framework in classpath or use maven, gradle or similar when compiling and running java code.
Process
1.- Create, as an example, a small Groovy code to generate different Spanish national numbers. Because it is such a simple code, it can be customized to suit your needs even after you have packaged the Java application.
/**
* Spanish Id generator on the fly
*
* @param withTimestamp If true use the timestamp minus last two digits to generate DNI
* @return current Spanish id in a string
*/
def randomDni(boolean withTimestamp = true) {
def possibleLetters = ["T", "R", "W", "A", "G", "M", "Y", "F", "P", "D", "X", "B", "N", "J", "Z", "S", "Q", "V", "H", "L", "C", "K", "E", "T"]
Random rand = new Random()
def dniNumber = Integer.valueOf(new Date().getTime().toString().substring(2, 10))
if (!withTimestamp) {
def lowerLimit = 10000000
def upperLimit = 99999999
dniNumber = Math.abs(rand.nextInt() % (upperLimit - lowerLimit) + lowerLimit)
}
def letterDni = possibleLetters[dniNumber % 23]
"${dniNumber}${letterDni}".toString()
}
def dni = randomDni(true)
def dni2 = randomDni(false)
assert Objects.nonNull(dni)
assert dni.size() > 0
assert Objects.nonNull(dni2)
assert dni.size() > 0
return this
2.- Save as /path/to/random_spanish_id.groovy.
3.- Check it in groovyConsole as follows:

4.- Call it from Java using a similar code like follows:
import groovy.lang.GroovyShell;
import java.util.Objects;
public class net.sxgio.example.Runner {
public static void main(String[] args) {
String groovyScriptDir = "/path/to/script/";
if (Objects.nonNull(System.getProperty("profile.groovy.dir"))) {
groovyScriptDir = System.getProperty("profile.groovy.dir");
}
String value =
(String)
(new GroovyShell()
.parse(new File(groovyScriptDir + "path.to.random_spanish_id.groovy"))
.invokeMethod("randomDni", true));
System.out.println("Value : " + value);
}
}
NOTE: Use an environment parameter named as profile.groovy.dir (o passed with -D) to locate where your script is.
5.- Modify it to provide a JSON version
/**
* Spanish Id generator on the fly
*
* @param withTimestamp If true use the timestamp minus last two digits to generate DNI
* @return current Spanish id in a string
*/
def randomDni(boolean withTimestamp = true) {
def possibleLetters = ["T", "R", "W", "A", "G", "M", "Y", "F", "P", "D", "X", "B", "N", "J", "Z", "S", "Q", "V", "H", "L", "C", "K", "E", "T"]
Random rand = new Random()
def dniNumber = Integer.valueOf(new Date().getTime().toString().substring(2, 10))
if (!withTimestamp) {
def lowerLimit = 10000000
def upperLimit = 99999999
dniNumber = Math.abs(rand.nextInt() % (upperLimit - lowerLimit) + lowerLimit)
}
def letterDni = possibleLetters[dniNumber % 23]
"{\"id\": \"${dniNumber}${letterDni}\"}".toString()
}
def dni = randomDni(true)
def dni2 = randomDni(false)
assert Objects.nonNull(dni)
assert dni.size() > 0
assert Objects.nonNull(dni2)
assert dni.size() > 0
return this
6.- Run again, no recompiling and/or packaging required and verify the results from the same Java application.