From: David ‘Bombe’ Roden Date: Mon, 2 Sep 2024 15:16:14 +0000 (+0200) Subject: 🚸 Add more methods to PersistentPutDir X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cd5240b01a57589cb4641fed2280854e8af85821;p=jFCPlib.git 🚸 Add more methods to PersistentPutDir Also, add a test. --- diff --git a/src/main/java/net/pterodactylus/fcp/PersistentPutDir.java b/src/main/java/net/pterodactylus/fcp/PersistentPutDir.java index d234c48..2cbb774 100644 --- a/src/main/java/net/pterodactylus/fcp/PersistentPutDir.java +++ b/src/main/java/net/pterodactylus/fcp/PersistentPutDir.java @@ -66,6 +66,20 @@ public class PersistentPutDir extends BaseMessage implements Identifiable { } /** + * Returns the persistence of the request. If the field value cannot be + * parsed into a {@link Persistence} values, {@code null} is returned. + * + * @return The persistence of the request, or {@code null} + */ + public Persistence getPersistence() { + try { + return Persistence.valueOf(getField("Persistence")); + } catch (IllegalArgumentException e) { + return null; + } + } + + /** * Returns the priority of the request. * * @return The priority of the request @@ -85,6 +99,24 @@ public class PersistentPutDir extends BaseMessage implements Identifiable { } /** + * Returns the type of the put dir command. + * + * @return “disk” or “complex” + */ + public String getPutDirType() { + return getField("PutDirType"); + } + + /** + * Returns the compatibility mode used for the insert. + * + * @return The name of the compatibility mode used for the insert + */ + public String getCompatibilityMode() { + return getField("CompatibilityMode"); + } + + /** * Returns the maximum number of retries for failed blocks. * * @return The maximum number of retries, or -1 for endless @@ -100,11 +132,7 @@ public class PersistentPutDir extends BaseMessage implements Identifiable { * @return The number of files in the request */ public int getFileCount() { - int fileCount = -1; - while (getField("Files." + ++fileCount + ".UploadFrom") != null) { - /* do nothing. */ - } - return fileCount; + return FcpUtils.safeParseInt(getField("Count"), 0); } /** diff --git a/src/test/java/net/pterodactylus/fcp/PersistentPutDirTest.java b/src/test/java/net/pterodactylus/fcp/PersistentPutDirTest.java new file mode 100644 index 0000000..b7eec06 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/PersistentPutDirTest.java @@ -0,0 +1,151 @@ +package net.pterodactylus.fcp; + +import org.hamcrest.Matcher; +import org.junit.Test; + +import java.util.function.Function; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + +public class PersistentPutDirTest { + + @Test + public void nameOfMessageIsRetrievedFromReceivedMessage() { + PersistentPutDir persistentPutDir = new PersistentPutDir(new FcpMessage("Test")); + assertThat(persistentPutDir.getName(), equalTo("Test")); + } + + @Test + public void identifierOfMessageIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Identifier", "TestRequest", PersistentPutDir::getIdentifier, equalTo("TestRequest")); + } + + @Test + public void uriOfMessageIsRetreivedFromReceivedMessage() { + createPersistentPutDirAndMatch("URI", "TestURI", PersistentPutDir::getURI, equalTo("TestURI")); + } + + @Test + public void verbosityOfMessageIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Verbosity", "999", persistentPutDir -> persistentPutDir.getVerbosity().toString(), equalTo("999")); + } + + @Test + public void verbosityNoneIsReturnedWhenVerbosityCannotBeParsed() { + createPersistentPutDirAndMatch("Verbosity", "Invalid", PersistentPutDir::getVerbosity, equalTo(Verbosity.NONE)); + } + + @Test + public void priorityOfMessageIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("PriorityClass", "2", PersistentPutDir::getPriority, equalTo(Priority.immediateSplitfile)); + } + + @Test + public void priorityUnknownIsReturnedWhenPriorityCannotBeParsed() { + createPersistentPutDirAndMatch("PriorityClass", "invalid", PersistentPutDir::getPriority, equalTo(Priority.unknown)); + } + + @Test + public void globalOfMessageIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Global", "true", PersistentPutDir::isGlobal, equalTo(true)); + } + + @Test + public void putDirTypeIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("PutDirType", "complex", PersistentPutDir::getPutDirType, equalTo("complex")); + } + + @Test + public void compatibilityModeIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("CompatibilityMode", "COMPAT_CURRENT", PersistentPutDir::getCompatibilityMode, equalTo("COMPAT_CURRENT")); + } + + @Test + public void maxRetriesOfMessageIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("MaxRetries", "3", PersistentPutDir::getMaxRetries, equalTo(3)); + } + + @Test + public void minusTwoIsReturnedWhenMaxRetriesCannotBeParsed() { + // I actually do not know anymore why -2 is returned… + createPersistentPutDirAndMatch("MaxRetries", "unknown", PersistentPutDir::getMaxRetries, equalTo(-2)); + } + + @Test + public void fileCountIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Count", "17", PersistentPutDir::getFileCount, equalTo(17)); + } + + @Test + public void fileNameOfFirstFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.0.Name", "first.txt", p -> p.getFileName(0), equalTo("first.txt")); + } + + @Test + public void fileNameOfSecondFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.1.Name", "second.txt", p -> p.getFileName(1), equalTo("second.txt")); + } + + @Test + public void fileLengthOfFirstFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.0.DataLength", "12345", p -> p.getFileDataLength(0), equalTo(12345L)); + } + + @Test + public void fileLengthOfThirdFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.2.DataLength", "23456", p -> p.getFileDataLength(2), equalTo(23456L)); + } + + @Test + public void uploadSourceOfFirstFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.0.UploadFrom", "disk", p -> p.getFileUploadFrom(0), equalTo(UploadFrom.disk)); + } + + @Test + public void uploadSourceOfFourthFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.3.UploadFrom", "direct", p -> p.getFileUploadFrom(3), equalTo(UploadFrom.direct)); + } + + @Test + public void uploadSourceOfFifthFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.4.UploadFrom", "redirect", p -> p.getFileUploadFrom(4), equalTo(UploadFrom.redirect)); + } + + @Test + public void contentTypeOfFirstFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.0.Metadata.ContentType", "text/plain", p -> p.getFileMetadataContentType(0), equalTo("text/plain")); + } + + @Test + public void contentTypeIsNullIfAFileDoesNotHaveAContentType() { + createPersistentPutDirAndMatch("Files.0.Metadata.ContentType", "text/plain", p -> p.getFileMetadataContentType(1), nullValue()); + } + + @Test + public void nameOfFirstOnDiskFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.0.Filename", "first.txt", p -> p.getFileFilename(0), equalTo("first.txt")); + } + + @Test + public void nameOfSecondOnDiskFileIsRetrievedFromReceivedMessage() { + createPersistentPutDirAndMatch("Files.1.Filename", "second.txt", p -> p.getFileFilename(1), equalTo("second.txt")); + } + + private static void createPersistentPutDirAndMatch(String field, String value, Function extractor, Matcher matcher) { + PersistentPutDir persistentPutDir = createPersistentPutDir(field, value); + assertThat(extractor.apply(persistentPutDir), matcher); + } + + private static PersistentPutDir createPersistentPutDir(String field, String value) { + return new PersistentPutDir(createReceivedMessage(field, value)); + } + + private static FcpMessage createReceivedMessage(String field, String value) { + FcpMessage receivedMessage = new FcpMessage("Test"); + receivedMessage.setField(field, value); + return receivedMessage; + } + +}