✅ Add tests for FCPPluginReply
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 24 Nov 2024 15:34:26 +0000 (16:34 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 24 Nov 2024 15:34:26 +0000 (16:34 +0100)
src/main/java/net/pterodactylus/fcp/FCPPluginReply.java
src/test/java/net/pterodactylus/fcp/FCPPluginReplyTest.java [new file with mode: 0644]

index 497a42f..2bdc73a 100644 (file)
 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<String, String> getReplies() {
-               Map<String, String> fields = getFields();
-               Map<String, String> replies = new HashMap<String, String>();
-               for (Entry<String, String> 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 (file)
index 0000000..984ef35
--- /dev/null
@@ -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");
+
+}