The Scenario

The following two diagrams highlight some basic workflows seen in many systems today. These examples aim to identify some of the challenges around virtualising services and how Service Virtualisation (HP SV) overcomes them.

Scenario One: Data creation
1. A client (user) makes a request to register a new customer.
2. The backend system generates an internal ID and stores it in the database.

HPSV1

Scenario Two: Data retrieval
1. A client (user) requests information about a registered customer.
2. The backend system performs a lookup on the database for the customer.
3. The client system displays the full registered customer information.

HPSV2

The Virtualisation Challenge

In many systems, simulation of the above scenarios would be very simple with HP SV – return the learned data. The client system would perform no verification on data at all – John Smith will always have an ID of 0001.

HPSV3

However, what would happen if the client system did perform verification of the data? What if:
• The client expects a unique ID to be generated each time, and stores the used IDs.
• The client stores the pairing of names to IDs.

The following diagrams outlines what would happen in HP SV for both of these client behaviours.

Scenario One: Data creation
1. A client (user) makes a request to register a new customer.
2. HP SV generates a unique ID via a sequence [core feature of HP SV simulation].
3. The client system accepts the simulation as the generated ID is unique.

HPSV4

All looking good so far, but then…

Scenario Two: Data retrieval
1. A client (user) requests information about a registered customer.
2. HP SV cannot use the ID from ‘Register’ – it is a different virtual service (data not available).
3. Instead, HP SV returns the learned data.
4. The client system rejects the simulation – it was expecting ID 7003 (as it also has a store).

HPSV5

The Solution – Service Virtualisation Contexts

To be able to pass data between virtual services, or store state, HP SV provides contexts. Effectively, contexts are pre-defined data structures that allow data to be stored and retrieved at various levels:
Server – Data is visible to all virtual services on the HP SV server.
Service – Data is visible only to the virtual service.
Operation – Data is visible only to a single operation within a virtual service.
Session – Data is visible only to the current user session.
Call – Data is visible only to one service call (i.e. until simulation of a single request ends).

These contexts are available for use in HP SV JavaScript and C# Rules (the latter introduced in HP SV 3.62). It is important to note that JavaScript rules may introduce performance impacts to simulated services. As a rule of thumb, if you are using HP SV to simulate services that are to be used in load testing – use the C# rules.

Implementation – JavaScript Rules

The following example code snippets provide an example JavaScript implementation of the scenarios outlined above. In this example, the ‘Register Customer’ and ‘Retrieve Customer’ APIs are implemented as separate virtual services. This indicates that the server context is required to pass data between them.

Scenario One: Data creation
1. Validate customer ID has been created in the response – allows other SV rules to generate it
2. Validate the first and last name have been provided in the request – data to be stored
3. Store the data in the server context – stores data state for future retrieval
Note: Typically you would use an ID as the key for storage/lookup – this is a simple example.

HPSV6

Scenario Two: Data retrieval
1. Validate there is data stored in the server context – name lookup
2. Set the customer ID in the response to the same value set in ‘Register Customer’
3. Delete the server context data – if necessary [free up memory]

HPSV7

Implementation – C# Rules
As of HP SV 3.62, C# rules have been introduced as an alternative to JavaScript rules. If performance is a consideration, it is recommended C# rules be used instead of JavaScript. The following example code snippets show the equivalent code to the JavaScript example above.

Scenario One: Data creation

HPSV8

Scenario Two: Data retrieval

HPSV9

Tech tips from JDS

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.