Add a custom guardrail warning
This knowledge sharing article shows how to configure a new custom guardrail warning that is client specific and not Pega OOTB.
Pega Infinity version used: 23.1.4
Client use case
Pega guardrail score is used to measure Pega application quality. It shows how well an application is built using Pega's best practices and OOTB capabilities, while limiting custom solutions that can hamper the updatability of the application.
However, sometimes, OOTB guardrail warnings might not meet some client-specific guardrail requirement. In our case, our client wanted to throw two custom guardrail warnings: 1) SEV3 (Informational) warning when unit test case rule is not documented and 2) SEV1 (Severe) warning when unit test case rule is linked to more than one test suite.
The following configurations show how to add a new custom guardrail warning using an extension activity provided in the Pega Platform. This same approach can be used to create other types of custom warnings for clients.
Configurations
The configurations are organized into:
- Part A - 'Save As' the OOTB @baseclass.CheckForCustomWarnings extension activity to the class of the rule type where the new custom warnings are applied to.
- Part B - Configure the client-specific logics for the new custom guardrail warnings.
- Part C - Run a test.
## Part A - 'Save As' the OOTB @baseclass.CheckForCustomWarnings extension activity to the class of the rule type where the new custom warnings are applied to.
Step 1: Open the OOTB @baseclass.CheckForCustomWarnings activity rule.
This is the extension point.
Step 2: 'Save As' the activity to the 'Rule-Test-Unit-Case' class.
This is the rule type where we need to create the new client-specific warnings.
Note: The target class will be different based on your rule type. For example, if you want to create a new custom warning for Data Transform rule type, then save the activity to the 'Rule-Obj-Model' class.
## Part B - Configure the client-specific logics for the new custom guardrail warnings.
Step 1: Configure a logic to throw SEV3 (Informational) warning if the description and usage of unit test case rule are both empty.
Call an OOTB function, @pxAddGuardrailMessageWrapper (in yellow), and pass the message rule (in purple).
In the WHEN pre-condition of Step 1, enter the following code to execute the step (if True) or skip it (if False).
@trim(.pyDescription) == "" && @trim(.pyUsage) == ""
Now, whenever a unit test case rule is saved, this activity will run. If both description and usage fields are empty, user will see the SEV3 (Informational) guardrail warning.
Step 2: Configure a logic to throw SEV1 (Severe) warning if the unit test case is linked to more than one test suite.
In Step 3 & 4, we are calling a report definition to find out a list of all test cases and their linked test suites. Building the report definition is not in the scope of this article. If you are interested in knowing more about the report definition, please post a message below.
In Step 5, if the result count is greater than 1, then we're proceeding and throwing a new SEV1 warning. Else, skip to the end of activity.
In Step 7, we are calling a Function to add the new SEV1 guardrail warning. We had to write this new function because the OOTB @pxAddGuardrailMessageWrapper Function rule wasn't taking a parameter.
Here is the new Function rule that shows how we are passing the two parameters to the OOTB pxAddGuadrailMessage function, which in turn references the message rule that contains the warning details. The parameters are used to show some variables in the warning messages such as the number of test suites and the names of test suites linked to the unit test case rule to better inform the developers.
// Get the parameter values String tsCount = tools.getParamValue("TSCount"); String tsNames = tools.getParamValue("TSNames");
// Setup the parameters passed to the pxAddGuardrailMessage function. HashStringMap params = new HashStringMap(); params.putString("TSCount", tsCount); params.putString("TSNames", tsNames);
// Add the guadrail warning. com.pegarules.generated.pega_wb_default.pxAddGuardrailMessage(null, "DuplicateTestCaseInTestSuite", params);
Here is the message rule.
## Part C - Run a test.
Test 1: Save a unit test case rule that is missing the Description & Usage documentation.
It throws the new custom SEV3 (Informational) guardrail warning.
Test 2: Save a unit test case rule that is linked to more than 1 test suite.
It throws the new custom SEV1 (Severe) guardrail warning. Notice the variables (in yellow) that we passed as parameters to the message rule.
Additional information
-
Whenever we save a unit test case rule, we can see that the Rule-Test-Unit-Case.CheckForCustomWarnings activity is running to evaluate the new custom warning logics.
-
Please leave any question or comment.