Skip to content

Commit

Permalink
Worked on the issue #701 and created test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
onurd86 committed Oct 3, 2023
1 parent 04fb24f commit 7da3df5
Show file tree
Hide file tree
Showing 7 changed files with 1,491 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ Migrations/
/test-old-libraries/target/
/e2e-tests/spring-web/target/
/e2e-tests/spring-rest-mongo/target/
/core/src/main/resources/openapi_pet.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.evomaster.core.remote.SutProblemException
import java.net.ConnectException
import java.net.URI
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import javax.ws.rs.client.ClientBuilder
import javax.ws.rs.core.MediaType
Expand Down Expand Up @@ -39,11 +40,11 @@ object OpenApiAccess {
fun getOpenAPIFromURL(openApiUrl: String): OpenAPI {

//could be either JSON or YAML
val data = if(openApiUrl.startsWith("http", true)){
val data = if(openApiUrl.startsWith("http", true)){
readFromRemoteServer(openApiUrl)
} else {
} else {
readFromDisk(openApiUrl)
}
}

return getOpenApi(data)
}
Expand All @@ -62,17 +63,40 @@ object OpenApiAccess {
}

private fun readFromDisk(openApiUrl: String) : String {

// file scheme
val fileScheme = "file:"
val path = if (openApiUrl.startsWith(fileScheme, true)) {
Paths.get(URI.create(openApiUrl))
} else {
Paths.get(openApiUrl)

// path variable
var path: Path? = null;

// checking the given URL
try {
path = when (openApiUrl.startsWith(fileScheme, true)) {

true -> Paths.get(URI.create(openApiUrl));
false -> Paths.get(openApiUrl);
}
}
if (!Files.exists(path)) {
throw SutProblemException("Cannot find OpenAPI schema at file location: $openApiUrl")
// if the URL is not a valid one
catch (e: Exception) {
throw SutProblemException("The file path provided for the OpenAPI Schema $openApiUrl ," +
" is not a valid path");
}

return path.toFile().readText()
// Path should not be null, either
// it is set or an exception is thrown
if (path == null) {
throw SutProblemException("Could not set up the path");
}
else {
// if the file does not exists, throw cannot find OpenAPI schema
if (!Files.exists(path)) {
throw SutProblemException("Cannot find OpenAPI schema at file location: $openApiUrl")
}
// return the schema text
return path.toFile().readText()
}
}

private fun connectToServer(openApiUrl: String, attempts: Int): Response {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,15 @@ abstract class AbstractRestSampler : HttpWsSampler<RestIndividual>() {
private fun initForBlackBox() {

swagger = OpenApiAccess.getOpenAPIFromURL(configuration.bbSwaggerUrl)
if (swagger.paths == null) {
if (swagger.paths == null ) {
throw SutProblemException("There is no endpoint definition in the retrieved Swagger file")
}
// Onur: to give the error message for invalid swagger
else if (swagger.paths.size == 0){
throw SutProblemException("The swagger file ${configuration.bbSwaggerUrl.toString()} " +
"is either invalid or it does not define endpoints")
}


// ONUR: Add all paths to list of paths to ignore except endpointFocus
val endpointsToSkip = EndpointFilter.getEndPointsToSkip(config,swagger);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package org.evomaster.core.problem.rest

import org.evomaster.core.remote.SutProblemException

import io.swagger.v3.oas.models.OpenAPI

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

/*
Testing the local URL issue with OpenAPI, 4 test cases:
- a local file which exists and a valid URL
- a local file which does not exist and a valid URL
- a local file which exists and an invalid URL
- a local file which does not exist and an invalid URL
- a local file which is not a valid swagger
- an invalid url
*/
class OpenAPILocalURLIssueTest {

private val executionPath :String = System.getProperty("user.dir")
private lateinit var swagger: OpenAPI


/*
Check that the swagger is created with a valid URL
and an existing file
*/
@Test
fun testExistingFileValidURL() {

// get the current directory
val urlToTest = "file://$executionPath/src/test/resources/openapi_pet.json";

// create swagger from URI
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);

// a valid swagger is created
Assertions.assertTrue(swagger != null);
}

/*
Check that an exception is thrown with a non-existing file
and a valid url
*/
@Test
fun testNonExistingFileValidURL() {

// get the current directory
val urlToTest = "file://$executionPath/src/test" +
"/resources/openapi_pet_non_existing.json";

// since the file does not exist,
// a valid swagger cannot be created but an
// SutException should be thrown
val exception = Assertions.assertThrows(
SutProblemException::class.java
) {
// create swagger
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);
}

// the message in the SutException should be
// Cannot find OpenAPI schema at file location: $urlToTest

// check that the message is correct
Assertions.assertTrue(
exception.message!!.contains("Cannot find OpenAPI " +
"schema at file location: $urlToTest"))
}

/*
Check that an exception is thrown when the file exists
but the URL is not valid, missing one slash (/)
*/
@Test
fun testExistingFileInvalidURL() {

// get the current directory
val urlToTest = "file:/$executionPath/src/test" +
"/resources/openapi_pet.json";

// since the file does not exist,
// a valid swagger cannot be created but an
// SutException should be thrown
val exception = Assertions.assertThrows(
SutProblemException::class.java
) {
// create swagger
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);
}

/*
the message in the SutException should be
The file path provided for the OpenAPI Schema
$urlToTest , is not a valid path
*/
Assertions.assertTrue(
exception.message!!.contains("The file path provided for the OpenAPI Schema $urlToTest ," +
" is not a valid path"))
}

/*
Check that an exception is thrown when the file
does not exist and the URL is not valid,
missing one slash (/)
*/
@Test
fun testNonExistingFileInvalidURL() {

// get the current directory
val urlToTest = "file:/$executionPath/src/test" +
"/resources/openapi_pet_non_existent.json";

// since the file does not exist,
// a valid swagger cannot be created but an
// SutException should be thrown
val exception = Assertions.assertThrows(
SutProblemException::class.java
) {
// create swagger
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);
}

/*
the message in the SutException should be
The file path provided for the OpenAPI Schema
$urlToTest , is not a valid path
*/
Assertions.assertTrue(
exception.message!!.contains("The file path provided for the OpenAPI Schema $urlToTest ," +
" is not a valid path"))
}

/*
Check that an exception is thrown when the file
does not exist and the URL is not valid,
missing one slash (/)
*/
@Test
fun testExistingFileInvalidSwagger() {

// get the current directory
val urlToTest = "file:/$executionPath/src/test" +
"/resources/openapi_pet_non_existent.json";

// since the file does not exist,
// a valid swagger cannot be created but an
// SutException should be thrown
val exception = Assertions.assertThrows(
SutProblemException::class.java
) {
// create swagger
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);
}

/*
the message in the SutException should be
The file path provided for the OpenAPI Schema
$urlToTest , is not a valid path
*/
Assertions.assertTrue(
exception.message!!.contains("The file path provided for the OpenAPI Schema $urlToTest ," +
" is not a valid path"))
}

/*
Check that when the swagger is invalid,
an exception is thrown with the message
*/
@Test
fun testInvalidSwagger() {

// get the current directory
val urlToTest = "file://$executionPath/src/test" +
"/resources/invalid_swagger.json";

// create swagger
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);

/*
An empty swagger should be created
*/
Assertions.assertTrue(swagger.paths.size == 0)
}

@Test
fun testInvalidURLI() {

// get the current directory
val urlToTest = "file://$executionPath/xxxxxxxx";

val exception = Assertions.assertThrows(
SutProblemException::class.java
) {
// create swagger
swagger = OpenApiAccess.getOpenAPIFromURL(urlToTest);
}

/*
the message in the SutException should be
The file path provided for the OpenAPI Schema
$urlToTest , is not a valid path
*/
Assertions.assertTrue(
exception.message!!.contains("Cannot find OpenAPI " +
"schema at file location: $urlToTest"))
}



}
3 changes: 3 additions & 0 deletions core/src/test/resources/invalid_swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key" : "value"
}
Loading

0 comments on commit 7da3df5

Please sign in to comment.