From 0aaa1cc38a1be5d361640e9809442ba846f8a69e Mon Sep 17 00:00:00 2001 From: Sterchi Daniel Date: Thu, 18 Jul 2024 08:40:57 +0200 Subject: [PATCH] update test --- .../generator/JavaCitrusCodegenIT.java | 152 +++++++++++------- 1 file changed, 92 insertions(+), 60 deletions(-) diff --git a/test-api-generator/citrus-test-api-generator-core/src/test/java/org/citrusframework/openapi/generator/JavaCitrusCodegenIT.java b/test-api-generator/citrus-test-api-generator-core/src/test/java/org/citrusframework/openapi/generator/JavaCitrusCodegenIT.java index ed25210132..10c14280f8 100644 --- a/test-api-generator/citrus-test-api-generator-core/src/test/java/org/citrusframework/openapi/generator/JavaCitrusCodegenIT.java +++ b/test-api-generator/citrus-test-api-generator-core/src/test/java/org/citrusframework/openapi/generator/JavaCitrusCodegenIT.java @@ -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; @@ -38,63 +37,96 @@ */ class JavaCitrusCodegenIT { -// static Stream 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 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 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 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 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 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 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); + } }