Skip to Content
Technical Articles
Author's profile photo SHOAIB Alam

Testing Groovy Script Locally using Intellij IDEA

SAP Integration Suite is an integration platform as a service (iPaaS) that supports cloud-to-cloud, cloud-to-on-premise, on-premise-to-on-premise, and B2B integration patterns. It provides a feature-rich and comprehensive capabilities to meet the modern integration requirements. It comes with large number of built-in components such as routing, message transformation and translation, and persistence steps to realise various integration patterns. It also provides various adapters to facilitates integration between SAP and non-SAP applications – both cloud and on premise.

SAP Integration Suite comes with browser-based UI interface which is used for discovery, design, and administration of integration artifacts. An integration flows are the basic design artifacts for processing messages. An integration developer can intuitively define the way messages should be processed in a graphical modelling environment. They are modelled as integration iFlows. A wide range of iFlow steps such as router, aggregator splitter etc. are available for developers to implement various enterprise integration patterns (EIPs).

Among these many iFlow steps available in SAP Integration Suite, one most important one is the scripting step. It allows the developer to write custom coding block in either Groovy or JavaScript. As I came from java and webMethods background it was smoother learning curve for me from Java to Groovy.

Groovy enables the developers to write custom step to address complex functionality such as:

  • Performing custom logging and exception handling
  • Sending alerts or generating service tickets
  • Complex message transformation in message mapping by writing custom groovy functions
  • Reading and/or setting values in a message’s header or property based on custom logic

To write Groovy script, I need to answer two basic but important questions:

  1. What are the IDE options available to write Groovy script? and
  2. How can test my Groovy script?

To answer first question there are various options available e.g. write or import script file in iFlow or use online IDE at https://groovyide.com/cpi. It is handy but if you want to do any serious development/testing/debugging work then you will be struggling. The other two options which provide full stack development tools and address the testing and debugging requirements are Eclipse or Intellij IDEA from JetBrains.

What I found is to set up Eclipse or Intellij for SAP CPI development is not straight forward process. I followed the following steps to setup both Eclipse and Intellij IDEA community edition. There are some differences in setting up both IDEs so I will go through Intellij in this article and Eclipse in next article.

 

Step 1: Find the correct version of Java, Groovy and Apache Camel

SAP Integration Suite runtime environment uses Java and Apache Camel integration framework.  It’s important to ensure that Java, Apache Camel and Groovy libraries versions that are used in SAP Integration Suite is in sync with those used in your IDE. If IDE is using a newer version of these libraries, then locally developed Groovy scripts are likely to fail at the SAP Integration Suite runtime.

A version of the Groovy library that is used in runtime can be identified with the help of the standard GDK API by calling the getVersion() method of the groovy.lang.GroovySystem class.

You can also find the Java version and Apache Camel version by calling the getProperty(String) method of the java.lang.System class and getVersion() method of CamelContext respectively.

Add a new Integration Flow, add Groovy Script Step, and add the following Groovy code:

import com.sap.gateway.ip.core.customdev.util.Message;
import org.apache.camel.impl.*

def Message processData(Message message) {

        def camelContext = new DefaultCamelContext()
        StringBuilder versions = new StringBuilder()
        versions << "Groovy: ${GroovySystem.getVersion()}\n"
        versions << "Java: ${System.getProperty('java.version')}\n"
        versions << "Camel: ${camelContext.getVersion()}"
        message.setBody(versions.toString())
        return message

}

 

Fig%201%3A%20Get%20versions%20iFlow

Fig 1: Get Versions for iFlow

 

Save and deploy the Integration Flow. Once it is started use Postman to call the flow:

Fig%202%3A%20Postman

Fig 2: Postman Result

 

It gives the following versions for Groovy, java and Apache Camel

  • Groovy: 2.4.21
  • Java: 1.8.0_311
  • Camel: 2.24.2-sap-23

 

Step 2: Download and install Intellij IDEA Community Edition

This article explains how to configure Intellij IDEA community edition. I am not going to describe in detail the installation process of IntelliJ IDEA. Instead, I assume you’re familiar with the core features and capabilities of IntelliJ IDEA and will focus on how IntelliJ IDEA (Community Edition) can be used for Groovy scripts development and testing for SAP Integration Suite.

Before installing Intellij make sure that java 1.8 is installed and configured. It not the latest version of java but version used by SAP Integration Suite runtime. Then download Intellij IDEA community edition from the following URL and install it.

https://www.jetbrains.com/idea/download/#section=windows

Installation steps are straight forward, and I am not going to go through them.

 

Step 3: Create Groovy Project and Script

Apache Groovy version 2.4.21 is required to setup a project in Intellij. This can be downloaded from the maven repository https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all/2.4.21. Or it can be downloaded and added to the project as a part of the Intellij project which I explain later.

Once you install Intellij successfully you’re ready to create your first the Groovy project. I will create a Groovy project with the help of the project wizard. To do so, launch IntelliJ IDEA, and follow these steps:

 

  1. If there are no opened projects, IntelliJ IDEA presents a Welcome screen on which you select Create New Project. If IntelliJ IDEA has already been in use and there are any opened projects, the earlier created project will be opened instead. If this happens, choose File à New à Project in the menu bar.
  2. In the New Project window select Groovy from a list of available types of projects. Make sure that Project SDK is selected as 1.8.xxx and select the Groovy library 2.4.21.

Fig%203%3A%20Groovy%20setup

Fig 3: Groovy Library Setup

Note: If Groovy library is not downloaded before you can download the correct version of Groovy Library. Your machine must be connected to internet.

Fig%204%3A%20Groovy%20Library%20Download

Fig 4: Groovy Library Download

 

