Skip to content

Commit

Permalink
Adding FTP testing
Browse files Browse the repository at this point in the history
  • Loading branch information
safouenaouadi committed Nov 13, 2023
1 parent 9f05dce commit 4ca3fdd
Show file tree
Hide file tree
Showing 11 changed files with 508 additions and 81 deletions.
8 changes: 6 additions & 2 deletions backend/src/main/java/ca/etsmtl/taf/jmeter/JMeterRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ private static Properties loadProperties() {
}
}

public static String runJMeter( ) {
String jmxFilePath = "backend/src/main/resources/jmeter/TestPlan.jmx";
public static String runJMeter( String testType) {
String jmxFilePath="";
if (testType=="http")
jmxFilePath= "backend/src/main/resources/jmeter/TestPlan.jmx";
else if (testType=="ftp")
jmxFilePath= "backend/src/main/resources/jmeter/FTPTestPlan.jmx";

// Generate a timestamp for uniqueness
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@


import ca.etsmtl.taf.jmeter.JMeterRunner;
import ca.etsmtl.taf.jmeter.model.JmeterTestPlan;
import ca.etsmtl.taf.jmeter.model.FTPTestPlan;
import ca.etsmtl.taf.jmeter.model.HttpTestPlan;
import com.opencsv.exceptions.CsvException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
Expand All @@ -20,11 +22,11 @@
import java.util.concurrent.atomic.AtomicReference;

@RestController
//@RequestMapping("/jmeter")
@RequestMapping("/jmeter")
public class JmeterController {

@PostMapping("/jmeter")
public ResponseEntity<?> getJmeterTestPlan(@RequestBody JmeterTestPlan jmeterTestPlan) throws IOException, CsvException {
@PostMapping("/http")
public ResponseEntity<?> getJmeterTestPlan(@RequestBody HttpTestPlan jmeterTestPlan) throws IOException, CsvException {


jmeterTestPlan.generateTestPlan();
Expand All @@ -33,7 +35,7 @@ public ResponseEntity<?> getJmeterTestPlan(@RequestBody JmeterTestPlan jmeterTes
// Submit the task to the ExecutorService
AtomicReference<String> resultPathRef = new AtomicReference<>();
Future<?> future = executorService.submit(() -> {
String result = JMeterRunner.runJMeter();
String result = JMeterRunner.runJMeter("http");
resultPathRef.set(result);
});
try {
Expand Down Expand Up @@ -64,4 +66,46 @@ public ResponseEntity<?> getJmeterTestPlan(@RequestBody JmeterTestPlan jmeterTes
return (ResponseEntity<?>) ResponseEntity.ok(result);
}

@PostMapping("/ftp")
public ResponseEntity<?> getFtpTestplan(@RequestBody FTPTestPlan ftpTestPlan) throws IOException, CsvException {


ftpTestPlan.generateTestPlan();
ExecutorService executorService = Executors.newSingleThreadExecutor();

// Submit the task to the ExecutorService
AtomicReference<String> resultPathRef = new AtomicReference<>();
Future<?> future = executorService.submit(() -> {
String result = JMeterRunner.runJMeter("ftp");
resultPathRef.set(result);
});
try {
// Wait for the task to finish
future.get();
} catch (InterruptedException | ExecutionException e) {
// Handle exceptions if necessary
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error occurred during task execution.");
} finally {
// Shutdown the ExecutorService to release resources
executorService.shutdown();
}
List<Map<String, String>> result=null;
String resultPath = resultPathRef.get();
System.out.println("Result Path: " + resultPath);
if (resultPath !="" && resultPath!=null){
try {
result =JMeterRunner.convertCSVtoJSON(resultPath);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (CsvException e) {
throw new RuntimeException(e);
}

}

return (ResponseEntity<?>) ResponseEntity.ok(result);
}


}
188 changes: 188 additions & 0 deletions backend/src/main/java/ca/etsmtl/taf/jmeter/model/FTPTestPlan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package ca.etsmtl.taf.jmeter.model;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FTPTestPlan {


private String nbThreads ;
private String rampTime ;
private String duration ;
private String domain ;
private String port ;
private String method;
private String remotefile;
private String localfile;
private String username;
private String password;
private String loop;
public FTPTestPlan() {

}

public FTPTestPlan(String nbThreads, String rampTime,
String duration, String domain, String port,
String remotefile, String localfile, String method, String username, String password) {
this.nbThreads = nbThreads;
this.rampTime = rampTime;
this.duration = duration;
this.domain = domain;
this.port = port;
this.method = method;
this.remotefile = remotefile;
this.localfile = localfile;
this.username = username;
this.password = password;
}

public String getLoop() {

return loop;
}

public void setLoop(String loop) {
this.loop = loop;
}

public String getNbThreads() {
return nbThreads;
}

public void setNbThreads(String nbThreads) {
this.nbThreads = nbThreads;
}

public String getRampTime() {
return rampTime;
}

public void setRampTime(String rampTime) {
this.rampTime = rampTime;
}

public String getDuration() {
return duration;
}

public void setDuration(String duration) {
this.duration = duration;
}

public String getDomain() {
return domain;
}

public void setDomain(String domain) {
this.domain = domain;
}

public String getPort() {
return port;
}

public void setPort(String port) {
this.port = port;
}

public String getRemotefile() {
return remotefile;
}

public void setRemotefile(String remotefile) {
this.remotefile = remotefile;
}

public String getLocalfile() {
return localfile;
}

public void setLocalfile(String localfile) {
this.localfile = localfile;
}

public String getMethod() {
return method=="get"? "false" : "true";

}

public void setMethod(String method) {
this.method = method;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


public void generateTestPlan() {
replaceAndSaveVariables();
}
private void replaceAndSaveVariables() {
try {
// Read the XML content from the file
String filePath = "backend/src/main/resources/jmeter/FTPSamplerTemplate.jmx";
String xmlContent = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
String target = "backend/src/main/resources/jmeter/FTPTestPlan.jmx";

// Replace variables with Java variables (using default values if not found)
xmlContent = replaceVariables(xmlContent);

// Save the modified content back to the file
Files.write(Paths.get(target), xmlContent.getBytes(StandardCharsets.UTF_8));

System.out.println("Variables replaced successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}

private String replaceVariables(String xmlContent) {
// Replace variables in the XML content using instance fields
xmlContent = xmlContent.replace("$NB_THREADS$", nbThreads)
.replace("$RAMP_TIME$", rampTime)
.replace("$DURATION$", duration)
.replace("$DOMAIN$", domain)
.replace("$PORT$", port)
.replace("$REMOTEFILE$", remotefile)
.replace("$LOCALFILE$", localfile)
.replace("$METHOD$", getMethod())
.replace("$USERNAME$", username)
.replace("$PASSWORD$", password)
.replace("$LOOP_COUNTER$", loop);


return xmlContent;
}

@Override
public String toString() {
return "JmeterFTPTestPlan{" +
"nbThreads='" + nbThreads + '\'' +
", rampTime='" + rampTime + '\'' +
", duration='" + duration + '\'' +
", domain='" + domain + '\'' +
", port='" + port + '\'' +
", remotefile='" + remotefile + '\'' +
", localfile='" + localfile + "\'" +
", method='" + method + '\'' +
", username='" + username + '\'' +
", password='" + password + '\''+
'}';
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;

public class JmeterTestPlan {
public class HttpTestPlan {


private String nbThreads ;
Expand Down Expand Up @@ -39,13 +39,13 @@ public void setData(String data) {
this.data = data;
}

public JmeterTestPlan() {
public HttpTestPlan() {

}

public JmeterTestPlan(String nbThreads, String rampTime,
String duration, String domain, String port,
String protocol, String path, String method) {
public HttpTestPlan(String nbThreads, String rampTime,
String duration, String domain, String port,
String protocol, String path, String method) {
this.nbThreads = nbThreads;
this.rampTime = rampTime;
this.duration = duration;
Expand Down
49 changes: 49 additions & 0 deletions backend/src/main/resources/jmeter/FTPSamplerTemplate.jmx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.5">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
<boolProp name="TestPlan.functional_mode">false</boolProp>
<boolProp name="TestPlan.tearDown_on_shutdown">false</boolProp>
<boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
<elementProp name="TestPlan.user_defined_variables" elementType="Arguments"
guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables"
enabled="true">
<collectionProp name="Arguments.arguments" />
</elementProp>
</TestPlan>
<hashTree>
<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group"
enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController"
guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller"
enabled="true">
<boolProp name="LoopController.continue_forever">false</boolProp>
<stringProp name="LoopController.loops">$LOOP_COUNTER$</stringProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">$NB_THREADS$</stringProp>
<stringProp name="ThreadGroup.ramp_time">$RAMP_TIME$</stringProp>
<boolProp name="ThreadGroup.scheduler">false</boolProp>
<stringProp name="ThreadGroup.duration"></stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>
<hashTree>
<FTPSampler guiclass="FtpTestSamplerGui" testclass="FTPSampler" testname="FTP Request"
enabled="true">
<stringProp name="FTPSampler.server">$DOMAIN$</stringProp>
<stringProp name="FTPSampler.port">$PORT$</stringProp>
<stringProp name="FTPSampler.filename">$REMOTEFILE$</stringProp>
<stringProp name="FTPSampler.localfilename">$LOCALFILE$</stringProp>
<stringProp name="FTPSampler.inputdata"></stringProp>
<boolProp name="FTPSampler.binarymode">false</boolProp>
<boolProp name="FTPSampler.saveresponse">false</boolProp>
<boolProp name="FTPSampler.upload">$METHOD$</boolProp>
<stringProp name="ConfigTestElement.username">$USERNAME$</stringProp>
<stringProp name="ConfigTestElement.password">$PASSWORD$</stringProp>
</FTPSampler>
<hashTree />
</hashTree>
</hashTree>
</hashTree>
</jmeterTestPlan>
Loading

0 comments on commit 4ca3fdd

Please sign in to comment.