All Articles

Setting up IntelliJ to Test Groovy Code in Boomi

intellij header

Boomi is a low code environment for integrations, which means that most situations will not require coding but occasionally it will be necessary. This article reviews how to setup a Groovy testing environment within Windows and code that you want to implement within Boomi. While Boomi can be helpful with testing, having an IDE will provide a substantially faster way to debug your Groovy code.

Install IntelliJ Community Edition IDE

The IntelliJ by JetBrains will be used as the IDE, integrated development environment. IntelliJ has a free and a paid version. We will be downloading the free community version. The Community Edition is generally located at the bottom of the page. Go ahead and install IntelliJ after downloading the install file. All of the defaults can be used during the installation, but change any settings that you feel are necessary. Once the install is complete, go ahead and open IntelliJ.

https://www.jetbrains.com/idea/download

JetBrains has a installation guide to installing IntelliJ. The installation guide also includes JetBrains Toolbox, which is a tool that can be used to manage all JetBrains products. The installation guide can be found at the link below.

https://www.jetbrains.com/help/idea/installation-guide.html

intellij install

Figure 1. IntelliJ Installation Link.

Once IntelliJ is open, select the New Project option.

intellij new project

Figure 2. New Project Option.

Provide a name to the project, the desired Location, and select Maven as the Build system. A suggested project name is ‘boomi-groovy’. Other build systems can be used, but the article covers Maven.

Next, select the Groovy option under New Project on the left. Select the dropdown next to JDK to download Java 11. Boomi currently uses Amazon Corretto Java 11 and it will be used for this project. Next to Groovy SDK, select download and then select the Groovy version 2.4.21.

intellij new project filled out

Figure 3. New Project with all fields populated.

intellij download java 11

Figure 4. Download Java 11

Once complete, click on Finish. Java 11 and Groovy will be downloaded, and the project will be created.

Set up folder structure

The following directories will be used within the project. The directories will be used to store the Groovy scripts and to store sample data files to be used within the Groovy scripts.

Create the following directory within the project. The input_files directory will be used to store sample data files that will be used within the Groovy scripts.

  • ./input_files

Create new scripts within ./src/main/groovy. New scripts can be created by righting clicking on the groovy directory and selecting New -> Groovy Script.

intellij folder structure

Figure 5. Folder Structure.

Add External Libraries

Open the pom.xml file that is within the project’s main directory. Replace the project/dependencies section of the XML and add the following dependencies. The groovy-all dependency should already be present, but the update removes the type=pom section. The boomi-groovy-debugger dependency will be used to mock the Boomi libraries used within the Data Process step and the map function.

    <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.21</version>
        </dependency>
        <dependency>
            <groupId>tech.adambedenbaugh</groupId>
            <artifactId>boomi-groovy-debugger</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

Once the pom file is updated, the external libraries can be downloaded by clicking on the Maven icon on the right side of the screen, and then clicking the refresh icon.

intellij maven refresh

Figure 6. Maven Refresh Dependencies.

When new dependencies are required, they they can be added to the pom.xml file and the Maven refresh icon can be clicked to download the new dependencies.

Additional information on the boomi-groovy-debugger library can be found at the links below.

Groovy Run Templates

Groovy runs within a JVM (Java Virtual Machine) and the default configurations need to be set. Select Run (top of the screen) -> Edit Configurations. Within the bottom left, click on “Edit configuration templates”. Select Groovy and then add the following to the VM options. The options below need to be set in the template. The PROJECT_DIR environment variable should be updated to the base directory of the project. This variable will be used within the Data Process step to set the base directory of where to find files to treat as inbound documents. The illegal-access option will be set to deny to avoid the illegal access warning that will be displayed when running a Groovy 2.4 script with Java 11.

intellij edit run config

Figure 7.: Edit Run Configurations.

intellij run configuration

Figure 8.: Edit Run Configuration.

VM options:
  --illegal-access=deny
Environment Variables:
  PROJECT_DIR=<add-your-base-directory>/IdeaProjects/boomi-groovy
Module
  boomi-groovy
JRE
  corretto-11 (select from dropdown)

Testing Scripts within IntelliJ

There are two locations within Boomi that you will use a Groovy script. The first location is within a map using the custom script function. The second location is inline within the process using a Data Process step. The Data Process step instantiating a dataContext class on the back end before any groovy code is executed within the Data Process step. The dataContext object needs to be either escaped or mock classes from an external library need to be used. Escaping the dataContext object is easy enough. The method below shows how to use test classes to better test your Groovy code.

Testing Scripts within IntelliJ within a Map

Testing a script that is meant for a map is generally straightforward because the map function sets input and output variables and there is a script in the middle. Variables that are set under input and output need to be declared within the script in IntelliJ, but should be removed when they are being added to the script map function within Boomi. Also, use println() to write the output to the console for testing. The println() method should be removed when you are running it in Boomi since it will have no effect.

