Lab 9 - Create test scenarios

Lab Goal

To create test scenarios to validate our rules, events, and process for the retail web shop

Creating test scenarios

Designing the web shop rules and rule flow process has completed. Now it is time to test what you've have created to validate before putting it into production using the Decision Manager's test suite:
  • we will guide you through the first test
  • you will design a few more based on detailed descriptions
  • extra credit if you come up with more tests!

Create new test scenario

Now let's create a new test scenario for our Domain Specific Language (DSL) rule:
  • First have to return to Asset view
  • Click on retail-webshop project in bread crumb menu at top

Create new test scenario

Click on ADD ASSET -> TEST SCENARIO (LEGACY) for pop-up:
  • Design a test to validate our Domain Specific Language (DSL)
  • Our 'Guided Rule with DSL' is about giving free shipping
  • TEST SCENARIO: Free Shipping Promotion DSL Test
  • Fill in package as shown
  • Click on +OK to create test

Create test scenario

Design a test to validate our DSL as follows:
  • if the shopping cart total value is $75 or more, free shipping
  • we have an empty test now in front of us
  • open tab 'Data Objects' to verify availability of our model

Create test scenario

Add the data model objects (4x) for our test, using +NEW ITEM button until looks as shown:

Creating test scenarios

Now in Test Scenario tab we focus on these sections:
  • Given - define input fact objects
  • Expect - define method to call on given fact objects

Create test scenario

Provide the test a given fact object:
  • Click on +GIVEN button in left top corner
  • assign field Activate rule flow group: promo-rules
  • click on ADD button next to field

Create test scenario

Provide the test a given fact object:
  • Click on +GIVEN button in left top corner
  • assign field Insert a new fact: ShoppingCart
  • assign field Fact name: $sc1
  • click on ADD button next to field

Create test scenario

Adding two fields to shopping cart ($sc1):
  • click on Add a field for ShoppingCart[$sc1]
  • using menu, select cartItemTotal and click on +OK button
  • click on ShoppingCart[$sc1] to add a second field
  • using menu, select shippingTotal and click on +OK button

Create test scenario

Add values to both fields in shopping cart ($sc1):
  • click on pencil icon to add Literal value to cartItemTotal
  • enter values of 75
  • repeat to add Literal value to shippingTotal
  • enter values of 10.99

Don't forget to save

  • Click SAVE or SAVE WITH COMMENTS
  • This is how it should look so far:

Create test scenario

Add a second shopping cart ($sc2) with following fields & values:
  • cartItemTotal with a value of 74.99
  • shippingTotal with a value of 10.99
  • Note that fields already exist and can be used for new values for $sc2

Don't forget to save

  • Click SAVE or SAVE WITH COMMENTS
  • This is how it should look so far:

Create test scenario

Detail what we expect (in EXPECT section) as outcome of our test:
  • click on +EXPECT
  • we want to add a rule expected to fire
  • insert this RULE: Free Shipping Promotion DSL
  • click on OK

Create test scenario

Now we need an expectation as to the outcome of the first shopping cart ($sc1):
  • click on +EXPECT
  • select from Fact value: $sc1
  • click on Add

Create test scenario

Then add field and set to the expected value of shipping total:
  • click on ShoppingCart $sc1
  • add field shippingTotal
  • click on +OK
  • as shopping cart value is 75 we expect value of 0

Create test scenario

Add expectation for the outcome of the second shopping cart ($sc2):
  • add the fact $sc2
  • add field shippingTotal
  • as shopping cart value is 74.99 we expect value of 10.99

Don't forget to save

  • Click SAVE or SAVE WITH COMMENTS
  • This is how it should look so far:

Run test scenario

We can now run our test scenario:
  • ensure under tab Settings that All rules may fire
  • click on Run scenario button in top right
  • want Test Results: Passed in Test Report pane

Run test scenario

Also note test output in Model tab:
  • looking for blue 100% passed in Test Report pane
  • EXPECT section validate results are marked with green check

