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

API test (Using API Spec) generated by RoostGPT #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<!--Plugin added by RoostGPT-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<outputDirectory>testReport</outputDirectory>
</configuration>
<!--Plugin added by RoostGPT-->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.1</version>
<configuration>
<outputDirectory>testReport</outputDirectory>
</configuration>
<!--Plugin added by RoostGPT-->
</plugin>
</plugins>
</build>
<dependencies>
Expand Down Expand Up @@ -61,5 +85,33 @@
<scope>compile</scope>
<!--Dependency added by RoostGPT-->
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
<!--Dependency added by RoostGPT-->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
<scope>compile</scope>
<!--Dependency added by RoostGPT-->
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.1</version>
<scope>compile</scope>
<!--Dependency added by RoostGPT-->
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
<scope>compile</scope>
<!--Dependency added by RoostGPT-->
</dependency>
</dependencies>
</project>
117 changes: 117 additions & 0 deletions src/test/java/org/springframework/RoostTest/TestdataLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

package org.springframework.RoostTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Arrays;

// Test Loader loads the data from ENV, Properties File and CSV file in given order.
public class TestdataLoader {

Properties properties = new Properties();

private String fromENV(String name) {
return System.getenv(name);
}

private void loadPropertiesFile() {
String propertiesFileName = this.fromENV("PROPERTIES_FILE");
InputStream content = null;
try {
if (propertiesFileName == null || propertiesFileName.equals("")) {
content = getClass().getClassLoader().getResourceAsStream("application.properties");
} else if (propertiesFileName.contains(File.separator)) {
// handles if PROPERTIES_FILE contains path.
content = new FileInputStream(propertiesFileName);
} else {
// handles if PROPERTIES_FILE is inside src/main/resources folder.
content = getClass().getClassLoader().getResourceAsStream(propertiesFileName);
}
if (content != null) {
this.properties.load(content);
} else {
System.out.println(
"Skip loading from properties file. ENV['PROPERTIES_FILE'] or src/main/resources/application.properties file doesn't exists.");
}
} catch (IOException e) {
String errorMessage = e.getMessage();
System.out.printf("Skip loading from properties file as encountered error : %s" + errorMessage);
}
}

private String fromPropertiesFile(String name) {
return properties.getProperty(name);
}

private List<Map<String, String>> fromCSVFile(String filePath) {
List<Map<String, String>> data = new ArrayList<>();
String delimiter = "\\^\\|\\^";

try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
boolean headerSkipped = false;
List<String> headers = new ArrayList<>();

while ((line = br.readLine()) != null) {
if (!headerSkipped) {
headers.addAll(List.of(line.split(delimiter)));
headerSkipped = true;
continue;
}

String[] values = line.split(delimiter);
if (values.length > 0) {
Map<String, String> row = new HashMap<>();
for (int i = 0; i < Math.min(headers.size(), values.length); i++) {
row.put(headers.get(i), values[i].trim());
}
data.add(row);
}
}
} catch (IOException e) {
String errorMessage = e.getMessage();
System.out.printf("Skip loading from csv file : %s" + errorMessage);
}
return data;
}

