Skip to content

Commit

Permalink
Fix errors and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vicato2000 committed Oct 6, 2023
1 parent cb4bae9 commit 4583213
Show file tree
Hide file tree
Showing 32 changed files with 727 additions and 150 deletions.
11 changes: 6 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ stateful_data.json
/src/main/resources/Examples/Ex1_RandomGeneration/test_cases/
/src/main/resources/Examples/Ex2_CreateTestConf/default_test_conf.yaml
/src/main/resources/Examples/Ex3_CBTGeneration/test_cases/
/src/main/resources/Examples/Ex4_CBTGenerationAuth/test_cases/
/src/main/resources/Examples/Ex5_RandomGeneration_DataMutation/test_cases/
/src/main/resources/Examples/Ex8_Generation_Execution/allure_report/anApiOfIceAndFire/
/src/main/resources/Examples/Ex8_Generation_Execution/csv_report/anApiOfIceAndFire/
/src/main/resources/Examples/Ex9_Iterative_Generation_Execution/allure_report/anApiOfIceAndFire/
/src/main/resources/Examples/Ex4_ARTestGeneration/test_cases/
/src/main/resources/Examples/Ex5_CBTGenerationAuth/test_cases/
/src/main/resources/Examples/Ex6_RandomGeneration_DataMutation/test_cases/
/src/main/resources/Examples/Ex9_Generation_Execution/allure_report/anApiOfIceAndFire/
/src/main/resources/Examples/Ex9_Generation_Execution/csv_report/anApiOfIceAndFire/
/src/main/resources/Examples/Ex10_Iterative_Generation_Execution/allure_report/anApiOfIceAndFire/
68 changes: 43 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,31 @@ In this example, we will explore the generation and execution of test cases usin

```java

public class Ex8_Generation_Execution {
public class Ex9_Generation_Execution {

public static String propertyFilePath="src/main/resources/Examples/Ex8_Generation_Execution/user_config.properties"; // Path to user properties file with configuration options
public static final String PROPERTY_FILE_PATH = "src/main/resources/Examples/Ex9_Generation_Execution/user_config.properties"; // Path to user properties file with configuration options

public static void main(String[] args) throws RESTestException {
// Load properties
RESTestRunner runner = new RESTestRunner(propertyFilePath);
public static final Logger logger = Logger.getLogger(Ex9_Generation_Execution.class.getName());

// Run workflow
runner.run();
public static void main(String[] args) throws RESTestException {
// Load properties
RESTestRunner runner = new RESTestRunner(PROPERTY_FILE_PATH);

System.out.println(runner.getNumberOfTestCases() + " test cases generated and written to " + runner.getTargetDirJava());
System.out.println("Allure report available at " + runner.getAllureReportsPath());
System.out.println("CSV stats available at " + PropertyManager.readProperty("data.tests.dir") + "/" + runner.getExperimentName());
System.out.println("Coverage report available at " + PropertyManager.readProperty("data.coverage.dir") + "/" + runner.getExperimentName());
}
// Run workflow
runner.run();

if (logger.isLoggable(java.util.logging.Level.INFO)) {
String message1 = String.format("%d test cases generated and written to %s", runner.getNumberOfTestCases(), runner.getTargetDirJava());
String message2 = String.format("Allure report available at %s", runner.getAllureReportsPath());
String message3 = String.format("CSV stats available at %s/%s", PropertyManager.readProperty("data.tests.dir"), runner.getExperimentName());
String message4 = String.format("Coverage report available at %s/%s", PropertyManager.readProperty("data.coverage.dir"), runner.getExperimentName());
logger.info(message1);
logger.info(message2);
logger.info(message3);
logger.info(message4);
}

}
}


Expand All @@ -160,7 +169,7 @@ Finally, test failures are collected and they can be easily spotted and analyzed

En this section, we will delve into a specific example to illustrate in detail how RESTEST operates. Throughout the following steps, we will break down a practical scenario, providing step-by-step explanations of how the system functions.

In this example, we will conduct tests on the Ice and Fire API using RESTest. To perform these tests, we will rely on the OpenAPI specification of the API, which is available at the following [link](src/main/resources/Examples/Ex9_Iterative_Generation_Execution/spec_iceandfire.yaml).
In this example, we will conduct tests on the Ice and Fire API using RESTest. To perform these tests, we will rely on the OpenAPI specification of the API, which is available at the following [link](src/main/resources/Examples/Ex10_Iterative_Generation_Execution/spec_iceandfire.yaml).

En the next step, we will proceed to obtain the configuration file that will encompass all the essential settings for our tests. This file plays a fundamental role as it sets key parameters, such as the types of generators to use, specific configurations for the Ice and Fire API, and any other relevant information for the successful execution of tests with RESTest.
This is the configuration file for this example:
Expand Down Expand Up @@ -351,22 +360,31 @@ Once we have this file, we will be ready to carry out the generation and executi

```java

