The first things we need to do is to setup a test context that provides the test data, as it is normally provided by the Boomi run-time. In our case, we must create the documents that we want to pass into the script for testing( see ).
We do not need any Execution Property or Dynamic Property or Process Property in our script, but the Context would the right place to add those.
Dynamic Document Properties would be added to each document, not to the Context itself.
Set Test expectations
Unlike in the template, we expect only one document as output, and we have to amend our test assertion in the Test class. Feel free to add more assertions to ensure the script works as expected.
println("\r\n--- Test Output ----------")int docCount = context.outputDocuments.size()println(docCount +" Document(s) after script execution")assert1== docCount // <<<<<< expect one document
Implement the Script
Maintaining the script header information is mandatory, especially the date and author tags. This is even more important when you work in a team!
We need the JsonSlurper object to work with JSON data. We create the js object instance before the documents loop because there is no need to have a new instance for each document. One js is enough for all documents.
You recognize that IntelliJ is smart enough to automatically insert the requires Imports:
Navigate to the script logic and replace Your document related code here ... with:
// *********** Document related functionality ************Map jsonDoc = js.parseText( textDoc)_logger.info( "DOC[$docNo]: ${jsonDoc.articleNo} = ${jsonDoc.price}")// ******** end of Document related functionality ********
We parse the text document into a JSON (map), so that we can access the JSON elements as properties (see Working with JSON). The scipt's logic so far is to log the incoming data. Good enough for a first Debug Run. Set a breakpoint on line 52, navigate to the Test and Debug test01().
The execution stops at the breakpoint. See the variables and output.
Test your Script in Boomi AtomSphere
and use it in your test process.
Let's improve the scripts functionality and build the business logic. Amend your code, define the articles result list and business logic, and set a breakpoint on the line // << set breakpoint here
// https://www.tutorialspoint.com/groovy/groovy_lists.htmList articles = []// Documents loopfor (int docNo =0; docNo < docCount; docNo++) {...// *********** Document related functionality ************Map jsonDoc = js.parseText(textDoc) _logger.info("DOC[$docNo]: ${jsonDoc.articleNo} = ${jsonDoc.price}")// Business Logic// Check if there is already an item with the same articleNo.// If yes, we must update that item. // If no, we create a new item and add it to the prices list.// it - iterator = List element (of type)def article = prices.find { it.articleNo == jsonDoc.articleNo }// article TYPE // { // articleNo// minPrice, maxPrice, priceCount, // prices[]// }if (article ==null) { // << set breakpoint here articles.add([ articleNo: jsonDoc.articleNo, priceCount : 1, minPrice : jsonDoc.price, maxPrice : jsonDoc.price, prices : [ jsonDoc.price] ]) } else { }// ******** end of Document related functionality ********
Debug your script and see the execution stopping with article == null. This is expected because - look at the Variables window - the priceslist is yet emtpy.
I think, you will find out yourself how to step over the next two lines (Single Step - F10) to stop at _setTextDocument(). While your colors might be different (depending in the IntelliJ theme you have chosen), the red line is the breakpoint and the yellow line marks the next line for execution.
More interesting to see is the Variables window where you can observe that the expect article object was added to the articles list.
Amend you code again and implement the else branch.
if (article ==null) { // << set breakpoint here articles.add([ articleNo: jsonDoc.articleNo, priceCount : 1, minPrice : jsonDoc.price, maxPrice : jsonDoc.price, prices : [ jsonDoc.price] ])} else {// article found -> update article.priceCount++// incrementif( jsonDoc.price < article.minPrice) article.minPrice = jsonDoc.price if( jsonDoc.price > article.maxPrice) article.maxPrice = jsonDoc.price article.prices.add( jsonDoc.price ) // We do _not_ check if the same articles already exists!}
Set a breakpoint on the _setTextDocument() line and debug a bit: document by document. After the third document you should check the console output and the variables. You will recognize the variables are close to what we expect to see in the output JSON.
Last but not least, we must write back the articles map into one output document. So, you must move the _setTextDocument() to outside of the document loop. Before, we must convert the articles map to a Json String.
// ******** end of Document related functionality ********} // documents loop// Your process related code (process properties etc.) hereString outputDoc =JsonOutput.toJson(articles)_setTextDocument(dataContext, outputDoc, newProperties()) // <<<<<
As the very last step, you can update your Test class and prettyPrint() the returned Json document:
Use the script
Do you rememeber why we did all that effort? We wanted to use that script in Boomi! Copy and paste all the script code 1:1 into the script component. Of course, the Test class is not needed in Boomi.
You can copy and paste the script code into the psgAggregatePricesscript component
Check the process logs to see the log entires your script has written