Make sure that the correct Groovy library version is selected i.e. 2.41.2 and depending upon your choice you can select Level as Project Library or Global Library.

Click Next and choose any suitable name of the project e.g. sap-cpi-groovy. Click Finish to create the project.

Optional: Modify the project structure and add src/main/groovy and src/main/test directories.

Now create a groovy script myFirstScript in a src/main/groovy folder.

Fig%205%3A%20myFirstScript

Fig 5: myFirstScript

Add the following code to the groovy script. It is an auto generated code when you create groovy step in CPI.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Body 
       def body = message.getBody();
       message.setBody(body + "Body is modified");
       //Headers 
       def map = message.getHeaders();
       def value = map.get("oldHeader");
       message.setHeader("oldHeader", value + "modified");
       message.setHeader("newHeader", "newHeader");
       //Properties 
       map = message.getProperties();
       value = map.get("oldProperty");
       message.setProperty("oldProperty", value + "modified");
       message.setProperty("newProperty", "newProperty");
       return message;
}

By running the above code, you will get the following error:

Fig%206%3A%20Message%20Class%20Not%20Found%20Error

Fig 6: Message Class Not Found Error

 

Step 4: Download and add Script API jar to project

In order to resolve the class com.sap.gateway.ip.core.customdev.util.Message you need to download the Script API from the following URL:

https://tools.hana.ondemand.com/#cloudintegration

Fig%207%3A%20Script%20API%20Library

Fig 7: Script API Library

 

Download and save the cloud.integration.script.apis-2.7.1.jar file in a directory. Now include this jar file by going to File à Project Structure and Library

Fig%208%3A%20Update%20Project%20Library

Fig 8: Update Project Library

 

Now run the myFirstScript.groovy again the class com.sap.gateway.ip.core.customdev.util.Message is resolved and you will see no error.

Fig%209%3A%20Result

Fig 9: No Error Message

 

Unfortunately, this approach only gives you a real bare bone JAR file and pretty much useless.

Step 5: Explore SAP Integration Suite filesystem content and download the Jars

If you want to do any serious development and testing work locally in Intellij or Eclipse you need to download corresponding jar files from SAP Integration Suite. Fortunately Ariel Bravo Ayala wrote a blog post which explain how to explore the SAP Integration Suite filesystem.

https://blogs.sap.com/2019/01/03/exploring-cpis-filesystems-content/

You need to make the two changes mentioned by Manish Kumar in his comments to make it working. Following the instructions in the blog and comments, deploy the iFlow. Once it is running explore the filesystem using browser:

Fig%2010%3A%20CPI%20Filesystem

Fig 10: CPI Filesystem

 

Navigate to /app/webapps and download stack.worker_cf.karaf-6.xx.x.war file. Unzip this file in a directory and it will provide all the required jars.

Fig%2011%3A%20Partial%20List%20of%20jar%20Files

Fig 11: Partial List of jar Files

 

 

Step 6: Setup IDE for testing groovy code

Now you have all the required jars for developing and testing your groovy code for SAP Integration Suite. Eng Swee Yeoh wrote an article about how to test groovy code in your IDE https://blogs.sap.com/2017/10/06/how-do-you-test-your-groovy-scripts/.

You can use the Test Script Example from the above article and create a testMyFirstScript.groovy:

Fig%2012%3A%20Groovy%20Test%20Code

Fig 12: Groovy Test Code

When you run the testMyFirstScript.groovy it will fail with the following error:

Fig%2013%3A%20Class%20Not%20Found%20Error

Fig 13: Class Not Found Error

Now the it is the time where fun really begins. You need to find out all required JAR file, then add it to the project’s build path. It is an iterative process and will take some time to find all the required jars. I found the following list of jars files from stack.worker_cf.karaf-6.xx.x.war required to run the testMyFirstScript.groovy code.

  • sap.it.script.custom-development
  • sap.cloud.adk.api
  • apache.camel.camel-core
  • sap.it.script.script.engine.api
  • sap.it.commons
  • sap.it.commons.logging.slf4j

Once you got all the error sorted, try executing the testing script: –

Fig%2014%3A%20Test%20Result

Fig 14: Test Result

Conclusion

I tried to illustrate how a local IDE (IntelliJ IDEA) can be used for development of Groovy scripts.
By using Script API it is possible to setup development environment but unfortunately this approach only gives a real bare bone JAR file and pretty much useless for testing and debugging purpose.

This article explains how to get the correct version of jars required to setup IntelliJ IDEA for developing, testing and debugging groovy code. Initial setup might seem a little bit lengthy and cumbersome but it is a one-off activity. Once completed, you can benefit from the productivity boost provided by the usage of an IDE. It will enhance development experience – code inspection and completion, and most importantly testing and debugging.

This blog post serves as an introduction to the usage of IntelliJ. To setup with Eclipse is almost similar.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Daniel Graversen
      Daniel Graversen

      Hi Shoaib

      Great post. Yes IntelliJ makes some things a lot easier to test and debug.

      I would recommned having multiply packages, and iflows into one project then it becomes a lot easier to manage.

       

      We have implemented a feature in our Figaf tool to simplify the process about running the plugins as easy as possible. Check it out at SAP CPI and GIT integration - Figaf

      We have among things implemed mocks for SAPs objects so you can run it locally wihtout having to download artifacs from CPI system.

       

      Author's profile photo Manish Kumar
      Manish Kumar

      Hello,

      Great post to start the work with Intellij IDEA and groovy testing. Below are few hints that helped me.

      Sending input file:

      You would need to use org.apache.camel.Exchange to set the input file and pass that to com.sap.gateway.ip.core.customdev.util.Message

       

      Library that I used for running CPI groovy: