From 46f3a7abd9af0191c8a46a9f0722999683fceffb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 24 Nov 2024 16:34:26 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20tests=20for=20FCPPluginReply?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/fcp/FCPPluginReply.java | 16 +++-- .../net/pterodactylus/fcp/FCPPluginReplyTest.java | 68 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/FCPPluginReplyTest.java diff --git a/src/main/java/net/pterodactylus/fcp/FCPPluginReply.java b/src/main/java/net/pterodactylus/fcp/FCPPluginReply.java index 497a42f..2bdc73a 100644 --- a/src/main/java/net/pterodactylus/fcp/FCPPluginReply.java +++ b/src/main/java/net/pterodactylus/fcp/FCPPluginReply.java @@ -18,10 +18,12 @@ package net.pterodactylus.fcp; import java.io.InputStream; -import java.util.HashMap; +import java.util.AbstractMap; import java.util.Map; import java.util.Map.Entry; +import static java.util.stream.Collectors.toMap; + /** * The “FCPPluginReply” is sent by a plugin as a response to a * {@link FCPPluginMessage} message. @@ -94,14 +96,10 @@ public class FCPPluginReply extends BaseMessage implements Identifiable { * @return All replies from the plugin */ public Map getReplies() { - Map fields = getFields(); - Map replies = new HashMap(); - for (Entry field : fields.entrySet()) { - if (field.getKey().startsWith("Replies.")) { - replies.put(field.getKey().substring(8), field.getValue()); - } - } - return replies; + return getFields().entrySet().stream() + .filter(entry -> entry.getKey().startsWith("Replies.")) + .map(entry -> new AbstractMap.SimpleImmutableEntry<>(entry.getKey().substring(8), entry.getValue())) + .collect(toMap(Entry::getKey, Entry::getValue)); } /** diff --git a/src/test/java/net/pterodactylus/fcp/FCPPluginReplyTest.java b/src/test/java/net/pterodactylus/fcp/FCPPluginReplyTest.java new file mode 100644 index 0000000..984ef35 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/FCPPluginReplyTest.java @@ -0,0 +1,68 @@ +package net.pterodactylus.fcp; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; + +public class FCPPluginReplyTest { + + @Test + public void identifierOfBaseMessageIsReturned() { + baseMessage.setField("Identifier", "test identifier"); + FCPPluginReply reply = new FCPPluginReply(baseMessage, null); + assertThat(reply.getIdentifier(), equalTo("test identifier")); + } + + @Test + public void pluginNameOfBaseMessageIsReturned() { + baseMessage.setField("PluginName", "test plugin name"); + FCPPluginReply reply = new FCPPluginReply(baseMessage, null); + assertThat(reply.getPluginName(), equalTo("test plugin name")); + } + + @Test + public void dataLengthOfBaseMessageIsReturned() { + baseMessage.setField("DataLength", "1234"); + FCPPluginReply reply = new FCPPluginReply(baseMessage, null); + assertThat(reply.getDataLength(), equalTo(1234L)); + } + + @Test + public void replyFromBaseMessageIsReturned() { + baseMessage.setField("Replies.Test", "value"); + FCPPluginReply reply = new FCPPluginReply(baseMessage, null); + assertThat(reply.getReply("Test"), equalTo("value")); + } + + @Test + public void allRepliesFromBaseMessageAreReturned() { + baseMessage.setField("Replies.Field1", "value1"); + baseMessage.setField("Replies.Field2", "value2"); + baseMessage.setField("Field3", "value3"); + FCPPluginReply reply = new FCPPluginReply(baseMessage, null); + assertThat(reply.getReplies(), allOf(aMapWithSize(2), hasEntry("Field1", "value1"), hasEntry("Field2", "value2"))); + } + + @Test + public void inputStreamFromBaseMessageIsReturned() throws IOException { + FCPPluginReply reply = new FCPPluginReply(baseMessage, new ByteArrayInputStream("Hello World!".getBytes(UTF_8))); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + InputStream inputStream = reply.getPayloadInputStream()) { + FcpUtils.copy(inputStream, outputStream); + assertThat(outputStream.toByteArray(), equalTo("Hello World!".getBytes(UTF_8))); + } + } + + private final FcpMessage baseMessage = new FcpMessage("FCPPluginReply"); + +} -- 2.7.4