improving integration test concept.

This commit is contained in:
Kristof Vrolijkx 2016-05-01 17:06:12 +02:00
parent f8046bc9c9
commit 15feb208e7
16 changed files with 120 additions and 85 deletions

View File

@ -0,0 +1,45 @@
package io.swagger.codegen;
import org.testng.annotations.Test;
import org.testng.reporters.Files;
import java.io.IOException;
import java.util.Map;
import io.swagger.codegen.utils.IntegrationTestPathsConfig;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
import static io.swagger.codegen.utils.AssertFile.assertPathEqualsRecursively;
public abstract class AbstractIntegrationTest {
protected abstract IntegrationTestPathsConfig getIntegrationTestPathsConfig();
protected abstract CodegenConfig getCodegenConfig();
protected abstract Map<String, String> configProperties();
@Test
public void generatesCorrectDirectoryStructure() throws IOException {
DefaultGenerator codeGen = new DefaultGenerator();
IntegrationTestPathsConfig integrationTestPathsConfig = getIntegrationTestPathsConfig();
String specContent = Files.readFile(integrationTestPathsConfig.getSpecPath().toFile());
Swagger swagger = new SwaggerParser().parse(specContent);
CodegenConfig codegenConfig = getCodegenConfig();
codegenConfig.setOutputDir(integrationTestPathsConfig.getOutputPath().toString());
ClientOpts clientOpts = new ClientOpts();
clientOpts.setProperties(configProperties());
ClientOptInput opts = new ClientOptInput()
.config(codegenConfig)
.opts(clientOpts)
.swagger(swagger);
codeGen.opts(opts).generate();
assertPathEqualsRecursively(integrationTestPathsConfig.getExpectedPath(), integrationTestPathsConfig.getOutputPath());
}
}

View File

@ -1,74 +0,0 @@
package io.swagger.codegen.typescript.integrationtest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.reporters.Files;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.ClientOpts;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.DefaultGenerator;
import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
import static io.swagger.codegen.typescript.integrationtest.AssertFile.assertPathEqualsRecursively;
public class Angular2GenerationWithAditionPropertiesTest {
private DefaultGenerator codeGen;
private Path integrationTestPath;
private Path outputPath;
private Path specPath;
private Path expectedPath;
@BeforeMethod
public void setUp() {
codeGen = new DefaultGenerator();
integrationTestPath = Paths.get("target/test-classes/integrationtest").toAbsolutePath();
outputPath = integrationTestPath.resolve("typescript/additional-properties-result");
expectedPath = integrationTestPath.resolve("typescript/additional-properties-expected");
specPath = integrationTestPath.resolve("typescript/additional-properties-spec.json");
}
protected CodegenConfig getCodegenConfig() {
return new TypeScriptAngular2ClientCodegen();
}
protected Map<String, String> configProperties() {
Map<String, String> propeties = new HashMap<>();
propeties.put("npmName", "additionalPropertiesTest");
propeties.put("npmVersion", "1.0.2");
propeties.put("snapshot", "false");
return propeties;
}
@Test(description = "The correct output is generated for a spec with additional-properties")
public void shouldGenerateCorrectTypescriptModels() throws IOException {
String specContent = Files.readFile(specPath.toFile());
Swagger swagger = new SwaggerParser().parse(specContent);
CodegenConfig codegenConfig = getCodegenConfig();
codegenConfig.setOutputDir(outputPath.toString());
ClientOpts clientOpts = new ClientOpts();
clientOpts.setProperties(configProperties());
ClientOptInput opts = new ClientOptInput()
.config(codegenConfig)
.opts(clientOpts)
.swagger(swagger);
codeGen.opts(opts).generate();
assertPathEqualsRecursively(expectedPath, outputPath);
}
}

View File

@ -0,0 +1,31 @@
package io.swagger.codegen.typescript.typescriptangular2;
import java.util.HashMap;
import java.util.Map;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen;
import io.swagger.codegen.utils.IntegrationTestPathsConfig;
public class TypescriptAngular2AdditionalPropertiesIntegrationTest extends io.swagger.codegen.AbstractIntegrationTest {
@Override
protected CodegenConfig getCodegenConfig() {
return new TypeScriptAngular2ClientCodegen();
}
@Override
protected Map<String, String> configProperties() {
Map<String, String> propeties = new HashMap<>();
propeties.put("npmName", "additionalPropertiesTest");
propeties.put("npmVersion", "1.0.2");
propeties.put("snapshot", "false");
return propeties;
}
@Override
protected IntegrationTestPathsConfig getIntegrationTestPathsConfig() {
return new IntegrationTestPathsConfig("typescript/additional-properties");
}
}

View File

@ -1,4 +1,4 @@
package io.swagger.codegen.typescript.integrationtest; package io.swagger.codegen.utils;
import org.testng.Assert; import org.testng.Assert;
@ -48,12 +48,12 @@ public class AssertFile {
Path actualDir = absoluteActual.resolve(relativeExpectedDir); Path actualDir = absoluteActual.resolve(relativeExpectedDir);
if (!Files.exists(actualDir)) { if (!Files.exists(actualDir)) {
fail(String.format("Directory \'%s\' missing in target.", expectedDir.getFileName())); fail(String.format("Directory '%s' is missing.", actualDir));
} }
assertEquals(expectedDir.toFile().list().length, assertEquals(expectedDir.toFile().list(),
actualDir.toFile().list().length, actualDir.toFile().list(),
String.format("Directory size of \'%s\' differ. ", relativeExpectedDir)); String.format("Directory content of '%s' and '%s' differ.", expectedDir, actualDir));
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
@ -64,11 +64,11 @@ public class AssertFile {
Path actualFile = absoluteActual.resolve(relativeExpectedFile); Path actualFile = absoluteActual.resolve(relativeExpectedFile);
if (!Files.exists(actualFile)) { if (!Files.exists(actualFile)) {
fail(String.format("File \'%s\' missing in target.", expectedFile.getFileName())); fail(String.format("File '%s' is missing.", actualFile));
} }
assertEquals(Files.readAllLines(expectedFile, Charset.defaultCharset()), assertEquals(Files.readAllLines(expectedFile, Charset.defaultCharset()),
Files.readAllLines(actualFile, Charset.defaultCharset()), Files.readAllLines(actualFile, Charset.defaultCharset()),
String.format("File content of \'%s\' differ. ", relativeExpectedFile)); String.format("File content of '%s' and '%s' differ.", expectedFile, actualFile));
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }

View File

@ -0,0 +1,33 @@
package io.swagger.codegen.utils;
import java.nio.file.Path;
import java.nio.file.Paths;
public class IntegrationTestPathsConfig {
private static final Path INTEGRATION_TEST_PATH = Paths.get("target/test-classes/integrationtests").toAbsolutePath();
private final Path outputPath;
private final Path specPath;
private final Path expectedPath;
public IntegrationTestPathsConfig(String location) {
this(location + "-spec.json", location + "-result", location + "-expected");
}
public IntegrationTestPathsConfig(String specLocation, String outputLocation, String expectedLocation) {
outputPath = INTEGRATION_TEST_PATH.resolve(outputLocation);
expectedPath = INTEGRATION_TEST_PATH.resolve(expectedLocation);
specPath = INTEGRATION_TEST_PATH.resolve(specLocation);
}
public Path getOutputPath() {
return outputPath;
}
public Path getSpecPath() {
return specPath;
}
public Path getExpectedPath() {
return expectedPath;
}
}