Merge pull request #2882 from Vrolijkx/feature/integration-test

Adding correct code generation for additional properties in typescript angular2.
This commit is contained in:
wing328
2016-05-19 14:37:30 +08:00
32 changed files with 671 additions and 45 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.testutils.IntegrationTestPathsConfig;
import io.swagger.models.Swagger;
import io.swagger.parser.SwaggerParser;
import static io.swagger.codegen.testutils.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

@@ -0,0 +1,132 @@
package io.swagger.codegen.testutils;
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 java.util.List;
import difflib.Delta;
import difflib.DiffUtils;
import difflib.Patch;
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 textual 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 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' is missing.", actualDir));
}
assertEquals(expectedDir.toFile().list(),
actualDir.toFile().list(),
String.format("Directory content of '%s' and '%s' differ.", expectedDir, actualDir));
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' is missing.", actualFile));
}
assertFilesAreEqual(expectedFile, actualFile);
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(), e);
}
}
public static void assertFilesAreEqual(final Path expected, final Path actual) {
if(!Files.isRegularFile(expected)) {
fail("expected: '%s' is not a readable file");
}
if(!Files.isRegularFile(actual)) {
fail("actual: '%s' is not a readable file");
}
try {
List<String> expectedLines = Files.readAllLines(expected, Charset.defaultCharset());
List<String> actualLines = Files.readAllLines(actual, Charset.defaultCharset());
Patch diff = DiffUtils.diff(expectedLines, actualLines);
List<Delta> deltas = diff.getDeltas();
if(!deltas.isEmpty()) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("files diff:\n");
stringBuilder.append("\tfile: '" + expected.toAbsolutePath().toString() + "' \n");
stringBuilder.append("\tfile: '" + actual.toAbsolutePath().toString() + "' \n");
stringBuilder.append("\tdiffs:\n");
for (Delta delta: deltas) {
stringBuilder.append(delta.toString() + "\n");
}
fail(stringBuilder.toString());
}
} catch (IOException e) {
fail(e.getMessage(), e);
}
}
}

View File

@@ -0,0 +1,33 @@
package io.swagger.codegen.testutils;
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;
}
}

View File

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

View File

@@ -1,6 +1,10 @@
package io.swagger.codegen.typescriptfetch;
package io.swagger.codegen.typescript.fetch;
import com.google.common.collect.Sets;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenProperty;
import io.swagger.codegen.DefaultCodegen;
@@ -8,9 +12,11 @@ import io.swagger.codegen.languages.TypeScriptFetchClientCodegen;
import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.*;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.DateTimeProperty;
import io.swagger.models.properties.LongProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
@SuppressWarnings("static-method")
public class TypeScriptFetchModelTest {

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;
@@ -178,6 +178,7 @@ public class TypeScriptAngular2ModelTest {
Assert.assertEquals(cm.description, "a map model");
Assert.assertEquals(cm.vars.size(), 0);
Assert.assertEquals(cm.imports.size(), 1);
Assert.assertEquals(cm.additionalPropertiesType, "models.Children");
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("models.Children")).size(), 1);
}
}

View File

@@ -0,0 +1,32 @@
package io.swagger.codegen.typescript.typescriptangular2;
import java.util.HashMap;
import java.util.Map;
import io.swagger.codegen.AbstractIntegrationTest;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen;
import io.swagger.codegen.testutils.IntegrationTestPathsConfig;
public class TypescriptAngular2AdditionalPropertiesIntegrationTest extends 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.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;