Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
toonvl
Discoverer

This blog is based mostly on shoaib-alam's blog https://community.sap.com/t5/technology-blogs-by-members/testing-groovy-script-locally-using-eclipse... , based on what I did to set up a new environment from scratch in 2024, as some of the links and blogs were now outdated, and everything was spread over several blogs.. this blog will combine all in one.

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

}

 

 

 

Add a timer, deploy, and use a trace in combination with CPI helper to get the result without effort

iFlow to get versions from CIiFlow to get versions from CI

 

 

version flow resultversion flow result

Step 2: Install Eclipse and Groovy Developer Tools

For this environment, I used the following versions:

Eclipse 2021-12 (4.22) For Java Developers https://www.eclipse.org/downloads/packages/release/2021-12/r/eclipse-ide-java-developers
Download & install this Eclipse version

After Eclipse is installed - start Eclipse and install Groovy Developer Tools:
Go to 'Help' > 'Install New Software..'
Click 'Add..'
Enter 'Groovy Developer Tools' as name
Location https://groovy.jfrog.io/artifactory/plugins-release/e4.22

You can also use a different version of Eclipse, as long as you set the project Java VM version to a Java 8 JDK.
If you used a different version of Eclipse, you can find a corresponding version of Groovy Developer Tools here: wiki

add new repository to Eclipse for Groovy Developer Toolsadd new repository to Eclipse for Groovy Developer Tools

Install the Main Package

toonvl_3-1706307573341.png

Step 3: Get all the required jar files

This step involves another integration flow in Cloud Integration, to explore and download the files we need from Cloud Integration itself

You can either download the flow from here and import it: CI Filesystem Explorer.zip
Or you can create it yourself with these instructions:

toonvl_4-1706308153858.png

You need 2 integration processes, one endpoint to allow browsing, and one for viewing or downloading the files.

Create a script and paste in the following code:

 

 

 

import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap

def Message browse(Message message) {
    def mapHeaders = message.getHeaders()
    
    String mainDir = mapHeaders.get("CamelHttpPath") 
    String mainUrl = mapHeaders.get("CamelHttpUrl") 
    String mainSrv = mapHeaders.get("CamelServletContextPath")
    String srvUrl  = mainUrl.minus(mainDir)
    String urlDown = srvUrl.minus(mainSrv)+"/amba/cpifilesystemdownload"
    
    File dir = new File(mainDir)
    StringBuilder strBuilder = new StringBuilder()
    strBuilder << '<meta charset="UTF-8"></br>'
    if (dir.parent != null){strBuilder << "<a href='${srvUrl}${dir.parent}'>&#x1F4C1 ..</a></br>"}
    dir.eachDir {strBuilder << "<a href='${mainUrl}/${it.name}'>&#x1F4C1 ${it.name}</a></br>"}
    dir.eachFile {if (it.isFile()) {strBuilder << "<a href='${urlDown}/${mainDir}/${it.name}'>${it.name}</a></br>" }}
    message.setBody(strBuilder.toString().replaceAll("//","/"))
    
    mapHeaders = ['content-type':'text/html']
    message.setHeaders(mapHeaders)
    return message
}

def Message download(Message message) {
    def mapHeaders = message.getHeaders()
    
    String filePath = mapHeaders.get("CamelHttpPath") 
    StringBuilder strBuilder = new StringBuilder()
    
    File file = new File(filePath);byte[] fileContent = file.bytes
    strBuilder << fileContent.encodeBase64().toString()    
    message.setBody(strBuilder.toString());
    
    mapHeaders = ['Content-Transfer-Encoding':'base64',
                  'Content-Disposition':'attachment; filename=${file.getName()}']
                  
                  
    return message
}

 

 

 

For browsing use this https endpoint address: /amba/cpifilesystem*

toonvl_5-1706308352248.png

Add a script block with the groovy script calling the browse function from the script

toonvl_7-1706308529662.png

For downloading use this https endpoint address: /amba/cpifilesystemdownload*

toonvl_6-1706308469754.png

Add a script block with the groovy script calling the download function from the script

toonvl_8-1706308563298.png

Also add a Base64 Decoder to the download process

Now deploy and go to https://XXXXXX.it-cpiXX-rt.cfapps.XXXX.hana.ondemand.com/http/amba/cpifilesystem/app/webapps and download the war file stack.worker_cf.karaf-6.xx.x.war. Unzip this file in a new! directory and it will provide most of the required jars.

Now also download slf4j-api-2.0.11.jar and slf4j-simple-2.0.9.jar from the maven repository here https://repo1.maven.org/maven2/org/slf4j/

Final Step 4: Create a Groovy testing project

Create a new Groovy project

toonvl_0-1706315076986.png

Make sure you add a Java 8 JDK to use as JRE, and make that the default JRE, like Zulu 8 https://www.azul.com/downloads/?version=java-8-lts&package=jdk#zulu

You can go to the properties of the JRE System Library, add the Java 8 JDK and then make it the default JRE

toonvl_1-1706316055943.png

You will also need to add all the jars that you have downloaded earlier to the project
To do so, richt click the project folder > click on 'Build Path' > 'Configure Build Path...'

toonvl_2-1706316178795.png

This will be easiest if you first create a new folder and move or copy all the following jars to it

toonvl_3-1706316315925.png

  • com.sap.cloud.adk.api-2.32.0.jar
  • com.sap.it.commons-1.110.0.jar
  • com.sap.it.commons.logging.slf4j-1.110.0.jar
  • com.sap.it.script.custom-development-2.23.0.jar
  • com.sap.it.script.script.engine.api-2.23.0.jar
  • org.apache.camel.camel-core-2.24.2.sap-32.jar
  • slf4j-api-2.0.11.jar
  • slf4j-simple-2.0.9.jar

Now over in Eclipse, in the build path window, go to the 'Libraries' tab, click on Add External JARs... and add all the jars I listed earlier.

toonvl_4-1706316607434.png

Now you can do the last steps, creating the actual Groovy script files

New > create a Groovy Type file

toonvl_6-1706316853950.png

You can call it 'GroovyCPITest', kind Script

toonvl_5-1706316819165.png

Paste in the script file the following contents, which will allow to run the other script as a script or Groovy shell

 

package com.groovy.testing

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("src/com/groovy/testing/script1.groovy"))

// Initialize message with body, header and property
Message msg = new MessageImpl()
msg.setBody(new String("Hello Groovy World"))
msg.setHeader("oldHeader", "MyGroovyHeader")
msg.setProperty("oldProperty", "MyGroovyProperty")

// Execute script
script.processData(msg)

// Display results of script in console
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())

 

And also a second time for a script called 'script1'

Here you can paste your own Groovy script that you want to test/debug. For this example, I used the default contents that SAP puts in a new Groovy script file.

That was finally all, you can now run/debug the script by right clicking the project > Run As/Debug As > Groovy Console/Groovy Shell

toonvl_0-1706317496147.png

 

toonvl_2-1706317650017.png

 

Labels in this area