public class Ex8_Generation_Execution {
public class Ex9_Generation_Execution {

public static String propertyFilePath="src/main/resources/Examples/Ex8_Generation_Execution/user_config.properties"; // Path to user properties file with configuration options
public static final String PROPERTY_FILE_PATH = "src/main/resources/Examples/Ex9_Generation_Execution/user_config.properties"; // Path to user properties file with configuration options

public static void main(String[] args) throws RESTestException {
// Load properties
RESTestRunner runner = new RESTestRunner(propertyFilePath);
public static final Logger logger = Logger.getLogger(Ex9_Generation_Execution.class.getName());

// Run workflow
runner.run();
public static void main(String[] args) throws RESTestException {
// Load properties
RESTestRunner runner = new RESTestRunner(PROPERTY_FILE_PATH);

System.out.println(runner.getNumberOfTestCases() + " test cases generated and written to " + runner.getTargetDirJava());
System.out.println("Allure report available at " + runner.getAllureReportsPath());
System.out.println("CSV stats available at " + PropertyManager.readProperty("data.tests.dir") + "/" + runner.getExperimentName());
System.out.println("Coverage report available at " + PropertyManager.readProperty("data.coverage.dir") + "/" + runner.getExperimentName());
}
// Run workflow
runner.run();

if (logger.isLoggable(java.util.logging.Level.INFO)) {
String message1 = String.format("%d test cases generated and written to %s", runner.getNumberOfTestCases(), runner.getTargetDirJava());
String message2 = String.format("Allure report available at %s", runner.getAllureReportsPath());
String message3 = String.format("CSV stats available at %s/%s", PropertyManager.readProperty("data.tests.dir"), runner.getExperimentName());
String message4 = String.format("Coverage report available at %s/%s", PropertyManager.readProperty("data.coverage.dir"), runner.getExperimentName());
logger.info(message1);
logger.info(message2);
logger.info(message3);
logger.info(message4);
}

}
}

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package es.us.isa.restest.examples;

import es.us.isa.restest.runners.RESTestIterativeRunner;
import es.us.isa.restest.util.PropertyManager;
import es.us.isa.restest.util.RESTestException;

import java.util.logging.Logger;

/**
* This example shows how to generate test cases, execute them, and generate an Allure report iteratively using RESTestIterativeRunner.
* This can be used to generate and run test cases in small batches, adding a delay among iterations, to prevent API quota violations.
* Also, this is helpful for generating and executing test cases for long periods of time, or even indefinitely until de process is stopped.
*
* For opening Allure reports in a local browser check cross-origin restrictions: https://stackoverflow.com/questions/51081754/cross-origin-request-blocked-when-loading-local-file
*
* The resources for this example are located at src/main/resources/Examples/Ex10_Iterative_Generation_Execution.
*
*/
public class Ex10_Iterative_Generation_Execution {

public static final String PROPERTY_FILE_PATH = "src/main/resources/Examples/Ex10_Iterative_Generation_Execution/user_config.properties"; // Path to user properties file with configuration options

public static final Logger logger = Logger.getLogger(Ex10_Iterative_Generation_Execution.class.getName());

public static void main(String[] args) throws RESTestException {

// Load properties
RESTestIterativeRunner runner = new RESTestIterativeRunner(PROPERTY_FILE_PATH);

// Run workflow
runner.run();

if (logger.isLoggable(java.util.logging.Level.INFO)) {
String message1 = String.format("%d test cases generated and written to %s", runner.getNumberOfTestCases(), runner.getTargetDirJava());
String message2 = String.format("Allure report available at %s", runner.getAllureReportsPath());
String message3 = String.format("CSV stats available at %s/%s", PropertyManager.readProperty("data.tests.dir"), runner.getExperimentName());
String message4 = String.format("Coverage report available at %s/%s", PropertyManager.readProperty("data.coverage.dir"), runner.getExperimentName());
logger.info(message1);
logger.info(message2);
logger.info(message3);
logger.info(message4);
}

}
}
13 changes: 10 additions & 3 deletions src/main/java/es/us/isa/restest/examples/Ex1_RandomGeneration.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import es.us.isa.restest.writers.restassured.RESTAssuredWriter;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import static es.us.isa.restest.util.FileManager.createDir;

Expand All @@ -22,11 +24,13 @@
*/
public class Ex1_RandomGeneration {

public static String propertyFilePath="src/main/resources/Examples/Ex1_RandomGeneration/user_config.properties"; // Path to user properties file with configuration options
public static final String PROPERTY_FILE_PATH="src/main/resources/Examples/Ex1_RandomGeneration/user_config.properties"; // Path to user properties file with configuration options

public static final Logger logger = Logger.getLogger(Ex1_RandomGeneration.class.getName());

public static void main(String[] args) throws RESTestException {
// Load properties
RESTestLoader loader = new RESTestLoader(propertyFilePath);
RESTestLoader loader = new RESTestLoader(PROPERTY_FILE_PATH);

// Create test case generator
RandomTestCaseGenerator generator = (RandomTestCaseGenerator) loader.createGenerator();
Expand All @@ -39,6 +43,9 @@ public static void main(String[] args) throws RESTestException {
RESTAssuredWriter writer = (RESTAssuredWriter) loader.createWriter();
writer.write(testCases);

System.out.println(testCases.size() + " test cases generated and written to " + loader.getTargetDirJava());
if (logger.isLoggable(Level.INFO)) {
String message = String.format("%d test cases generated and written to %s", testCases.size(), loader.getTargetDirJava());
logger.info(message);
}
}
}
31 changes: 15 additions & 16 deletions src/main/java/es/us/isa/restest/examples/Ex2_CreateTestConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

import es.us.isa.restest.configuration.TestConfigurationFilter;
import es.us.isa.restest.configuration.generators.DefaultTestConfigurationGenerator;
import es.us.isa.restest.generators.RandomTestCaseGenerator;
import es.us.isa.restest.runners.RESTestLoader;
import es.us.isa.restest.specification.OpenAPISpecification;
import es.us.isa.restest.testcases.TestCase;
import es.us.isa.restest.util.RESTestException;
import es.us.isa.restest.writers.restassured.RESTAssuredWriter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import static es.us.isa.restest.util.FileManager.checkIfExists;
import static es.us.isa.restest.util.FileManager.deleteFile;
import static org.junit.Assert.assertTrue;

/**
* All RESTest test case generators requires an input test configuration file specifying the API operations to be tested,
Expand All @@ -28,26 +21,32 @@
*/
public class Ex2_CreateTestConf {

public static String specPath="src/main/resources/Examples/Ex2_CreateTestConf/spec_bigoven.yaml"; // Path to OAS specification file
public static String confPath="src/main/resources/Examples/Ex2_CreateTestConf/default_test_conf.yaml"; // Path to test configuration file
public static final String SPEC_PATH = "src/main/resources/Examples/Ex2_CreateTestConf/spec_bigoven.yaml"; // Path to OAS specification file
public static final String CONF_PATH = "src/main/resources/Examples/Ex2_CreateTestConf/default_test_conf.yaml"; // Path to test configuration file

public static final Logger logger = Logger.getLogger(Ex2_CreateTestConf.class.getName());

public static void main(String[] args) throws RESTestException {

public static void main(String[] args) {

// Load specification file
OpenAPISpecification spec = new OpenAPISpecification(specPath);
OpenAPISpecification spec = new OpenAPISpecification(SPEC_PATH);

// Create filters to indicate which operations (paths and http methods) to include in the test configuration file.
List<TestConfigurationFilter> filters = new ArrayList<TestConfigurationFilter>();
List<TestConfigurationFilter> filters = new ArrayList<>();
TestConfigurationFilter filter = new TestConfigurationFilter();
filter.setPath("/recipes");
filter.addGetMethod();
filters.add(filter);

// Generate default test configuration file
DefaultTestConfigurationGenerator gen = new DefaultTestConfigurationGenerator(spec);
gen.generate(confPath, filters);
gen.generate(CONF_PATH, filters);

if (logger.isLoggable(Level.INFO)) {
String message = String.format("Default test configuration file generated at %s", CONF_PATH);
logger.info(message);
}

System.out.println("Default test configuration file generated at " + confPath);
}
}
15 changes: 11 additions & 4 deletions src/main/java/es/us/isa/restest/examples/Ex3_CBTGeneration.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package es.us.isa.restest.examples;

import es.us.isa.restest.generators.ConstraintBasedTestCaseGenerator;
import es.us.isa.restest.generators.RandomTestCaseGenerator;
import es.us.isa.restest.runners.RESTestLoader;
import es.us.isa.restest.testcases.TestCase;
import es.us.isa.restest.util.RESTestException;
import es.us.isa.restest.writers.restassured.RESTAssuredWriter;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import static es.us.isa.restest.util.FileManager.createDir;

Expand All @@ -22,11 +23,13 @@
*/
public class Ex3_CBTGeneration {

public static String propertyFilePath="src/main/resources/Examples/Ex3_CBTGeneration/user_config.properties"; // Path to user properties file with configuration options
public static final String PROPERTY_FILE_PATH = "src/main/resources/Examples/Ex3_CBTGeneration/user_config.properties"; // Path to user properties file with configuration options

public static final Logger logger = Logger.getLogger(Ex3_CBTGeneration.class.getName());

public static void main(String[] args) throws RESTestException {
// Load properties
RESTestLoader loader = new RESTestLoader(propertyFilePath);
RESTestLoader loader = new RESTestLoader(PROPERTY_FILE_PATH);

// Create test case generator
ConstraintBasedTestCaseGenerator generator = (ConstraintBasedTestCaseGenerator) loader.createGenerator();
Expand All @@ -39,6 +42,10 @@ public static void main(String[] args) throws RESTestException {
RESTAssuredWriter writer = (RESTAssuredWriter) loader.createWriter();
writer.write(testCases);

System.out.println(testCases.size() + " test cases generated and written to " + loader.getTargetDirJava());
if (logger.isLoggable(Level.INFO)) {
String message = String.format("%d test cases generated and written to %s", testCases.size(), loader.getTargetDirJava());
logger.info(message);
}

}
}
52 changes: 52 additions & 0 deletions src/main/java/es/us/isa/restest/examples/Ex4_ARTestGeneration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package es.us.isa.restest.examples;

import es.us.isa.restest.generators.ARTestCaseGenerator;
import es.us.isa.restest.runners.RESTestLoader;
import es.us.isa.restest.testcases.TestCase;
import es.us.isa.restest.util.RESTestException;
import es.us.isa.restest.writers.restassured.RESTAssuredWriter;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

import static es.us.isa.restest.util.FileManager.createDir;

/**
* This example shows how to generate a set of test cases using SemanticGenerator and write them to a file using the RESTAssured writer.
* These types of tests are based on modifying a generated test so that it is entirely different from the previous one.
*
* The resources for this example are located at src/main/resources/Examples/Ex4_ARTestGeneration.
*
* **/

public class Ex4_ARTestGeneration {

// Need to create the file src\test\resources\auth\OMDb\apikeys.json
public static final String PROPERTY_FILE_PATH = "src/main/resources/Examples/Ex4_ARTestGeneration/omdb.properties"; // Path to user properties file with configuration options

public static final Logger logger = Logger.getLogger(Ex4_ARTestGeneration.class.getName());

public static void main(String[] args) throws RESTestException {
// Load properties
RESTestLoader loader = new RESTestLoader(PROPERTY_FILE_PATH);

// Create test case generator
ARTestCaseGenerator generator = (ARTestCaseGenerator) loader.createGenerator();
Collection<TestCase> testCases = generator.generate();

// Create target directory for test cases if it does not exist
createDir(loader.getTargetDirJava());

// Write (RestAssured) test cases
RESTAssuredWriter writer = (RESTAssuredWriter) loader.createWriter();
writer.write(testCases);

if (logger.isLoggable(Level.INFO)) {
String message = String.format("%d test cases generated and written to %s", testCases.size(), loader.getTargetDirJava());
logger.info(message);
}

}

}
Loading

0 comments on commit 4583213

Please sign in to comment.