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

Testing Groovy Script Locally using Eclipse

This is the continuation of my blog post “Testing Groovy Script Locally using Intellij IDEA”.  In which I tried to explain step-by-step process to set up Intellij IDEA for Testing Groovy script locally. Another popular feature-rich IDE is Eclipse. There are some differences in setting up Eclipse IDE. In this blog post I will go through these steps.

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

Follow the step 1 in a “Testing Groovy Script Locally using Intellij IDEA” and find out the correct version of Groovy, Java and Camel:

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

Step 2: Download and install Eclipse

Before downloading the Eclipse find out the Latest Groovy release and corresponding Eclipse Level from the URL https://github.com/groovy/groovy-eclipse/wiki.   For example, I am using the eclipse level (2021-12).

Find and download the correct level of Eclipse from https://www.eclipse.org/downloads/packages/installer. You can either download the Eclipse as Installer or as Package. Find out Eclipse 2021-12 (4.22) and download the Installer.

Before installing Eclipse make sure that java 1.8 is installed and configured. It is not the latest version of java but version used by SAP CPI runtime.

I downloaded the Installer and install “Eclipse IDE for Java Developers”. Installation steps are straight forward, and I am not going to go through them.

Step 3: Setup Eclipse and Install Groovy Support

From the following https://github.com/groovy/groovy-eclipse/wiki copy the corresponding URL for Groovy release.

 

Once you have Eclipse installed and running copy the corresponding Groovy URL, then follow these steps:

  1. Go to Help > Install New Software…
  2. Click on Add…
  3. Paste the update site URL appropriate for your version of Eclipse and click Add
  4. Select the new entry in the Work with list of update sites
  5. Expand Main Package and select the Eclipse Groovy Development Tools feature

Click Next and follow the remaining prompts

Step 4: Create Groovy Project and Script

Once Eclipse is installed and restarted create a New Groovy Project:

  • File –> New –> Project –> Groovy Project

Let’s project name is com.groovy.testing. Now create a New Groovy Type under the project:

Let’s name of the Script is GroovyCPITest

 

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.processor.MessageImpl
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;
}

 

 

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

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

But this approach only gives you a real bare bone JAR file and it is pretty much useless.

As I explained in Step 5 of my post “Testing Groovy Script Locally using Intellij IDEA” download stack.worker_cf.karaf-6.xx.x.war file. Unzip this file in a directory and it will provide all the required jars. For further details, Ariel Bravo Ayala wrote a blog post which explain how to explore the CPI 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 work.

Step 5: Setup Eclipse for testing groovy code

Now you have all the required jars for developing and testing your groovy code for CPI. 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/.

Create a new script.groovy file with a script in step 4 using any text editor and save it.  You can use the GroovyCPITest Script Example created above or create a new. Use the code from the above-mentioned blog by Eng Swee Yeoh to test your script.

import com.sap.gateway.ip.core.customdev.processor.MessageImpl
import com.sap.gateway.ip.core.customdev.util.Message

// Load Groovy Script
GroovyShell shell = new GroovyShell()
def script = shell.parse(new File("script.groovy"))

Message msg = new MessageImpl()
msg.setBody(new String("Hello Groovy World "))
msg.setHeader("oldHeader", "MyGroovyHeader")
msg.setProperty("oldProperty", "MyGroovyProperty")

script.processData(msg)

println("Body:\r\n" + msg.getBody())

def displayMaps = { String mapName, Map map ->
	println mapName
	map.each { key, value -> println( key + " = " + value) }
}
displayMaps("Headers:", msg.getHeaders())
displayMaps("Properties:", msg.getProperties())

You need to find out all required JAR files to resolve com.sap.gateway.ip.core.customdev.processor.MessageImpl & com.sap.gateway.ip.core.customdev.util.Message.

I found the following list of jars files from stack.worker_cf.karaf-6.xx.x.war required to run the 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

I also downloaded and add the following 3 jars to run it successfully:

  • slf4j-api-1.7.2.jar
  • slf4j-simple-1.7.2.jar
  • activation.jar

Now everything set to test your Groovy Code using Eclipse IDE. You can run the test either as a Java Application or via Groovy Console.

Following is the Groovy Console output:

Conclusion

This is a continuation of my previous post in which I tried to explain how to setup Intellij IDE for local development and testing of Groovy Code. Eclipse is another feature rich and popular IDE. In this blog I tried to illustrate how to use Eclipse IDE to development and test Groovy scripts.

There are some differences in setting up Intellij and Eclipse. For example, Eclipse requires additional activation.jar library as compared to Intellij.

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 of your choice either Intellij or Eclipse. It will enhance development experience – code inspection and completion, and most importantly testing and debugging.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.