VIRTUALAN -VIRTUAL MOCK SERVICE



Hello Everyone!!!
   This is article about how can deploy virtualized services with swagger/OAS contract(s)?

Service Virtualization(https://en.wikipedia.org/wiki/Service_virtualization):
  •  Service virtualization is a method to emulate the behavior of specific components in heterogeneous component-based applications such as API-driven applications, cloud-based applications and service-oriented architectures.
  • It is used to provide software development and QA/testing teams access to dependent system components that are needed to exercise an application under test (AUT), but are unavailable or difficult-to-access for development and testing purposes. With the behavior of the dependent components "virtualized", testing and development can proceed without accessing the actual live components.
  • Service virtualization is recognized by vendors, industry analysts, and industry publications as being different than mocking

 OpenAPISpescification/Swagger Contract:(https://swagger.io/blog/why-you-should-create-an-api-definition/)
  • Today, the most commonly used definition for APIs is the OpenAPI Specification. The OpenAPI Specification (OAS), which is based on the original Swagger Specification, defines RESTful standards for APIs. OAS Definitions map resources and operations for an API.

In the Agile,  We need to develop (micro)services & Testing in parallel. How can tester or development team can develop or test parallel to all the APIs before the real Microservices would be developed? Here Service Virtualization comes into the rescue.
What would be the benefits for?
  •  Development team :
    • If My Services needs to be connected to 1 or more microservices in the development/Testing Environment if the other dependent services would not be available.
    • If the dependent services contract had changed then the new changes(implementation) may not be available in day 1 of the sprint
    • If Need to create several use-case scenarios during the development phase even though the dependent service(s) would not be available or not developed yet
    • Etc..
  • Testing team(Shift left approach):         
    • Start developing automation scripts in day one of the sprint in parallel with service implementation.
    • Develop and test right away.
    • Prepare all the test scenarios and test cases and test/validate all the test scripts before the actual service is ready.
    • Tester can create error scenarios and create automated test cases before the service is ready.
    • Once the service is ready change the virtual endpoint to real service endpoint and validate all the testcases in few hours..
    • Etc..


 Virtualized Services can be generated using swagger code gen CLI utility using Spring  with adding an additional option for "generateDynamicMock": true in the json config.

 ****** java-config.json*******
 {
   "dateLibrary" : "java8-localdatetime",
   "java8": true,
   "generateDynamicMock": true,
   "modelPackage" : "io.swagger.api.pet.model",
   "apiPackage"   : "io.swagger.api"
 }

Current version is Beta version.

 BETA version uses a  h2 memory database to serve request and response based on swagger/Rest contract.

  • Always can change DB setting in the application.properties to point to any other DB environment

Prerequisites: 


Virtual Mock Service in 4 STEPS:

STEP I :(One Time Setup)

1) Download the "master" branch code from https://github.com/elans3/swagger-codegen.git.

         
  • Click the "Clone or download" link 

  • Click the "Download ZIP"
  • Extract the Zip file in the following folder: c:/VirtualService
  • Code would be extracted as c:/VirtualService/swagger-codegen-master
  

  2) Build the project to set up the Virtualized services and Run the maven command. 

  • Open the command prompt and navigate to the VirtualService/swagger-codegen-master folder
    • C:/VirtualService/swagger-codegen-master>mvn -Dmaven.javadoc.skip=true clean install   
    • If you have any proxy issue use this command: 
      • mvn -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2 -Dmaven.javadoc.skip=true clean install 
  • Maven would download all the required dependencies to build the required jar to set up the virtualized services.
    • This is one time activity, thereafter it would not happen.
  • Check and see after successful installation:


STEP II :(Ready to setup the virtual Service for the given contracts)

  • Create directory "setup" under the  C:/VirtualService/
  • Copy the swagger-codegen-cli.jar  from extracted folder C:/VirtualService/modules/swagger-codegen-cli/target to C:/VirtualService/setup directory.
  • Copy the swagger contract file(yaml or json) to C:/VirtualService/setup directory
  • Create the  java-config.json under C:/VirtualService/setup directory with following "json" information 
          {
                      "dateLibrary" : "java8-localdatetime",
                      "java8": true,
                      "generateDynamicMock": true,
                     "modelPackage" : "io.swagger.api.pet.model",
                     "apiPackage"   : "io.swagger.api"
           }

  • Run the following command once you have your Yaml copied:
    •    C:/VirtualService/setup> java -jar swagger-codegen-cli.jar generate -i <yaml-fileName> -l spring -o mockservice -c java-config.json 

    • Following would be the sample command for the given contract file  petstore.yaml[Petstore Yaml Download]. 

      • C:/VirtualService/setup> java -jar swagger-codegen-cli.jar generate -i petstore.yaml -l spring -o mockservice -c java-config.json

  • Navigate to the mockservice folder under C:/VirtualService/setup

  • Run the following command
    • C:/VirtualService/setup/mockservice> mvn -Dmaven.javadoc.skip=true clean install 
    • Maven would download all the required dependencies to build the required jar to set up the virtualized services. 
    • This is one time activity, thereafter it would not happen.
  • After the successful installation. Your service build successfully. Let's start the service

STEP III : (Start the virtual mock service)

  • Navigate to the C:/VirtualService/setup/mockservice/target  directory

  • Run the following command
    •  C:/VirtualService/setup/mockservice/target>java -jar swagger-spring-1.0.0.jar 
  • Now Virtual service is ready :) 

STEP IV :
Navigate to http://localhost:8080/virtualservices/request.. You are ready to use it. 



















Set up Mock Data Screen:


















Test the Service with any rest client:  Hope you have satisfied with the response :)

Thank you for visiting to this page.. Have a good day!!!!



Comments

Popular posts from this blog

Service virtualization - Virtualan integration with Openapi

Benefits of Virtualan - "The Service Virtualization Project"