✨ Add SendBookmark message
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 6 Jan 2025 10:22:58 +0000 (11:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Mon, 6 Jan 2025 10:22:58 +0000 (11:22 +0100)
src/main/java/net/pterodactylus/fcp/SendBookmark.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/SendBookmarkTest.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/fcp/SendBookmark.java b/src/main/java/net/pterodactylus/fcp/SendBookmark.java
new file mode 100644 (file)
index 0000000..2266028
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.fcp;
+
+import java.io.ByteArrayInputStream;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+public class SendBookmark extends FcpMessage {
+
+       public SendBookmark(String identifier, String nodeIdentifier, String uri, String name) {
+               super("SendBookmark");
+               setField("Identifier", identifier);
+               setField("NodeIdentifier", nodeIdentifier);
+               setField("URI", uri);
+               setField("Name", name);
+       }
+
+       public void setDescription(String description) {
+               byte[] encodedDescription = description.getBytes(UTF_8);
+               setField("DataLength", String.valueOf(encodedDescription.length));
+               setPayloadInputStream(new ByteArrayInputStream(encodedDescription));
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/fcp/SendBookmarkTest.java b/src/test/java/net/pterodactylus/fcp/SendBookmarkTest.java
new file mode 100644 (file)
index 0000000..3520f5f
--- /dev/null
@@ -0,0 +1,62 @@
+package net.pterodactylus.fcp;
+
+import org.junit.Test;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static net.pterodactylus.fcp.test.InputStreamMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.nullValue;
+
+public class SendBookmarkTest {
+
+       @Test
+       public void sendBookmarkMessageHasCorrectName() {
+               assertThat(sendBookmark.getName(), equalTo("SendBookmark"));
+       }
+
+       @Test
+       public void sendBookmarkMessageSetsCorrectIdentifierField() {
+               assertThat(sendBookmark.getField("Identifier"), equalTo("identifier"));
+       }
+
+       @Test
+       public void sendBookmarkMessageSetCorrectNodeIdentifierField() {
+               assertThat(sendBookmark.getField("NodeIdentifier"), equalTo("node-identifier"));
+       }
+
+       @Test
+       public void sendBookmarkMessageSetsCorrectUriField() {
+               assertThat(sendBookmark.getField("URI"), equalTo("uri"));
+       }
+
+       @Test
+       public void sendBookmarkMessageSetsCorrectNameField() {
+               assertThat(sendBookmark.getField("Name"), equalTo("name"));
+       }
+
+       @Test
+       public void sendBookmarkMessageWithoutDescriptionDoesNotHaveDataLengthField() {
+               assertThat(sendBookmark.getField("DataLength"), nullValue());
+       }
+
+       @Test
+       public void sendBookmarkMessageWithoutDescriptionDoesNotHavePayload() {
+               assertThat(sendBookmark.getPayloadInputStream(), nullValue());
+       }
+
+       @Test
+       public void sendBookmarkMessageWithDescriptionSetsDataLengthFieldCorrectly() {
+               sendBookmark.setDescription("Hällo Wőrld");
+               assertThat(sendBookmark.getField("DataLength"), equalTo("13"));
+       }
+
+       @Test
+       public void sendBookmarkMessageWithDescriptionHasCorrectPayload() {
+               sendBookmark.setDescription("Hällo Wőrld");
+               assertThat(sendBookmark.getPayloadInputStream(), equalTo("Hällo Wőrld".getBytes(UTF_8)));
+       }
+
+       private final SendBookmark sendBookmark = new SendBookmark("identifier", "node-identifier", "uri", "name");
+
+}