Skip to content

Commit 9f854ea

Browse files
committed
add scripts
1 parent 387180d commit 9f854ea

File tree

10 files changed

+305
-0
lines changed

10 files changed

+305
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
groovy-*/
2+
lib/
3+
apache-groovy-binary-*

.java-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# scpi-script-executor
2+
3+
Run your SAP Cloud Platform Integration Groovy scripts on your local machine for easier test and debugging.
4+
5+
## Prerequites
6+
7+
- JDK installed and configured (e.g. [SAPMachine][machine])\
8+
Make sure the environment variable `JAVA_HOME` points to the JDK
9+
- `curl` to download dependencies
10+
11+
[machine]: https://sap.github.io/SapMachine/
12+
13+
## Setup
14+
15+
1. Clone/download the repo
16+
1. Run `bootstrap.sh`
17+
18+
## How to use
19+
20+
1. Copy-paste your script into `script.groovy`
21+
1. Adapt `execute.groovy` to:
22+
- Setup the test message
23+
- Change the log output
24+
- Add assertions
25+
- ...
26+
1. `./run.sh`\
27+
The first run downloads additional dependencies (cached for subsequent runs). Be patient :wink:
28+
29+
## Notes
30+
31+
This project only implements a limited subset of the [Scripting API][api] of SCPI.
32+
Check the classes in the `impl` subfolder for details.
33+
34+
[api]: https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/LATEST/en-US/c5c7933b77ba46dbaa9a2c1576dbb381.html

bootstrap.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env sh
2+
3+
# ref. https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/LATEST/en-US/03b32eb2c5c249f0a59bcd27c44d1e4e.html
4+
GROOVY_VERSION=2.4.12
5+
echo "Setting up Groovy runtime $GROOVY_VERSION..."
6+
curl -O "https://archive.apache.org/dist/groovy/$GROOVY_VERSION/distribution/apache-groovy-binary-$GROOVY_VERSION.zip"
7+
unzip -qo apache-groovy-binary*.zip
8+
echo "Setting up Groovy runtime $GROOVY_VERSION... DONE"
9+
10+
# ref. https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/LATEST/en-US/062f7a7e5c374e67b568dddc7b7844a9.html
11+
echo "Downloading SCPI APIs..."
12+
mkdir -p 'lib'
13+
(
14+
cd lib || exit 1
15+
curl -O --cookie 'eula_3_1_agreed=tools.hana.ondemand.com/developer-license-3_1.txt' https://tools.hana.ondemand.com/additional/com.sap.it.public.generic.api-2.20.0.jar
16+
curl -O --cookie 'eula_3_1_agreed=tools.hana.ondemand.com/developer-license-3_1.txt' https://tools.hana.ondemand.com/additional/cloud.integration.script.apis-1.36.1.jar
17+
)
18+
echo "Downloading SCPI APIs... DONE"

execute.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@GrabConfig(systemClassLoader=true)
2+
// additonal dependencies required by the Generic API
3+
@Grab(group='org.apache.camel', module='camel-core', version='2.20.4')
4+
@Grab(group='javax.activation', module='activation', version='1.1.1')
5+
6+
import groovy.json.*
7+
import impl.*
8+
9+
// setup Groovy scripting engine
10+
def binding = new Binding()
11+
def logFactory = new MessageLogFactoryImpl()
12+
binding.setVariable("messageLogFactory", logFactory)
13+
def shell = new GroovyShell(binding)
14+
15+
def scriptFile = new File('script.groovy')
16+
shell.evaluate(scriptFile)
17+
def script = shell.parse(scriptFile)
18+
19+
// setup message
20+
def message = new MessageImpl()
21+
message.headers.foo = "bar"
22+
message.properties.bar = "foo"
23+
message.body = "Hello World"
24+
25+
// execute script and log output
26+
println "==== BEFORE ===="
27+
println message
28+
29+
result = script.invokeMethod("processData", message)
30+
31+
println ""
32+
println "==== AFTER ===="
33+
println result
34+
println ""
35+
println logFactory.messageLog
36+
37+
// Assert correct result
38+
assert result.body == "Hello World processed"

