|
| 1 | +== CA Service Virtualization As Code |
| 2 | + |
| 3 | +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. + |
| 4 | +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 **CA Service Virtualization**. **Your tests become more reliable, repeatable and automated**. + |
| 5 | +With this approach, using **CA Virtual Services** from your Continuous Integration plateform becomes native. |
| 6 | + |
| 7 | +== Projects description |
| 8 | +- **devtest-unit-test-java** : Source code of java annotations and java API wrapped on **CA Devtest Rest API** to build and deploy virtual services |
| 9 | +- **lisabank-demo** : Demo project using SV as Code annotation to deploy virtual services in junit test |
| 10 | + |
| 11 | +== Pre-requises |
| 12 | + |
| 13 | +You should have **CA 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* . |
| 14 | +This both parameters will be used to build and deploy your virtual services. |
| 15 | +This annotations will use ***CA DevTest Rest API (CA DevTest Invoke 2)*** and it's compatible **from CA DevTest 8.0 to 10.1**. |
| 16 | + |
| 17 | +== Getting started |
| 18 | + |
| 19 | +In the **pom file** of your maven project add a new repository to get the libraries dependencies. |
| 20 | +[source,xml] |
| 21 | +---- |
| 22 | + <repositories> |
| 23 | + <repository> |
| 24 | + <id>git-devtest</id> |
| 25 | + <name>DevTest's Git based repo</name> |
| 26 | + <url>https://raw.github.com/helpingdev/maven/master/</url> |
| 27 | + </repository> |
| 28 | + </repositories> |
| 29 | +---- |
| 30 | + |
| 31 | +Add below dependency with scope test in your **pom file** : |
| 32 | +[source,xml] |
| 33 | +---- |
| 34 | + <dependency> |
| 35 | + <groupId>com.ca.devtest.sv.devtools</groupId> |
| 36 | + <artifactId>devtest-unit-test-java</artifactId> |
| 37 | + <version>1.1.11</version> |
| 38 | + </dependency> |
| 39 | +
|
| 40 | +---- |
| 41 | + |
| 42 | +Below is a quick sample of how to use " SV as code " in your Junit classes: |
| 43 | + |
| 44 | +[source,java,indent=0] |
| 45 | +---- |
| 46 | +/** |
| 47 | + |
| 48 | + * |
| 49 | + */ |
| 50 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 51 | +@SpringApplicationConfiguration(classes = LisaBankClientApplication.class) |
| 52 | +@DevTestVirtualServer(registryHost = "localhost", deployServiceToVse = "VSE") |
| 53 | +
|
| 54 | +public class SimpleDemo { |
| 55 | + static final Log logger = LogFactory.getLog(SimpleDemo.class); |
| 56 | + @Autowired |
| 57 | + private BankService bankServices; |
| 58 | + @Rule |
| 59 | + public VirtualServicesRule rules = new VirtualServicesRule(); |
| 60 | +
|
| 61 | + @DevTestVirtualService(serviceName = "UserServiceTest-EJB3UserControlBean", |
| 62 | + port = 9080, basePath = "/itkoExamples/EJB3UserControlBean", |
| 63 | + rrpairsFolder = "UserServiceTest/getListUser/EJB3UserControlBean", |
| 64 | + requestDataProtocol = {@Protocol(ProtocolType.DPH_SOAP) }) |
| 65 | + @Test |
| 66 | + public void getListUser() { |
| 67 | + User[] users = bankServices.getListUser(); |
| 68 | + assertNotNull(users); |
| 69 | + assertEquals(9, users.length); |
| 70 | + } |
| 71 | +} |
| 72 | +---- |
| 73 | + |
| 74 | +First, flag your Junit class as a Test using CA Virtual Services. This annotation will be used to refer to the CA DevTest Server : |
| 75 | + |
| 76 | +* *registryHost* :* Registry hostname _(default value localhost)_ |
| 77 | +* *deployServiceToVse :* Name of VSE |
| 78 | +* *login :* CA Devtest username _(default value svpower)_ |
| 79 | +* *password :* CA Devtest password _(default value svpower)_ |
| 80 | +all of this parameters are optional. You could set values in local-svascode.porperties file. |
| 81 | +[source,java,indent=0] |
| 82 | +---- |
| 83 | +@DevTestVirtualServer() |
| 84 | +---- |
| 85 | +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 |
| 86 | +[source,java,indent=0] |
| 87 | +---- |
| 88 | + @Rule |
| 89 | + public VirtualServicesRule rules = new VirtualServicesRule(); |
| 90 | +---- |
| 91 | + |
| 92 | +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 |
| 93 | +[source,java,indent=0] |
| 94 | +---- |
| 95 | +@DevTestVirtualService(serviceName = "UserServiceTest-EJB3UserControlBean", |
| 96 | + port = 9080, basePath = "/itkoExamples/EJB3UserControlBean", |
| 97 | + rrpairsFolder = "UserServiceTest/getListUser/EJB3UserControlBean", |
| 98 | + requestDataProtocol = {@Protocol(ProtocolType.DPH_SOAP) }) |
| 99 | + |
| 100 | +---- |
| 101 | +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. |
| 102 | +First you should add Junit Class Rule as described below |
| 103 | +[source,java,indent=0] |
| 104 | +---- |
| 105 | + @ClassRule |
| 106 | + public static VirtualServiceClassScopeRule ruleClass= new VirtualServiceClassScopeRule(); |
| 107 | +---- |
| 108 | +Then you could use DevTestVirtualService annotations on top of your class. |
| 109 | +[source,java,indent=0] |
| 110 | +---- |
| 111 | +/** |
| 112 | + * |
| 113 | + */ |
| 114 | +package com.ca.devtest.lisabank.demo.sv.http; |
| 115 | +
|
| 116 | +import static org.junit.Assert.assertEquals; |
| 117 | +import static org.junit.Assert.assertNotNull; |
| 118 | +
|
| 119 | +import org.apache.commons.logging.Log; |
| 120 | +import org.apache.commons.logging.LogFactory; |
| 121 | +import org.junit.ClassRule; |
| 122 | +import org.junit.Test; |
| 123 | +import org.junit.runner.RunWith; |
| 124 | +import org.springframework.beans.factory.annotation.Autowired; |
| 125 | +import org.springframework.boot.test.context.SpringBootTest; |
| 126 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 127 | +
|
| 128 | +import com.ca.devtest.lisabank.demo.LisaBankClientApplication; |
| 129 | +import com.ca.devtest.lisabank.demo.business.BankService; |
| 130 | +import com.ca.devtest.lisabank.wsdl.User; |
| 131 | +import com.ca.devtest.sv.devtools.annotation.DevTestVirtualServer; |
| 132 | +import com.ca.devtest.sv.devtools.annotation.DevTestVirtualService; |
| 133 | +import com.ca.devtest.sv.devtools.annotation.Protocol; |
| 134 | +import com.ca.devtest.sv.devtools.annotation.ProtocolType; |
| 135 | +import com.ca.devtest.sv.devtools.junit.VirtualServiceClassScopeRule; |
| 136 | +
|
| 137 | +/** |
| 138 | + |
| 139 | + * |
| 140 | + */ |
| 141 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 142 | +@SpringBootTest(classes = LisaBankClientApplication.class) |
| 143 | +// Mark as Test using CA Service Virtualization |
| 144 | +@DevTestVirtualServer() |
| 145 | +// Define Virtual Service with Clazz scope => Deploy once for all methods |
| 146 | +@DevTestVirtualService(serviceName = "VSClazzScopeSimpleDemo", |
| 147 | +basePath = "/itkoExamples/EJB3UserControlBean", |
| 148 | +port = 9081, |
| 149 | +workingFolder = "UserServiceTest/getListUser/EJB3UserControlBean", |
| 150 | +requestDataProtocol = { |
| 151 | + @Protocol(ProtocolType.DPH_SOAP) }) |
| 152 | +
|
| 153 | +public class VSClazzScopeSimpleDemo { |
| 154 | + static final Log logger = LogFactory.getLog(VSClazzScopeSimpleDemo.class); |
| 155 | + @Autowired |
| 156 | + private BankService bankServices; |
| 157 | +
|
| 158 | + // handle VS with Class scope |
| 159 | + @ClassRule |
| 160 | + public static VirtualServiceClassScopeRule clazzRule = new VirtualServiceClassScopeRule(); |
| 161 | +
|
| 162 | + @Test |
| 163 | + public void getListUser() { |
| 164 | + User[] users = bankServices.getListUser(); |
| 165 | + assertNotNull(users); |
| 166 | + printUsers(users); |
| 167 | + assertEquals(9, users.length); |
| 168 | + } |
| 169 | +
|
| 170 | + private void printUsers(User[] users) { |
| 171 | + for (User user : users) { |
| 172 | + logger.info(user.getFname() + " " + user.getLname() + " " + user.getLogin()); |
| 173 | + } |
| 174 | +
|
| 175 | + } |
| 176 | +} |
| 177 | +
|
| 178 | +---- |
| 179 | + |
| 180 | +== Contributors |
| 181 | +Pascal Gasp *Sr Architect Devops @ CA Technologies* + |
| 182 | +Vincent Mazot *Sr Consultant Devops @ CA Technologies* + |
| 183 | +Olivier Laplace *Sr Presales Devops @ CA Technologies* + |
| 184 | +Benoit Boulc'h *Developer* + |
| 185 | + |
0 commit comments