diff --git a/src/main/jastadd/InferParameter.jrag b/src/main/jastadd/InferParameter.jrag index 04c0cd5f8366dee4392869ae0f68f59c1ffa1fb7..1074485a34c3291d57c74ef8e9b21423111e4572 100644 --- a/src/main/jastadd/InferParameter.jrag +++ b/src/main/jastadd/InferParameter.jrag @@ -4,7 +4,20 @@ import com.fasterxml.jackson.databind.node.ArrayNode; aspect InferParameter{ + /** + * Saves response data names generated by random requests. + * <p>This is an auxiliary method to distinguish saved dictionary values.</p> + * <p>(Future work: translate this to Map or Tuple).</p> + * @return first String part divided by <code>?</code>. + */ syn String ParameterObject.dictName(String dict) = dict.substring(0, dict.indexOf("?")); + + /** + * Saves response data values generated by random requests. + * <p>This is an auxiliary method to distinguish saved dictionary values.</p> + * <p>(Future work: translate this to Map or Tuple).</p> + * @return second String part divided by <code>?</code>. + */ syn String ParameterObject.dictValue(String dict) = dict.substring(dict.indexOf("?")+1); inh List<String> PathsObject.inferUrl(List<String> dict); diff --git a/src/main/jastadd/RandomRequestGenerator.jrag b/src/main/jastadd/RandomRequestGenerator.jrag index 03e7a013188f263a9f0ffbbc4efb00570d1c41ed..3c616751ec38c15258464503581f953afce224d1 100644 --- a/src/main/jastadd/RandomRequestGenerator.jrag +++ b/src/main/jastadd/RandomRequestGenerator.jrag @@ -1,44 +1,9 @@ aspect RandomRequestGenerator{ - syn String ParameterObject.randomPathParameter(String pathRef){ - SchemaObject s = getSchema().schemaObject(); - String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); - - if (s.getType().equals("string")) - pathRef = pathRef.replace(pathPart, generateRandomString(s.getEList())); - else if (s.getType().equals("integer")) - pathRef = pathRef.replace(pathPart, generateRandomInt( - -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, - 10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1 - )); - return pathRef; - } - - syn String ParameterObject.randomQueryParameter(String pathRef){ - SchemaObject s = getSchema().schemaObject(); - - if (s.getType().equals("string")) - pathRef = pathRef + "?" + getName() + "=" + generateRandomString(s.getEList()); - else if (s.getType().equals("integer")) - 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 = pathWithEnum(e, pathRef); - } else if (s.getI().getSchema().schemaObject().getType().equals("integer")) { - for (int i = 0; i < 5; i++) - pathRef = pathRef + "&" + getName() + "=" + generateRandomInt( - -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, - 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 - } - pathRef = pathRef.replaceFirst("&", "?"); - } - return pathRef; - } - - syn List<String> OpenAPIObject.generateRequests(){ + /** + * Calls <code>generateUrl()</code> for all paths. + * @return The list of String representing the generated URLs. + */ syn List<String> OpenAPIObject.generateRequests(){ List<String> urls = new ArrayList<>(); try { for (PathsObject p : getPList()) @@ -50,6 +15,11 @@ aspect RandomRequestGenerator{ return urls; } + /** + * Checks if a path has the request types <code>GET</code> and/or <code>POST</code>, and calls <code>generateRandomUrl(String pathRef)</code>. + * <p>Afterwards, generated URLs are saved in a list.</p> + * @return The list of String representing the generated URLs. + */ inh List<String> PathsObject.generateUrl(); eq OpenAPIObject.getP(int i).generateUrl(){ List<String> urls = new ArrayList<>(); @@ -59,7 +29,7 @@ aspect RandomRequestGenerator{ if (p.hasG()) urls.add(p.getG().generateRandomUrl(path + getP(i).getRef())); - else if (p.hasPostOb()) + if (p.hasPostOb()) urls.add(p.getPostOb().generateRandomUrl(path + getP(i).getRef())); return urls; @@ -69,6 +39,11 @@ aspect RandomRequestGenerator{ } } + /** + * Checks which parameter types the targeted GET request has (Path or Query) and calls corresponding random parameter generator. + * <p>Afterwards, generated parameters are written in the url.</p> + * @return An URL with the generated parameters in String. + */ syn String Get.generateRandomUrl(String pathRef){ try { for (ParameterOb o : getO().getPList()) { @@ -85,6 +60,12 @@ aspect RandomRequestGenerator{ return null; } } + + /** + * Checks which parameter types the targeted POST request has (Path or Query) and calls corresponding random parameter generator. + * <p>Afterwards, generated parameters are written in the url.</p> + * @return An URL with the generated parameters in String. + */ syn String Post.generateRandomUrl(String pathRef){ try { for (ParameterOb o : getO().getPList()) { @@ -102,6 +83,44 @@ aspect RandomRequestGenerator{ } } + syn String ParameterObject.randomPathParameter(String pathRef){ + SchemaObject s = getSchema().schemaObject(); + String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); + + if (s.getType().equals("string")) + pathRef = pathRef.replace(pathPart, generateRandomString(s.getEList())); + else if (s.getType().equals("integer")) + pathRef = pathRef.replace(pathPart, generateRandomInt( + -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, + 10 // s.getMaximum() != null ? s.getMaximum().intValue() : -1 + )); + return pathRef; + } + + syn String ParameterObject.randomQueryParameter(String pathRef){ + SchemaObject s = getSchema().schemaObject(); + + if (s.getType().equals("string")) + pathRef = pathRef + "?" + getName() + "=" + generateRandomString(s.getEList()); + else if (s.getType().equals("integer")) + 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 = pathWithEnum(e, pathRef); + } else if (s.getI().getSchema().schemaObject().getType().equals("integer")) { + for (int i = 0; i < 5; i++) + pathRef = pathRef + "&" + getName() + "=" + generateRandomInt( + -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, + 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 + } + pathRef = pathRef.replaceFirst("&", "?"); + } + return pathRef; + } + public String ParameterObject.generateRandomString(JastAddList<EnumObj> objs){ Random rand = new Random(); if (objs.getNumChild() != 0)