impl/MessageImpl.groovy

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package impl
2+
3+
import com.sap.gateway.ip.core.customdev.util.Message
4+
5+
class MessageImpl implements Message {
6+
Map properties = [:]
7+
Map headers = [:]
8+
def body = ""
9+
10+
void setAttachmentHeader(java.lang.String a, java.lang.String b, org.apache.camel.Attachment c) {
11+
throw new RuntimeException()
12+
}
13+
14+
long getBodySize() {
15+
-1
16+
}
17+
18+
void setAttachmentHeader(java.lang.String a, java.lang.String b, com.sap.gateway.ip.core.customdev.util.AttachmentWrapper c) {
19+
throw new RuntimeException()
20+
}
21+
22+
void addAttachmentHeader(java.lang.String a, java.lang.String b, com.sap.gateway.ip.core.customdev.util.AttachmentWrapper c) {
23+
throw new RuntimeException()
24+
}
25+
26+
void setAttachmentWrapperObjects(java.util.Map a) {
27+
throw new RuntimeException()
28+
}
29+
30+
java.lang.Object getHeader(java.lang.String a, java.lang.Class c) {
31+
throw new RuntimeException()
32+
}
33+
34+
long getAttachmentsSize() {
35+
throw new RuntimeException()
36+
}
37+
38+
java.lang.String getAttachmentHeader(java.lang.String a, org.apache.camel.Attachment b) {
39+
throw new RuntimeException()
40+
}
41+
42+
void addAttachmentObject(java.lang.String a, org.apache.camel.Attachment b) {
43+
throw new RuntimeException()
44+
}
45+
46+
void setHeader(java.lang.String a, java.lang.Object b) {
47+
throw new RuntimeException()
48+
}
49+
50+
void removeAttachmentHeader(java.lang.String a, org.apache.camel.Attachment b) {
51+
throw new RuntimeException()
52+
}
53+
54+
void removeAttachmentHeader(java.lang.String a, com.sap.gateway.ip.core.customdev.util.AttachmentWrapper b) {
55+
throw new RuntimeException()
56+
}
57+
58+
void addAttachmentObject(java.lang.String a, com.sap.gateway.ip.core.customdev.util.AttachmentWrapper b) {
59+
throw new RuntimeException()
60+
}
61+
62+
java.util.Map getAttachmentObjects() {
63+
throw new RuntimeException()
64+
}
65+
66+
java.util.Map getAttachmentWrapperObjects() {
67+
throw new RuntimeException()
68+
}
69+
70+
void addAttachmentHeader(java.lang.String a, java.lang.String b, org.apache.camel.Attachment c) {
71+
throw new RuntimeException()
72+
}
73+
74+
java.lang.String getAttachmentHeader(java.lang.String a, com.sap.gateway.ip.core.customdev.util.AttachmentWrapper b) {
75+
throw new RuntimeException()
76+
}
77+
78+
void setAttachmentObjects(java.util.Map a) {
79+
throw new RuntimeException()
80+
}
81+
82+
java.util.Map getAttachments() {
83+
throw new RuntimeException()
84+
}
85+
86+
java.lang.Object getBody(java.lang.Class clazz) {
87+
if (clazz == java.io.InputStream.class) {
88+
InputStream stream = new ByteArrayInputStream(this.body.getBytes("UTF-8"));
89+
return stream
90+
} else if (clazz == java.lang.String.class) {
91+
return body
92+
} else {
93+
throw new RuntimeException("Unsupported class " + clazz)
94+
}
95+
}
96+
97+
void setAttachments(java.util.Map a) {
98+
throw new RuntimeException()
99+
}
100+
101+
@Override
102+
String toString() {
103+
def result = "MessageImpl\n"
104+
result += " Properties\n"
105+
properties.each {
106+
result += " - ${it.key} -> ${it.value}\n"
107+
}
108+
result += " Headers\n"
109+
headers.each {
110+
result += " - ${it.key} -> ${it.value}\n"
111+
}
112+
result += " >>> Body\n"
113+
result += " ${body}\n"
114+
result += " <<< Body\n"
115+
}
116+
}

impl/MessageLogFactoryImpl.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package impl
2+
3+
import com.sap.it.api.msglog.MessageLogFactory
4+
import com.sap.it.api.msglog.MessageLog
5+
6+
class MessageLogFactoryImpl implements MessageLogFactory {
7+
def messageLog = new MessageLogImpl()
8+
9+
MessageLog getMessageLog(Object context) {
10+
return messageLog
11+
}
12+
}

impl/MessageLogImpl.groovy

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package impl
2+
3+
import com.sap.it.api.msglog.MessageLog
4+
5+
class MessageLogImpl implements MessageLog {
6+
def attachments = []
7+
def headers = [:]
8+
def properties = [:]
9+
10+
void addAttachmentAsString(String name, String body, String type) {
11+
attachments.add([
12+
"name": name,
13+
"body": body,
14+
"type": type
15+
])
16+
}
17+
18+
void addCustomHeaderProperty(String name, String value) {
19+
headers[name] = value
20+
}
21+
22+
void setBooleanProperty(String name, Boolean value) {
23+
properties[name] = value
24+
}
25+
26+
void setDateProperty(String name, Date value) {
27+
properties[name] = value
28+
}
29+
30+
void setDoubleProperty(String name, Double value) {
31+
properties[name] = value
32+
}
33+
34+
void setFloatProperty(String name, Float value) {
35+
properties[name] = value
36+
}
37+
38+
void setIntegerProperty(String name, Integer value) {
39+
properties[name] = value
40+
}
41+
42+
void setLongProperty(String name, Long value) {
43+
properties[name] = value
44+
}
45+
46+
void setStringProperty(String name, String value) {
47+
properties[name] = value
48+
}
49+
50+
String toString() {
51+
def result = "MessageLogImpl\n"
52+
result += " Properties\n"
53+
properties.each {
54+
result += " - ${it.key} -> ${it.value}"
55+
}
56+
result += " Custom Headers\n"
57+
headers.each {
58+
result += " - ${it.key} -> ${it.value}"
59+
}
60+
result += " Attachments\n"
61+
attachments.sort{ it.name }.each{
62+
result += " >>>\"${it.name}\" (${it.type})\n"
63+
result += " ${it.body}\n"
64+
result += " <<<\"${it.name}\" (${it.type})\n"
65+
}
66+
return result
67+
}
68+
}

run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
3+
./groovy-*/bin/groovy -cp './lib/*' execute.groovy "$@"

script.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import com.sap.gateway.ip.core.customdev.util.Message;
2+
3+
def Message processData(Message message) {
4+
def body = message.getBody(java.lang.String.class)
5+
message.body = body + " processed"
6+
7+
def messageLog = messageLogFactory.getMessageLog(message)
8+
messageLog.addAttachmentAsString("Attachment #1", "header: ${message.headers.foo}", "plain/txt")
9+
messageLog.addAttachmentAsString("Attachment #2", message.properties.bar, "plain/txt")
10+
11+
return message
12+
}

0 commit comments

Comments
 (0)