diff --git a/src/main/jastadd/InferParameter.jrag b/src/main/jastadd/InferParameter.jrag index aa271cb10685d033ce04f2b3957d8df75f0adaa3..6da911fd277a62de1a064aeb6403ccc27aa5bcb6 100644 --- a/src/main/jastadd/InferParameter.jrag +++ b/src/main/jastadd/InferParameter.jrag @@ -1,4 +1,5 @@ -/*import com.fasterxml.jackson.databind.JsonNode; +/* +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -144,60 +145,5 @@ aspect InferParameter{ writeDictionary(schema.schemaObject().getItemsSchema().getSchemaOb(), props.next().toString()); } - - public void Get.connectGET(String path){ - - try { - URL url = new URL(path); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header - - con.setRequestMethod("GET"); // optional default is GET - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("Inferred path : " + path); - System.out.println("HTTP status code (GET) : " + responseCode); - } catch (Exception e) { - System.out.println(e.toString()); - } - } - - public void Post.connectPOST(String path){ - try { - URL url = new URL(path); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - - con.setRequestMethod("POST"); // HTTP POST - con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header - con.setDoOutput(true); // POST - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("Inferred path : " + path); - System.out.println("HTTP status code (POST) : " + responseCode); - } catch (Exception e) { - System.out.println(e.toString()); - } - } } - */ \ No newline at end of file diff --git a/src/main/jastadd/RandomRequestGenerator.jrag b/src/main/jastadd/RandomRequestGenerator.jrag index 9473e1b2ddebe0d99248072317b825be0899366a..a29e3f105fd5a4e81c65750e3fbd4c13fa26c700 100644 --- a/src/main/jastadd/RandomRequestGenerator.jrag +++ b/src/main/jastadd/RandomRequestGenerator.jrag @@ -1,49 +1,39 @@ -/* -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import javax.net.ssl.HttpsURLConnection; -import java.util.Random; -import java.util.stream.IntStream; - aspect RandomRequestGenerator{ syn String ParameterObject.randomPathParameter(String pathRef){ Random rand = new Random(); - SchemaObject s = getSchemaOb().schemaObject(); + SchemaObject s = getSchema().schemaObject(); String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); if (s.getType().equals("string")) - pathRef = pathRef.replace(pathPart, generateRandomString(rand, s.getEnumObjs())); + pathRef = pathRef.replace(pathPart, generateRandomString(rand, s.getEList())); else if (s.getType().equals("integer")) pathRef = pathRef.replace(pathPart, generateRandomInt(rand, - -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, - 10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1 + -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, + 10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1 )); return pathRef; } syn String ParameterObject.randomQueryParameter(String pathRef){ Random rand = new Random(); - SchemaObject s = getSchemaOb().schemaObject(); + SchemaObject s = getSchema().schemaObject(); if (s.getType().equals("string")) - pathRef = pathRef + "?" + getName() + "=" + generateRandomString(rand, s.getEnumObjs()); + pathRef = pathRef + "?" + getName() + "=" + generateRandomString(rand, s.getEList()); else if (s.getType().equals("integer")) pathRef = pathRef + "?" + getName() + "=" + generateRandomInt(rand, - -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, - 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 + -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, + 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 else if (s.getType().equals("array")) { - if (s.getItemsSchema().getSchemaOb().schemaObject().getType().equals("string")) { - for (EnumObj e : s.getItemsSchema().getSchemaOb().schemaObject().getEnumObjs()) + if (s.getI().getSchema().schemaObject().getType().equals("string")) { + for (EnumObj e : s.getI().getSchema().schemaObject().getEList()) pathRef = rand.nextDouble() < 0.5 ? pathRef + "&" + getName() + "=" + e.getEnumOb() : pathRef; - } else if (s.getItemsSchema().getSchemaOb().schemaObject().getType().equals("integer")) { + } else if (s.getI().getSchema().schemaObject().getType().equals("integer")) { for (int i = 0; i < 5; i++) pathRef = pathRef + "&" + getName() + "=" + generateRandomInt(rand, - -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, - 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 + -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, + 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 } pathRef = pathRef.replaceFirst("&", "?"); } @@ -52,7 +42,7 @@ aspect RandomRequestGenerator{ syn boolean OpenAPIObject.generateRequests(){ try { - for (PathsObject p : getPathsObjects()) + for (PathsObject p : getPList()) p.generateUrl(); } catch (Exception e) { return false; @@ -61,7 +51,8 @@ aspect RandomRequestGenerator{ } inh boolean PathsObject.generateUrl(); - eq OpenAPIObject.getPathsObject(int i).generateUrl(){ + eq OpenAPIObject.getP(int i).generateUrl(){ + /* try { PathItemObject p = getPathsObject(i).getPathItemObject(); String path = getServerObject(0).getUrl(); @@ -74,10 +65,12 @@ aspect RandomRequestGenerator{ return true; } catch (Exception e) { return false; - } + }*/ + return true; } syn boolean Get.generateRandomUrl(String pathRef){ + /* try { for (ParameterOb o : getOperationObject().getParameterObs()) { ParameterObject p = o.parameterObject(); @@ -88,25 +81,6 @@ aspect RandomRequestGenerator{ } System.out.println("Generated path : " + pathRef); - URL url = new URL(pathRef); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header - - con.setRequestMethod("GET"); // optional default is GET - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("HTTP status code (GET) : " + responseCode); - //System.out.println("Response : " + response.toString()); for (ResponseTuple t : getOperationObject().getResponseTuples()) { if (t.getKey().equals("200") && responseCode == 200) { //System.out.println("Response successfully saved!"); @@ -121,9 +95,11 @@ aspect RandomRequestGenerator{ } catch (Exception e) { System.out.println(e.toString()); return false; - } + }*/ + return true; } syn boolean Post.generateRandomUrl(String pathRef){ + /* try { for (ParameterOb o : getOperationObject().getParameterObs()) { ParameterObject p = o.parameterObject(); @@ -134,27 +110,6 @@ aspect RandomRequestGenerator{ } System.out.println("Generated path : " + pathRef); - URL url = new URL(pathRef); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - - - con.setRequestMethod("POST"); // HTTP POST - con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header - con.setDoOutput(true); // POST - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("HTTP status code (POST) : " + responseCode); - //System.out.println("Response : " + response.toString()); for (ResponseTuple t : getOperationObject().getResponseTuples()) { if (t.getKey().equals("200") && responseCode == 200) { //System.out.println("Response successfully saved!"); @@ -169,7 +124,8 @@ aspect RandomRequestGenerator{ } catch (Exception e) { System.out.println(e.toString()); return false; - } + }*/ + return true; } syn String ParameterObject.generateRandomString(Random rand,JastAddList<EnumObj> objs){ @@ -192,6 +148,4 @@ aspect RandomRequestGenerator{ return String.valueOf(rand.nextInt(maximum)); return String.valueOf(rand.nextInt()); } -} - - */ \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/jastadd/Reference.jrag b/src/main/jastadd/Reference.jrag index 98e783d02bb09789761a898ca3e40b02638c7e3f..d9f02e91264233b6a89c6f188821fa4a92aaacd5 100644 --- a/src/main/jastadd/Reference.jrag +++ b/src/main/jastadd/Reference.jrag @@ -1,4 +1,3 @@ -/* import java.util.*; aspect Reference { @@ -42,7 +41,7 @@ aspect Reference { eq ParameterReference.parameterObject() { for (ParameterTuple t : root().parameterTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getParameterOb().parameterObject(); + return t.getO().parameterObject(); } return new ParameterObject(); } @@ -52,7 +51,7 @@ aspect Reference { eq SchemaReference.schemaObject() { for (SchemaTuple t : root().schemaTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getSchemaOb().schemaObject(); + return t.getO().schemaObject(); } return new SchemaObject(); } @@ -62,7 +61,7 @@ aspect Reference { eq RequestBodyReference.requestBodyObject() { for (RequestBodyTuple t : root().requestBodyTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getRequestBodyOb().requestBodyObject(); + return t.getO().requestBodyObject(); } return new RequestBodyObject(); } @@ -72,7 +71,7 @@ aspect Reference { eq ResponseReference.responseObject() { for (ResponseTuple t : root().responseTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getResponseOb().responseObject(); + return t.getO().responseObject(); } return new ResponseObject(); } @@ -82,7 +81,7 @@ aspect Reference { eq CallbackReference.callbackObject() { for (CallbackTuple t : root().callbackTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getCallbackOb().callbackObject(); + return t.getO().callbackObject(); } return new CallbackObject(); } @@ -92,7 +91,7 @@ aspect Reference { eq LinkReference.linkObject() { for (LinkTuple t : root().linkTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getLinkOb().linkObject(); + return t.getO().linkObject(); } return new LinkObject(); } @@ -102,7 +101,7 @@ aspect Reference { eq HeaderReference.headerObject() { for (HeaderTuple t : root().headerTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getHeaderOb().headerObject(); + return t.getO().headerObject(); } return new HeaderObject(); } @@ -112,10 +111,9 @@ aspect Reference { eq SecuritySchemeReference.securitySchemeObject() { for (SecuritySchemeTuple t : root().securitySchemeTuples()) { if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) - return t.getSecuritySchemeOb().securitySchemeObject(); + return t.getO().securitySchemeObject(); } return new SecuritySchemeObject(); } -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/java/de/tudresden/inf/st/openapi/HttpURLConnectionExample.java b/src/main/java/de/tudresden/inf/st/openapi/HttpURLConnectionExample.java deleted file mode 100644 index b55ab23c71a5e63d02ceddb9a8edf586a75edbc8..0000000000000000000000000000000000000000 --- a/src/main/java/de/tudresden/inf/st/openapi/HttpURLConnectionExample.java +++ /dev/null @@ -1,109 +0,0 @@ -package de.tudresden.inf.st.openapi; - -import javax.net.ssl.HttpsURLConnection; -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - -public class HttpURLConnectionExample { - - private final String USER_AGENT = "Mozilla/5.0"; - - public static void main(String[] args) throws Exception { - - HttpURLConnectionExample http = new HttpURLConnectionExample(); - - System.out.println("Data call with GET"); - http.sendGet("https://petstore.swagger.io/v2/pet/9222968140497310446"); - - //System.out.println("Data call with POST"); - //String urlParameters = ""; - //http.sendPost("https://petstore.swagger.io/v2/pet", urlParameters); - - //System.out.println("Data call with DELETE"); - //http.sendDelete("https://petstore.swagger.io/v2/pet/123123"); - } - - // HTTP GET request - private void sendGet(String targetUrl) throws Exception { - - URL url = new URL(targetUrl); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - - con.setRequestMethod("GET"); // optional default is GET - con.setRequestProperty("User-Agent", USER_AGENT); // add request header - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("HTTP status code : " + responseCode); - System.out.println("HTTP body : " + response); - } - - // HTTP POST request - private void sendPost(String targetUrl, String parameters) throws Exception { - - URL url = new URL(targetUrl); - HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); - - con.setRequestMethod("POST"); // HTTP POST - con.setRequestProperty("User-Agent", USER_AGENT); - //con.addRequestProperty("body", ""); - //con.addRequestProperty("status", "available"); - con.setDoOutput(true); // POST - // Send post request - DataOutputStream wr = new DataOutputStream(con.getOutputStream()); - wr.writeBytes(parameters); - wr.flush(); - wr.close(); - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("HTTP status code (POST) : " + responseCode); - System.out.println("HTTP body : " + response); - - } - - // HTTP DELETE request - private void sendDelete(String targetUrl) throws Exception { - - URL url = new URL(targetUrl); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - - con.setRequestMethod("DELETE"); // optional default is GET - con.setRequestProperty("User-Agent", USER_AGENT); // add request header - - int responseCode = con.getResponseCode(); - BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer response = new StringBuffer(); - - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - in.close(); - - // print result - System.out.println("HTTP status code : " + responseCode); - //System.out.println("HTTP body : " + response.toString()); - } -} \ No newline at end of file diff --git a/src/main/java/de/tudresden/inf/st/openapi/OpenAPIMain.java b/src/main/java/de/tudresden/inf/st/openapi/OpenAPIMain.java index f08e2738450ce940d3c770edde02f169a9df7537..7e2a6a6b4d05c26eef23e68b6fd26dde7cab3b94 100644 --- a/src/main/java/de/tudresden/inf/st/openapi/OpenAPIMain.java +++ b/src/main/java/de/tudresden/inf/st/openapi/OpenAPIMain.java @@ -2,6 +2,7 @@ package de.tudresden.inf.st.openapi; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import de.tudresden.inf.st.openapi.ast.OpenAPIObject; import io.swagger.models.reader.SwaggerParser; import io.swagger.parser.OpenAPIParser; import io.swagger.report.MessageBuilder; @@ -19,8 +20,6 @@ import org.apache.http.impl.client.HttpClientBuilder; import javax.net.ssl.HttpsURLConnection; import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -31,43 +30,13 @@ public class OpenAPIMain { * main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd **/ public static void main(String[] args) throws Exception { - List<String> filenames = new ArrayList<>(); - String genDir = "./gen-api-ex/"; - File genDirectory = new File(genDir); - File[] contents; - File resource = new File("./src/main/resources"); - - // init parser String fileName = "./src/main/resources/3.0/petstore.yaml"; - /* - ParseOptions options = new ParseOptions(); - options.setResolve(true); - options.setResolveFully(true); - options.setAllowEmptyString(false); - */ + OpenAPIObject jastAddObject; SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null); - - String resultString; OpenAPI openAPI = result.getOpenAPI(); + List<String> generatedURLs = new ArrayList<>(); - OpenAPIDeserializer deserializer = new OpenAPIDeserializer(); - String res = Json.mapper().writeValueAsString(openAPI); - ObjectMapper mapper = new ObjectMapper(); - JsonNode node = mapper.readTree(res); - //System.out.println(node.toPrettyString()); - - - /** OpenAPI Validator! **/ - List<String> validation = new OpenAPIV3Parser().readContents(res).getMessages(); - for(String mes : validation) - System.out.println("message : " + mes); - - - //Json.mapper().createParser(Json.mapper().writeValueAsString(openAPI)). - - // Yaml String - //System.out.println(Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI)); - //resultString = Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI); + jastAddObject = OpenAPIObject.parseOpenAPI(openAPI); URL expUrl = OpenAPIMain.class.getClassLoader().getResource(fileName); File file = null; @@ -85,6 +54,62 @@ public class OpenAPIMain { if (args.length > 0) { fileName = args[0]; } + } + + public void sendRandomRequests() {} + + public void sendInferredRequests(List<String> randomRequests) {} + + public void connectGET(String path){ + try { + URL url = new URL(path); + HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); + con.setRequestProperty("User-Agent", "Mozilla/5.0"); // request header + + con.setRequestMethod("GET"); // optional default is GET + + int responseCode = con.getResponseCode(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // print result + System.out.println("connected path : " + path); + System.out.println("HTTP status code (GET) : " + responseCode); + } catch (Exception e) { + System.out.println(e.toString()); + } + } + public void connectPOST(String path){ + try { + URL url = new URL(path); + HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); + + con.setRequestMethod("POST"); // HTTP POST + con.setRequestProperty("User-Agent", "Mozilla/5.0"); // request header + con.setDoOutput(true); // POST + + int responseCode = con.getResponseCode(); + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + // print result + System.out.println("connected path : " + path); + System.out.println("HTTP status code (POST) : " + responseCode); + } catch (Exception e) { + System.out.println(e.toString()); + } } } \ No newline at end of file