♻️ Move Peer creation into its own class
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 11:17:38 +0000 (13:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 6 Sep 2024 11:17:38 +0000 (13:17 +0200)
src/test/java/net/pterodactylus/fcp/PeerTest.java
src/test/java/net/pterodactylus/fcp/test/Peers.java [new file with mode: 0644]

index ceb0c30..382e087 100644 (file)
@@ -2,10 +2,11 @@ package net.pterodactylus.fcp;
 
 import org.junit.Test;
 
-import java.util.function.Consumer;
-
 import static net.pterodactylus.fcp.test.ArkMatchers.isArk;
 import static net.pterodactylus.fcp.test.DsaGroupMatchers.isDsaGroup;
+import static net.pterodactylus.fcp.test.Peers.addMetadataFields;
+import static net.pterodactylus.fcp.test.Peers.addVolatileFields;
+import static net.pterodactylus.fcp.test.Peers.createPeer;
 import static net.pterodactylus.fcp.test.VersionMatchers.isVersion;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.aMapWithSize;
@@ -133,7 +134,7 @@ public class PeerTest {
 
        @Test
        public void peerReturnsCorrectVolatileFields() {
-               assertThat(peer.getVolatileFields(), allOf(
+               assertThat(createPeer(addVolatileFields()).getVolatileFields(), allOf(
                                aMapWithSize(3),
                                hasEntry("volatile.a", "A1"),
                                hasEntry("volatile.b", "B2"),
@@ -143,12 +144,12 @@ public class PeerTest {
 
        @Test
        public void peerReturnsCorrectVolatileField() {
-               assertThat(peer.getVolatile("a"), equalTo("A1"));
+               assertThat(createPeer(addVolatileFields()).getVolatile("a"), equalTo("A1"));
        }
 
        @Test
        public void peerReturnsCorrectMetadataFields() {
-               assertThat(peer.getMetadataFields(), allOf(
+               assertThat(createPeer(addMetadataFields()).getMetadataFields(), allOf(
                                aMapWithSize(3),
                                hasEntry("metadata.a", "MA1"),
                                hasEntry("metadata.b", "MB2"),
@@ -158,40 +159,7 @@ public class PeerTest {
 
        @Test
        public void peerReturnsCorrectMetadataField() {
-               assertThat(peer.getMetadata("a"), equalTo("MA1"));
-       }
-
-       private static Peer createPeer() {
-               return createPeer(m -> {
-               });
-       }
-
-       private static Peer createPeer(Consumer<FcpMessage> messageModifier) {
-               FcpMessage peerMessage = new FcpMessage("Peer");
-               peerMessage.setField("Identifier", "identifier");
-               peerMessage.setField("ark.number", "123");
-               peerMessage.setField("ark.privURI", "ark-private-uri");
-               peerMessage.setField("ark.pubURI", "ark-public-uri");
-               peerMessage.setField("auth.negTypes", "1;2;3");
-               peerMessage.setField("dsaGroup.g", "dsa-group-base");
-               peerMessage.setField("dsaGroup.p", "dsa-group-prime");
-               peerMessage.setField("dsaGroup.q", "dsa-group-subprime");
-               peerMessage.setField("dsaPubKey.y", "dsa-public-key");
-               peerMessage.setField("identity", "identity");
-               peerMessage.setField("lastGoodVersion", "Node,0.1.2,1.2.3,234");
-               peerMessage.setField("location", "0.4");
-               peerMessage.setField("myName", "Test Node");
-               peerMessage.setField("physical.udp", "physical-udp");
-               peerMessage.setField("sig", "signature");
-               peerMessage.setField("version", "TestNode,1.2.3,2.3.4,345");
-               peerMessage.setField("volatile.a", "A1");
-               peerMessage.setField("volatile.b", "B2");
-               peerMessage.setField("volatile.c", "C3");
-               peerMessage.setField("metadata.a", "MA1");
-               peerMessage.setField("metadata.b", "MB2");
-               peerMessage.setField("metadata.c", "MC3");
-               messageModifier.accept(peerMessage);
-               return new Peer(peerMessage);
+               assertThat(createPeer(addMetadataFields()).getMetadata("a"), equalTo("MA1"));
        }
 
        private final Peer peer = createPeer();
diff --git a/src/test/java/net/pterodactylus/fcp/test/Peers.java b/src/test/java/net/pterodactylus/fcp/test/Peers.java
new file mode 100644 (file)
index 0000000..1e0c8c3
--- /dev/null
@@ -0,0 +1,53 @@
+package net.pterodactylus.fcp.test;
+
+import net.pterodactylus.fcp.FcpMessage;
+import net.pterodactylus.fcp.Peer;
+
+import java.util.function.Consumer;
+
+public class Peers {
+
+       public static Peer createPeer() {
+               return createPeer(m -> {
+               });
+       }
+
+       public static Consumer<FcpMessage> addVolatileFields() {
+               return peerMessage -> {
+                       peerMessage.setField("volatile.a", "A1");
+                       peerMessage.setField("volatile.b", "B2");
+                       peerMessage.setField("volatile.c", "C3");
+               };
+       }
+
+       public static Consumer<FcpMessage> addMetadataFields() {
+               return peerMessage -> {
+                       peerMessage.setField("metadata.a", "MA1");
+                       peerMessage.setField("metadata.b", "MB2");
+                       peerMessage.setField("metadata.c", "MC3");
+               };
+       }
+
+       public static Peer createPeer(Consumer<FcpMessage> messageModifier) {
+               FcpMessage peerMessage = new FcpMessage("Peer");
+               peerMessage.setField("Identifier", "identifier");
+               peerMessage.setField("ark.number", "123");
+               peerMessage.setField("ark.privURI", "ark-private-uri");
+               peerMessage.setField("ark.pubURI", "ark-public-uri");
+               peerMessage.setField("auth.negTypes", "1;2;3");
+               peerMessage.setField("dsaGroup.g", "dsa-group-base");
+               peerMessage.setField("dsaGroup.p", "dsa-group-prime");
+               peerMessage.setField("dsaGroup.q", "dsa-group-subprime");
+               peerMessage.setField("dsaPubKey.y", "dsa-public-key");
+               peerMessage.setField("identity", "identity");
+               peerMessage.setField("lastGoodVersion", "Node,0.1.2,1.2.3,234");
+               peerMessage.setField("location", "0.4");
+               peerMessage.setField("myName", "Test Node");
+               peerMessage.setField("physical.udp", "physical-udp");
+               peerMessage.setField("sig", "signature");
+               peerMessage.setField("version", "TestNode,1.2.3,2.3.4,345");
+               messageModifier.accept(peerMessage);
+               return new Peer(peerMessage);
+       }
+
+}