Skip to content

Commit

Permalink
Added step
Browse files Browse the repository at this point in the history
  • Loading branch information
cjayswal committed Dec 18, 2022
1 parent 4745324 commit 25d3045
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
50 changes: 45 additions & 5 deletions src/com/qmetry/qaf/automation/openapi/v3/CodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ of this software and associated documentation files (the "Software"), to deal
*/
package com.qmetry.qaf.automation.openapi.v3;

import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -52,6 +54,8 @@ of this software and associated documentation files (the "Software"), to deal
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;

/**
* @author chirag.jayswal
Expand Down Expand Up @@ -87,7 +91,14 @@ public CodeGenerator(String specUrl, OpenAPI api) {
config = new LinkedHashMap<String, Map<String, Object>>();
}

public void generate() throws ConfigurationException, IOException {
public List<String> generate() throws ConfigurationException, IOException {
String dest = getBundle().getString("autogenerated.file.location","resources/auto_generated/");
if(!dest.endsWith("/")) {dest=dest+"/";}

if(dest.indexOf("auto_generated")<0) {
dest = dest+"auto_generated/";
}
ArrayList<String> genratedFiles = new ArrayList<String>();
prefix = StringUtil.toCamelCaseIdentifier(api.getInfo().getTitle());
tmplKey = prefix + ".tmpl";

Expand All @@ -113,10 +124,39 @@ public void generate() throws ConfigurationException, IOException {
globalParams.put("specUrl", specUrl);
tmpl.put("parameters", globalParams);

JSONUtil.writeJsonObjectToFile("resources/" + prefix + ".wscj", config);
//config.save("resources/" + prefix + ".wscj");
dataConfig.save("resources/" + prefix + "-data.xml");
FileUtil.write(new File("scenarios/"+prefix + "SanitySuite.feature"), bddSrc, Charset.defaultCharset());
String wscjFile = dest + prefix + ".wscj";
JSONUtil.writeJsonObjectToFile(wscjFile, config);
genratedFiles.add(wscjFile);

String resourceFile = dest + prefix + "-data.xml";
dataConfig.save(resourceFile);
genratedFiles.add(resourceFile);


String featureFile="scenarios/auto_generated/"+prefix + "SanitySuite.feature";
FileUtil.write(new File(featureFile), bddSrc, Charset.defaultCharset());
genratedFiles.add(featureFile);

return genratedFiles;
}

public static List<String> importWSC(String... specUrls){
ArrayList<String> genratedFiles = new ArrayList<String>();

for (String specUrl : specUrls) {
System.out.println("Processing: " + specUrl);
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI api = new OpenAPIV3Parser().read(specUrl, null, options);
CodeGenerator codeGenerator = new CodeGenerator(specUrl, api);
try {
genratedFiles.addAll(codeGenerator.generate());
} catch (ConfigurationException | IOException e) {
e.printStackTrace();
}
}
return genratedFiles;
}

private void recordResponses(String key, HttpMethod method, Operation operation, Map<String, Object> params) {
Expand Down
18 changes: 4 additions & 14 deletions src/com/qmetry/qaf/automation/openapi/v3/CodeGeneratorCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ of this software and associated documentation files (the "Software"), to deal
import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;

/**
* This class can be run through command line to generate request call
* repository and BDD source from OpenAPI specification. Set URL of the
Expand Down Expand Up @@ -62,15 +58,9 @@ public static void main(String... args) throws ConfigurationException, IOExcepti
}
}

System.out.println("Open API Specifications to process: " + Arrays.asList(getBundle().getStringArray("openapi.specUrl")));
for (String specUrl : getBundle().getStringArray("openapi.specUrl")) {
System.out.println("Processing: " + specUrl);
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
OpenAPI api = new OpenAPIV3Parser().read(specUrl, null, options);
CodeGenerator codeGenerator = new CodeGenerator(specUrl, api);
codeGenerator.generate();
List<String> res = CodeGenerator.importWSC(getBundle().getStringArray("openapi.specUrl"));
if(null!=res && !res.isEmpty()) {
System.out.println("Genereted files: " + res);
}
}
}
9 changes: 9 additions & 0 deletions src/com/qmetry/qaf/automation/step/openapi/OpenApiSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ of this software and associated documentation files (the "Software"), to deal

import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;

import java.util.List;

import com.atlassian.oai.validator.OpenApiInteractionValidator;
import com.atlassian.oai.validator.model.Request.Method;
import com.atlassian.oai.validator.model.Response;
import com.atlassian.oai.validator.model.SimpleResponse;
import com.atlassian.oai.validator.report.ValidationReport;
import com.atlassian.oai.validator.report.ValidationReport.Message;
import com.qmetry.qaf.automation.core.MessageTypes;
import com.qmetry.qaf.automation.openapi.v3.CodeGenerator;
import com.qmetry.qaf.automation.step.QAFTestStep;
import com.qmetry.qaf.automation.util.Reporter;
import com.qmetry.qaf.automation.ws.WsRequestBean;
Expand Down Expand Up @@ -67,6 +70,12 @@ public static boolean validateResponseSchema(Object requestCall) {
}
return !result.hasErrors();
}

@QAFTestStep(description = "import openapi spec {openapi-spec-file} into {dest}")
public static List<String> importOpenAPISpec(String file, String dest){
getBundle().setProperty("autogenerated.file.location",dest);
return CodeGenerator.importWSC(file);
}
private static OpenApiInteractionValidator getValidator(String specUrl){
String validatorKey = specUrl.replaceAll("\\W", "");
OpenApiInteractionValidator validator = (OpenApiInteractionValidator) getBundle().getObject(validatorKey);
Expand Down

0 comments on commit 25d3045

Please sign in to comment.