Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sterchi Daniel committed Jul 18, 2024
1 parent eb75b44 commit 0aaa1cc
Showing 1 changed file with 92 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.apache.commons.lang3.stream.Streams;
import org.citrusframework.exceptions.CitrusRuntimeException;
Expand All @@ -38,63 +37,96 @@
*/
class JavaCitrusCodegenIT {

// static Stream<Arguments> getResourcesForRest() throws IOException {
// return geClassResourcesIgnoringInnerClasses("org/citrusframework/openapi/generator/rest");
// }
//
// @ParameterizedTest
// @MethodSource("getResourcesForRest")
// void testGeneratedFiles(Resource resource) throws IOException {
// File classFile = resource.getFile();
// String absolutePath = classFile.getAbsolutePath();
// String javaFilePath = absolutePath.replace("test-classes", "generated-test-sources")
// .replace(".class", ".java");
//
// assertFileContent(new File(javaFilePath), "rest");
// }
//
// static Stream<Arguments> getResourcesForSoap() throws IOException {
// return geClassResourcesIgnoringInnerClasses(
// "org/citrusframework/openapi/generator/soap/bookservice");
// }
//
// @ParameterizedTest
// @MethodSource("getResourcesForSoap")
// void testGeneratedSoapFiles(Resource resource) throws IOException {
// File classFile = resource.getFile();
// String absolutePath = classFile.getAbsolutePath();
//
// String javaFilePath = absolutePath.replace("test-classes", "generated-test-sources")
// .replace(".class", ".java");
//
// assertFileContent(new File(javaFilePath), "soap");
// }
//
// private static Stream<Arguments> geClassResourcesIgnoringInnerClasses(String path)
// throws IOException {
// return Streams.of(new PathMatchingResourcePatternResolver().getResources(
// path + "/**/*.class")).filter(resource -> {
// try {
// return !resource.getURI().toString().contains("$");
// } catch (Exception e) {
// throw new CitrusRuntimeException("Unable to retrieve URL from resource!");
// }
// }).map(Arguments::arguments);
// }
//
// private void assertFileContent(File file, String apiDir) throws IOException {
// assertThat(file).exists();
// String expectedFilePath =
// "org/citrusframework/openapi/generator/JavaCitrusCodegenIntegrationTest/expectedgen/"
// + file.getAbsolutePath().substring(file.getAbsolutePath().indexOf(apiDir));
//
// ClassPathResource classPathResource = new ClassPathResource(expectedFilePath);
//
// /*
// * NOTE: when changes have been performed to mustache templates, the expected files need to be updated.
// * Be aware that file content may change according to IDE formatting rules if the files are copied via IDE.
// * Files should therefore be copied using a file explorer which ensures that content of files does not change.
// */
// assertThat(file).hasSameTextualContentAs(classPathResource.getFile());
// }
public static final String BASE_PACKAGE = "org/citrusframework/openapi/generator";

private static long countFilesRecursively(Path dir) throws IOException {
try (Stream<Path> walk = walk(dir)) {
return walk.filter(Files::isRegularFile).count();
}
}

@Test
void noAdditionalFiles() throws IOException {
long expectedFileCount = countFilesRecursively(
Path.of(getAbsoluteTestResourcePath(
BASE_PACKAGE + "/JavaCitrusCodegenIT/expectedgen/rest")));
long actualFileCount = countFilesRecursively(
Path.of(getAbsoluteTargetDirectoryPath(
"generated-test-sources/" + BASE_PACKAGE + "/rest")));

assertEquals(expectedFileCount, actualFileCount,
"Directories do not have the same number of files.");
}

static Stream<Arguments> getResourcesForRest() throws IOException {
return geClassResourcesIgnoringInnerClasses(BASE_PACKAGE + "/rest");
}

@ParameterizedTest
@MethodSource("getResourcesForRest")
void testGeneratedFiles(Resource resource) throws IOException {
File classFile = resource.getFile();
String absolutePath = classFile.getAbsolutePath();
String javaFilePath = absolutePath
.replace("test-classes", "generated-test-sources")
.replace(".class", ".java");

assertFileContent(new File(javaFilePath), "rest");
}

static Stream<Arguments> getResourcesForSoap() throws IOException {
return geClassResourcesIgnoringInnerClasses(
BASE_PACKAGE + "/soap/bookservice");
}

@ParameterizedTest
@MethodSource("getResourcesForSoap")
void testGeneratedSoapFiles(Resource resource) throws IOException {
File classFile = resource.getFile();
String absolutePath = classFile.getAbsolutePath();

String javaFilePath = absolutePath
.replace("test-classes", "generated-test-sources")
.replace(".class", ".java");

assertFileContent(new File(javaFilePath), "soap");
}

private static Stream<Arguments> geClassResourcesIgnoringInnerClasses(String path)
throws IOException {
return Streams.of(
new PathMatchingResourcePatternResolver().getResources(path + "/**/*.class"))
.filter(resource -> {
try {
return !resource.getURI().toString().contains("$");
} catch (Exception e) {
throw new CitrusRuntimeException("Unable to retrieve URL from resource!");
}
}
).map(Arguments::arguments);
}

/*
* NOTE: when changes have been performed to mustache templates, the expected files need to be updated.
* Be aware that file content may change according to IDE formatting rules if the files are copied via IDE.
* Files should therefore be copied using a file explorer which ensures that content of files does not change.
*/
private void assertFileContent(File file, String apiDir) throws IOException {
assertThat(file).exists();

String expectedFilePath = BASE_PACKAGE + "/JavaCitrusCodegenIT/expectedgen/" + file.getAbsolutePath().substring(file.getAbsolutePath().indexOf(apiDir));
ClassPathResource classPathResource = new ClassPathResource(expectedFilePath);

String actualContent = readString(file.toPath());
String expectedContent = readString(classPathResource.getFile().toPath());

// Replace "Generated" with a placeholder
String generatedAnnotationPattern = "@jakarta\\.annotation\\.Generated\\(.*?\\)";
String placeholder = "@jakarta.annotation.Generated(value = \"org.citrusframework.openapi.generator.JavaCitrusCodegen\", date = \"TIMESTAMP\", comments = \"Generator version: VERSION\")";

actualContent = actualContent.replaceAll(generatedAnnotationPattern, placeholder);
expectedContent = expectedContent.replaceAll(generatedAnnotationPattern, placeholder);

assertThat(actualContent).isEqualToIgnoringWhitespace(expectedContent);
}
}

0 comments on commit 0aaa1cc

Please sign in to comment.