Testing Oracle Business Rules using Rules Designer
By Bob Webster on Jun 22, 2010
The JDeveloper 11G Rules editor allows developers to test business rules directly from within the Rules Designer.
The "User's Guide for Oracle Business Rules" does a nice job of introducing testing fundamentals such as creating the test function and passing a simple fact.
This posting presents a more complex example illustrating;
- How to create test data for input data structures marked as a list or a tree.
- How to create test data for both Java and XML facts.
Our example scenario involves checking line items in an order to determine if additional shipping charges apply.
An Order is represented by an XML document that contains a list of order Items.
An Order and its line items are represented by XML facts using the following XML Schema
To determine Shipping charges we consult a list of Java Facts.
The ShipCode class contains the shipping cost category for a given product.
The ShipCodeList contains a list of all possible ShipCodes.
For our simple example we use a single rule that checks if one of the Items asserted in the order matches a ShipCode for the same product. If we find a match, we set the extraCharge variable to true for the corresponding order item
The ShippingRules are exposed using a Decision Function.
Our “DetermineShippingCosts” Decision Function takes two inputs.
The first input is an OrderT fact based on our XML Facts. This parameter is marked as a tree structure.
This ensure that the entire XML data structure is asserted into the engine.
The second input is List of ShipCode Java based facts. We indicate a parameter of type ShipCode, but since the List checkbox is selected we will provide an input object of type ShipCodeList.
The output for our decision function is a modified order with line items that require additional shipping charges marked accordingly.
To test this decision function we will create three different functions.
Our first function is the top level test function “testShipRateRules”, We will execute this function later to perform the test. By standard the top level function must be defined to return a boolean. In our case the boolean returned is not relevant. When we run the test later our results will be displayed in a popup dialog.
Within the body of the function we call our Decision Function named DetermineShippingCosts. The decision function we defined takes two parameters. We create two other functions, createOrderTestData and createShipRateData to create the test data for these parameters.
The next two screen snapshots show the creatOrderTestData() and createShipRateData() functions.
In the body of both these functions we use assign and modify commands to create our test objects.
We then create an instance of our list object and add the test objects to the list.
The key point to understand is that both XML and Java fact lists are represented under the covers by the java.util.List interface. The Rules Designer normally assists the user with the creation of Rule statements, but to add the objects to a list you will need to author the line without assistance. Choose “expression” from the drop down list presented when entering the beginning of each source line. Invoke the underlying methods to get the list and to add() items to the list.
Finally we are ready to execute our test.
We run the test by selecting the testShipRateRules function and clicking the test link in the upper right corner. Note that this link will not be enabled if there are any rule validation errors.
When we run the test, our results are output to a dialog window.
We requested this output by including the RL.watch.all() statement.
Our test data is designed to find one match for item 001 and no match for item 123.
In the bottom 4 lines of the output we see that the rule has fired and our order line for product 001 has a been modified to set the value of the extraCharge property to true.
The complete example can be downloaded in a zip file from github here