diff --git a/src/main/jastadd/InferParameter.jrag b/src/main/jastadd/InferParameter.jrag index 6da911fd277a62de1a064aeb6403ccc27aa5bcb6..04c0cd5f8366dee4392869ae0f68f59c1ffa1fb7 100644 --- a/src/main/jastadd/InferParameter.jrag +++ b/src/main/jastadd/InferParameter.jrag @@ -1,149 +1,140 @@ -/* import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; aspect InferParameter{ - syn String PropertyItem.writePropertyName(SchemaOb schema,PropertyItem p)=((SchemaReference) schema).getRef().substring(((SchemaReference) schema).getRef().lastIndexOf("/") + 1) + p.getName(); - syn String PropertyItem.writePropertyValue(JsonNode node,PropertyItem p)=node.get(p.getName()).toString().startsWith("\"") && node.get(p.getName()).toString().endsWith("\"") ? node.get(p.getName()).toString().substring(1, node.get(p.getName()).toString().length() - 1) : node.get(p.getName()).toString(); + syn String ParameterObject.dictName(String dict) = dict.substring(0, dict.indexOf("?")); + syn String ParameterObject.dictValue(String dict) = dict.substring(dict.indexOf("?")+1); - coll Set<InferredParameter> OpenAPIObject.collectInferredParameters()[new HashSet<InferredParameter>()]; - InferredParameter contributes this - to OpenAPIObject.collectInferredParameters(); - - syn String InferredParameter.name()=getParameter().substring(0, getParameter().indexOf("?")); - syn String InferredParameter.value()=getParameter().substring(getParameter().indexOf("?") + 1); - - public void OpenAPIObject.generateRequestsWithInferredParameters()throws Exception{ - Set<String> urls = new HashSet<>(); - - generateRequests(); - - for (PathsObject p : getPathsObjects()) - p.inferUrl(); - } - - inh boolean PathsObject.inferUrl(); - eq OpenAPIObject.getPathsObject(int i).inferUrl(){ - PathItemObject p = getPathsObject(i).getPathItemObject(); - String path = getServerObject(0).getUrl(); + inh List<String> PathsObject.inferUrl(List<String> dict); + eq OpenAPIObject.getP(int i).inferUrl(List<String> dict){ + List<String> paths = new ArrayList<>(); + PathItemObject p = getP(i).getP().pathItemObject(); + String path = getServ(0).getUrl(); - if (p.hasGet()) - p.getGet().inferRandomUrl(path + getPathsObject(i).getRef(), p.getGet().getOperationObject()); - else if (p.hasPost()) - p.getPost().inferRandomUrl(path + getPathsObject(i).getRef(), p.getPost().getOperationObject()); + if (p.hasG()) + paths.addAll(p.getG().inferRandomUrl(path + getP(i).getRef(), p.getG().getO(), dict)); + else if (p.hasPostOb()) + paths.addAll(p.getPostOb().inferRandomUrl(path + getP(i).getRef(), p.getPostOb().getO(), dict)); - return true; + return paths; } - syn String Get.inferRandomUrl(String pathRef,OperationObject operationObject){ + syn List<String> Get.inferRandomUrl(String pathRef,OperationObject operationObject, List<String> dict){ List<String> paths = new ArrayList<>(); - for (ParameterOb o : operationObject.getParameterObs()) { + for (ParameterOb o : operationObject.getPList()) { ParameterObject p = o.parameterObject(); - SchemaObject s = p.getSchemaOb().schemaObject(); + SchemaObject s = p.getSchema().schemaObject(); // check if the parameter is in type 'path'. if (p.getIn().equals("path")) - paths = p.addinfPathParameters(pathRef, paths); + paths = p.addinfPathParameters(pathRef, dict); // check if the parameter is in type 'query' else if (p.getIn().equals("query")) - paths = p.addinfQueryParameters(pathRef, paths); - - System.out.println(paths.size() + " Paths are inferred."); + paths = p.addinfQueryParameters(pathRef, dict); } - for (String path : paths) - connectGET(path); - return pathRef; + + System.out.println(paths.size() + " paths for " + pathRef + " GET request are inferred"); + return paths; } - syn String Post.inferRandomUrl(String pathRef,OperationObject operationObject){ + syn List<String> Post.inferRandomUrl(String pathRef,OperationObject operationObject, List<String> dict){ List<String> paths = new ArrayList<>(); - for (ParameterOb o : operationObject.getParameterObs()) { + for (ParameterOb o : operationObject.getPList()) { ParameterObject p = o.parameterObject(); - SchemaObject s = p.getSchemaOb().schemaObject(); + SchemaObject s = p.getSchema().schemaObject(); // check if the parameter is in type 'path'. if (p.getIn().equals("path")) - paths = p.addinfPathParameters(pathRef, paths); + paths = p.addinfPathParameters(pathRef, dict); // check if the parameter is in type 'query' else if (p.getIn().equals("query")) - paths = p.addinfQueryParameters(pathRef, paths); - - System.out.println(paths.size() + " Paths are inferred."); + paths = p.addinfQueryParameters(pathRef, dict); } - for (String path : paths) - connectPOST(path); - return pathRef; + + System.out.println(paths.size() + " paths for " + pathRef + " POST request are inferred"); + return paths; } - syn List<String> ParameterObject.addinfPathParameters(String pathRef,List<String> paths){ - for (InferredParameter i : root().collectInferredParameters()) { + syn List<String> ParameterObject.addinfPathParameters(String pathRef,List<String> dict){ + List<String> paths = new ArrayList<>(); + for (String d : dict){ // get the field which must be modified - String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); + String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}")+1); // case insensitive comparison of parameter name and name of inferred parameters - if (getName().equalsIgnoreCase(i.name())) + if (getName().equalsIgnoreCase(d.substring(0, d.indexOf("?")))) // add inferred parameter in url - paths.add(pathRef.replace(pathPart, i.value())); + paths = root().addInList(paths, pathRef.replace(pathPart, d.substring(d.indexOf("?")+1))); } return paths; } - syn List<String> ParameterObject.addinfQueryParameters(String pathRef,List<String> paths){ - SchemaObject s = getSchemaOb().schemaObject(); + syn List<String> ParameterObject.addinfQueryParameters(String pathRef,List<String> dict){ + List<String> paths = new ArrayList<>(); + SchemaObject s = getSchema().schemaObject(); // check if query parameter is in type 'array' if (s.getType().equals("array")) { - for (InferredParameter i : root().collectInferredParameters()) { + for (String d : dict){ // case insensitive comparison of parameter name and name of inferred parameters - if (getName().equalsIgnoreCase(i.name())) + if (getName().equalsIgnoreCase(dictName(d)) && !pathRef.contains(getName() + "=" + dictValue(d))) // add inferred parameter in url - pathRef = pathRef + "&" + getName() + "=" + i.value(); + pathRef = pathRef + "&" + getName() + "=" + dictValue(d); } - paths.add(pathRef.replaceFirst("&", "?")); + paths = root().addInList(paths, pathRef.replaceFirst("&", "?")); } else { - for (InferredParameter i : root().collectInferredParameters()) { + for (String d : dict){ // case insensitive comparison of parameter name and name of inferred parameters - if (getName().equalsIgnoreCase(i.name())) + if (getName().equalsIgnoreCase(dictName(d))) // add inferred parameter in url - paths.add(pathRef + "?" + getName() + "=" + i.value()); + paths = root().addInList(paths, pathRef + "?" + getName() + "=" + dictValue(d)); } } return paths; } - public void OperationObject.writeDictionary(SchemaOb schema,String resp)throws Exception{ + public List<String> OperationObject.writeDictionary(SchemaOb schema,String resp, List<String> dict)throws Exception{ ObjectMapper mapper = new ObjectMapper(); JsonNode respNode = mapper.readTree(resp); String value; if (schema instanceof SchemaReference) { - for (PropertyItem p : schema.schemaObject().getPropertyItems()) { - String infName = p.writePropertyName(schema, p); - value = p.writePropertyValue(respNode, p); - - root().addInferredParameter(new InferredParameter(infName + "?" + value)); - root().addInferredParameter(new InferredParameter(p.getName() + "?" + value)); + for (PropertyItem p : schema.schemaObject().getPList()) { + String infName = ((SchemaReference) schema).getRef().substring(((SchemaReference) schema).getRef().lastIndexOf("/") + 1) + p.getName(); + value = respNode.get(p.getName()).toString().startsWith("\"") && + respNode.get(p.getName()).toString().endsWith("\"") ? + respNode.get(p.getName()).toString().substring(1, respNode.get(p.getName()).toString().length() - 1) : respNode.get(p.getName()).toString(); + + dict = root().addInList(dict, infName + "?" + value); + dict = root().addInList(dict, p.getName() + "?" + value); } } else { - for (PropertyItem p : schema.schemaObject().getPropertyItems()) { - value = p.writePropertyValue(respNode, p); + for (PropertyItem p : schema.schemaObject().getPList()) { + value = respNode.get(p.getName()).toString().startsWith("\"") && + respNode.get(p.getName()).toString().endsWith("\"") ? + respNode.get(p.getName()).toString().substring(1, respNode.get(p.getName()).toString().length() - 1) : respNode.get(p.getName()).toString(); - root().addInferredParameter(new InferredParameter(p.getName() + "?" + respNode.get(p.getName()).textValue())); + dict = root().addInList(dict, p.getName() + "?" + respNode.get(p.getName()).textValue()); } } - + return dict; } - public void OperationObject.writeDictionaryWithArray(SchemaOb schema,String resp)throws Exception{ + public List<String> OperationObject.writeDictionaryWithArray(SchemaOb schema,String resp, List<String> dict)throws Exception{ ObjectMapper mapper = new ObjectMapper(); ArrayNode respNode = ((ArrayNode) mapper.readTree(resp)); Iterator<JsonNode> props = respNode.elements(); while (props.hasNext()) - writeDictionary(schema.schemaObject().getItemsSchema().getSchemaOb(), props.next().toString()); + dict = writeDictionary(schema.schemaObject().getI().getSchema(), props.next().toString(), dict); + return dict; + } + public List<String> OpenAPIObject.addInList(List<String> list, String value){ + if( !list.contains(value) ) + list.add(value); + return list; } -} - */ \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/jastadd/RandomRequestGenerator.jrag b/src/main/jastadd/RandomRequestGenerator.jrag index f6a996b0eecfd406e0c9296e556329730c0ab2c2..03e7a013188f263a9f0ffbbc4efb00570d1c41ed 100644 --- a/src/main/jastadd/RandomRequestGenerator.jrag +++ b/src/main/jastadd/RandomRequestGenerator.jrag @@ -1,14 +1,13 @@ aspect RandomRequestGenerator{ syn String ParameterObject.randomPathParameter(String pathRef){ - Random rand = new Random(); 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.getEList())); + pathRef = pathRef.replace(pathPart, generateRandomString(s.getEList())); else if (s.getType().equals("integer")) - pathRef = pathRef.replace(pathPart, generateRandomInt(rand, + pathRef = pathRef.replace(pathPart, generateRandomInt( -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, 10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1 )); @@ -16,22 +15,21 @@ aspect RandomRequestGenerator{ } syn String ParameterObject.randomQueryParameter(String pathRef){ - Random rand = new Random(); SchemaObject s = getSchema().schemaObject(); if (s.getType().equals("string")) - pathRef = pathRef + "?" + getName() + "=" + generateRandomString(rand, s.getEList()); + pathRef = pathRef + "?" + getName() + "=" + generateRandomString(s.getEList()); else if (s.getType().equals("integer")) - pathRef = pathRef + "?" + getName() + "=" + generateRandomInt(rand, + pathRef = pathRef + "?" + getName() + "=" + generateRandomInt( -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 else if (s.getType().equals("array")) { 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; + pathRef = pathWithEnum(e, pathRef); } else if (s.getI().getSchema().schemaObject().getType().equals("integer")) { for (int i = 0; i < 5; i++) - pathRef = pathRef + "&" + getName() + "=" + generateRandomInt(rand, + pathRef = pathRef + "&" + getName() + "=" + generateRandomInt( -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 } @@ -104,7 +102,8 @@ aspect RandomRequestGenerator{ } } - syn String ParameterObject.generateRandomString(Random rand,JastAddList<EnumObj> objs){ + public String ParameterObject.generateRandomString(JastAddList<EnumObj> objs){ + Random rand = new Random(); if (objs.getNumChild() != 0) return objs.getChild(rand.nextInt(objs.getNumChild())).getEnumOb().toString(); @@ -115,7 +114,8 @@ aspect RandomRequestGenerator{ .toString(); } - syn String ParameterObject.generateRandomInt(Random rand,int minimum,int maximum){ + public String ParameterObject.generateRandomInt(int minimum,int maximum){ + Random rand = new Random(); if (minimum > -1 && maximum > 0) return String.valueOf(rand.nextInt(minimum + maximum) - minimum); else if (minimum > -1) @@ -124,4 +124,18 @@ aspect RandomRequestGenerator{ return String.valueOf(rand.nextInt(maximum)); return String.valueOf(rand.nextInt()); } + + public String ParameterObject.pathWithEnum(EnumObj e, String pathRef){ + Random rand = new Random(); + return rand.nextDouble() < 0.5 ? + pathRef + "&" + this.getName() + "=" + e.getEnumOb() : pathRef; + } + + syn String OpenAPIObject.getURLFromGeneratedURLs(List<String> urls, String ref){ + for( String url : urls ){ + if( url.contains(ref) ) + return url; + } + return null; + } } \ 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 48aa7c374a1f2cf29ec14e5ba74f0e4d44d344fc..c5960626435bf0a8ea61f788d1a28d7904798ae6 100644 --- a/src/main/java/de/tudresden/inf/st/openapi/OpenAPIMain.java +++ b/src/main/java/de/tudresden/inf/st/openapi/OpenAPIMain.java @@ -2,7 +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 de.tudresden.inf.st.openapi.ast.*; import io.swagger.models.reader.SwaggerParser; import io.swagger.parser.OpenAPIParser; import io.swagger.report.MessageBuilder; @@ -22,9 +22,11 @@ import javax.net.ssl.HttpsURLConnection; import java.io.*; import java.net.URL; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class OpenAPIMain { + static List<String> successfulUrls = new ArrayList<>(); /** * main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd @@ -34,12 +36,20 @@ public class OpenAPIMain { OpenAPIObject jastAddObject; SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null); OpenAPI openAPI = result.getOpenAPI(); - List<String> generatedURLs = new ArrayList<>(); - List<String> dictionary = new ArrayList<>(); + List<String> generatedURLs, dictionary; + List<String> inferredURLs = new ArrayList<>(); jastAddObject = OpenAPIObject.parseOpenAPI(openAPI); generatedURLs = jastAddObject.generateRequests(); + dictionary = sendRandomRequests(jastAddObject, generatedURLs); + + for ( PathsObject p : jastAddObject.getPList() ) + inferredURLs.addAll(p.inferUrl(dictionary)); + sendInferredRequests(jastAddObject, inferredURLs); + System.out.println("Requests generated by Parameter Inference are :"); + for ( String url : successfulUrls ) + System.out.println("--> " + url); URL expUrl = OpenAPIMain.class.getClassLoader().getResource(fileName); File file = null; @@ -59,16 +69,34 @@ public class OpenAPIMain { } } - public void sendRandomRequests() {} + public static List<String> sendRandomRequests(OpenAPIObject api, List<String> urls) { + List<String> dict = new ArrayList<>(); + if( api.getNumP() != 0 ){ + for( PathsObject p : api.getPList() ){ + PathItemObject item = p.getP().pathItemObject(); + String url = api.getURLFromGeneratedURLs(urls, p.getRef()); + if( item.hasG() ) + dict = connectGET(item.getG().getO(), url, dict); + if( item.hasPostOb() ) + dict = connectPOST(item.getPostOb().getO(), url, dict); + } + } + return dict; + } - public void sendInferredRequests(List<String> randomRequests) {} + public static void sendInferredRequests(OpenAPIObject api, List<String> inferredURLs) { + if( api.getNumP() != 0 ){ + for( PathsObject p : api.getPList() ){ + connectInferredURLs(p, inferredURLs); + } + } + } - public void connectGET(String path){ + public static List<String> connectGET(OperationObject op, String path,List<String> dict){ 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(); @@ -82,14 +110,25 @@ public class OpenAPIMain { in.close(); // print result - System.out.println("connected path : " + path); - System.out.println("HTTP status code (GET) : " + responseCode); + //System.out.println("connected path : " + path); + //System.out.println("HTTP status code (GET) : " + con.getResponseCode()); + successfulUrls.add(path); + for (ResponseTuple t : op.getR().getRList()) { + if (t.getKey().equals("200") && responseCode == 200) { + SchemaOb respSchema = t.getO().responseObject().getC(0).getM().getS(); + if (respSchema.schemaObject().getType().equals("array")) + dict.addAll(op.writeDictionaryWithArray(respSchema, response.toString(), dict)); + else + dict.addAll(op.writeDictionary(respSchema, response.toString(), dict)); + } + } } catch (Exception e) { - System.out.println(e.toString()); + //System.out.println(e.toString()); } + return dict; } - public void connectPOST(String path){ + public static List<String> connectPOST(OperationObject op, String path, List<String> dict) { try { URL url = new URL(path); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); @@ -109,10 +148,34 @@ public class OpenAPIMain { in.close(); // print result - System.out.println("connected path : " + path); - System.out.println("HTTP status code (POST) : " + responseCode); + //System.out.println("connected path : " + path); + //System.out.println("HTTP status code (POST) : " + responseCode); + successfulUrls.add(path); + for (ResponseTuple t : op.getR().getRList()) { + if (t.getKey().equals("200") && responseCode == 200) { + SchemaOb respSchema = t.getO().responseObject().getC(0).getM().getS(); + if (respSchema.schemaObject().getType().equals("array")) + dict.addAll(op.writeDictionaryWithArray(respSchema, response.toString(), dict)); + else + dict.addAll(op.writeDictionary(respSchema, response.toString(), dict)); + } + } } catch (Exception e) { - System.out.println(e.toString()); + //System.out.println(e.toString()); + } + return dict; + } + + public static void connectInferredURLs(PathsObject p, List<String> urls){ + PathItemObject item = p.getP().pathItemObject(); + System.out.println("Connect with inferred URLs is running..."); + for ( String url : urls ){ + if( url.contains(p.getRef()) ){ + if( item.hasG() ) + connectGET(item.getG().getO(), url, new ArrayList<>()); + if( item.hasPostOb() ) + connectPOST(item.getPostOb().getO(), url, new ArrayList<>()); + } } } } \ No newline at end of file