From 5adadbf2118b6ea35370c767bfb9da3bc753a243 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 9 Feb 2023 09:20:58 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20=E2=80=9CClientPu?= =?utf8?q?tComplexDir=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also, fix a bug in it which would have prevent direct file uploads from having worked. --- .../net/pterodactylus/fcp/ClientPutComplexDir.java | 2 +- .../pterodactylus/fcp/ClientPutComplexDirTest.java | 168 +++++++++++++++++++++ .../java/net/pterodactylus/fcp/test/Matchers.java | 34 ++++- 3 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java diff --git a/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java b/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java index 4148b9d..c2cc547 100644 --- a/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java +++ b/src/main/java/net/pterodactylus/fcp/ClientPutComplexDir.java @@ -242,7 +242,7 @@ public class ClientPutComplexDir extends FcpMessage { @Override public void write(OutputStream outputStream) throws IOException { /* create payload stream. */ - setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams))); + super.setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams))); /* write out all the fields. */ super.write(outputStream); } diff --git a/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java b/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java new file mode 100644 index 0000000..63ade6f --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/ClientPutComplexDirTest.java @@ -0,0 +1,168 @@ +package net.pterodactylus.fcp; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.function.Consumer; + +import org.junit.Test; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static net.pterodactylus.fcp.Persistence.unknown; +import static net.pterodactylus.fcp.Priority.minimum; +import static net.pterodactylus.fcp.Verbosity.COMPRESSION; +import static net.pterodactylus.fcp.Verbosity.PROGRESS; +import static net.pterodactylus.fcp.test.Matchers.isDataMessage; +import static net.pterodactylus.fcp.test.Matchers.isMessage; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anything; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; + +/** + * Unit test for {@link ClientPutComplexDir}. + */ +public class ClientPutComplexDirTest { + + @Test + public void canCreateClientPutComplexDir() { + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", "Identifier=identifier", "URI=uri")); + } + + @Test + public void verbosityCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setVerbosity(COMPRESSION.add(PROGRESS)), "Verbosity=513"); + } + + @Test + public void maxRetriesCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setMaxRetries(12), "MaxRetries=12"); + } + + @Test + public void priorityCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setPriority(minimum), "PriorityClass=6"); + } + + @Test + public void getChkOnlyCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setGetCHKOnly(true), "GetCHKOnly=true"); + } + + @Test + public void forkOnCacheableCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setForkOnCacheable(true), "ForkOnCacheable=true"); + } + + @Test + public void extraInsertsSingleBlockCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setExtraInsertsSingleBlock(12), "ExtraInsertsSingleBlock=12"); + } + + @Test + public void extraInsertsSplitfileHeaderBlockCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setExtraInsertsSplitfileHeaderBlock(23), "ExtraInsertsSplitfileHeaderBlock=23"); + } + + @Test + public void globalCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setGlobal(true), "Global=true"); + } + + @Test + public void dontCompressCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setDontCompress(true), "DontCompress=true"); + } + + @Test + public void clientTokenCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setClientToken("client-token"), "ClientToken=client-token"); + } + + @Test + public void persistenceCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setPersistence(unknown), "Persistence=unknown"); + } + + @Test + public void targetFilenameCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setTargetFilename("target-filename"), "TargetFilename=target-filename"); + } + + @Test + public void earlyEncodeCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setEarlyEncode(true), "EarlyEncode=true"); + } + + @Test + public void defaultNameCanBeSet() { + testThatFieldCanBeSet(cp -> cp.setDefaultName("default-name"), "DefaultName=default-name"); + } + + private void testThatFieldCanBeSet(Consumer setter, String expectedParameter) { + setter.accept(clientPutComplexDir); + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", hasItem(expectedParameter))); + } + + @Test + public void directFilesAreAddedCorrectly() { + clientPutComplexDir.addFileEntry(FileEntry.createDirectFileEntry("file.dat", "text/plain", 5, createInputStream("Test\n"))); + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", allOf( + hasItem("Files.0.Name=file.dat"), + hasItem("Files.0.UploadFrom=direct"), + hasItem("Files.0.DataLength=5"), + hasItem("Files.0.Metadata.ContentType=text/plain") + ), contains("Test"))); + } + + @Test + public void multipleDirectFilesAreAddedInCorrectOrder() { + clientPutComplexDir.addFileEntry(FileEntry.createDirectFileEntry("file1.dat", "text/plain1", 4, createInputStream("Tes\n"))); + clientPutComplexDir.addFileEntry(FileEntry.createDirectFileEntry("file2.dat", "text/plain2", 6, createInputStream("tData\n"))); + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", allOf( + hasItem("Files.0.Name=file1.dat"), + hasItem("Files.0.UploadFrom=direct"), + hasItem("Files.0.DataLength=4"), + hasItem("Files.0.Metadata.ContentType=text/plain1"), + hasItem("Files.1.Name=file2.dat"), + hasItem("Files.1.UploadFrom=direct"), + hasItem("Files.1.DataLength=6"), + hasItem("Files.1.Metadata.ContentType=text/plain2") + ), contains("Tes", "tData"))); + } + + @Test + public void diskFileEntryIsAddedCorrectly() { + clientPutComplexDir.addFileEntry(FileEntry.createDiskFileEntry("file1.dat", "/file/name", "text/plain1", 10)); + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", allOf( + hasItem("Files.0.Name=file1.dat"), + hasItem("Files.0.UploadFrom=disk"), + hasItem("Files.0.Filename=/file/name"), + hasItem("Files.0.Metadata.ContentType=text/plain1") + ))); + } + + @Test + public void redirectFileEntryIsAddedCorrectly() { + clientPutComplexDir.addFileEntry(FileEntry.createRedirectFileEntry("file1.dat", "target-uri")); + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", allOf( + hasItem("Files.0.Name=file1.dat"), + hasItem("Files.0.UploadFrom=redirect"), + hasItem("Files.0.TargetURI=target-uri") + ))); + } + + private static InputStream createInputStream(String text) { + return new ByteArrayInputStream(text.getBytes(UTF_8)); + } + + @Test + public void manuallySetPayloadIsIgnored() { + clientPutComplexDir.setPayloadInputStream(createInputStream("Test\n")); + assertThat(clientPutComplexDir, isDataMessage("ClientPutComplexDir", anything(), not(hasItem("Test")))); + } + + private final ClientPutComplexDir clientPutComplexDir = new ClientPutComplexDir("identifier", "uri"); + +} diff --git a/src/test/java/net/pterodactylus/fcp/test/Matchers.java b/src/test/java/net/pterodactylus/fcp/test/Matchers.java index 962c6fd..645b4a0 100644 --- a/src/test/java/net/pterodactylus/fcp/test/Matchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/Matchers.java @@ -10,15 +10,33 @@ import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; +import static org.hamcrest.Matchers.anything; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.equalTo; public class Matchers { public static Matcher isMessage(String name, String... lines) { - return isMessage(name, containsInAnyOrder(lines)); + return isMessage(name, containsInAnyOrder(lines), equalTo("EndMessage"), anything()); + } + + public static Matcher isDataMessage(String name, String... lines) { + return isMessage(name, containsInAnyOrder(lines), equalTo("Data"), anything()); } public static Matcher isMessage(String name, Matcher> linesMatcher) { + return isMessage(name, linesMatcher, equalTo("EndMessage"), anything()); + } + + public static Matcher isDataMessage(String name, Matcher> linesMatcher) { + return isMessage(name, linesMatcher, equalTo("Data"), anything()); + } + + public static Matcher isDataMessage(String name, Matcher> linesMatcher, Matcher> dataMatcher) { + return isMessage(name, linesMatcher, equalTo("Data"), dataMatcher); + } + + public static Matcher isMessage(String name, Matcher> linesMatcher, Matcher terminatorMatcher, Matcher> dataMatcher) { return new TypeSafeDiagnosingMatcher() { @Override protected boolean matchesSafely(FcpMessage fcpMessage, Description mismatchDescription) { @@ -34,10 +52,20 @@ public class Matchers { if (actualLine.contains("=")) { parameterLines.add(actualLine); } else { + if (!terminatorMatcher.matches(actualLine)) { + terminatorMatcher.describeMismatch(actualLine, mismatchDescription); + return false; + } if (!linesMatcher.matches(parameterLines)) { linesMatcher.describeMismatch(parameterLines, mismatchDescription); return false; } + List dataLines = new ArrayList<>(); + messageLines.forEachRemaining(dataLines::add); + if (!dataMatcher.matches(dataLines)) { + dataMatcher.describeMismatch(dataLines, mismatchDescription); + return false; + } return true; } } @@ -46,7 +74,9 @@ public class Matchers { @Override public void describeTo(Description description) { description.appendText("is message named ").appendValue(name) - .appendText(" with parameters ").appendDescriptionOf(linesMatcher); + .appendText(" with parameters ").appendDescriptionOf(linesMatcher) + .appendText(" terminated with ").appendDescriptionOf(terminatorMatcher) + .appendText(" and data of ").appendDescriptionOf(dataMatcher); } }; } -- 2.7.4