small example of a first integration-test of the code-generator

This commit is contained in:
Kristof Vrolijkx
2016-05-01 14:27:41 +02:00
parent de5363c21b
commit f8046bc9c9
19 changed files with 474 additions and 52 deletions

View File

@@ -0,0 +1,74 @@
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,94 @@
package io.swagger.codegen.typescript.integrationtest;
import org.testng.Assert;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
/**
* Assertion for recursively testing directories.
*
* @author andreas
*/
public class AssertFile {
private AssertFile() {
throw new RuntimeException("This class should not be instantiated");
}
/**
* Asserts that two directories are recursively equal. If they are not, an {@link AssertionError} is thrown with the
* given message.<br/>
* There will be a binary comparison of all files under expected with all files under actual. File attributes will
* not be considered.<br/>
* Missing or additional files are considered an error.<br/>
*
* @param expected Path expected directory
* @param actual Path actual directory
*/
public static final void assertPathEqualsRecursively(final Path expected, final Path actual) {
Assert.assertNotNull(expected);
Assert.assertNotNull(actual);
final Path absoluteExpected = expected.toAbsolutePath();
final Path absoluteActual = actual.toAbsolutePath();
try {
Files.walkFileTree(expected, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path expectedDir, BasicFileAttributes attrs) throws IOException {
Path relativeExpectedDir = absoluteExpected.relativize(expectedDir.toAbsolutePath());
Path actualDir = absoluteActual.resolve(relativeExpectedDir);
if (!Files.exists(actualDir)) {
fail(String.format("Directory \'%s\' missing in target.", expectedDir.getFileName()));
}
assertEquals(expectedDir.toFile().list().length,
actualDir.toFile().list().length,
String.format("Directory size of \'%s\' differ. ", relativeExpectedDir));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path expectedFile, BasicFileAttributes attrs) throws IOException {
Path relativeExpectedFile = absoluteExpected.relativize(expectedFile.toAbsolutePath());
Path actualFile = absoluteActual.resolve(relativeExpectedFile);
if (!Files.exists(actualFile)) {
fail(String.format("File \'%s\' missing in target.", expectedFile.getFileName()));
}
assertEquals(Files.readAllLines(expectedFile, Charset.defaultCharset()),
Files.readAllLines(actualFile, Charset.defaultCharset()),
String.format("File content of \'%s\' differ. ", relativeExpectedFile));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
fail(exc.getMessage());
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
fail(e.getMessage());
}
}
}

View File

@@ -1,4 +1,4 @@
package io.swagger.codegen.typescriptangular;
package io.swagger.codegen.typescript.typescriptangular;
import io.swagger.codegen.AbstractOptionsTest;
import io.swagger.codegen.CodegenConfig;

View File

@@ -1,4 +1,4 @@
package io.swagger.codegen.typescriptangular;
package io.swagger.codegen.typescript.typescriptangular;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;

View File

@@ -1,4 +1,4 @@
package io.swagger.codegen.typescriptangular2;
package io.swagger.codegen.typescript.typescriptangular2;
import io.swagger.codegen.AbstractOptionsTest;
import io.swagger.codegen.CodegenConfig;

View File

@@ -1,4 +1,4 @@
package io.swagger.codegen.typescriptangular2;
package io.swagger.codegen.typescript.typescriptangular2;
import com.google.common.collect.Sets;

View File

@@ -1,4 +1,4 @@
package io.swagger.codegen.typescriptnode;
package io.swagger.codegen.typescript.typescriptnode;
import io.swagger.codegen.AbstractOptionsTest;
import io.swagger.codegen.CodegenConfig;

View File

@@ -1,4 +1,4 @@
package io.swagger.codegen.typescriptnode;
package io.swagger.codegen.typescript.typescriptnode;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;