Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: move to static DW script invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
pozil committed Jun 20, 2023
1 parent 1d8787b commit 6a19f5c
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 71 deletions.
33 changes: 13 additions & 20 deletions force-app/main/default/classes/CsvToJsonConversionTest.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@IsTest
public with sharing class CsvToJsonConversionTest {

// Uses the ./src/dw/csvtojson.dwl script to convert from a CSV static resource to JSON
// Uses the /dw/csvToJson.dwl script to convert from a CSV static resource to JSON
@IsTest
public static void convertCsvToJsonContacts() {
// Load CSV data as a blob from static resource
Expand All @@ -12,15 +12,12 @@ public with sharing class CsvToJsonConversionTest {
Name = 'contacts'
LIMIT 1].Body;

String scriptName = 'csvtojson';
dataweave.Script dwscript = DataWeave.Script.createScript(scriptName);
String scriptNameReported = dwscript.toString();
System.assertEquals(scriptName, scriptNameReported);

DataWeave.Result result = dwscript.execute(new Map<String, Object>{'data' => data.toString()});
DataWeave.Script script = new DataWeaveScriptResource.csvToJson();
String scriptNameReported = script.toString();
System.assertEquals('csvToJson', scriptNameReported);

DataWeave.Result result = script.execute(new Map<String, Object>{'data' => data.toString()});
String jsonResult = result.getValueAsString();
//system.debug('JSON output '+ jsonResult );

List<Contact> contacts = (List<Contact>)JSON.deserialize(jsonResult, List<Contact>.class);
System.assertEquals(51, contacts.size(), 'The static resource contacts.csv has 51 rows of data (less headers)');
Expand All @@ -35,18 +32,17 @@ public with sharing class CsvToJsonConversionTest {
}
}

// Uses the ./src/dw/csvSeparatorTojson.dwl script to convert from a CSV with ; delimiters to JSON
// Uses the /dw/csvSeparatorToJson.dwl script to convert from a CSV with ; delimiters to JSON
@IsTest
public static void csvSeparatorToJson() {
// Note the use of the ; separator rather than ,
Map<String, Object> args = new Map<String, Object>{ 'records' => '1;2\na;b' };

String scriptName = 'csvSeparatorTojson';
dataweave.Script dwscript = DataWeave.Script.createScript(scriptName);
String scriptNameReported = dwscript.toString();
Assert.areEqual(scriptName, scriptNameReported);
DataWeave.Script script = new DataWeaveScriptResource.csvSeparatorToJson();
String scriptNameReported = script.toString();
Assert.areEqual('csvSeparatorToJson', scriptNameReported);

DataWeave.Result result = dwscript.execute(args);
DataWeave.Result result = script.execute(args);

String jsonResult = result.getValueAsString();
system.debug('JSON output '+ jsonResult );
Expand All @@ -55,17 +51,15 @@ public with sharing class CsvToJsonConversionTest {
Assert.isTrue(jsonResult.contains('"2": "b"'), 'Expected first record header and second value');
}

// Uses the ./src/dw/csvToJsonBasic.dwl script to convert from a CSV to JSON with no extra conversion
// Uses the /dw/csvToJsonBasic.dwl script to convert from a CSV to JSON with no extra conversion
@IsTest
public static void convertCsvToJsonBasic() {
String csvPayload = 'first_name,last_name,company,address\n' +
'Abel,Maclead,"Rousseaux, Michael Esq","6649 N Blue Gum St,\n' +
'New Orleans"';

String scriptName = 'csvToJsonBasic';
DataWeave.Script dwscript = DataWeave.Script.createScript(scriptName);

DataWeave.Result result = dwscript.execute(new Map<String, Object>{'payload' => csvPayload});
DataWeave.Script script = new DataWeaveScriptResource.csvToJsonBasic();
DataWeave.Result result = script.execute(new Map<String, Object>{'payload' => csvPayload});

String jsonResult = result.getValueAsString();
Assert.areNotEqual(0, jsonResult.trim().length(), 'Expected to have JSON output from DW script');
Expand All @@ -85,6 +79,5 @@ public with sharing class CsvToJsonConversionTest {
Assert.areEqual('Maclead', jsonMap.get('last_name'));
Assert.areEqual('Rousseaux, Michael Esq', jsonMap.get('company'));
Assert.areEqual('6649 N Blue Gum St,\nNew Orleans', jsonMap.get('address'));

}
}
10 changes: 4 additions & 6 deletions force-app/main/default/classes/CsvToObjectTest.cls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@IsTest
public with sharing class CsvToObjectTest {

// Uses the ./src/dw/csvToCustomObject.dwl script to convert from a CSV static resource to objects
// Uses the /dw/csvToCustomObject.dwl script to convert from a CSV static resource to objects
@IsTest
public static void convertCsvToObjects() {
// Load CSV data as a blob from static resource
Expand All @@ -12,8 +12,7 @@ public with sharing class CsvToObjectTest {
Name = 'contacts'
LIMIT 1].Body;

String scriptName = 'csvToCustomObject';
DataWeave.Script script = DataWeave.Script.createScript(scriptName);
DataWeave.Script script = new DataWeaveScriptResource.csvToCustomObject();
DataWeave.Result dwresult = script.execute(new Map<String, Object>{'records' => data.toString()});

// Note that CsvData here is an Apex POTATO (Apex equivalent of a POJO)
Expand All @@ -31,7 +30,7 @@ public with sharing class CsvToObjectTest {
}
}

// Uses the ./src/dw/csvToCustomObject.dwl script to convert from a large CSV static resource to objects
// Uses the /dw/csvToCustomObject.dwl script to convert from a large CSV static resource to objects
@IsTest
public static void convertLargeCsvToObjects() {
// Load CSV data as a blob from static resource
Expand All @@ -45,8 +44,7 @@ public with sharing class CsvToObjectTest {
System.debug(LoggingLevel.Warn, 'Before parse Heap: ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
System.debug(LoggingLevel.Warn, 'Before parse CPU: ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());

String scriptName = 'csvToCustomObject';
Dataweave.Script script = DataWeave.Script.createScript(scriptName);
DataWeave.Script script = new DataWeaveScriptResource.csvToCustomObject();
DataWeave.Result dwresult = script.execute(new Map<String, Object>{'records' => data.toString()});

System.debug(LoggingLevel.Warn, 'After parse Heap: ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
Expand Down
4 changes: 1 addition & 3 deletions force-app/main/default/classes/DWJsonToSObjectsList.cls
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public with sharing class DWJsonToSObjectsList {
Map<String, Object> parameters = new Map<String, Object>();
parameters.put('mapping', keyMap);
parameters.put('incomingJson', incomingJson);
Dataweave.Script script = Dataweave.Script.createScript(
'json2sObjects'
);
DataWeave.Script script = new DataWeaveScriptResource.json2sObjects();
DataWeave.Result result = script.execute(parameters);
return result.getValueAsString();
}
Expand Down
4 changes: 2 additions & 2 deletions force-app/main/default/classes/ExcelOutputTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public with sharing class ExcelOutputTest {
List<Contact> data = [SELECT FirstName, LastName FROM Contact ORDER BY LastName ASC LIMIT 1];
Assert.isFalse(data.isEmpty(), 'Contacts required for input data');

Dataweave.Script dwscript = DataWeave.Script.createScript('excelOutput');
DataWeave.Script script = new DataWeaveScriptResource.excelOutput();
try {
DataWeave.Result result = dwscript.execute(new Map<String, Object>{ 'records' => data });
DataWeave.Result result = script.execute(new Map<String, Object>{ 'records' => data });

String output = result.getValueAsString();

Expand Down
8 changes: 4 additions & 4 deletions force-app/main/default/classes/ExceptionHandlingTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ public with sharing class ExceptionHandlingTest {
// Uses the /dw/error.dwl script to log a message
@IsTest
public static void runtimeDataWeaveError() {

Dataweave.Script dwscript = DataWeave.Script.createScript('error');
DataWeave.Script script = new DataWeaveScriptResource.error();
try {
DataWeave.Result result = dwscript.execute(new Map<String, Object>());
DataWeave.Result result = script.execute(new Map<String, Object>());
Assert.fail('Exception expected');
} catch (System.DataWeaveScriptException ex) {
} catch (Exception ex) {
Assert.isInstanceOfType(ex, DataWeaveScriptException.class, 'Exceptionn of type DataWeaveScriptException expected');
System.debug(LoggingLevel.Error, ex);
Assert.isTrue(ex.getMessage().startsWith('Division by zero'));
System.debug('Message:' + ex.getMessage());
Expand Down
18 changes: 14 additions & 4 deletions force-app/main/default/classes/HelloWorldTest.cls
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
@IsTest
public with sharing class HelloWorldTest {

// Uses the /dw/helloworld.dwl script to log a message
// Uses the /dw/helloWorld.dwl script to log a message
@IsTest
public static void helloWorld() {
Dataweave.Script dwscript = DataWeave.Script.createScript('helloworld');
DataWeave.Result result = dwscript.execute(new Map<String, Object>());
public static void helloWorldStaticInvocation() {
DataWeave.Script script = new DataWeaveScriptResource.helloWorld();
DataWeave.Result result = script.execute(new Map<String, Object>());
Assert.areEqual('"Hello World"', result.getValueAsString(), 'Log output becomes the value when there are no other results');

Assert.areEqual('"Hello World"', result.getValueAsString(), 'getValueAsString should be idempotent');
}

// Uses the /dw/helloWorld.dwl script to log a message
@IsTest
public static void helloWorldDynamicInvocation() {
DataWeave.Script script = DataWeave.Script.createScript('helloWorld');
DataWeave.Result result = script.execute(new Map<String, Object>());
Assert.areEqual('"Hello World"', result.getValueAsString(), 'Log output becomes the value when there are no other results');

Assert.areEqual('"Hello World"', result.getValueAsString(), 'getValueAsString should be idempotent');
Expand Down
4 changes: 2 additions & 2 deletions force-app/main/default/classes/JsonDateFormatTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public with sharing class JsonDateFormatTest {
List<Contact> data = [SELECT FirstName, LastName, CreatedDate FROM Contact ORDER BY LastName ASC LIMIT 5];
Assert.isFalse(data.isEmpty(), 'Contacts required for input data');

Dataweave.Script dwscript = DataWeave.Script.createScript('jsonDateFormat');
DataWeave.Result result = dwscript.execute(new Map<String, Object>{ 'records' => data });
DataWeave.Script script = new DataWeaveScriptResource.jsonDateFormat();
DataWeave.Result result = script.execute(new Map<String, Object>{ 'records' => data });

// Note that DW uses Java 8 java.time.format, so uuuu is the year. vs. yyyy for Apex.
String expected = '{\n'+
Expand Down
8 changes: 3 additions & 5 deletions force-app/main/default/classes/LoggingTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ public with sharing class LoggingTest {

@IsTest
public static void captureDataWeaveLogging() {
String jsonPayload = '[{\"name\": \"Hare\",\"isWinner\": false},{\"name\": \"Turtles\",\"isWinner\": true}]';
String jsonPayload = '[{"name": "Hare", "isWinner": false}, {"name": "Turtles", "isWinner": true}]';

String scriptName = 'logFilter';
DataWeave.Script dwscript = DataWeave.Script.createScript(scriptName);

DataWeave.Result result = dwscript.execute(new Map<String, Object>{'payload' => jsonPayload});
DataWeave.Script script = new DataWeaveScriptResource.logFilter();
DataWeave.Result result = script.execute(new Map<String, Object>{'payload' => jsonPayload});

// While we can assert on the script outcome here, the key part is the logging that occurs
// to DATAWEAVE_USER_DEBUG
Expand Down
7 changes: 3 additions & 4 deletions force-app/main/default/classes/MultipleInputsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
public with sharing class MultipleInputsTest {

// Based on the DW example from https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-reference-multiple-inputs
// Uses the ./src/dw/multipleInputs.dwl script to log a message
// Uses the /dw/multipleInputs.dwl script to log a message
@IsTest
public static void multipleInputs() {
Map<String, Object> inputs = new Map<String, Object>();

inputs.put('payload', '[ { "type": "book", "price": 30, "properties": { "title": "Everyday Italian", "author": [ "Giada De Laurentiis" ], "year": 2005 } }, { "type": "book", "price": 29.99, "properties": { "title": "Harry Potter", "author": [ "J K. Rowling" ], "year": 2005 } }, { "type": "book", "price": 41.12, "properties": { "title": "Mule in Action", "author": [ "David Dossot", "John D\'Emic" ], "year": 2009 } }, { "type": "book", "price": 49.99, "properties": { "title": "XQuery Kick Start", "author": [ "James McGovern", "Per Bothner", "Kurt Cagle", "James Linn", "Kurt Cagle", "Vaidyanathan Nagarajan" ], "year": 2003 } }, { "type": "book", "price": 39.95, "properties": { "title": "Learning XML", "author": [ "Erik T. Ray" ], "year": 2003 } }]');
inputs.put('attributes', '{"publishedAfter": 2004}');
inputs.put('exchangeRate', '{ "USD": [ {"currency": "EUR", "ratio":0.92}, {"currency": "ARS", "ratio":8.76}, {"currency": "GBP", "ratio":0.66} ]}');

DataWeave.Script dwscript = DataWeave.Script.createScript('multipleInputs');
DataWeave.Result result = dwscript.execute(inputs);
DataWeave.Script script = new DataWeaveScriptResource.multipleInputs();
DataWeave.Result result = script.execute(inputs);

String output = result.getValueAsString();

Expand Down
28 changes: 14 additions & 14 deletions force-app/main/default/classes/MultipleScriptsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,45 @@
public with sharing class MultipleScriptsTest {

// Based on the DW example from https://docs.mulesoft.com/dataweave/2.3/dataweave-cookbook-reference-multiple-inputs
// Uses the ./src/dw/multipleInputs.dwl script to log a message
// Uses the /dw/multipleInputs.dwl script to log a message
@IsTest
public static void multipleScriptCreationsExecutions() {
for(integer i = 0; i<100; i++) {
Dataweave.Script dwscript = DataWeave.Script.createScript('helloworld');
DataWeave.Result result = dwscript.execute(new Map<String, Object>());
dwscript = null;
DataWeave.Script script = new DataWeaveScriptResource.helloWorld();
DataWeave.Result result = script.execute(new Map<String, Object>());
script = null;
}
}

@IsTest
public static void multipleScriptExecutions() {
Dataweave.Script dwscript = DataWeave.Script.createScript('helloworld');
DataWeave.Script script = new DataWeaveScriptResource.helloWorld();
for(integer i = 0; i<100; i++) {
DataWeave.Result result = dwscript.execute(new Map<String, Object>());
DataWeave.Result result = script.execute(new Map<String, Object>());
}
}

@IsTest
public static void multipleSameScriptExecutions() {
Dataweave.Script dwscript = DataWeave.Script.createScript('helloworld');
Dataweave.Script dwscript2 = DataWeave.Script.createScript('helloworld');
DataWeave.Script script = new DataWeaveScriptResource.helloWorld();
DataWeave.Script script2 = new DataWeaveScriptResource.helloWorld();
for(integer i = 0; i<100; i++) {
DataWeave.Result result = dwscript.execute(new Map<String, Object>());
DataWeave.Result result2 = dwscript2.execute(new Map<String, Object>());
DataWeave.Result result = script.execute(new Map<String, Object>());
DataWeave.Result result2 = script2.execute(new Map<String, Object>());
}
}

@IsTest
public static void multipleDualScriptExecutions() {
Dataweave.Script dwscript = DataWeave.Script.createScript('helloworld');
Dataweave.Script dwscript2 = DataWeave.Script.createScript('objectProcessing');
DataWeave.Script script = new DataWeaveScriptResource.helloWorld();
DataWeave.Script script2 = new DataWeaveScriptResource.objectProcessing();

List<Contact> data = new List<Contact>();
data.add(new Contact(FirstName='John', LastName='Goe'));

for(integer i = 0; i<100; i++) {
DataWeave.Result result = dwscript.execute(new Map<String, Object>());
DataWeave.Result result2 = dwscript2.execute(new Map<String, Object>{ 'records' => data });
DataWeave.Result result = script.execute(new Map<String, Object>());
DataWeave.Result result2 = script2.execute(new Map<String, Object>{ 'records' => data });
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions force-app/main/default/classes/ObjectProcessingTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public with sharing class ObjectProcessingTest {
List<Contact> data = [SELECT FirstName, LastName FROM Contact ORDER BY LastName ASC LIMIT 5];
Assert.isFalse(data.isEmpty(), 'Contacts required for input data');

DataWeave.Script script = DataWeave.Script.createScript('objectProcessing');
DataWeave.Script script = new DataWeaveScriptResource.objectProcessing();
DataWeave.Result result = script.execute(new Map<String, Object>{ 'records' => data });

String output = result.getValueAsString();
Expand Down Expand Up @@ -73,8 +73,9 @@ public with sharing class ObjectProcessingTest {
public static void sObjectsFromDataWeave() {
// CSV data for Contacts
String inputCsv = 'first_name,last_name,email\nCodey,"The Bear",[email protected]';
DataWeave.Script dwscript = DataWeave.Script.createScript('csvToContacts');
DataWeave.Result dwresult = dwscript.execute(new Map<String, Object>{'records' => inputCsv});

DataWeave.Script script = new DataWeaveScriptResource.csvToContacts();
DataWeave.Result dwresult = script.execute(new Map<String, Object>{'records' => inputCsv});
List<Contact> results = (List<Contact>)dwresult.getValue();

Assert.areEqual(1, results.size());
Expand Down
6 changes: 3 additions & 3 deletions force-app/main/default/classes/PluralizeTest.cls
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
@IsTest
public with sharing class PluralizeTest {

// Uses the ./src/dw/pluralize.dwl script to apply function: https://docs.mulesoft.com/dataweave/2.3/dw-strings-functions-pluralize
// Uses the /dw/pluralize.dwl script to apply function: https://docs.mulesoft.com/dataweave/2.3/dw-strings-functions-pluralize
@IsTest
public static void pluralize() {
Map<String, Object> inputs = new Map<String, Object>();
inputs.put('inputs', '[ "box", "cat", "deer", "die", "person", "cactus", "datum" ]');

dataweave.Script dwscript = DataWeave.Script.createScript('pluralize');
DataWeave.Result result = dwscript.execute(inputs);
DataWeave.Script script = new DataWeaveScriptResource.pluralize();
DataWeave.Result result = script.execute(inputs);
String jsonOutput = result.getValueAsString();
System.debug(jsonOutput);

Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/ReservedKeywordTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public with sharing class ReservedKeywordTest {
public static void processReservedKeywords() {
String jsonString = '[{"currency" : "ABC"}]';

DataWeave.Script script = DataWeave.Script.createScript('reservedKeywords');
DataWeave.Script script = new DataWeaveScriptResource.reservedKeywords();
DataWeave.Result result = script.execute(new Map<String, Object>{ 'incomingJson' => jsonString });

String output = result.getValueAsString();
Expand Down

0 comments on commit 6a19f5c

Please sign in to comment.