Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/update dependency #17

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ gradle.properties
results/
gradlew
gradlew.bat
.DS_Store
.DS_Store
gatling-results
117 changes: 55 additions & 62 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'gov.dvsa.vol'
version '1.2'
group = 'gov.dvsa.vol'
version = '1.3'

apply plugin: 'idea'
apply plugin: 'scala'
Expand All @@ -9,106 +9,99 @@ apply plugin: 'application'
repositories {
mavenLocal()
mavenCentral()
maven {

url "https://maven.pkg.github.com/dvsa/*"
credentials {
username = project.findProperty("gpr.user") ?: System.getenv("GITHUB_USER")
password = project.findProperty("gpr.key") ?: System.getenv("GITHUB_TOKEN")
}
maven {
url = uri("https://maven.pkg.github.com/dvsa/*")
credentials {
username = project.findProperty("gpr.user") ?: System.getenv("GITHUB_USER")
password = project.findProperty("gpr.key") ?: System.getenv("GITHUB_TOKEN")
}
}
}

dependencies {
implementation group: 'io.gatling.highcharts', name: 'gatling-charts-highcharts', version: '3.9.5'
implementation group: 'io.gatling', name: 'gatling-core', version:'3.9.5'
implementation group: 'io.gatling', name: 'gatling-http', version:'3.9.5'
implementation group: 'org.scala-lang', name: 'scala-library', version: '2.13.12'
implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.10.0'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.1'

implementation group: 'org.dvsa.testing.lib', name: 'active-support', version:'2.4.4'
implementation group: 'org.dvsa.testing.lib', name: 'vol-uri-constructor', version:'2.0.4'
implementation group: 'org.dvsa.testing.framework', name: 'vol-api-calls', version:'2.7.1'
}

implementation 'io.gatling.highcharts:gatling-charts-highcharts:3.13.4'
implementation 'org.apache.commons:commons-csv:1.13.0'
testImplementation platform("org.junit:junit-bom:5.11.4") // Use latest stable JUnit 5
testImplementation "org.junit.jupiter:junit-jupiter"
implementation group: 'io.gatling', name: 'gatling-maven-plugin', version: '4.15.1'


tasks.withType(JavaExec) {
systemProperties = System.getProperties() as Map<String, ?>
implementation 'org.dvsa.testing.lib:active-support:2.5.14'
implementation 'org.dvsa.testing.lib:vol-uri-constructor:2.0.7'
implementation 'org.dvsa.testing.framework:vol-api-calls:2.8.0'
}

tasks.withType(JavaExec).configureEach {
systemProperties.putAll(System.getProperties())
}

tasks.withType(Test) {
systemProperties = System.getProperties() as Map<String, ?>
tasks.withType(Test).configureEach {
systemProperties.putAll(System.getProperties())
useJUnitPlatform()
}

task registerUser(type: JavaExec) {
tasks.register('registerUser', JavaExec) {
description = "Load testing with Gatling"
classpath = sourceSets.test.runtimeClasspath
main = "io.gatling.app.Gatling"
//Specify the simulation to run
mainClass.set("io.gatling.app.Gatling")

args = [
"--simulation", "simulations.RegisterUserSimulation",
"--results-folder", "${buildDir}/gatling-results",
"--bodies-folder", sourceSets.test.resources
"--results-folder", file("${rootDir}/gatling-results").absolutePath,
"--bodies-folder", file(sourceSets.test.resources.srcDirs.first()).absolutePath
]
}

task createApplication(type: JavaExec) {
tasks.register('createApplication', JavaExec) {
dependsOn("registerUsersViaAPI")
classpath = sourceSets.test.runtimeClasspath
mainClass.set("io.gatling.app.Gatling")

doFirst {
registerUsersViaAPI.executeTests()
println("Creating users via app")
}
description = "Load testing with Gatling"
classpath = sourceSets.test.runtimeClasspath
main = "io.gatling.app.Gatling"
//Specify the simulation to run

args = [
"--simulation", "simulations.CreateApplicationSimulation",
"--results-folder", "${buildDir}/gatling-results",
"--bodies-folder", sourceSets.test.resources
"--results-folder", file("${rootDir}/gatling-results").absolutePath
]
doLast {
cleanUp.executeTests()
}
outputs.dir file("${rootDir}/gatling-results")
}

task searchOperator(type: JavaExec) {

tasks.register('searchOperator', JavaExec) {
description = "Load testing with Gatling"
classpath = sourceSets.test.runtimeClasspath
main = "io.gatling.app.Gatling"
//Specify the simulation to run
mainClass.set("io.gatling.app.Gatling")

args = [
"--simulation", "simulations.ExternalOperatorSearchSimulation",
"--results-folder", "${buildDir}/gatling-results",
"--bodies-folder", sourceSets.test.resources
"--results-folder", file("${rootDir}/gatling-results").absolutePath,
"--bodies-folder", file(sourceSets.test.resources.srcDirs.first()).absolutePath
]
}

task internalSearchLicence(type: JavaExec) {
tasks.register('internalSearchLicence', JavaExec) {
description = "Load testing with Gatling"
classpath = sourceSets.test.runtimeClasspath
main = "io.gatling.app.Gatling"
//Specify the simulation to run
mainClass.set("io.gatling.app.Gatling")

args = [
"--simulation", "simulations.InternalLicenceSearchSimulation",
"--results-folder", "${buildDir}/gatling-results",
"--bodies-folder", sourceSets.test.resources
"--results-folder", file("${rootDir}/gatling-results").absolutePath,
"--bodies-folder", file(sourceSets.test.resources.srcDirs.first()).absolutePath
]
}

task registerUsersViaAPI(type: Test) {
filter {
includeTest "*SelfServeRegisterUser", "mainTest"
tasks.register('registerUsersViaAPI', Test) {
useJUnitPlatform()
description = "Runs SelfServeRegisterUser and mainTest"
doFirst {
println "Running API User Registration Tests..."
}
}

task cleanUp(type: Test) {
filter {
println("*********deleting loginId file************")
sleep 2000
includeTest "*SelfServeRegisterUser", "deleteFile"
println("***********finishing deleting file**********")
test {
filter {
includeTestsMatching ("SelfServeRegisterUserTest.registerUserTest")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package utils;

public class SQLquery {

public static String getUsersSql(String users) {
Expand Down
139 changes: 139 additions & 0 deletions src/test/java/SelfServeRegisterUserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import activesupport.database.url.DbURL;
import activesupport.mailPit.MailPit;
import activesupport.ssh.SSH;
import apiCalls.actions.RegisterUser;
import org.apache.commons.codec.net.QuotedPrintableCodec;
import org.apache.commons.io.FileUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import com.jcraft.jsch.Session;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.naming.ConfigurationException;
import java.io.*;
import java.sql.*;
import java.util.Arrays;
import java.util.Optional;


public class SelfServeRegisterUserTest {
private static final Logger LOGGER = LogManager.getLogger(SelfServeRegisterUserTest.class);

private static final String LOGIN_CSV_FILE = "src/test/resources/loginId.csv";
private static final String CSV_HEADERS = "Username,Forename,Password";
private static final String DB_URL = "jdbc:mysql://olcsdb-rds.int.olcs.dvsacloud.uk:3306/OLCS_RDS_OLCSDB?useSSL=FALSE";
private static final String DB_USER = System.getProperty("dbUser");
private static final String DB_PASSWORD = System.getProperty("dbPassword");

private final String users = Optional.ofNullable(System.getProperty("users")).orElse("0");


@BeforeAll
public static void deleteFile() {
File loginDetails = new File(LOGIN_CSV_FILE);
if (loginDetails.exists() && loginDetails.delete()) {
LOGGER.info("{} has been deleted", loginDetails);
} else {
LOGGER.warn("{} does not exist", loginDetails);
}
}

public static void getExternalUsersFromTable() {
try {
createTunnel();
Class.forName("com.mysql.cj.jdbc.Driver");

try (Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQLquery.getUsersSql("10"))) {

while (rs.next()) {
LOGGER.info("Username: {}, Forename: {}", rs.getString("Username"), rs.getString("Forename"));
}
}
} catch (Exception e) {
LOGGER.error("Error fetching external users", e);
}
}

@Test
public void registerUserTest() {
String env = System.getProperty("env", "").toLowerCase();
if (!"prep".equals(env)) {
registerUser();
}
}

public void registerUser() {
MailPit mailPit = new MailPit();
QuotedPrintableCodec codec = new QuotedPrintableCodec();
int userCount = Integer.parseInt(users);

for (int i = 0; i < userCount; i++) {
try {
RegisterUser registerUser = new RegisterUser();
registerUser.registerUser();

String emailAddress = registerUser.getEmailAddress();
String password = codec.decode(mailPit.retrieveTempPassword(emailAddress));

writeToFile(registerUser.getUserName(), registerUser.getForeName(), password);
} catch (Exception e) {
LOGGER.error("Error registering user", e);
}
}
}

private static void createTunnel() {
DbURL dbURL = new DbURL();

Optional<String> ldapUsername = Optional.ofNullable(System.getProperty("ldapUser"));
Optional<String> sshPrivateKeyPath = Optional.ofNullable(System.getProperty("sshPrivateKeyPath"));

System.setProperty("dbUsername", "dbUsername");
System.setProperty("dbPassword", "dbPassword");

ldapUsername.ifPresent(username -> {
try {
dbURL.setPortNumber(createSSHsession(username, sshPrivateKeyPath.orElseThrow(ConfigurationException::new)));
} catch (Exception e) {
LOGGER.error("Error creating SSH tunnel", e);
}
});
}

private void writeToFile(String userId, String forename, String password) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOGIN_CSV_FILE, true));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) {

if (!searchForString()) {
csvPrinter.printRecord((Object[]) CSV_HEADERS.split(","));
}
csvPrinter.printRecord(Arrays.asList(userId, forename, password));
} catch (IOException e) {
LOGGER.error("Error writing to file", e);
}
}

private boolean searchForString() {
try {
File file = new File(LOGIN_CSV_FILE);
if (file.exists()) {
String content = FileUtils.readFileToString(file, "UTF-8");
return content.contains(CSV_HEADERS);
}
} catch (IOException e) {
LOGGER.error("Error reading file", e);
}
LOGGER.info("File not found or text not found");
return false;
}

private static int createSSHsession(String username, String pathToSSHKey) throws Exception {
Session session = SSH.openTunnel(username, "dbam.olcs.int.prod.dvsa.aws", pathToSSHKey);
return SSH.portForwarding(3309, "localhost", 3306, session);
}
}
1 change: 0 additions & 1 deletion src/test/resources/InternalLoginId.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#This file needs populating with new list of internal users
Username,Forename,Licence
jonesia,Steven,OM1020634
jonesnb,Nigel,OM0035264
Expand Down
19 changes: 0 additions & 19 deletions src/test/resources/loginId_.csv

This file was deleted.

Loading