Below is an example script that outputs the first initial and last name and would be used within a map function in Boomi. First create a new script within the ./src/main/groovy directory by right clicking on that directory and selecting Groovy script. Populate the script below within the new script. Next, click on Run (top of the screen) -> Run. The output will be displayed within the console. There is also a green play symbol on the upper-right side of the screen that can be used to run the script.

import java.util.Properties
import java.io.InputStream

// fullname needs to be set within IntelliJ but would be the input variable within Boomi
String fullname = “joe smith” 
int idx = fullName.lastIndexOf(' ')
// This will print to results to the Console but should be removed in Boomi.
println("idx: " + idx);

String firstName = fullName.substring(0,1);
String lastName = fullName.substring(idx + 1);

// This will print the results to the Console but should be removed within Boomi.
println ("firstName: " + firstName);
println("lastName: " + lastName);

// truncatedName would not need to be declared within Boomi because it will be set as an output variable.
String truncatedName = firstName + lastName + "@boomi.com"

// This will print the results to the Console but should be removed in Boomi.
println(truncatedName)

Inline Script with Data Process Steps

The inline Groovy script within the Data Process Steps is more involved because there are some parts of the script that need to use mock classes within IntelliJ. These mock classes are used to mimic the code being executed within the Data Process step before the script within the Data Process step is executed. They were added as an external library when the boomi-groovy-debugger library was added to the project.

Test Script for Data Process Steps

Now that we have IntelliJ prepped we can go ahead and write a Groovy script that will mimic exactly what you can put within Boomi. The ability to create a script within IntelliJ that will also run within Boomi is the real power in using this external library. Within the Groovy script below there are two parts. The first part will be specific only to IntelliJ and will not be copied to Boomi and the second part will be the portion that gets directly copied from IntelliJ and pasted into Boomi.

Before running the script, create a new file called emptyfile.txt within the previously created input_files directory.

// Use for testing. Remove when adding to Data Process step.
import com.boomi.execution.ContextCreator

// Place directory for multiple files and file name for single file
String pathFiles = "${System.getenv("PROJECT_DIR")}/input_files/emptyfile.txt"
println pathFiles
dataContext = new ContextCreator()
dataContext.AddFiles(pathFiles)
ExecutionUtil ExecutionUtil = new ExecutionUtil()

/* Add any Dynamic Process Properties or Dynamic Document Properties. If
   setting DDPs for multiple files, then index needs to be set for each and
   the index range starts at 0. */
ExecutionUtil.setDynamicProcessProperty("DPP_name", "DPP_value", false)
dataContext.addDynamicDocumentPropertyValues(0, "DDP_name", "DDP_value")


// Place script after this line.
//----------------------------------------------------------------------------------------------------


import java.util.Properties
import java.io.InputStream
import com.boomi.execution.ExecutionUtil

logger = ExecutionUtil.getBaseLogger();

// Example of how to use logger.info() to print to console for testing and to logs within Boomi.
logger.info("Number of documents: " + dataContext.getDataCount())

for (int i = 0; i < dataContext.getDataCount(); i++) {
    InputStream is = dataContext.getStream(i)
    Properties props = dataContext.getProperties(i)
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    int lineNum = 0
    while ((line = reader.readLine()) != null) {
        lineNum++
    }
    println lineNum

    // Set properties
    props.setProperty("document.dynamic.userdefined.lineCount", lineNum.toString())

    // Reset the InputStream data
    is.reset()
    dataContext.storeStream(is, props)
}

In the first section of the script before the dashes, the code is meant to mimic the code that is executed within the Data Process step before any added code is executed. A dataContext class is being instantiating, which will mimic Boomi, and setting any process properties or dynamic document properties that are expected to be used within the script. The pathFile variable can be set to a specific file or a directory if you want to call multiple files. Then the AddFiles() method is used to add files into dataContext. Once everything has been prepped, then the script after the long line of dashes can be created with exactly the same script that would be found within Boomi’s Data Process step. The script above brings in a flat file, counts the number of lines, and then sets the Dynamic Document Property lineCount with the number of lines within the document.

While you are writing code within IntelliJ you will want to write to the console with println() to see the output of a script. Writing to the console can be helpful for both the map and inline scripts.

Conclusion

Congratulations on setting up IntelliJ! You now have the tools to run and test your Groovy scripts locally, paving the way for a more efficient development process. This environment not only allows you to troubleshoot and optimize your code before adding it into Boomi, but it also opens up new avenues for creativity and innovation in your integration projects.

The article was originally posted at Boomi Community.

Published Sep 14, 2024

Developing a better world.© All rights reserved.