Skip to content

Commit

Permalink
Added logic for XML transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
engswee committed Jul 25, 2024
1 parent 07748a7 commit 0703209
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 26 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/sync-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
name: sync-and-deploy
on:
workflow_dispatch:
push:
branches:
- devtober-demo

jobs:
# Unit test
unit_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
- name: 'Local unit test with Maven'
run: mvn clean test

# Sync designtime artifact to tenant
sync:
runs-on: ubuntu-latest
needs: unit_test
container:
image: engswee/flashpipe:latest
steps:
- uses: actions/checkout@v4

- name: 'Update/Upload Groovy XML Transformation to design time'
uses: engswee/flashpipe-action/sync@v1
with:
tmn-host: equaliseit.it-cpi023.cfapps.eu20-001.hana.ondemand.com
oauth-host: equaliseit.authentication.eu20.hana.ondemand.com
oauth-clientid: ${{ secrets.DEV_CLIENT_ID }}
oauth-clientsecret: ${{ secrets.DEV_CLIENT_SECRET }}
target: tenant
package-id: FlashPipeDemo

# Deploy to runtime
deploy:
runs-on: ubuntu-latest
needs: sync
container:
image: engswee/flashpipe:latest
steps:
- uses: actions/checkout@v4

- name: 'Deploy Groovy XML Transformation to runtime'
uses: engswee/flashpipe-action/deploy@v1
with:
tmn-host: equaliseit.it-cpi023.cfapps.eu20-001.hana.ondemand.com
oauth-host: equaliseit.authentication.eu20.hana.ondemand.com
oauth-clientid: ${{ secrets.DEV_CLIENT_ID }}
oauth-clientsecret: ${{ secrets.DEV_CLIENT_SECRET }}
artifact-ids: GroovyXMLTransformation
2 changes: 1 addition & 1 deletion Demo_Flow/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Demo Flow
Bundle-SymbolicName: Demo_Flow; singleton:=true
Bundle-Version: 0.1.0
Bundle-Version: 0.1.1
SAP-BundleType: IntegrationFlow
SAP-NodeType: IFLMAP
SAP-RuntimeProfile: iflmap
Expand Down
14 changes: 14 additions & 0 deletions Demo_Flow/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.equaliseit</groupId>
<artifactId>flashpipe-demo</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>Demo_Flow</artifactId>
<version>1.0.0</version>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
</ifl:property>
<ifl:property>
<key>script</key>
<value>script1.groovy</value>
<value>XMLTransformation.groovy</value>
</ifl:property>
</bpmn2:extensionElements>
<bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
Expand Down
37 changes: 37 additions & 0 deletions Demo_Flow/src/main/resources/script/XMLTransformation.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import com.sap.gateway.ip.core.customdev.util.Message
import com.sap.it.api.ITApiFactory
import com.sap.it.api.mapping.ValueMappingApi
import groovy.xml.MarkupBuilder

import java.text.SimpleDateFormat

def Message processData(Message message) {
Reader reader = message.getBody(Reader)
def Order = new XmlSlurper().parse(reader)
Writer writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def sourceDocType = message.getProperty('DocType')
ValueMappingApi api = ITApiFactory.getService(ValueMappingApi, null)

def items = Order.Item.findAll { it.Valid.text() == 'true' }
builder.PurchaseOrder {
'Header' {
'ID' Order.Header.OrderNumber
'DocumentDate' new SimpleDateFormat('yyyy-MM-dd').format(new SimpleDateFormat('yyyyMMdd').parse(Order.Header.Date.text()))
if (!items.size())
'DocumentType' api.getMappedValue('S4', 'DocType', sourceDocType, 'ACME', 'DocumentType')
}

items.each { item ->
'Item' {
'ItemNumber' item.ItemNumber.text().padLeft(3, '0')
'ProductCode' item.MaterialNumber
'Quantity' item.Quantity
}
}
}

message.setBody(writer.toString())
return message
}
24 changes: 0 additions & 24 deletions Demo_Flow/src/main/resources/script/script1.groovy

This file was deleted.

61 changes: 61 additions & 0 deletions Demo_Flow/src/test/groovy/XMLTransformationSpec.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import com.sap.gateway.ip.core.customdev.util.Message
import com.sap.it.api.mapping.ValueMappingApi
import spock.lang.Shared
import spock.lang.Specification

class XMLTransformationSpec extends Specification {
@Shared
Script script
Message msg

def setupSpec() {
GroovyShell shell = new GroovyShell()
script = shell.parse(this.getClass().getResource('/script/XMLTransformation.groovy').toURI())
}

def setup() {
msg = new Message()
}

def 'Scenario 1 - Order has items'() {
given: 'the message body is initialized'
msg.setBody(this.getClass().getResource('input1.xml').newInputStream())

when: 'we execute the Groovy script'
script.processData(msg)

then: 'the output message body is as expected'
def root = new XmlSlurper().parse(msg.getBody(Reader))
verifyAll {
root.Header.ID.text() == 'ORD60001'
root.Header.DocumentDate.text() == '2019-02-18'
root.Header.DocumentType.text() == ''
root.Item.size() == 1
root.Item.ItemNumber.text() == '010'
root.Item.ProductCode.text() == 'MT70001'
root.Item.Quantity.text() == '57'
}
}

def 'Scenario 2 - Order does not have items'() {
given: 'the message body and property are initialized'
msg.setBody(this.getClass().getResource('input2.xml').newInputStream())
msg.setProperty('DocType', 'HDR')

// Set up value mapping entries
ValueMappingApi vmapi = ValueMappingApi.getInstance()
vmapi.addEntry('S4', 'DocType', 'HDR', 'ACME', 'DocumentType', 'ACME-HDR')

when: 'we execute the Groovy script'
script.processData(msg)

then: 'the output message body is as expected'
def root = new XmlSlurper().parse(msg.getBody(Reader))
verifyAll {
root.Header.ID.text() == 'ORD80002'
root.Header.DocumentDate.text() == '2020-02-18'
root.Header.DocumentType.text() == 'ACME-HDR'
root.Item.size() == 0
}
}
}
18 changes: 18 additions & 0 deletions Demo_Flow/src/test/resources/input1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Order>
<Header>
<OrderNumber>ORD60001</OrderNumber>
<Date>20190218</Date>
</Header>
<Item>
<ItemNumber>10</ItemNumber>
<MaterialNumber>MT70001</MaterialNumber>
<Quantity>57</Quantity>
<Valid>true</Valid>
</Item>
<Item>
<ItemNumber>20</ItemNumber>
<MaterialNumber>MT80001</MaterialNumber>
<Quantity>28</Quantity>
<Valid>false</Valid>
</Item>
</Order>
12 changes: 12 additions & 0 deletions Demo_Flow/src/test/resources/input2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Order>
<Header>
<OrderNumber>ORD80002</OrderNumber>
<Date>20200218</Date>
</Header>
<Item>
<ItemNumber>10</ItemNumber>
<MaterialNumber>MT90001</MaterialNumber>
<Quantity>55</Quantity>
<Valid>false</Valid>
</Item>
</Order>
Loading

0 comments on commit 0703209

Please sign in to comment.