Skip to content

Commit

Permalink
[FLINK-32888] Expose file comparison assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol committed Aug 18, 2023
1 parent b93216f commit 329ee55
Showing 1 changed file with 26 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,36 +138,35 @@ public void before(ExtensionContext context) throws Exception {
(request, restfulGateway) -> {
// the default verifier checks for identiy (i.e. same name and content) of all
// uploaded files
List<Path> expectedFiles =
getFilesToUpload().stream()
.map(File::toPath)
.collect(Collectors.toList());
List<Path> uploadedFiles =
request.getUploadedFiles().stream()
.map(File::toPath)
.collect(Collectors.toList());

assertThat(uploadedFiles).hasSameSizeAs(expectedFiles);

List<Path> expectedList = new ArrayList<>(expectedFiles);
List<Path> actualList = new ArrayList<>(uploadedFiles);
expectedList.sort(Comparator.comparing(Path::toString));
actualList.sort(Comparator.comparing(Path::toString));

for (int x = 0; x < expectedList.size(); x++) {
Path expected = expectedList.get(x);
Path actual = actualList.get(x);

assertThat(actual.getFileName())
.hasToString(expected.getFileName().toString());

byte[] originalContent = Files.readAllBytes(expected);
byte[] receivedContent = Files.readAllBytes(actual);
assertThat(receivedContent).isEqualTo(originalContent);
}
assertUploadedFilesEqual(request, getFilesToUpload());
});
}

public static void assertUploadedFilesEqual(HandlerRequest<?> request, Collection<File> files)
throws IOException {
List<Path> expectedFiles = files.stream().map(File::toPath).collect(Collectors.toList());
List<Path> uploadedFiles =
request.getUploadedFiles().stream().map(File::toPath).collect(Collectors.toList());

assertThat(uploadedFiles).hasSameSizeAs(expectedFiles);

List<Path> expectedList = new ArrayList<>(expectedFiles);
List<Path> actualList = new ArrayList<>(uploadedFiles);
expectedList.sort(Comparator.comparing(Path::toString));
actualList.sort(Comparator.comparing(Path::toString));

for (int x = 0; x < expectedList.size(); x++) {
Path expected = expectedList.get(x);
Path actual = actualList.get(x);

assertThat(actual.getFileName()).hasToString(expected.getFileName().toString());

byte[] originalContent = Files.readAllBytes(expected);
byte[] receivedContent = Files.readAllBytes(actual);
assertThat(receivedContent).isEqualTo(originalContent);
}
}

public void setFileUploadVerifier(
BiConsumerWithException<
HandlerRequest<? extends RequestBody>, RestfulGateway, Exception>
Expand Down

0 comments on commit 329ee55

Please sign in to comment.