Skip to content
Snippets Groups Projects
Commit c9b42fc4 authored by Jueun Park's avatar Jueun Park
Browse files

Added generateParameters()

parent 527bc6b1
No related branches found
No related tags found
1 merge request!6Resolve "check and fix random request generator"
Pipeline #13325 passed
...@@ -6,7 +6,8 @@ aspect RandomRequestGenerator{ ...@@ -6,7 +6,8 @@ aspect RandomRequestGenerator{
/** /**
* Calls <code>generateUrl()</code> for all paths. * Calls <code>generateUrl()</code> for all paths.
* @return The list of String representing the generated URLs. * @return The list of String representing the generated URLs.
*/ syn List<String> OpenAPIObject.generateRequests(){ */
syn List<String> OpenAPIObject.generateRequests(){
List<String> urls = new ArrayList<>(); List<String> urls = new ArrayList<>();
try { try {
for (PathsObject p : getPList()) for (PathsObject p : getPList())
...@@ -139,6 +140,45 @@ aspect RandomRequestGenerator{ ...@@ -139,6 +140,45 @@ aspect RandomRequestGenerator{
return pathRef; return pathRef;
} }
/**
* Generates parameters for all paths.
* @return The tuple (Map) representing the generated parameter values mapped into corresponding parameter names.
*/
public Map<String, List<Object>> OpenAPIObject.generateParameters() {
Map<String, List<Object>> parameters = new HashMap<>();
ParameterObject o;
SchemaObject s;
for (ParameterOb p : parameterObs()) {
List<Object> values = new ArrayList<>();
o = p.parameterObject();
s = o.getSchema().schemaObject();
if (s.getType().equals("string"))
values.add(o.generateRandomString(s.getEList()));
else if (s.getType().equals("integer"))
values.add(o.generateRandomInt(-1, 100)); // boundary at -1 and 100
else if (s.getType().equals("array")) {
List<Object> arrayValues = new ArrayList<>();
if (s.getI().getSchema().schemaObject().getType().equals("string")) {
for (EnumObj e : s.getI().getSchema().schemaObject().getEList())
arrayValues.add(o.randomValueFromEnum(e));
values.add(arrayValues);
} else if (s.getI().getSchema().schemaObject().getType().equals("integer")) {
for (int i = 0; i < 5; i++)
arrayValues.add(o.generateRandomInt(-1, 100));
values.add(arrayValues);
}
}
if( parameters.get(o.getName()) != null )
values.addAll(parameters.get(o.getName()));
parameters.put(o.getName(), values);
}
return parameters;
}
/** /**
* Generates a random String value. * Generates a random String value.
* @return A random String. * @return A random String.
...@@ -180,6 +220,15 @@ aspect RandomRequestGenerator{ ...@@ -180,6 +220,15 @@ aspect RandomRequestGenerator{
pathRef + "&" + this.getName() + "=" + URLEncoder.encode(e.getEnumOb().toString(), Charset.forName("US-ASCII")) : pathRef; pathRef + "&" + this.getName() + "=" + URLEncoder.encode(e.getEnumOb().toString(), Charset.forName("US-ASCII")) : pathRef;
} }
/**
* Generates a random value of Enum.
* @return A random value of Enum.
*/
public String ParameterObject.randomValueFromEnum(EnumObj e){
Random rand = new Random();
return rand.nextDouble() < 0.5 ? URLEncoder.encode(e.getEnumOb().toString(), Charset.forName("US-ASCII")) : "";
}
/** /**
* This is a method to avoid the duplication of Urls. * This is a method to avoid the duplication of Urls.
* @return The list of Urls. * @return The list of Urls.
......
...@@ -40,6 +40,10 @@ aspect Reference { ...@@ -40,6 +40,10 @@ aspect Reference {
PathItemTuple contributes this PathItemTuple contributes this
to OpenAPIObject.pathItemTuples(); to OpenAPIObject.pathItemTuples();
coll List<ParameterOb> OpenAPIObject.parameterObs() [new ArrayList<>()] root OpenAPIObject;
ParameterOb contributes this
to OpenAPIObject.parameterObs();
syn PathItemObject PathItemOb.pathItemObject(); syn PathItemObject PathItemOb.pathItemObject();
eq PathItemObject.pathItemObject() = this; eq PathItemObject.pathItemObject() = this;
eq PathItemReference.pathItemObject() { eq PathItemReference.pathItemObject() {
......
...@@ -10,7 +10,9 @@ import java.io.*; ...@@ -10,7 +10,9 @@ import java.io.*;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class OpenAPIMain { public class OpenAPIMain {
static List<String> successfulUrls = new ArrayList<>(); static List<String> successfulUrls = new ArrayList<>();
...@@ -19,7 +21,7 @@ public class OpenAPIMain { ...@@ -19,7 +21,7 @@ public class OpenAPIMain {
* main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd * main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd
**/ **/
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
String fileName = "./src/main/resources/APIs/1password.local/connect/1.3.0/openapi.yaml"; String fileName = "./src/main/resources/3.0/petstore-v2.yaml";
OpenAPIObject jastAddObject; OpenAPIObject jastAddObject;
SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null); SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null);
OpenAPI openAPI = result.getOpenAPI(); OpenAPI openAPI = result.getOpenAPI();
...@@ -28,6 +30,8 @@ public class OpenAPIMain { ...@@ -28,6 +30,8 @@ public class OpenAPIMain {
jastAddObject = OpenAPIObject.parseOpenAPI(openAPI); jastAddObject = OpenAPIObject.parseOpenAPI(openAPI);
jastAddObject.generateParameters();
generatedURLs = jastAddObject.generateRequests(); generatedURLs = jastAddObject.generateRequests();
dictionary = sendRandomRequests(jastAddObject, generatedURLs); dictionary = sendRandomRequests(jastAddObject, generatedURLs);
...@@ -51,6 +55,8 @@ public class OpenAPIMain { ...@@ -51,6 +55,8 @@ public class OpenAPIMain {
System.out.println("Loading expression DSL file '" + fileName + "'."); System.out.println("Loading expression DSL file '" + fileName + "'.");
Map<Object, Object> parameters = new HashMap<>();
if (args.length > 0) { if (args.length > 0) {
fileName = args[0]; fileName = args[0];
} }
......
...@@ -16,12 +16,9 @@ import org.apache.commons.validator.routines.UrlValidator; ...@@ -16,12 +16,9 @@ import org.apache.commons.validator.routines.UrlValidator;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -141,6 +138,10 @@ public class OpenAPIMain_test { ...@@ -141,6 +138,10 @@ public class OpenAPIMain_test {
return true; return true;
} }
static boolean isAlphaNumeric(String s) {
return s != null && s.matches("^[a-zA-Z0-9]*$");
}
static void initResources(File file) { static void initResources(File file) {
if (file.isDirectory()) { if (file.isDirectory()) {
for (File f : file.listFiles()) for (File f : file.listFiles())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment