From: David ‘Bombe’ Roden Date: Wed, 4 Sep 2024 20:02:08 +0000 (+0200) Subject: ✅ Add test for getOpennetPeers() X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4505321ee7767d4cae2b06011269ad6e223b6ef8;p=jFCPlib.git ✅ Add test for getOpennetPeers() --- diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 4a85934..e808c5c 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -206,6 +206,31 @@ public class FcpClientTest { sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getDarknetPeers, false, false, anything(), contains(peerWithIdentity(equalTo("3")))); } + @Test + public void getOpennetPeersWithMetadataFlagSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getOpennetPeers, true, false, contains(hasField("WithMetadata", equalTo("true"))), anything()); + } + + @Test + public void getOpennetPeersWithMetadataFlagNotSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getOpennetPeers, false, false, contains(hasField("WithMetadata", equalTo("false"))), anything()); + } + + @Test + public void getOpennetPeersWithVolatileFlagSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getOpennetPeers, false, true, contains(hasField("WithVolatile", equalTo("true"))), anything()); + } + + @Test + public void getOpennetPeersWithVolatileFlagNotSetSendsCorrectMessage() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getOpennetPeers, false, false, contains(hasField("WithVolatile", equalTo("false"))), anything()); + } + + @Test + public void getOpennetPeersReturnsPeersWithCorrectIdentifier() throws Exception { + sendListPeersAndVerifySentMessagesAndReturnedPeers(FcpClientTest::getOpennetPeers, false, false, anything(), contains(peerWithIdentity(equalTo("1")))); + } + private static BiFunction> getPeers(FcpClient fcpClient) { return (withMetadata, withVolatile) -> { try { @@ -226,6 +251,16 @@ public class FcpClientTest { }; } + private static BiFunction> getOpennetPeers(FcpClient fcpClient) { + return (withMetadata, withVolatile) -> { + try { + return fcpClient.getOpennetPeers(withMetadata, withVolatile); + } catch (IOException | FcpException e) { + throw new RuntimeException(e); + } + }; + } + private static void sendListPeersAndVerifySentMessagesAndReturnedPeers(Function>> peerRetrieval, boolean withMetadataFlag, boolean withVolatileFlag, Matcher> sentMessagesMatcher, Matcher> peersMatcher) throws IOException, FcpException { List sentMessages = new ArrayList<>(); FcpConnection fcpConnection = createFcpConnection(message -> { @@ -235,7 +270,7 @@ public class FcpClientTest { String identifier = message.getField("Identifier"); listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "1").put("opennet", "true"))); listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", "Other Identifier").put("identity", "4"))); - listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "2").put("seed", "true"))); + listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "2").put("opennet", "true").put("seed", "true"))); listener.receivedEndListPeers(connection, new EndListPeers(new FcpMessage("EndListPeers").put("Identifier", "Other Identifier"))); listener.receivedPeer(connection, new Peer(new FcpMessage("Peer").put("Identifier", identifier).put("identity", "3"))); listener.receivedEndListPeers(connection, new EndListPeers(new FcpMessage("EndListPeers").put("Identifier", identifier)));