From 04b98af9deb2b0eb8fd25c94d0f35d19fce27ed4 Mon Sep 17 00:00:00 2001 From: aldettinger Date: Thu, 2 Nov 2023 22:48:06 +0100 Subject: [PATCH] file: migrate charset test to non flaky test harness #3584 --- .../component/file/it/FileResource.java | 46 +------------------ .../quarkus/component/file/it/FileRoutes.java | 4 +- .../quarkus/component/file/it/FileTest.java | 8 ---- .../component/file/it/NonFlakyFileTest.java | 39 ++++++++++++++++ .../file/it/NonFlakyFileTestResource.java | 36 +++++++++++---- 5 files changed, 67 insertions(+), 66 deletions(-) diff --git a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java index 572abcec4e25..88a2c6fe16b6 100644 --- a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java +++ b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileResource.java @@ -16,15 +16,9 @@ */ package org.apache.camel.quarkus.component.file.it; -import java.io.File; import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import jakarta.enterprise.context.ApplicationScoped; @@ -43,7 +37,6 @@ import org.apache.camel.Exchange; import org.apache.camel.ProducerTemplate; import org.apache.camel.component.mock.MockEndpoint; -import org.awaitility.Awaitility; @Path("/file") @ApplicationScoped @@ -97,6 +90,7 @@ public void startRoute(String routeId) throws Exception { @Path("/getFromMock/{mockId}") @GET + @Produces(MediaType.TEXT_PLAIN) public String getFromMock(@PathParam("mockId") String mockId) { MockEndpoint mockEndpoint = context.getEndpoint("mock:" + mockId, MockEndpoint.class); @@ -168,42 +162,4 @@ public String pollEnrich(String body, @PathParam("route") String route) throws E return producerTemplate.requestBody("direct:" + route, body, String.class); } - @Path("/writeThenReadFileWithCharsetShouldSucceed") - @GET - public void writeThenReadFileWithCharsetShouldSucceed() throws Exception { - - // Delete any charset encoded files that would reside from a previous run - Files.deleteIfExists(Paths.get("target/charsetIsoRead/charsetEncodedFile.txt")); - Files.deleteIfExists(Paths.get("target/charsetIsoWrite/charsetEncodedFile.txt")); - - // Using a charset that has few chance to be the default one on the build platform - String charsetName = "ISO-8859-1"; - String unencodedContent = "A string with ð char"; - byte[] encodedContent = unencodedContent.getBytes(charsetName); - - // Produce in the folder named 'charsetIsoWrite' and check the content is encoded as expected - producerTemplate.request("file:target/charsetIsoWrite/?charset=" + charsetName, ex -> { - ex.getMessage().setHeader(Exchange.FILE_NAME, "charsetEncodedFile.txt"); - ex.getMessage().setBody(unencodedContent); - }); - Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> { - File file = new File("target/charsetIsoWrite/charsetEncodedFile.txt"); - return Arrays.equals(encodedContent, Files.readAllBytes(file.toPath())); - }); - - // Move the encoded file to the read folder - java.nio.file.Path source = Paths.get("target/charsetIsoWrite/charsetEncodedFile.txt"); - java.nio.file.Path destination = Paths.get("target/charsetIsoRead/charsetEncodedFile.txt"); - Files.move(source, destination, StandardCopyOption.ATOMIC_MOVE); - - // Start the route to consume the encoded file - context.getRouteController().startRoute("charsetIsoRead"); - - // Check that the consumed file content has been decoded as expected - Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> { - String decodedContent = getFromMock("charsetIsoRead"); - return unencodedContent.equals(decodedContent); - }); - } - } diff --git a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java index fb21e60d9e70..85c89403477c 100644 --- a/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java +++ b/integration-tests/file/src/main/java/org/apache/camel/quarkus/component/file/it/FileRoutes.java @@ -61,9 +61,7 @@ public void configure() { .convertBodyTo(String.class) .to("mock:" + CONSUME_BATCH); - from("file://target/charsetIsoRead?initialDelay=0&delay=10&delete=true&charset=ISO-8859-1") - .routeId("charsetIsoRead") - .autoStartup(false) + from("file://target/test-files/charset-read?initialDelay=0&delay=10&delete=true&charset=ISO-8859-1") .convertBodyTo(String.class) .to("mock:charsetIsoRead"); diff --git a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java index 7ab88b2419cc..10fe1e76503a 100644 --- a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java +++ b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/FileTest.java @@ -65,14 +65,6 @@ public void afterEach() { pathsToDelete.clear(); } - @Test - public void writeThenReadFileWithCharsetShouldSucceed() { - RestAssured - .get("/file/writeThenReadFileWithCharsetShouldSucceed") - .then() - .statusCode(204); - } - @Test public void batch() throws InterruptedException, UnsupportedEncodingException { // Create 2 files diff --git a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTest.java b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTest.java index b94d2a999d89..dff751bbfb59 100644 --- a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTest.java +++ b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTest.java @@ -30,6 +30,10 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_READ_FILE_CONTENT; +import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_WRITE_FILE_CONTENT; +import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_WRITE_FILE_CREATION_FOLDER; +import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.CHARSET_WRITE_FILE_NAME; import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILE_CREATION_FILE_CONTENT; import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILE_CREATION_FILE_NAME; import static org.apache.camel.quarkus.component.file.it.NonFlakyFileTestResource.FILE_CREATION_FOLDER; @@ -136,4 +140,39 @@ public void createFileShouldSucceed() throws IOException { assertEquals(FILE_CREATION_FILE_CONTENT, readFileToString(expectedFilePath.toFile(), charset)); } + @Test + void readFileWithIso8859_1CharsetShouldSucceed() { + await().atMost(10, TimeUnit.SECONDS).until( + () -> RestAssured + .get("/file/getFromMock/charsetIsoRead") + .then() + .extract().asString(), + equalTo(CHARSET_READ_FILE_CONTENT)); + } + + @Test + void writeFileWithIso8859_1CharsetShouldSucceed() throws IOException { + String charset = "ISO-8859-1"; + Path expectedFilePath = TEST_FILES_FOLDER + .resolve(Paths.get(CHARSET_WRITE_FILE_CREATION_FOLDER, CHARSET_WRITE_FILE_NAME)); + + assertFalse(Files.exists(expectedFilePath)); + + // Create a new file in the "test-files" folder so that it's cleared at each run by the NonFlakyFileTestResource + RestAssured.given() + .contentType(ContentType.TEXT) + .body(CHARSET_WRITE_FILE_CONTENT) + .queryParam("folder", CHARSET_WRITE_FILE_CREATION_FOLDER) + .queryParam("charset", charset) + .queryParam("fileName", CHARSET_WRITE_FILE_NAME) + .post("/file/create-file") + .then() + .statusCode(201); + + await().atMost(1, TimeUnit.SECONDS).until(() -> Files.exists(expectedFilePath)); + + assertEquals(CHARSET_WRITE_FILE_CONTENT, readFileToString(expectedFilePath.toFile(), charset)); + + } + } diff --git a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTestResource.java b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTestResource.java index dc9c82d42ae2..b228847d60f1 100644 --- a/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTestResource.java +++ b/integration-tests/file/src/test/java/org/apache/camel/quarkus/component/file/it/NonFlakyFileTestResource.java @@ -17,12 +17,14 @@ package org.apache.camel.quarkus.component.file.it; import java.io.IOException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; import org.apache.commons.io.FileUtils; @@ -42,11 +44,16 @@ public class NonFlakyFileTestResource implements QuarkusTestResourceLifecycleMan static final String POLL_ENRICH_FILE_CONTENT = POLL_ENRICH_FILE_NAME + "-CONTENT"; static final String QUARTZ_SCHEDULED_FILE_NAME = "quartz-schedule-file"; static final String QUARTZ_SCHEDULED_FILE_CONTENT = QUARTZ_SCHEDULED_FILE_NAME + "-CONTENT"; + static final String CHARSET_READ_FILE_NAME = "charset-read-file"; + static final String CHARSET_READ_FILE_CONTENT = "A string with \u00F0 char to be read"; + static final String CHARSET_WRITE_FILE_CREATION_FOLDER = "charset-write"; + static final String CHARSET_WRITE_FILE_NAME = "charset-write-file"; + static final String CHARSET_WRITE_FILE_CONTENT = "A string with \u00F0 char to be written"; static final String FILE_CREATION_FOLDER = "file-creation"; static final String FILE_CREATION_FILE_NAME = "should-be-created"; static final String FILE_CREATION_FILE_CONTENT = FILE_CREATION_FILE_NAME + "-CONTENT"; - private final List createdTestFiles = new ArrayList(); + private final Map createdTestFilesExpectedContent = new HashMap<>(); @Override public Map start() { @@ -62,7 +69,10 @@ public Map start() { createTestFile("quartz-scheduled", QUARTZ_SCHEDULED_FILE_NAME); + createTestFile("charset-read", CHARSET_READ_FILE_NAME, CHARSET_READ_FILE_CONTENT, StandardCharsets.ISO_8859_1); + // Do not use createTestFile("file-creation"... as it is already reserved by the createFileShouldSucceed test + // Do not use createTestFile("charset-write"... as it is already reserved by the writeFileWithIso8859_1CharsetShouldSucceed test ensureAllTestFilesCreatedWithExpectedContent(); } catch (Exception ex) { @@ -71,19 +81,25 @@ public Map start() { return null; } - private void createTestFile(String testName, String testFileName) throws IOException { + private void createTestFile(String testName, String testFileName, String content, Charset charset) throws IOException { Path testFilePath = TEST_FILES_FOLDER.resolve(Paths.get(testName, testFileName)); - FileUtils.writeStringToFile(testFilePath.toFile(), testFileName + "-CONTENT", StandardCharsets.UTF_8); - createdTestFiles.add(testFilePath); + byte[] contentBytes = content.getBytes(charset); + FileUtils.writeByteArrayToFile(testFilePath.toFile(), contentBytes); + createdTestFilesExpectedContent.put(testFilePath, contentBytes); + } + + private void createTestFile(String testName, String testFileName) throws IOException { + createTestFile(testName, testFileName, testFileName + "-CONTENT", StandardCharsets.UTF_8); } private void ensureAllTestFilesCreatedWithExpectedContent() { - for (Path path : createdTestFiles) { + for (Entry createdTestFileExpectedContent : createdTestFilesExpectedContent.entrySet()) { + Path path = createdTestFileExpectedContent.getKey(); try { - String content = FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8); - String expectedContent = path.toFile().getName() + "-CONTENT"; + byte[] content = FileUtils.readFileToByteArray(path.toFile()); + byte[] expectedContent = createdTestFileExpectedContent.getValue(); - if (!expectedContent.equals(content)) { + if (!Arrays.equals(expectedContent, content)) { String format = "The NonFlakyFileTestResource failed to create the test file %s with expected content"; throw new RuntimeException(String.format(format, path)); }