Skip to content

This project provides simple Java annotations that can be used in your Junit Test to deploy Virtual Services before starting your test

Notifications You must be signed in to change notification settings

CA-DevTest/SV-as-Code-UnitTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

Service Virtualization As Code

This project provides simple Java annotations that can be used in your Junit Test to deploy Virtual Services before starting your test. The scope of annotations are test methods.
This java annotation helps to embed your Virtual Services in your source code. This approach makes your application ready for Continous Integration testing by removing system and data constraints with Service Virtualization. Your tests become more reliable, repeatable and automated.
With this approach, using Virtual Services from your Continuous Integration plateform becomes native.

Projects description

  • devtest-unit-test-java : Source code of java annotations and java API wrapped on Devtest Rest API to build and deploy virtual services

  • lisabank-demo : Demo project using SV as Code annotation to deploy virtual services in junit test

Pre-requises

You should have DevTest Server up and running. This server could be installed on your machine or on remote server. You will setup registry url and VSE name through Java Annotation @DevTestVirtualServer . This both parameters will be used to build and deploy your virtual services. This annotations will use DevTest Rest API (DevTest Invoke 2) and it’s compatible from DevTest 8.0 and above.

Getting started

In the pom file of your maven project add a new repository to get the libraries dependencies.

	<repositories>
         <repository>
                <id>bintray-ca-sv</id>
                <name>bintray-ca</name>
                <url>http://ca.bintray.com/sv</url>
	  </repository>
	</repositories>

Add below dependency with scope test in your pom file :

	<dependency>
		<groupId>com.ca.devtest.sv.devtools</groupId>
		<artifactId>devtest-unit-test-java</artifactId>
		<version>1.4.0</version>
	</dependency>

Below is a quick sample of how to use " SV as code " in your Junit classes:

/**
 * @author pascal.gasp@ca.com
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = LisaBankClientApplication.class)
@DevTestVirtualServer(registryHost = "localhost", deployServiceToVse = "VSE")

public class SimpleDemo {
	static final Log logger = LogFactory.getLog(SimpleDemo.class);
	@Autowired
	private BankService bankServices;
	@Rule
	public VirtualServicesRule rules = new VirtualServicesRule();

	@DevTestVirtualService(serviceName = "UserServiceTest-EJB3UserControlBean",
			port = 9080, basePath = "/itkoExamples/EJB3UserControlBean",
			rrpairsFolder = "UserServiceTest/getListUser/EJB3UserControlBean",
			requestDataProtocol = {@Protocol(ProtocolType.DPH_SOAP) })
	@Test
	public void getListUser() {
		User[] users = bankServices.getListUser();
		assertNotNull(users);
		assertEquals(9, users.length);
	}
}

First, flag your Junit class as a Test using Virtual Services. This annotation will be used to refer to the DevTest Server :

  • registryHost :* Registry hostname (default value localhost)

  • deployServiceToVse : Name of VSE

  • login : Devtest username (default value svpower)

  • password : Devtest password (default value svpower)

  • protocol : Protcol to connect with Registry (default value http)

  • keystore : Keystore location required when Protocol is https

  • keystorePasswored : Keystore Passwored

all of this parameters are optional. You could set values in local-svascode.porperties file with following properties:

  • devtest.vsename : Name of VSE

  • devtest.registry : Registry hostname

  • devtest.registryUrl : Registry url

  • devtest.login : Devtest username

  • devtest.password : Devtest password

  • devtest.protocol : Protocol to connect with Registry

  • devtest.keystore : Keystore location

  • devtest.keystorePassword : Keystore password

  • devtest.undeploy.ifexists : Flag to undeploy existing Virtual Service before deploying

@DevTestVirtualServer()

Add VirtualServices rule as a field member of Junit class. This rule will handle SV as Code annotations during Junit life cycle. Rules allow very flexible addition or redefinition of the behavior of each test method in a test class

@Rule
public VirtualServicesRule rules = new VirtualServicesRule();

Above of each test method, add virtual service annotations. This annotation should refer to the Requests/Responses folder and define virtual service configuration such as service name, listnen port, path, type of protocole

DevTestVirtualService annotation which uses DCM capabilities
@DevTestVirtualService(serviceName = "UserServiceTest-EJB3UserControlBean",
	port = 9080, basePath = "/itkoExamples/EJB3UserControlBean",
	rrpairsFolder = "UserServiceTest/getListUser/EJB3UserControlBean",
	requestDataProtocol = {@Protocol(ProtocolType.DPH_SOAP) })

DevTestVirtualServiceV3 annotation which uses V3 capabilities
 @DevTestVirtualServiceV3(
            serviceName = "vsV3_SSLConfig",
            description = "Created with annotation provided for function vsV3_RRPairConfigObject",
            port = "24778",
            workingFolder = "v3/rrpair",
            inputFile2 = "operation-8-req.txt",
            inputFile1 = "operation-8-rsp.txt",
            transportProtocolConfig = @TransportProtocolConfig(
                    typeId = "HTTP",
                    useGateway = false,
                    hostHeaderPassThrough = true,
                    recordingEndpoint = @RecordingEndpointConfig(
                            host = "recordinghost",
                            useSSL = true,
                            sslConfig = @SSLConfig (
                                    keystorePassword = "passphrase",
                                    keystoreFile = "/Applications/CA/DevTest/webreckeys.ks",
                                    aliasPassword = "passphrase",
                                    alias = "lisa"
                            )
                    )
            )
    )

It’s possible to define a set of Virtual Services with Class scope. In this case all virtual services will be deployed once at class level. First you should add Junit Class Rule as described below

@ClassRule
public static VirtualServiceClassScopeRule ruleClass= new VirtualServiceClassScopeRule();

Then you could use DevTestVirtualService annotations on top of your class.

/**
 *
 */