Run test scenario

Note that there is little feedback on test failures, this makes finding problems difficult at this time. Strong hint is to design a test at a time:
  • if test works, move to next test
  • unable to run 'All Test Scenarios' when more tests available
  • have to run each tests in suite

Creating four test scenarios

You'll now create each of the following tests with less hand holding:
  • Shipping Rules - One Cart Test
  • Shipping Rules Test
  • Total Shopping Cart Items Test
  • Total Shopping Cart Test

Shipping rules - one cart test

Outline for Shipping Rules - One Cart Test:
  • Add all four data objects in tab Data Objects
  • use shipping-rules as rule flow group and default package
  • add ShoppingCart data object
  • Given:
    • ShoppingCart ($sc) with cartItemTotal of 0.00
  • Expect:
    • expect rules Row 1 Shipping Rules to fire just 1 time
    • ShoppingCart [$sc] has shippingTotal value of 2.99
    • (continued on next slide...)

Should look like this so far...

Shipping rules - one cart test

Use the MORE button to extend this test:
  • Given:
    • modify existing fact ShoppingCart [$sc]
    • set cartItemTotal to 24.00 & shippingTotal to 0
  • Expect:
    • expect rules Row 1 Shipping Rules to fire just 1 time
    • ShoppingCart [$sc] has shippingTotal value of 2.99
    • (continued on next slide...)

It should look like this so far (split)...

Shipping rules - one cart test

You've notices this is going to be a long test scenario, so the next test sections you create based on following images using the MORE button, then save and run the tests to validate:

Shipping rules - one cart test

Create two more test sections based on following images using MORE button, then save and run the tests to validate:

Shipping rules test

Create a new test scenario called Shipping Rules Test:
  • add all four data objects in tab Data Objects
  • shipping-rules as rule flow group and default package
  • add ShoppingCart data object
  • Given:
    • add ShoppingCart ($sc1) - ShoppingCart ($sc9)
    • add cartItemTotal with values for each: 0.00, 24.99, 25, 49.99, 50, 74.99, 75, 99.99, 100

Shipping rules test

Setup EXPECT section as shown below:

Shipping rules test

Setup EXPECT section for fired rules as shown below:

Total shopping cart items test

Create a new test scenario Total Shopping Cart Items Test:
  • PACKAGE: com.redhat.demo.retail.retail_webshop
  • use pricing-rules as rule flow group
  • Given:
    • setup given section of this rule based on image below

Total shopping cart items test

Setup EXPECT section as shown below, save and run scenario to validate:

Total shopping cart test

Create a new test scenario Total Shopping Cart Test:
  • PACKAGE: default
  • use total-rules as rule flow group
  • GIVEN: setup given section of this rule based on image below

Total shopping cart test

Setup EXPECT section as shown below, save and run scenario to validate:

Creating Test Scenarios

  • Run 'Total Shopping Cart Test' test, it fails!? fix it!
  • Hint: Total shopping cart guided rule is totaling the cart to '0.0' and it should be adding together the shopping cart item total with the shipping total. Fix this by changing the literal field to formula:
    • $sc.cartItemTotal + $sc.shippingTotal

Total shopping cart test

After fixing 'Total Shopping Cart' guided rule, run this test scenario:
  • Note it still fails due to a rounding error, so fix by adjusting that field
  • Now the test scenario passes!!!

Validate project builds:

Click on breadcrumb 'retail-webshop' to return to asset view:
  • click 'Build' button at top
  • note green validation of build

Lab results...

Lab Completed

What you should see...

  • Click on retail-web shop project (use bread crumb menu at the top) for assets view, this ends lab 9:
Eric D. Schabell
Portfolio Architect Director
@ericschabell
http://schabell.org
More Red Hat Decision Manager Workshops

Red Hat Decision Manager Articles

Red Hat Decision Manager Demos

Decision Manager Workshop

Up next...

Lab 10 - Deploying your retail web shop