Hello World - Quick Start

Hello World is a simple demonstration of stateful simulation of REST service. The Hello World demo is located in the Lab/demo/hello-world directory in the Service Virtualization installation folder.

Running the Hello World demo

This section describes how to run the Hello World demo and shows you the source code.

To run the Hello World demo:

  1. Do one of the following, depending on where you want SV Lab Server to run from:

    To run the demo Launch the pre-compiled demo class using:
    On the local simulator:

    run[.sh|.bat]

    The prompt is displayed when the demo is ready.

    On a different SV Lab Server:

    run-with-server[.sh|.bat]

    The simulator runs in a separate process (on a separate machine), keeping the library dependencies that are introduced to the lab initialization code significantly more light-weight.

  2. Load the simulated endpoint from the web browser:

    http://localhost:30000/hello

Source Code

.
|  pom.xml ............................... project for your IDE
+--src
   \--main
      +--java
      |  +--demo
      |     HelloWorld.java .............. the code configures and launches
      |                                    the virtual REST service in
      +--resources                         simulation mode
         |  LabConfig.json ............... virtual lab configuration
         +--demo
            HelloApplicationModel.js ..... links to the simulated service
            HelloServiceModel.js ......... describes the simulated behavior
                                           of the Hello Service

Back to top

How It Works

This section describes basic concepts and shows you how to create a virtual lab, known as SV Lab.

This section includes:

Introducing Service Model

The behavior of the service is described by service model as defined in src/main/resources/demo/HelloServiceModel.js. The model is written using domain specific language based on ECMAScript 6. Following is a brief introduction for service model. For more details, see ECMAScript 6.

All the service virtualization domain related language constructs are imported from the lib/sv-vsl.js file:

import * as sv from "../../../../../../lib/sv-vsl.js"

Every service model extends the ServiceModel class:

export class HelloServiceModel extends sv.ServiceModel {
    ...

The service is passed as a constructor parameter and is stored in scenarios for later use. The type of the service can be either a custom service interface or a generic RestServiceInterface, for example:

constructor(s: sv.RestServiceInterface) {
    super(s);
    this.hello = s;
}

To learn more, see Service Models.

Back to top

Service Scenario

Every behavior that the service can simulate is described in a scenario. Every scenario is a method annotated with the @scenario annotation, for example:

@sv.scenario
simpleSayHello() {
    ...
}

The scenario contains a sequence of service calls describing the request and response, for example:

this.hello.GET("/hello")
    .withRequest()
    .withResponse({greeting : "Hello, world!"}, sv.JSON)    // first response                     
        .withHeaders({"Content-Type": "application/json"})     
        .withStatusCode(200);

this.hello.GET("/hello")
    .withRequest()
    .withResponse({greeting : "Hello again!"}, sv.JSON)     // second response                     
        .withHeaders({"Content-Type": "application/json"})     
        .withStatusCode(200); 

Every time a scenario like this is simulated, it expects a GET request. The simulator returns the first response as a JSON type. Then the scenario expects a similar request and returns the second response (Hello again!).

Back to top

Stateless Simulation Fallback

When the simulator does not match the expected message, for example, because of request parameters, headers, or body mismatches, the simulator falls back to the stateless simulation strategy, responding with the first message from the scenario.

To learn more, see Simulating Stateless Services

Back to top

Ignoring the Request Headers

Different REST clients send different request headers. To build a robust simulation, it is reasonable to ignore the headers in most cases rather than use them to control the simulation. You can do this using a simulation variable and assigning it the ignore validator. See the Hello-Maven demo in Run Virtual Lab from Test (JSON, Maven) if you want to see simulation variables in action.

return this.hello.GET("/hello")
    .withRequest()
        .withHeaders(sv.svVar({"Accept": "application/json"})   // usual request headers
            .withValidator(sv.Ignore))                          // ignore headers during simulation
    .withResponse(...)                       
        ...

To learn more, see Ignoring Parts of Message, Headers During Simulation.

Back to top

Application Model

SV Lab can simulate multiple virtual services in parallel, exhibiting various behaviors during different test cases. For each test case, an application scenario is declared and then run at when the test is initiated.

In this example, one sayHello application scenario is declared using only one service:

@sv.applicationScenario
sayHello() {
    this.hello.simpleSayHello();
}

After the scenario is declared, the scenario launches from Java:

sv.runSimulation("sayHello");

Learn more about the application model in Application Models.

Back to top

Create a virtual lab

To run and use the simulation, you must configure and start a virtual lab. You can configure the virtual lab using a JSON file, or you can set it up and control it using the SV API. For details, see Configure Virtual Lab Programmatically Using Service Virtualization Java API.

Example

  1. Load the lab configuration from the main/resources/LabConfig.json file and compile the module from the application and service model sources—HelloApplicationModel.js and HelloServiceModel.js:

    sv.loadActiveVirtualLab("classpath:/LabConfig.json", sv.compileModuleFromSources("classpath:/demo/*"));
  2. In the configuration file, define the virtual lab where all of the services will run and pick the application model to use:

    "virtualLab": [
      {
        "id": "virtual-lab:30000",
        "displayName": "Hello Lab",
        "applicationModelId": "HelloApplicationModel"
      }
    ],
  3. Create an HTTP connector to provide connectivity for the REST virtual service to simulate:

    "connector": [
      {
        "id": "connector",
        "connectorType": "httpGateway",
        "properties": {
          "bindPort": 30000
        }
      }
    ],
  4. Create a service instance specifying where the virtual endpoint path will be exposed and picking the SIMULATE_SERVICE simulation mode.

     

    "virtualServiceInstance": [
      {
        "virtualServiceId": "HelloServiceModel.RestServiceInterface",
        "serviceMode": "SIMULATE_SERVICE",
        "endpoints": [
          {
            "properties": {
              "httpVirtualPath": "/"
            },
            "requestConnectorId": "connector",
            "responseConnectorId": "connector",
            "realRequestConnectorId": "connector",
            "realResponseConnectorId": "connector"
          }
        ]
      }
    ]
  5. Start virtual lab and the sayHello scenario from Java.

    sv.startActiveVirtualLab();
    sv.runSimulation("sayHello");

Back to top

Next steps: