Adding JSON scenarios

You define unit test scenarios in JSON files. You create one JSON file per scenario.

About this task

A JSON file consists of three parts: a name, input data, and a list of assertions.

You can retrieve the input data content from Decision Designer by copying a JSON payload from a data set in the Run tab. For more information about the Run feature, see Running models with test data. Alternatively, you can find your data sets in the <model>/resources/validate_dataset folder of your decision library.

You can define one or more assertions in a test. Each assertion consists of five items:
label (optional)
Specifies a name for the assertion. It is displayed when the assertion fails. If no label is provided, a default value in the format #<assertion number> is used.
pointer
Specifies the location of the value in the output to validate, by using a JSON Pointer. The following examples are valid JSON Pointers:
  • "" points to the root of the document. For decisions that return a single value, such as a number or a string, it is the only possible pointer.
  • "/birthdate" points to the value of the property named "birthdate" in the top‑level object of the output.
  • "3" points to the fourth element of a list (index starts at 0).
For more information about JSON Pointers, see JavaScript Object Notation (JSON) Pointer External link opens a new window or tab.
operator
Specifies the comparison operation. The list of supported operators is available in Table 1.
ignoreOrder (optional)
Indicates whether to ignore the order of elements when comparing lists. It is set to false by default.
expected
Defines the expected value. For most operators, this value is required to have the same JSON type as the actual value, that is, the value at the pointer’s target within the output.
Table 1. List of supported operators in assertions
Operator Definition Example Details
Contains
{
      "label": "Assertion 1",
      "pointer": "/customer/address", 
      "operator": "⊃",
      "expected": "Paris"
}

These operators can be used on any type of data.

They support the ignoreOrder option for lists.

Does not contain
{
      "label": "Assertion 1",
      "pointer": "/customer/address", 
      "operator": "⊅",
      "expected": "London"
}
= Equals
{
      "label": "Assertion 1",
      "pointer": "/approval/approved", 
      "operator": "=",
      "expected": false
}

These operators can be used on any type of data.

They support the ignoreOrder option for lists.

!= Does not equal
{
      "label": "Assertion 1",
      "pointer": "/approval/approved",
      "operator": "!=",
      "expected": false
}
> Is greater than
{
      "label": "Assertion 1",
      "pointer": "/customer/age", 
      "operator": ">",
      "expected": 18 
}
These operators can be used on numbers.
Is greater than or equal to
{
      "label": "Assertion 1",
      "pointer": "/customer/age", 
      "operator": "≥",
      "expected": 18 
}
< Is less than
{
      "label": "Assertion 1",
      "pointer": "/customer/age", 
      "operator": "<",
      "expected": 18 
}
Is less than or equal to
{
      "label": "Assertion 1",
      "pointer": "/customer/age", 
      "operator": "≤",
      "expected": 18 
}
Is within a range
{
      "label": "Assertion 1",
      "pointer": "/customer/age", 
      "operator": "∈",
      "expected": { 
        "min": 10, 
        "max":15, 
        "minExclusive": true, 
        "maxExclusive": true 
  }
}
These operators can be used on numbers.
Is not within a range
{
      "label": "Assertion 1",
      "pointer": "/customer/age", 
      "operator": "∉",
      "expected": { 
        "min": 10, 
        "max":15,  
  }
}

Procedure

  1. Create a .json file in the src/test/resources/<functionID> directory, for example lowcreditscore.json.
    For more information about creating the src/test directory, see Where to define test scenarios.
  2. Define your scenario using the details provided in the About this task section.
    The following example shows a complete test file that is named too big debt income ratio that contains two assertions:
    {
      "name": "too big debt income ratio",
      "input": {
        "loan": {
          "numberOfMonthlyPayments": 72,
          "startDate": "2020-06-01T00:00:00Z",
          "amount": 185000,
          "loanToValue": 0.7
        },
        "borrower": {
          "firstName": "John",
          "lastName": "Doe",
          "SSN": {
            "areaNumber": "123",
            "groupCode": "45",
            "serialNumber": "6789"
          },
          "latestBankruptcy": {
            "date": "2019-03-10T00:00:00Z",
            "chapter": 7,
            "reason": "Unemployment"
          },
          "yearlyIncome": 100000,
          "zipCode": "91320",
          "creditScore": 500,
          "birthDate": "1968-05-12T00:00:00Z"
        },
        "currentTime": "2020-02-04T08:41:21.242Z"
      },
      "assertions": [
        {
          "label": "Approved",
          "pointer": "/approval/approved",
          "operator": "=",
          "expected": false
        },
        {
          "label": "Message, sorry",
          "pointer": "/approval/message",
          "operator": "⊃",
          "expected": "sorry"
        }
      ]
    }

What to do next

You are now ready to run your unit tests:
  • To run unit tests locally, follow the instructions that are described in Building and deploying from a CI/CD stack.
  • To run unit tests in Decision Designer, you must first push all the content of the src/test folder to your Git repository and load it to Decision Designer. When you are ready to run your unit tests, follow the instructions that are described in Building and deploying from Decision Designer. Click the View report icon View logs icon next to the test status to see the test results.
Important: Unit tests run every time that you deploy your decision service. Update unit tests whenever you update the model that you are testing to avoid deployment failures.