Decision Management (ODM,ADS)

 View Only
Expand all | Collapse all

how to unit test individual rule

  • 1.  how to unit test individual rule

    Posted Tue February 28, 2023 08:34 AM

    Hello,

    i know we can use test suite to test the ruleset.
    but how do we unit test individual rule? how to do trigger CICD to unit test the rules?
    it seem like there is not so obvious way of doing it.



    ------------------------------
    Goh Kian Seng
    ------------------------------


  • 2.  RE: how to unit test individual rule

    Posted Wed March 01, 2023 03:17 AM

    Hi Goh,

    It is normal practice to test a ruleset using Decision Runner, and scenario testing, but if you need to do unit testing i.e. test each rule one at a time, you can do it using runtime rule selection. The steps are:

    • Create a ruleflow with a single task and add all the rules to the task
    • Create a string variable called the fully qualified name of the rule
    • Add runtime rule selection to the task, so that only one rule is selected for evaluation based on the value of the variable
    • Map the variable to a IN ruleset parameter
    • Generate a test suite using Decision Runner
    • Add the name of the rule to the test to define the target rule to unit test in the spreadsheet, along with your unit test data.

    Using runtime rule selection in this way will ensure only the target rule gets selected for evaluation.

    It is fiddly to initially set up, but once you get it going it is straight forward. You will of course end up up with a very large number of tests. And you are not testing how rules interact together for which you need scenario testing.

    FYI I used this approach to unit test Modified Cored Types in ODM Accelerator 

    Hope this helps.

    Peter

    Mobile:  France +33 753 423 222  UK +44 (0)7446 092554


    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 3.  RE: how to unit test individual rule

    Posted Wed March 01, 2023 03:19 AM

    Hello,

    there is no built-in feature to test a single rule. The unit of work is a ruleset.

    If you really want to test one or a few rules (a subset of the full ruleset that would go to production), you can create a dedicated Decision Operation for testing, along with a dedicated ruleflow, that is only selecting the subset of rules. 



    ------------------------------
    Antony Viaud
    Product Manager, IBM Decision Automation
    ------------------------------



  • 4.  RE: how to unit test individual rule

    Posted Tue March 07, 2023 04:02 AM

    Hi Mr Antony,

    we have close to 3k plus of rules. to maintain 3k of rules set will be a nightmare for us.



    ------------------------------
    Goh Kian Seng
    ------------------------------



  • 5.  RE: how to unit test individual rule

    Posted Wed March 01, 2023 05:16 AM
    Edited by Peter Warde Wed March 01, 2023 06:03 AM

    Hi Goh

    Here is what you need to add to the runtime rule selection on the ruleflow task:

          String modName = ?rule.name.replaceAll("\\$", "");
          modName = ?rule.name.replaceAll("_", " ");
          return modName.equals(offerTest.targetRule.trim());

    The first 2 lines clean up the the fully qualified name of the ODM rule. The last line selects the rule to be evaluated ('offerTest' is the ruleset parameter variable and 'targetRule' I added to the BOM and is the name of the rule to evaluate).

    In my Decision Runner test Scenario I added the name of the rule I wanted to test as follows:

    It works :-) !

    Peter




    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 6.  RE: how to unit test individual rule

    Posted Tue March 07, 2023 04:06 AM

    Hi Mr Peter,

    this seem working i would like to test it out on my environment.
    will get back to u once we got it :) thank u

    hope u can advice me on how to import excel(test suites) into ODM decision center :)
    i guess this is the last piece of puzzle to get the best out of ODM



    ------------------------------
    Goh Kian Seng
    ------------------------------



  • 7.  RE: how to unit test individual rule

    Posted Wed March 08, 2023 09:37 PM

    Hi Peter, possible to share this sample project?



    ------------------------------
    Goh Kian Seng
    ------------------------------



  • 8.  RE: how to unit test individual rule

    Posted Thu March 09, 2023 01:45 AM

    Hi Goh

    I only make ODM Accelerator available to clients I work for, and then under certain conditions.

    However you have everything there that you need to setup you own project. If you need to understand runtime rule selection look in the documentation here: Runtime rule selection

    I will try and reply to your other message at the weekend if nobody from IBM replies.

    Peter



    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 9.  RE: how to unit test individual rule

    Posted Thu March 09, 2023 02:20 AM
    Edited by Goh Kian Seng Thu March 09, 2023 02:21 AM

    Hi Peter i got this error.
    can advice where is my mistake? :)



  • 10.  RE: how to unit test individual rule

    Posted Thu March 09, 2023 09:44 AM

    Hi Goh

    You need to the code in the body i.e.

    body = dynamicselect(?rule) {

          String modName = ?rule.name.replaceAll("\\$", "");
          modName = ?rule.name.replaceAll("_", " ");
          return modName.equals(offerTest.targetRule.trim());

    }

    ?rule is an ODM implicit variable. Check the documentation link I sent for runtime rule selection.

    Peter



    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 11.  RE: how to unit test individual rule

    Posted Thu March 09, 2023 11:43 PM

    Hi Peter thanks for the quick reply. will try that out.

    By the way does IBM ODM has a API way to import back the testsuites to DC?



    ------------------------------
    Goh Kian Seng
    ------------------------------



  • 12.  RE: how to unit test individual rule

    Posted Thu March 16, 2023 06:05 AM

    Hi Goh,

    Hope it worked.

    With regard to importing testsuites in to DC, there is nothing in the REST API to do this so you will have to look at the ODM Java APIs ..... which can prove to be very complicated. As per you message on your CI/CD environment, I am not sure why you would want to have a DC in SIT/QA, PRE-PROD etc. when testing a ruleset. I suggest that you look at your CI/CD pipelines. The article here might help you re-think it through.

    Peter



    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 13.  RE: how to unit test individual rule

    Posted Wed March 01, 2023 11:22 PM

    Goh,

    What use case do you have in mind? Are you interested in testing a rule in the absence of other rules, or do you just want to ensure that each rule is thoroughly tested as part of you CI/CD process?

    Typically, it is not worthwhile to test stand-alone rules because:

    1. a well designed rule is very simple, to the point of being trivial
    2. the execution of a rule depends on the context (such as ruleflow, ruleset variables, etc.) and a stand-alone rule rarely makes sense from an execution standpoint. What is important is to test interactions among rules.

    Therefore, best practice is to test the entire ruleset, but have well designed test cases (built using Java / JSON) to activate and execute each individual rule. A rule-of-thumb is to have 2 positive and 2 negative test cases that test each individual rule. Use a JUnit-based test framework to execute them and use the rule trace to ensure that the rule is being triggered where appropriate. This suite of test cases should be executed as part of your CI/CD process.



    ------------------------------
    Raj Rao
    ------------------------------



  • 14.  RE: how to unit test individual rule

    Posted Sat March 04, 2023 03:08 AM
    Edited by Peter Warde Sat March 04, 2023 04:06 AM

    Hi Raj,


    Testing can be divided into 2 parts - IT and business testing. IT testing is for errors, and exceptions such as NPEs. Business testing is for business correctness.


    The amount of business testing of a ruleset is a business requirement. Some decisions are more valuable or critical than others; some rules in a decision may be so too. Hence testing of stand-alone rules may be required. The level of test coverage must be defined by the business upfront based on risk and resources. When you have a large ruleset with decision tables with many rows it might be not possible to test all rules, all conditions in rules. So the business might adopt a test strategy based on focusing or sampling, and accept some risk.


    With regard to JUnit, business testing cannot done with code testing fraemworks as business users do not understand them. That's why ODM provides Decision Runner. Business testing is best done by business users using Decision Center and Decision Runner. Once the tests are created developers can automated the tests for CI/CD using the ODM REST APIs as described here.

    Peter




    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 15.  RE: how to unit test individual rule

    Posted Tue March 14, 2023 08:51 AM

    Hi Peter,

    Completely agree with your division of testing into IT and business testing. I did not mean to minimize the utility of Decision Runner offered by ODM -- in fact, to encourage business testing, I've created this RuleScape Lynx Excel add-in. It is completely free to use -- if you have a complex domain model, I'd strongly encourage you to use this add-in.

    However, coming to the original post -- I maintain for reasons I already stated that it rarely, if ever, makes sense to test an isolated rule. In fact, that may point to the rule needing redesign. Additionally, CI/CD is an IT process and eventually you need to execute all IT tests and business tests as part of this process.



    ------------------------------
    Raj Rao
    ------------------------------



  • 16.  RE: how to unit test individual rule

    Posted Thu March 16, 2023 05:59 AM

    Thanks Raj ... and thanks for the link to your Excel add-in!



    ------------------------------
    Peter Warde
    peterwarde@rulearchitect.com
    ------------------------------



  • 17.  RE: how to unit test individual rule

    Posted Tue March 07, 2023 04:01 AM

    Hello guys! 

    sorry for the late reply. was busy recently.

    we have about 2000+ to 3k rules and individual rule from my perspective has some kind of complexity, unfortunately.
    i do understand that testing rules in a set, and we are doing that using test suites.
    we also like to test the individual rule regarding corner cases. 

    I do agree with Mr Peter, ruleset are are testing the business. but often business scenarios doesn't 100% cover IT issues.

    this also leads me to another questions. Why are can't import testSuite(excel) into ODM Decision center? because of our unique business/governance, we have use API (/v1/decisionservices/{decisionServiceId}/import) to import our project to our CICD environment. Apparently the API didn't import test suites.
    please advice how can we run this on automation.



    ------------------------------
    Goh Kian Seng
    ------------------------------