package com.ca.devtest.lisabank.demo.sv.http;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ca.devtest.lisabank.demo.LisaBankClientApplication;
import com.ca.devtest.lisabank.demo.business.BankService;
import com.ca.devtest.lisabank.wsdl.User;
import com.ca.devtest.sv.devtools.annotation.DevTestVirtualServer;
import com.ca.devtest.sv.devtools.annotation.DevTestVirtualService;
import com.ca.devtest.sv.devtools.annotation.Protocol;
import com.ca.devtest.sv.devtools.annotation.ProtocolType;
import com.ca.devtest.sv.devtools.junit.VirtualServiceClassScopeRule;

/**
 * @author pascal.gasp@ca.com
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = LisaBankClientApplication.class)
// Mark as Test using Service Virtualization
@DevTestVirtualServer()
// Define Virtual Service with Clazz scope => Deploy once for all methods
@DevTestVirtualService(serviceName = "VSClazzScopeSimpleDemo",
basePath = "/itkoExamples/EJB3UserControlBean",
port = 9081,
workingFolder = "UserServiceTest/getListUser/EJB3UserControlBean",
requestDataProtocol = {
		@Protocol(ProtocolType.DPH_SOAP) })

public class VSClazzScopeSimpleDemo {
	static final Log logger = LogFactory.getLog(VSClazzScopeSimpleDemo.class);
	@Autowired
	private BankService bankServices;

	// handle VS with Class scope
	@ClassRule
	public static VirtualServiceClassScopeRule clazzRule = new VirtualServiceClassScopeRule();

	@Test
	public void getListUser() {
		User[] users = bankServices.getListUser();
		assertNotNull(users);
		printUsers(users);
		assertEquals(9, users.length);
	}

	private void printUsers(User[] users) {
		for (User user : users) {
			logger.info(user.getFname() + " " + user.getLname() + " " + user.getLogin());
		}

	}

	@DevTestVirtualServiceV3(
            serviceName = "vsV3_SSLConfig",
            description = "Created with annotation provided for function vsV3_RRPairConfigObject",
            port = "24778",
            workingFolder = "v3/rrpair",
            inputFile2 = "operation-8-req.txt",
            inputFile1 = "operation-8-rsp.txt",
            transportProtocolConfig = @TransportProtocolConfig(
                    typeId = "HTTP",
                    useGateway = false,
                    hostHeaderPassThrough = true,
                    recordingEndpoint = @RecordingEndpointConfig(
                            host = "recordinghost",
                            useSSL = true,
                            sslConfig = @SSLConfig (
                                    keystorePassword = "passphrase",
                                    keystoreFile = "/Applications/CA/DevTest/webreckeys.ks",
                                    aliasPassword = "passphrase",
                                    alias = "lisa"
                            )
                    )
            )
    )
    @Test
    public void vsV3_SSLConfig(){
         ResponseParser responseParser = HttpUtils.GET(HttpUtils.URL_FORMAT,  "https", "localhost",
                "24778","import/test/operation-8");
       ResponseParser vsResponseParser = HttpUtils.GET_VS_DETAILS(API_PROTOCOL, "localhost", "1505", "VSE", "V3Test.vsV3_Deploy");
       assert (responseParser!=null);
       assert (responseParser.getValue("$.TCEntry[0].termsType").equals("Operation 8 terms"));
    }
}

About

This project provides simple Java annotations that can be used in your Junit Test to deploy Virtual Services before starting your test

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages