Skip to content

Commit d3258f9

Browse files
Added RedisUtility.java
1 parent 2daf0da commit d3258f9

File tree

12 files changed

+231
-35
lines changed

12 files changed

+231
-35
lines changed

Jenkinsfile

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,18 @@ pipeline {
2929
}
3030
stage('Test environment availability') {
3131
steps {
32-
<<<<<<< HEAD
33-
sh "./gradlew clean test -Denv.mode=${params.MODE} -Denv.urlPrefix=${params.TARGET} -Denv.browser=${params.BROWSER} -Denv.platform=${params.PLATFORM} -Denv.threadCount=1 -Dcucumber.options=\"--tags @sitecheck\""
34-
=======
3532
sh "./gradlew clean test -Denv.mode=${params.MODE} -Denv.urlPrefix=${params.TARGET} -Denv.browser=${params.BROWSER} -Denv.threadCount=1 -Dcucumber.tags=\"@sitecheck\""
36-
>>>>>>> refs/remotes/origin/master
3733
}
3834
}
3935
stage('Test bed setup') {
4036
steps {
41-
<<<<<<< HEAD
42-
sh "./gradlew clean test -Denv.mode=${params.MODE} -Denv.urlPrefix=${params.TARGET} -Denv.browser=${params.BROWSER} -Denv.platform=${params.PLATFORM} -Denv.threadCount=1 -Dcucumber.options=\"--tags @usercheck\""
43-
=======
4437
sh "./gradlew clean test -Denv.mode=${params.MODE} -Denv.urlPrefix=${params.TARGET} -Denv.browser=${params.BROWSER} -Denv.threadCount=1 -Dcucumber.tags=\"@usercheck\""
45-
>>>>>>> refs/remotes/origin/master
4638
}
4739
}
4840
stage('Test') {
4941
steps {
5042
sh "mvn clean"
51-
<<<<<<< HEAD
52-
sh "./gradlew clean test -Denv.mode=${params.MODE} -Denv.urlPrefix=${params.TARGET} -Denv.browser=${params.BROWSER} -Denv.platform=${params.PLATFORM} -Denv.threadCount=${params.SELENIUM_GRID_NODE} -Dcucumber.options=\"${params.TAGS}\""
53-
=======
5443
sh "./gradlew clean test -Denv.mode=${params.MODE} -Denv.urlPrefix=${params.TARGET} -Denv.browser=${params.BROWSER} -Denv.threadCount=${params.SELENIUM_GRID_NODE} -Dcucumber.tags=\"${params.TAGS}\""
55-
>>>>>>> refs/remotes/origin/master
5644
}
5745
}
5846
}

build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies {
3535
compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: webDriverManagerVersion
3636
compile group: 'javax.mail', name: 'mail', version: javaMailVersion
3737
compile group: 'org.freemarker', name: 'freemarker', version: freemarkerVersion
38+
compile group: 'redis.clients', name: 'jedis', version: jedisVersion
3839
}
3940

4041
test {
@@ -71,6 +72,11 @@ test {
7172
if(platform == null || cucumberOptions == ''){
7273
platform = 'local'
7374
}
75+
76+
def redisHost = System.getProperty("env.redisHost");
77+
if(redisHost == null){
78+
redisHost = 'localhost'
79+
}
7480

7581
systemProperties 'env.browser': browser
7682
systemProperties 'env.driverPath': driverPath
@@ -83,6 +89,7 @@ test {
8389
systemProperties 'env.baseURL': 'http://'+ urlPrefixCustom + customDomain
8490
systemProperties 'file.encoding': 'utf-8'
8591
systemProperties 'env.platform': platform
92+
systemProperties 'env.redisHost' : redisHost
8693
include '**/*Courgette.class'
8794

8895
outputs.upToDateWhen { false }

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
redis:
2+
image: redis:latest
3+
ports:
4+
- "6379:6379"
5+
16
hub:
27
image: selenium/hub
38
ports:

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ webDriverManagerVersion = 2.0.1
1818
restAssuredVersion = 3.0.6
1919
javaMailVersion = 1.4.1
2020
freemarkerVersion = 2.3.27-incubating
21+
jedisVersion = 2.4.2

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@
125125
<artifactId>rest-assured</artifactId>
126126
<version>3.0.0</version>
127127
</dependency>
128+
<dependency>
129+
<groupId>redis.clients</groupId>
130+
<artifactId>jedis</artifactId>
131+
<version>2.4.2</version>
132+
</dependency>
128133
</dependencies>
129134
<build>
130135
<resources>

src/main/java/com/mindstix/cb/page/LogIn.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public void enterUserData(String emailAddressValue, String passwordValue) {
4242
sendKeysToElement(emailAddress, emailAddressValue);
4343
sendKeysToElement(password, passwordValue);
4444
}
45+
46+
public void enterUserDataUsingRedis(String user){
47+
emailAddress = getElementByCSS("emailAddressCSS");
48+
password = getElementByCSS("passwordCSS");
49+
sendKeysToElement(emailAddress, user);
50+
sendKeysToElement(password, getTestData("password"));
51+
}
4552

4653
public MyAccount clickOnSignInButton() {
4754
signInButton = getElementByCSS("signInButtonCSS");

src/main/java/com/mindstix/cb/utils/Constants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
*/
1111
public final class Constants {
1212

13+
/**
14+
* @see RedisUtility.java
15+
*/
16+
public static final int REDIS_PORT = 6379;
17+
public static final String USERS_KEY = "users";
18+
public static final int LOCK_TIMEOUT = 2000;
19+
public static final String LEADER_LOCK = "LEADER_LOCK";
20+
public static final int WAIT_TIMEOUT_IN_SEC = 600;
21+
public static final String FALLBACK_USER = "test1@mx.com";
22+
1323
/**
1424
* Constructor
1525
*/

src/main/java/com/mindstix/cb/utils/DriverUtility.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public static WebDriver loadDriver(String browserName) {
8080
}
8181

8282
public static void quitWebDriver(WebDriver webDriver) {
83-
webDriver.quit();
83+
if(webDriver !=null){
84+
webDriver.quit();
85+
}
8486
}
8587

8688
public void closeWebDriver(WebDriver webDriver) {
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (c) 2017 Mindstix Inc.
3+
*/
4+
package com.mindstix.cb.utils;
5+
6+
import java.util.List;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import redis.clients.jedis.Jedis;
12+
import redis.clients.jedis.JedisPool;
13+
import redis.clients.jedis.Transaction;
14+
15+
/**
16+
* This utility handles all the Redis database and Jedis related operations.
17+
* Following operations are handled by RedisUtility.java
18+
* <ol>
19+
* <li>Distributed coordination for running tests with specific set of users.
20+
* </li>
21+
* <li>Acting as a temporary storage for report data and notification stages in
22+
* Jenkins pipeline</li>
23+
* </ol>
24+
* Please refer following URLs for more information about Redis and Jedis
25+
* <ol>
26+
* <li><a href="https://redis.io/commands">Redis Commands</a></li>
27+
* <li><a href="http://www.baeldung.com/jedis-java-redis-client-library"> Jedis:
28+
* java redis client library</a></li>
29+
* </ol>
30+
*
31+
* @author Mindstix
32+
*/
33+
public final class RedisUtility {
34+
35+
private final static Logger LOGGER = LoggerFactory.getLogger(RedisUtility.class);
36+
37+
private static JedisPool pool;
38+
private static Jedis jedis;
39+
40+
private static boolean isRedisAlive;
41+
42+
/**
43+
* private constructor
44+
*/
45+
private RedisUtility() {
46+
}
47+
48+
static {
49+
configureJedisPool();
50+
}
51+
52+
/**
53+
* This method is used to configure Jedis pool. It configures the host and
54+
* port of Jedis pool. It takes redisHost from command line. If not passed from command line
55+
* it takes localhost as default redis host. You can change the default redisHost in build.gradle.
56+
* For remaining Redis configurations please @see Constants.java
57+
*
58+
*/
59+
private static void configureJedisPool() {
60+
try {
61+
LOGGER.info("Configuring jedis pool");
62+
pool = new JedisPool(System.getProperty("env.redisHost"), Constants.REDIS_PORT);
63+
jedis = pool.getResource();
64+
isRedisAlive = true;
65+
} catch (Exception e) {
66+
LOGGER.error("Failed to configure redis", e);
67+
isRedisAlive = false;
68+
}
69+
}
70+
71+
/**
72+
* This method is used to acquire username which are stored in Redis set. If
73+
* usernames are available in set then it randomly pops one user else it
74+
* waits for 10 minutes for user to be available. If after 10 minutes user
75+
* is not available then it throws RunTimeException. It takes set-key as
76+
* parameter
77+
*
78+
* @param userKey
79+
* @return userName
80+
*/
81+
public static String acquireUser(String userKey) {
82+
if (!isRedisAlive) {
83+
String userName = Constants.FALLBACK_USER;
84+
LOGGER.warn("Returning fallback user {} as jedis is not configured", userName);
85+
return userName;
86+
}
87+
88+
LOGGER.info("Acquiring user");
89+
String userName = null;
90+
List<Object> result = null;
91+
Transaction transaction;
92+
for (int i = 0; i < Constants.WAIT_TIMEOUT_IN_SEC; i++) {
93+
transaction = jedis.multi();
94+
transaction.spop(userKey);
95+
result = transaction.exec();
96+
if (result != null) {
97+
break;
98+
}
99+
try {
100+
Thread.sleep(1000);
101+
} catch (InterruptedException e) {
102+
LOGGER.error("InterruptedException", e);
103+
}
104+
}
105+
if (result == null) {
106+
throw new RuntimeException("Time limit exceeded to acquire the user");
107+
} else {
108+
userName = result.get(0).toString();
109+
}
110+
LOGGER.info("User {} acquired", userName);
111+
return userName;
112+
}
113+
114+
/**
115+
* This method is used to add or release user back to redis set. It takes
116+
* userKey and username as parameter.
117+
*
118+
* @param userKey
119+
* @param userName
120+
*/
121+
public static void releaseUser(String userKey, String userName) {
122+
if (userName != null && isRedisAlive) {
123+
LOGGER.info("Releasing user {}.", userName);
124+
Transaction transaction = jedis.multi();
125+
transaction.sadd(userKey, userName);
126+
transaction.exec();
127+
LOGGER.info("User {} released.", userName);
128+
}
129+
}
130+
131+
}

src/test/java/com/mindstix/cb/stepdefinition/BaseScenarioContext.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import org.slf4j.LoggerFactory;
1818
import org.yaml.snakeyaml.Yaml;
1919

20+
import com.mindstix.cb.utils.Constants;
2021
import com.mindstix.cb.utils.DriverUtility;
2122
import com.mindstix.cb.utils.PropertiesUtility;
23+
import com.mindstix.cb.utils.RedisUtility;
2224

2325
import cucumber.api.Scenario;
2426

@@ -32,9 +34,12 @@ public abstract class BaseScenarioContext {
3234
private WebDriver webDriver;
3335
private StopWatch stopWatch;
3436

35-
public List<String> allProductID;
3637
private String testResult;
3738

39+
public List<String> allProductID;
40+
41+
public String userName;
42+
3843
/**
3944
* Returns webDriver
4045
*
@@ -61,8 +66,8 @@ protected void initializationMethod(String urlKey) {
6166
}
6267

6368
/**
64-
* Method which will execute after each scenario after execution of each Takes
65-
* screenshot when Scenario fails and saves it into HTML report
69+
* Method which will execute after each scenario after execution of each
70+
* Takes screenshot when Scenario fails and saves it into HTML report
6671
*
6772
* @param scenario
6873
*/
@@ -79,15 +84,15 @@ protected void teardownMethod(Scenario scenario) {
7984
} else {
8085
testResult = "Pass";
8186
}
82-
if (webDriver != null) {
83-
stopWatch.stop();
84-
LOGGER.info("Total time taken to Execute Test Scenario {} Milliseconds", stopWatch.getTime());
85-
DriverUtility.quitWebDriver(webDriver);
86-
}
87+
stopWatch.stop();
88+
LOGGER.info("Total time taken to Execute Test Scenario {} Milliseconds", stopWatch.getTime());
89+
DriverUtility.quitWebDriver(webDriver);
8790

8891
if (this.allProductID.size() > 0) {
8992
addDataInReport(scenario.getName());
9093
}
94+
95+
RedisUtility.releaseUser(Constants.USERS_KEY, userName);
9196
}
9297

9398
private void addDataInReport(String scenario) {

0 commit comments

Comments
 (0)