Chapter 74. Executing a test scenario using the KIE Server REST API

Directly interacting with the REST endpoints of KIE Server provides the most separation between the calling code and the decision logic definition. You can use the KIE Server REST API to execute the test scenarios externally. It executes the test scenarios against the deployed project.

Note

This functionality is disabled by default, use org.kie.scenariosimulation.server.ext.disabled system property to enable it.

For more information about the KIE Server REST API, see Interacting with Red Hat Process Automation Manager using KIE APIs.

Prerequisites

  • KIE Server is installed and configured, including a known user name and credentials for a user with the kie-server role. For installation options, see Planning a Red Hat Process Automation Manager installation.
  • You have built the project as a KJAR artifact and deployed it to KIE Server.
  • You have the ID of the KIE container.

Procedure

  1. Determine the base URL for accessing the KIE Server REST API endpoints. This requires knowing the following values (with the default local deployment values as an example):

    • Host (localhost)
    • Port (8080)
    • Root context (kie-server)
    • Base REST path (services/rest/)

    Example base URL in local deployment for the traffic violations project:

    http://localhost:8080/kie-server/services/rest/server/containers/traffic_1.0.0-SNAPSHOT

  2. Determine user authentication requirements.

    When users are defined directly in the KIE Server configuration, HTTP Basic authentication is used and requires the user name and password. Successful requests require that the user have the kie-server role.

    The following example demonstrates how to add credentials to a curl request:

    curl -u username:password <request>

    If KIE Server is configured with Red Hat Single Sign-On, the request must include a bearer token:

    curl -H "Authorization: bearer $TOKEN" <request>
  3. Specify the format of the request and response. The REST API endpoints work with XML format and are set using request headers:

    XML

    curl -H "accept: application/xml" -H "content-type: application/xml"

  4. Execute the test scenario:

    [POST] server/containers/{containerId}/scesim

    Example curl request:

    curl -X POST "http://localhost:8080/kie-server/services/rest/server/containers/traffic_1.0.0-SNAPSHOT/scesim"\ -u 'wbadmin:wbadmin;' \ -H "accept: application/xml" -H "content-type: application/xml"\ -d @Violation.scesim

    Example XML request:

    <ScenarioSimulationModel version="1.8">
      <simulation>
        <scesimModelDescriptor>
          <factMappings>
            <FactMapping>
              <expressionElements/>
              <expressionIdentifier>
                <name>Index</name>
                <type>OTHER</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>#</name>
                <className>java.lang.Integer</className>
              </factIdentifier>
              <className>java.lang.Integer</className>
              <factAlias>#</factAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>70.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements/>
              <expressionIdentifier>
                <name>Description</name>
                <type>OTHER</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Scenario description</name>
                <className>java.lang.String</className>
              </factIdentifier>
              <className>java.lang.String</className>
              <factAlias>Scenario description</factAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>300.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Driver</step>
                </ExpressionElement>
                <ExpressionElement>
                  <step>Points</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|1</name>
                <type>GIVEN</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Driver</name>
                <className>Driver</className>
              </factIdentifier>
              <className>number</className>
              <factAlias>Driver</factAlias>
              <expressionAlias>Points</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Violation</step>
                </ExpressionElement>
                <ExpressionElement>
                  <step>Type</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|6</name>
                <type>GIVEN</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Violation</name>
                <className>Violation</className>
              </factIdentifier>
              <className>Type</className>
              <factAlias>Violation</factAlias>
              <expressionAlias>Type</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Violation</step>
                </ExpressionElement>
                <ExpressionElement>
                  <step>Speed Limit</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|7</name>
                <type>GIVEN</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Violation</name>
                <className>Violation</className>
              </factIdentifier>
              <className>number</className>
              <factAlias>Violation</factAlias>
              <expressionAlias>Speed Limit</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Violation</step>
                </ExpressionElement>
                <ExpressionElement>
                  <step>Actual Speed</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|8</name>
                <type>GIVEN</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Violation</name>
                <className>Violation</className>
              </factIdentifier>
              <className>number</className>
              <factAlias>Violation</factAlias>
              <expressionAlias>Actual Speed</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Fine</step>
                </ExpressionElement>
                <ExpressionElement>
                  <step>Points</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|11</name>
                <type>EXPECT</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Fine</name>
                <className>Fine</className>
              </factIdentifier>
              <className>number</className>
              <factAlias>Fine</factAlias>
              <expressionAlias>Points</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Fine</step>
                </ExpressionElement>
                <ExpressionElement>
                  <step>Amount</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|12</name>
                <type>EXPECT</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Fine</name>
                <className>Fine</className>
              </factIdentifier>
              <className>number</className>
              <factAlias>Fine</factAlias>
              <expressionAlias>Amount</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
            <FactMapping>
              <expressionElements>
                <ExpressionElement>
                  <step>Should the driver be suspended?</step>
                </ExpressionElement>
              </expressionElements>
              <expressionIdentifier>
                <name>0|13</name>
                <type>EXPECT</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Should the driver be suspended?</name>
                <className>Should the driver be suspended?</className>
              </factIdentifier>
              <className>string</className>
              <factAlias>Should the driver be suspended?</factAlias>
              <expressionAlias>value</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
          </factMappings>
        </scesimModelDescriptor>
        <scesimData>
          <Scenario>
            <factMappingValues>
              <FactMappingValue>
                <factIdentifier>
                  <name>Scenario description</name>
                  <className>java.lang.String</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>Description</name>
                  <type>OTHER</type>
                </expressionIdentifier>
                <rawValue class="string">Above speed limit: 10km/h and 30 km/h</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Driver</name>
                  <className>Driver</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|1</name>
                  <type>GIVEN</type>
                </expressionIdentifier>
                <rawValue class="string">10</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Violation</name>
                  <className>Violation</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|6</name>
                  <type>GIVEN</type>
                </expressionIdentifier>
                <rawValue class="string">&quot;speed&quot;</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Violation</name>
                  <className>Violation</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|7</name>
                  <type>GIVEN</type>
                </expressionIdentifier>
                <rawValue class="string">100</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Violation</name>
                  <className>Violation</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|8</name>
                  <type>GIVEN</type>
                </expressionIdentifier>
                <rawValue class="string">120</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Fine</name>
                  <className>Fine</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|11</name>
                  <type>EXPECT</type>
                </expressionIdentifier>
                <rawValue class="string">3</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Fine</name>
                  <className>Fine</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|12</name>
                  <type>EXPECT</type>
                </expressionIdentifier>
                <rawValue class="string">500</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>Should the driver be suspended?</name>
                  <className>Should the driver be suspended?</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>0|13</name>
                  <type>EXPECT</type>
                </expressionIdentifier>
                <rawValue class="string">&quot;No&quot;</rawValue>
              </FactMappingValue>
              <FactMappingValue>
                <factIdentifier>
                  <name>#</name>
                  <className>java.lang.Integer</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>Index</name>
                  <type>OTHER</type>
                </expressionIdentifier>
                <rawValue class="string">1</rawValue>
              </FactMappingValue>
            </factMappingValues>
          </Scenario>
        </scesimData>
      </simulation>
      <background>
        <scesimModelDescriptor>
          <factMappings>
            <FactMapping>
              <expressionElements/>
              <expressionIdentifier>
                <name>1|1</name>
                <type>GIVEN</type>
              </expressionIdentifier>
              <factIdentifier>
                <name>Empty</name>
                <className>java.lang.Void</className>
              </factIdentifier>
              <className>java.lang.Void</className>
              <factAlias>Instance 1</factAlias>
              <expressionAlias>PROPERTY 1</expressionAlias>
              <factMappingValueType>NOT_EXPRESSION</factMappingValueType>
              <columnWidth>114.0</columnWidth>
            </FactMapping>
          </factMappings>
        </scesimModelDescriptor>
        <scesimData>
          <BackgroundData>
            <factMappingValues>
              <FactMappingValue>
                <factIdentifier>
                  <name>Empty</name>
                  <className>java.lang.Void</className>
                </factIdentifier>
                <expressionIdentifier>
                  <name>1|1</name>
                  <type>GIVEN</type>
                </expressionIdentifier>
              </FactMappingValue>
            </factMappingValues>
          </BackgroundData>
        </scesimData>
      </background>
      <settings>
        <dmnFilePath>src/main/resources/org/kie/example/traffic/traffic_violation/Traffic Violation.dmn</dmnFilePath>
        <type>DMN</type>
        <fileName></fileName>
        <dmnNamespace>https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF</dmnNamespace>
        <dmnName>Traffic Violation</dmnName>
        <skipFromBuild>false</skipFromBuild>
        <stateless>false</stateless>
      </settings>
      <imports>
        <imports/>
      </imports>
    </ScenarioSimulationModel>

    Example XML response:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <response type="SUCCESS" msg="Test Scenario successfully executed">
          <scenario-simulation-result>
                <run-count>5</run-count>
                <ignore-count>0</ignore-count>
                <run-time>31</run-time>
          </scenario-simulation-result>
    </response>