Select Git revision
RandomRequestGenerator.jadd
RandomRequestGenerator.jadd 7.37 KiB
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 {
public void OpenAPIObject.generateRequests() throws Exception {
String baseUrl = this.getServerObject(0).getUrl();
for( PathsObject p : this.getPathsObjects() )
p.sendRandomRequests(baseUrl);
}
public void PathsObject.sendRandomRequests(String baseUrl) throws Exception {
if( this.getPathItem().hasGet() ){
IntStream.range(0, 1).forEach( i -> {
try {
this.getPathItem().getGet().getOperationObject().sendRandomGET(baseUrl+this.getRef());
} catch (Exception e) {
e.printStackTrace();
}
});
}
if( this.getPathItem().hasPost() ){
IntStream.range(0, 1).forEach( i -> {
try {
this.getPathItem().getPost().getOperationObject().sendRandomPOST(baseUrl+this.getRef());
} catch (Exception e) {
e.printStackTrace();
}
});}
}
public void OperationObject.sendRandomGET(String targetUrl) throws Exception {
Random rand = new Random();
for( ParameterObject p : this.getParameterObjects() ){
if( p.getIn().equals("path") ){
String pathPart = targetUrl.substring(targetUrl.indexOf("{") ,targetUrl.indexOf("}") + 1);
if( p.getSchemaObject().getType().equals("string") )
targetUrl = targetUrl.replace(pathPart, this.generateRandomString(rand, p.getSchemaObject().getEnumObjs()));
else if( p.getSchemaObject().getType().equals("integer") )
targetUrl = targetUrl.replace(pathPart, this.generateRandomInt( rand,
p.getSchemaObject().getMinimum() != null ? p.getSchemaObject().getMinimum().intValue() : -1,
p.getSchemaObject().getMaximum() != null ? p.getSchemaObject().getMaximum().intValue() : -1
));
}
else if( p.getIn().equals("query") ){
if( p.getSchemaObject().getType().equals("string") )
targetUrl = targetUrl + "&" + p.getName() + "=" + this.generateRandomString(rand, p.getSchemaObject().getEnumObjs());
else if( p.getSchemaObject().getType().equals("integer") )
targetUrl = targetUrl + "&" + p.getName() + "=" + this.generateRandomInt( rand,
p.getSchemaObject().getMinimum() != null ? p.getSchemaObject().getMinimum().intValue() : -1,
p.getSchemaObject().getMaximum() != null ? p.getSchemaObject().getMaximum().intValue() : -1 );
else if( p.getSchemaObject().getType().equals("array") ){
if( p.getSchemaObject().getItemsSchema().getSchemaObject().getType().equals("string") ){
for( EnumObj e : p.getSchemaObject().getItemsSchema().getSchemaObject().getEnumObjs() )
targetUrl=rand.nextDouble()< 0.5?targetUrl+"&"+p.getName()+"="+e.getEnumOb():targetUrl;
}
else if( p.getSchemaObject().getItemsSchema().getSchemaObject().getType().equals("integer") ){
for( int i = 0 ; i < 5 ; i++ )
targetUrl = targetUrl + "&" + p.getName() + "=" + this.generateRandomInt( rand, p.getSchemaObject().getMinimum() != null ? p.getSchemaObject().getMinimum().intValue() : -1,
p.getSchemaObject().getMaximum() != null ? p.getSchemaObject().getMaximum().intValue() : -1 );
}
}
}
}
targetUrl = targetUrl.replaceFirst("&", "?");
System.out.println(targetUrl);
URL url = new URL(targetUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET"); // optional default is GET
int responseCode = con.getResponseCode();
// print result
System.out.println("HTTP status code (GET) : " + responseCode);
}
public void OperationObject.sendRandomPOST(String targetUrl) throws Exception {
Random rand = new Random();
for( ParameterObject p : this.getParameterObjects() ){
if( p.getIn().equals("path") ){
String pathPart = targetUrl.substring(targetUrl.indexOf("{") ,targetUrl.indexOf("}") + 1);
if( p.getSchemaObject().getType().equals("string") )
targetUrl = targetUrl.replace(pathPart, this.generateRandomString(rand, p.getSchemaObject().getEnumObjs()));
else if( p.getSchemaObject().getType().equals("integer") )
targetUrl = targetUrl.replace(pathPart, this.generateRandomInt( rand,
-1, // p.getSchemaObject().getMinimum() != null ? p.getSchemaObject().getMinimum().intValue() : -1,
10 // p.getSchemaObject().getMaximum() != null ? p.getSchemaObject().getMaximum().intValue() : -1
));
}
else if( p.getIn().equals("query") ){
if( p.getSchemaObject().getType().equals("string") )
targetUrl = targetUrl + "&" + p.getName() + "=" + this.generateRandomString(rand, p.getSchemaObject().getEnumObjs());
else if( p.getSchemaObject().getType().equals("integer") )
targetUrl = targetUrl + "&" + p.getName() + "=" + this.generateRandomInt( rand,
-1, // p.getSchemaObject().getMinimum() != null ? p.getSchemaObject().getMinimum().intValue() : -1,
10); // p.getSchemaObject().getMaximum() != null ? p.getSchemaObject().getMaximum().intValue() : -1
else if( p.getSchemaObject().getType().equals("array") ){
if( p.getSchemaObject().getItemsSchema().getSchemaObject().getType().equals("string") ){
for( EnumObj e : p.getSchemaObject().getItemsSchema().getSchemaObject().getEnumObjs() )
targetUrl=rand.nextDouble()< 0.5?targetUrl+"&"+p.getName()+"="+e.getEnumOb():targetUrl;
}
else if( p.getSchemaObject().getItemsSchema().getSchemaObject().getType().equals("integer") ){
for( int i = 0 ; i < 5 ; i++ )
targetUrl = targetUrl + "&" + p.getName() + "=" + this.generateRandomInt( rand,
-1, // p.getSchemaObject().getMinimum() != null ? p.getSchemaObject().getMinimum().intValue() : -1,
10); // p.getSchemaObject().getMaximum() != null ? p.getSchemaObject().getMaximum().intValue() : -1
}
}
}
}
targetUrl = targetUrl.replaceFirst("&", "?");
System.out.println(targetUrl);
URL url = new URL(targetUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST"); // HTTP POST
con.setDoOutput(true); // POST
int responseCode = con.getResponseCode();
// print result System.out.println("HTTP status code (POST) : " + responseCode);
}
public String OperationObject.generateRandomString(Random rand, JastAddList<EnumObj> objs) {
if( objs.getNumChild() != 0 )
return objs.getChild(rand.nextInt(objs.getNumChild())).getEnumOb().toString();
return rand
.ints(97, 123)
.limit(10)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
public String OperationObject.generateRandomInt(Random rand, int minimum, int maximum){
if( minimum > -1 && maximum > 0 )
return String.valueOf(rand.nextInt(minimum+maximum)-minimum);
else if( minimum > -1 )
return String.valueOf(rand.nextInt()+minimum);
else if( maximum > 0 )
return String.valueOf(rand.nextInt(maximum));
return String.valueOf(rand.nextInt());
}
}