How to use SV as Code

Document created by srisa09 Employee on Sep 8, 2017Last modified by srisa09 Employee on Oct 24, 2017
Version 3Show Document
  • View in full screen mode

CodeSV is a framework for HTTP-based Services. It’s an open-source lightweight edition of Service Virtualization, enabling end user to create their own Virtual Service at Unit Testing level.

 

It enables end user to stay productive when the dependent APIs doesn't exist or isn't complete. It supports generic Java IDEs like Eclipse, IntelliJ, etc.

CodeSV can be downloaded from GitHub CodeSV - Github page, under Release.

 

Why CodeSV:

 

  • Simple - The simple, fluent, Java-based interface (API) lets developers virtualize HTTP traffic in just a few minutes. By using CodeSV, end user will no longer need to spend unnecessary time configuring endpoints. Endpoints are auto-generated. “In-process virtualization” (IPV) artifacts are created on the fly, saving time and avoiding the annoyance of doing it manually.
  • Versatile - Request/Response (RR) pairs can be imported and exported so that virtual assets can be reused and even shared across teams. This eliminates the need to create new assets from scratch and, again, accelerating the time to test.
  • For Developers - CodeSV has been built by developers for developers. It supports virtualizing both REST and SOAP services. So, we’ve got your web service virtualization covered.


How to integrate with Current Setup:

CodeSV libraries needs to be integrated with the current Project Setup, libraries can be manually imported or the build file can be modified to add the dependencies.

The configuration information is available at the site -> CodeSV - Quick Start Guide.

CodeSV requires JDK (not jre) 1.8 to execute.

 

How to use CodeSV:

Define
Integration with CodeSV requires following Step

1. Import Classes for CodeSV

import static com.ca.svcode.protocols.http.fluent.HttpFluentInterface.*; 
import com.ca.svcode.engine.junit4.VirtualServerRule;

2. Create a jUnit rule
@Rule
public VirtualServerRule vs = new VirtualServerRule();

3. Define Global Variable to reuse(Its not important to define these variables, but defining it will save time)
//URL - For Http Get/Post/Put/Delete
private static final String URL= "http://www.ca.com/portfolio";
//Sample Json Data {"productNamesList": ["CA Server Automation","CA Service Catalog","CA Service Desk Manager","CA Service Management","CA Service Operations Insight","CA Service Virtualization"]}
private static final String JSON_EXAMPLES_PORTFOLIO = "{\"productNamesList\": [\"CA Server Automation\",\"CA Service Catalog\"]}";

4. Create Virtual Rest API:
//URL is the same global variable defined, JSON_EXAMPLES_PORTFOLIO is the response send when the URL is hit.
forGet(URL).doReturn(
okMessage()
.withJsonBody(JSON_EXAMPLES_PORTFOLIO)
);
This method will acts as a Virtual Service providing a recorded response which can be used to test the functionality

Example:

 

CodeSV using Magic Strings, Magic Dates e.g.:
   
private static final String BASE_URL = "http://www.ca.com/portfolio";
   private static final String QUERY = "&tokenQuery=X4sPhj15WQE";

   // This will be create this URL- > http://www.ca.com/portfolio/{id}?year=2016&tokenQuery=X4sPhj15WQE
   private static final String URL = BASE_URL + "/{id}" + "?year={year}" +QUERY;

   private static final String JSON_EXAMPLES_PORTFOLIO = "{" + "\"portfolio\": {\n" + " \"id\": \"${attribute.id}\",\n" + " \"year\": \"${argument.year}\",\n"+ " \"language\": \"${metadata.Language}\",\n"+" \"productNamesList\": [\n"
   + " \"CA Server Automation\" + " ]\n" + "}}";
Used with forGet Method:
 
  forGet(URL)
      .matchesHeader("Language", is("en_us")).matchesQuery("year", "2016")

      .doReturn(okMessage() .withJsonBody(JSON_EXAMPLES_PORTFOLIO)
      );

More information is available at the site -> CodeSV Magic Strings.

 

Example:


// HttpGet request can be made by constructing URL, "/1" is the id and it can be any value and also the year
HttpGet request = new HttpGet(BASE_URL + "/1" + "?year=2017" + QUERY);

CodeSV can be used for Post Call Verification using the verifyGet method available with the library.

Example:

 

CodeSV be used to create Request/Response pairs that can be reused for Integration/Acceptance testing:

In order to export the R/R pairs the below code needs to be used:

   //RR pairs will be created in the folder defined for export.

   @Before
      public void beforeClass() {
         System.setProperty("svcode.export", "src/test/resources/generatedRRPairs/");
      }

   @After
      public void afterClass() {
         System.clearProperty("svcode.export");
      }

This will create a request/response file post the execution of test case.

Example:



CodeSV javadoc api is available at the site -> CodeSV Javadoc

Attachments

    Outcomes