From 32b4890844aa8644668d3e2a3b7aa0b2d83df631 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 6 Sep 2024 13:17:38 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Move=20Peer=20creation=20in?= =?utf8?q?to=20its=20own=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/test/java/net/pterodactylus/fcp/PeerTest.java | 46 +++---------------- .../java/net/pterodactylus/fcp/test/Peers.java | 53 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/test/Peers.java diff --git a/src/test/java/net/pterodactylus/fcp/PeerTest.java b/src/test/java/net/pterodactylus/fcp/PeerTest.java index ceb0c30..382e087 100644 --- a/src/test/java/net/pterodactylus/fcp/PeerTest.java +++ b/src/test/java/net/pterodactylus/fcp/PeerTest.java @@ -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 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 index 0000000..1e0c8c3 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/Peers.java @@ -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 addVolatileFields() { + return peerMessage -> { + peerMessage.setField("volatile.a", "A1"); + peerMessage.setField("volatile.b", "B2"); + peerMessage.setField("volatile.c", "C3"); + }; + } + + public static Consumer addMetadataFields() { + return peerMessage -> { + peerMessage.setField("metadata.a", "MA1"); + peerMessage.setField("metadata.b", "MB2"); + peerMessage.setField("metadata.c", "MC3"); + }; + } + + public static Peer createPeer(Consumer 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); + } + +} -- 2.7.4