add api test and model test files

This commit is contained in:
wing328 2016-01-19 00:51:21 +08:00 committed by Maelig Nantel
parent ccd0db4434
commit fb1cc254e4
4 changed files with 128 additions and 0 deletions

View File

@ -19,10 +19,14 @@ public interface CodegenConfig {
Map<String, Object> additionalProperties();
String testPackage();
String apiPackage();
String apiFileFolder();
String apiTestFileFolder();
String fileSuffix();
String outputFolder();
@ -33,6 +37,8 @@ public interface CodegenConfig {
String modelFileFolder();
String modelTestFileFolder();
String modelPackage();
String toApiName(String name);
@ -87,6 +93,10 @@ public interface CodegenConfig {
Map<String, String> modelTemplateFiles();
Map<String, String> apiTestTemplateFiles();
Map<String, String> modelTestTemplateFiles();
Set<String> languageSpecificPrimitives();
void preprocessSwagger(Swagger swagger);
@ -97,6 +107,10 @@ public interface CodegenConfig {
String toModelFilename(String name);
String toApiTestFilename(String name);
String toModelTestFilename(String name);
String toModelImport(String name);
String toApiImport(String name);
@ -115,6 +129,8 @@ public interface CodegenConfig {
String apiFilename(String templateName, String tag);
String apiTestFilename(String templateName, String tag);
boolean shouldOverwrite(String filename);
boolean isSkipOverwrite();

View File

@ -63,8 +63,11 @@ public class DefaultCodegen {
protected Set<String> languageSpecificPrimitives = new HashSet<String>();
protected Map<String, String> importMapping = new HashMap<String, String>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
protected String testPackage = "";
protected Map<String, String> apiTemplateFiles = new HashMap<String, String>();
protected Map<String, String> modelTemplateFiles = new HashMap<String, String>();
protected Map<String, String> apiTestTemplateFiles = new HashMap<String, String>();
protected Map<String, String> modelTestTemplateFiles = new HashMap<String, String>();
protected String templateDir;
protected String embeddedTemplateDir;
protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
@ -180,6 +183,10 @@ public class DefaultCodegen {
return importMapping;
}
public String testPackage() {
return testPackage;
}
public String modelPackage() {
return modelPackage;
}
@ -204,6 +211,14 @@ public class DefaultCodegen {
}
}
public Map<String, String> apiTestTemplateFiles() {
return apiTestTemplateFiles;
}
public Map<String, String> modelTestTemplateFiles() {
return modelTestTemplateFiles;
}
public Map<String, String> apiTemplateFiles() {
return apiTemplateFiles;
}
@ -220,6 +235,14 @@ public class DefaultCodegen {
return outputFolder + "/" + modelPackage().replace('.', '/');
}
public String apiTestFileFolder() {
return outputFolder + "/" + testPackage().replace('.', '/');
}
public String modelTestFileFolder() {
return outputFolder + "/" + testPackage().replace('.', '/');
}
public Map<String, Object> additionalProperties() {
return additionalProperties;
}
@ -270,6 +293,16 @@ public class DefaultCodegen {
return toApiName(name);
}
/**
* Return the file name of the Api
*
* @param name the file name of the Api
* @return the file name of the Api
*/
public String toApiTestFilename(String name) {
return toApiName(name) + "Test";
}
/**
* Return the variable name in the Api
*
@ -290,6 +323,16 @@ public class DefaultCodegen {
return initialCaps(name);
}
/**
* Return the capitalized file name of the model
*
* @param name the model name
* @return the file name of the model
*/
public String toModelTestFilename(String name) {
return initialCaps(name) + "Test";
}
/**
* Return the operation ID (method name)
*
@ -2050,6 +2093,11 @@ public class DefaultCodegen {
return apiFileFolder() + '/' + toApiFilename(tag) + suffix;
}
public String apiTestFilename(String templateName, String tag) {
String suffix = apiTestTemplateFiles().get(templateName);
return apiTestFileFolder() + '/' + toApiTestFilename(tag) + suffix;
}
public boolean shouldOverwrite(String filename) {
return !(skipOverwrite && new File(filename).exists());
}

View File

@ -211,6 +211,28 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(models));
files.add(new File(filename));
}
// to generate model test files
for (String templateName : config.modelTestTemplateFiles().keySet()) {
String suffix = config.modelTestTemplateFiles().get(templateName);
String filename = config.modelTestFileFolder() + File.separator + config.toModelTestFilename(name) + suffix;
if (!config.shouldOverwrite(filename)) {
continue;
}
String templateFile = getFullTemplateFile(config, templateName);
String template = readTemplate(templateFile);
Template tmpl = Mustache.compiler()
.withLoader(new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) {
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
}
})
.defaultValue("")
.compile(template);
writeToFile(filename, tmpl.execute(models));
files.add(new File(filename));
}
} catch (Exception e) {
throw new RuntimeException("Could not generate model '" + name + "'", e);
}
@ -288,6 +310,29 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename));
}
for (String templateName : config.apiTestTemplateFiles().keySet()) {
String filename = config.apiTestFilename(templateName, tag);
if (!config.shouldOverwrite(filename) && new File(filename).exists()) {
continue;
}
String templateFile = getFullTemplateFile(config, templateName);
String template = readTemplate(templateFile);
Template tmpl = Mustache.compiler()
.withLoader(new Mustache.TemplateLoader() {
@Override
public Reader getTemplate(String name) {
return getTemplateReader(getFullTemplateFile(config, name + ".mustache"));
}
})
.defaultValue("")
.compile(template);
writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename));
}
} catch (Exception e) {
throw new RuntimeException("Could not generate api file for '" + tag + "'", e);
}

View File

@ -42,9 +42,12 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
outputFolder = "generated-code" + File.separator + "php";
modelTemplateFiles.put("model.mustache", ".php");
apiTemplateFiles.put("api.mustache", ".php");
modelTestTemplateFiles.put("model_test.mustache", ".php");
apiTestTemplateFiles.put("api_test.mustache", ".php");
embeddedTemplateDir = templateDir = "php";
apiPackage = invokerPackage + "\\Api";
modelPackage = invokerPackage + "\\Model";
testPackage = invokerPackage + "\\Tests";
reservedWords = new HashSet<String>(
Arrays.asList(
@ -237,6 +240,16 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return (outputFolder + "/" + toPackagePath(modelPackage, srcBasePath));
}
@Override
public String apiTestFileFolder() {
return (outputFolder + "/" + toPackagePath(testPackage, srcBasePath));
}
@Override
public String modelTestFileFolder() {
return (outputFolder + "/" + toPackagePath(testPackage, srcBasePath));
}
@Override
public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
@ -365,6 +378,12 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
return toModelName(name);
}
@Override
public String toModelTestFilename(String name) {
// should be the same as the model name
return toModelName(name) + "Test";
}
@Override
public String toOperationId(String operationId) {
// throw exception if method name is empty