public List<Map<String, String>> load(String csvFileName, String[] envVarsList) {
Map<String, String> envMap = new HashMap<>();
List<Map<String, String>> csvData = this.fromCSVFile(csvFileName);
List<Map<String, String>> envVars = new ArrayList<>();
this.loadPropertiesFile();

for (String key : envVarsList) {
envMap.put(key, "");
String value = this.fromENV(key);
if (value != null) {
envMap.put(key, value);
}
value = this.fromPropertiesFile(key);
if (value != null) {
envMap.put(key, value);
}
}
for (Map<String, String> row : csvData) {
Map<String, String> envMapwithCSV = new HashMap<>(envMap);
for (Map.Entry<String, String> entry : row.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (!value.equals("")) {
envMapwithCSV.put(key, value);
}
}
envVars.add(envMapwithCSV);
}
return envVars;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// ********RoostGPT********
/*
Test generated by RoostGPT for test sample-api-petstore-rest-assured-test using AI Type Open AI and AI Model gpt-4

Test generated for /pet/findByStatus_get for http method type GET in rest-assured framework

RoostTestHash=d737da9198


*/

// ********RoostGPT********
package org.springframework.RoostTest;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.MatcherAssert;
import static org.hamcrest.Matchers.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.json.JSONObject;
import org.json.XML;
import org.json.JSONException;
import org.json.JSONArray;
import java.util.Arrays;

public class petFindByStatusGetTest {

List<Map<String, String>> envList = new ArrayList<>();


@BeforeEach
public void setUp() {
TestdataLoader dataloader = new TestdataLoader();
String[] envVarsList = {""};
envList = dataloader.load("src/test/java/org/springframework/RoostTest/pet_findByStatusGetTest.csv", envVarsList);
}


@Test
public void petFindByStatusGet_Test() throws JSONException {
this.setUp();
Integer testNumber = 1;
for (Map<String, String> testData : envList) {
RestAssured.baseURI = (testData.get("BASE_URL") != null && !testData.get("BASE_URL").isEmpty()) ? testData.get("BASE_URL"): "https://petstore.swagger.io/v2";
JSONObject requestBodyObject = new JSONObject();
if(testData.get("RequestBody") != null){
requestBodyObject = new JSONObject(testData.get("RequestBody"));
}
Response responseObj = given()
.queryParam("status", testData.get("status") != null ? testData.get("status") : "")
.when()
.get("/pet/findByStatus")
.then()
.extract().response();
JsonPath response;
String contentType = responseObj.getContentType();

System.out.printf("Test Case %d: petFindByStatusGet_Test \n", testNumber++);
System.out.println("Request: GET /pet/findByStatus");
System.out.println("Status Code: " + responseObj.statusCode());
if (testData.get("statusCode") != null) {
String statusCodeFromCSV = testData.get("statusCode");
if (statusCodeFromCSV.contains("X")) {
MatcherAssert.assertThat(
"Expected a status code of category " + statusCodeFromCSV + ", but got "
+ Integer.toString(responseObj.statusCode()) + " instead",
Integer.toString(responseObj.statusCode()).charAt(0), equalTo(statusCodeFromCSV.charAt(0)));
} else {
MatcherAssert.assertThat(
Integer.toString(responseObj.statusCode()), equalTo(statusCodeFromCSV));
}
}
else {
List<Integer> expectedStatusCodes = Arrays.asList(200,400);
MatcherAssert.assertThat(responseObj.statusCode(), is(in(expectedStatusCodes)));
}
String stringifiedStatusCode = Integer.toString(responseObj.statusCode());
if (contentType.contains("application/xml") || contentType.contains("text/xml")) {
String xmlResponse = responseObj.asString();
JSONObject jsonResponse = XML.toJSONObject(xmlResponse);
JSONObject jsonData = jsonResponse.getJSONObject("xml");
String jsonString = jsonData.toString();
response = new JsonPath(jsonString);

} else if(contentType.contains("application/json")){
response = responseObj.jsonPath();
} else {
System.out.println("Response content type found: "+contentType+", but RoostGPT currently only supports the following response content types: application/json,text/xml,application/xml");
continue;
}

if(stringifiedStatusCode.equals("200")){
JSONArray respoJsonArray = new JSONArray(responseObj.asString());

for (int it = 0; it < respoJsonArray.length(); it++) {
response = new JsonPath(respoJsonArray.getJSONObject(it).toString());

if (response.get("id") != null) {

MatcherAssert.assertThat(response.get("id"), instanceOf(Integer.class));
}

if (response.get("category") != null) {

if (response.get("category.id") != null) {

MatcherAssert.assertThat(response.get("category.id"), instanceOf(Integer.class));
}

if (response.get("category.name") != null) {

MatcherAssert.assertThat(response.get("category.name"), instanceOf(String.class));
}

}

if (response.get("name") != null) {

MatcherAssert.assertThat(response.get("name"), instanceOf(String.class));
}

if (response.get("photoUrls") != null) {

for (int i = 0; i < response.getList("photoUrls").size(); i++) {
}
MatcherAssert.assertThat(response.getList("photoUrls"), instanceOf(List.class));

}

if (response.get("tags") != null) {

for (int i = 0; i < response.getList("tags").size(); i++) {
if (response.get("tags["+ i +"].id") != null) {

MatcherAssert.assertThat(response.get("tags["+ i +"].id"), instanceOf(Integer.class));
}

if (response.get("tags["+ i +"].name") != null) {

MatcherAssert.assertThat(response.get("tags["+ i +"].name"), instanceOf(String.class));
}

}
MatcherAssert.assertThat(response.getList("tags"), instanceOf(List.class));

}

if (response.get("status") != null) {

MatcherAssert.assertThat(response.get("status"), instanceOf(String.class));
MatcherAssert.assertThat(response.getString("status"), anyOf(equalTo("available"), equalTo("pending"), equalTo("sold")));

}

}
}
if(stringifiedStatusCode.equals("400")){ System.out.println("Description: Invalid status value");
}


}
}
}
Loading