From: David ‘Bombe’ Roden Date: Mon, 6 Jan 2025 10:22:58 +0000 (+0100) Subject: ✨ Add SendBookmark message X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=08e830c5c1d9e685c7d7740bbbbabb6faf1400be;p=jFCPlib.git ✨ Add SendBookmark message --- diff --git a/src/main/java/net/pterodactylus/fcp/SendBookmark.java b/src/main/java/net/pterodactylus/fcp/SendBookmark.java new file mode 100644 index 0000000..2266028 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/SendBookmark.java @@ -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 index 0000000..3520f5f --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/SendBookmarkTest.java @@ -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"); + +}