Skip to content

Commit

Permalink
file: migrate charset test to non flaky test harness #3584 (#5480)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldettinger authored Nov 8, 2023
1 parent 30e6cb1 commit a10e95d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Path> createdTestFiles = new ArrayList<Path>();
private final Map<Path, byte[]> createdTestFilesExpectedContent = new HashMap<>();

@Override
public Map<String, String> start() {
Expand All @@ -62,7 +69,10 @@ public Map<String, String> 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) {
Expand All @@ -71,19 +81,25 @@ public Map<String, String> 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<Path, byte[]> 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));
}
Expand Down

0 comments on commit a10e95d

Please sign in to comment.