add back ruby tests

This commit is contained in:
wing328
2018-03-26 23:17:33 +08:00
parent 7fbc8fa31e
commit 9cec2b3673
4 changed files with 195 additions and 9 deletions

View File

@@ -0,0 +1,70 @@
package org.openapitools.codegen.ruby;
import org.openapitools.codegen.ClientOpts;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.languages.RubyClientCodegen;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.io.FileUtils;
import org.junit.rules.TemporaryFolder;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.junit.Assert.fail;
import static org.testng.Assert.*;
/**
* Tests for RubyClientCodegen-generated templates
*/
public class RubyClientCodegenTest {
public TemporaryFolder folder = new TemporaryFolder();
@BeforeMethod
public void setUp() throws Exception {
folder.create();
}
@AfterMethod
public void tearDown() throws Exception {
folder.delete();
}
@Test
public void testGenerateRubyClientWithHtmlEntity() throws Exception {
final File output = folder.getRoot();
final OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/2_0/pathWithHtmlEntity.yaml");
CodegenConfig codegenConfig = new RubyClientCodegen();
codegenConfig.setOutputDir(output.getAbsolutePath());
ClientOptInput clientOptInput = new ClientOptInput().opts(new ClientOpts()).openAPI(openAPI).config(codegenConfig);
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();
boolean apiFileGenerated = false;
for (File file : files) {
if (file.getName().equals("default_api.rb")) {
apiFileGenerated = true;
// Ruby client should set the path unescaped in the api file
assertTrue(FileUtils.readFileToString(file, StandardCharsets.UTF_8).contains("local_var_path = '/foo=bar'"));
}
}
if (!apiFileGenerated) {
fail("Default api file is not generated!");
}
}
}

View File

@@ -0,0 +1,52 @@
package org.openapitools.codegen.ruby;
import org.openapitools.codegen.AbstractOptionsTest;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.languages.RubyClientCodegen;
import org.openapitools.codegen.options.RubyClientOptionsProvider;
import mockit.Expectations;
import mockit.Tested;
public class RubyClientOptionsTest extends AbstractOptionsTest {
@Tested
private RubyClientCodegen clientCodegen;
public RubyClientOptionsTest() {
super(new RubyClientOptionsProvider());
}
@Override
protected CodegenConfig getCodegenConfig() {
return clientCodegen;
}
@SuppressWarnings("unused")
@Override
protected void setExpectations() {
new Expectations(clientCodegen) {{
clientCodegen.setGemName(RubyClientOptionsProvider.GEM_NAME_VALUE);
times = 1;
clientCodegen.setModuleName(RubyClientOptionsProvider.MODULE_NAME_VALUE);
times = 1;
clientCodegen.setGemVersion(RubyClientOptionsProvider.GEM_VERSION_VALUE);
times = 1;
clientCodegen.setGemLicense(RubyClientOptionsProvider.GEM_LICENSE_VALUE);
times = 1;
clientCodegen.setGemRequiredRubyVersion(RubyClientOptionsProvider.GEM_REQUIRED_RUBY_VERSION_VALUE);
times = 1;
clientCodegen.setGemHomepage(RubyClientOptionsProvider.GEM_HOMEPAGE_VALUE);
times = 1;
clientCodegen.setGemDescription(RubyClientOptionsProvider.GEM_DESCRIPTION_VALUE);
times = 1;
clientCodegen.setGemSummary(RubyClientOptionsProvider.GEM_SUMMARY_VALUE);
times = 1;
clientCodegen.setGemAuthor(RubyClientOptionsProvider.GEM_AUTHOR_VALUE);
times = 1;
clientCodegen.setGemAuthorEmail(RubyClientOptionsProvider.GEM_AUTHOR_EMAIL_VALUE);
times = 1;
}};
}
}

View File

@@ -0,0 +1,56 @@
package org.openapitools.codegen.options;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.languages.RubyClientCodegen;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
public class RubyClientOptionsProvider implements OptionsProvider {
public static final String GEM_NAME_VALUE = "swagger_client_ruby";
public static final String MODULE_NAME_VALUE = "SwaggerClientRuby";
public static final String GEM_VERSION_VALUE = "1.0.0-SNAPSHOT";
public static final String SORT_PARAMS_VALUE = "false";
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
public static final String GEM_LICENSE_VALUE = "MIT";
public static final String GEM_REQUIRED_RUBY_VERSION_VALUE = ">= 1.9";
public static final String GEM_HOMEPAGE_VALUE = "homepage";
public static final String GEM_SUMMARY_VALUE = "summary";
public static final String GEM_DESCRIPTION_VALUE = "description";
public static final String GEM_AUTHOR_VALUE = "foo";
public static final String GEM_AUTHOR_EMAIL_VALUE = "foo";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String PREPEND_FORM_OR_BODY_PARAMETERS_VALUE = "true";
@Override
public String getLanguage() {
return "ruby";
}
@Override
public Map<String, String> createOptions() {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
return builder.put(RubyClientCodegen.GEM_NAME, GEM_NAME_VALUE)
.put(RubyClientCodegen.MODULE_NAME, MODULE_NAME_VALUE)
.put(RubyClientCodegen.GEM_VERSION, GEM_VERSION_VALUE)
.put(RubyClientCodegen.GEM_LICENSE, GEM_LICENSE_VALUE)
.put(RubyClientCodegen.GEM_REQUIRED_RUBY_VERSION, GEM_REQUIRED_RUBY_VERSION_VALUE)
.put(RubyClientCodegen.GEM_DESCRIPTION, GEM_DESCRIPTION_VALUE)
.put(RubyClientCodegen.GEM_HOMEPAGE, GEM_HOMEPAGE_VALUE)
.put(RubyClientCodegen.GEM_SUMMARY, GEM_SUMMARY_VALUE)
.put(RubyClientCodegen.GEM_AUTHOR, GEM_AUTHOR_VALUE)
.put(RubyClientCodegen.GEM_AUTHOR_EMAIL, GEM_AUTHOR_EMAIL_VALUE)
.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, SORT_PARAMS_VALUE)
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put(CodegenConstants.HIDE_GENERATION_TIMESTAMP, "true")
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.put(CodegenConstants.PREPEND_FORM_OR_BODY_PARAMETERS, PREPEND_FORM_OR_BODY_PARAMETERS_VALUE)
.build();
}
@Override
public boolean isServer() {
return false;
}
}

View File

@@ -1,10 +1,18 @@
---
---
swagger: "2.0"
basePath: "/"
paths:
/foo=bar:
get:
parameters: []
responses:
200:
description: "success"
basePath: /
info:
description: "Test for response code default"
title: "path with html entity test"
version: "1.0.0"
paths:
/foo=bar:
get:
produces:
- application/json
responses:
200:
description: "successful operation"
default:
description: "Internal server error"
summary: Test