✅ Add test for “AllData”
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 10 Feb 2023 07:49:55 +0000 (08:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 10 Feb 2023 07:49:55 +0000 (08:49 +0100)
Also, make a member variable final.

src/main/java/net/pterodactylus/fcp/AllData.java
src/test/java/net/pterodactylus/fcp/AllDataTest.java [new file with mode: 0644]

index 9c85c9a..82931db 100644 (file)
@@ -33,7 +33,7 @@ import java.io.InputStream;
 public class AllData extends BaseMessage implements Identifiable {
 
        /** The payload. */
-       private InputStream payloadInputStream;
+       private final InputStream payloadInputStream;
 
        /**
         * Creates an “AllData” message that wraps the received message.
diff --git a/src/test/java/net/pterodactylus/fcp/AllDataTest.java b/src/test/java/net/pterodactylus/fcp/AllDataTest.java
new file mode 100644 (file)
index 0000000..6ec8812
--- /dev/null
@@ -0,0 +1,78 @@
+package net.pterodactylus.fcp;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import com.google.common.io.ByteStreams;
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+
+/**
+ * Unit test for {@link AllData}
+ */
+public class AllDataTest {
+
+       @Test
+       public void allDataReturnsIdentifierFromFcpMessage() {
+               receivedMessage.setField("Identifier", "identifier");
+               assertThat(allData.getIdentifier(), equalTo("identifier"));
+       }
+
+       @Test
+       public void allDataCanParseTheDataLengthFromTheFcpMessage() {
+               receivedMessage.setField("DataLength", "1234567");
+               assertThat(allData.getDataLength(), equalTo(1234567L));
+       }
+
+       @Test
+       public void allDataReturnsMinus1ForUnparseableDataLength() {
+               receivedMessage.setField("DataLength", "broken");
+               assertThat(allData.getDataLength(), equalTo(-1L));
+       }
+
+       @Test
+       public void allDataCanParseStartupTimeFromFcpMessage() {
+               receivedMessage.setField("StartupTime", "1234567");
+               assertThat(allData.getStartupTime(), equalTo(1234567L));
+       }
+
+       @Test
+       public void allDataReturnsMinus1ForUnparseableStartupTime() {
+               receivedMessage.setField("StartupTime", "broken");
+               assertThat(allData.getStartupTime(), equalTo(-1L));
+       }
+
+       @Test
+       public void allDataCanParseCompletionTimeFromFcpMessage() {
+               receivedMessage.setField("CompletionTime", "1234567");
+               assertThat(allData.getCompletionTime(), equalTo(1234567L));
+       }
+
+       @Test
+       public void allDataReturnsMinus1ForUnparseableCompletionTime() {
+               receivedMessage.setField("CompletionTime", "broken");
+               assertThat(allData.getCompletionTime(), equalTo(-1L));
+       }
+
+       @Test
+       public void allDataReturnsContentTypeFromFcpMessage() {
+               receivedMessage.setField("Metadata.ContentType", "application/test");
+               assertThat(allData.getContentType(), equalTo("application/test"));
+       }
+
+       @Test
+       public void allDataReturnsGivenPayloadInputStream() throws IOException {
+               byte[] payload = new byte[4];
+               assertThat(allData.getPayloadInputStream().read(payload), equalTo(4));
+               assertThat(payload, equalTo(new byte[] { 0, 1, 2, 3 }));
+               assertThat(allData.getPayloadInputStream().read(), equalTo(-1));
+       }
+
+       private final FcpMessage receivedMessage = new FcpMessage("AllData");
+       private final AllData allData = new AllData(receivedMessage, new ByteArrayInputStream(new byte[] { 0, 1, 2, 3 }));
+
+}