From 0376031ea129c2279f364d3e152990af58984d4a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 6 Sep 2024 14:27:15 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20tests=20for=20ModifyPeer,=20de?= =?utf8?q?precate=20and=20replace=20a=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/fcp/ModifyPeer.java | 9 +++ .../java/net/pterodactylus/fcp/ModifyPeerTest.java | 82 ++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/test/java/net/pterodactylus/fcp/ModifyPeerTest.java diff --git a/src/main/java/net/pterodactylus/fcp/ModifyPeer.java b/src/main/java/net/pterodactylus/fcp/ModifyPeer.java index 0105c09..25f0052 100644 --- a/src/main/java/net/pterodactylus/fcp/ModifyPeer.java +++ b/src/main/java/net/pterodactylus/fcp/ModifyPeer.java @@ -61,8 +61,17 @@ public class ModifyPeer extends FcpMessage { setField("IsBurstOnly", String.valueOf(burstOnly)); } + /** + * @param ignoreSource {@code true} to ignore the source port of the peer + * @deprecated Use {@link #setIgnoreSourcePort(boolean)} + */ + @Deprecated public void setIgnoreSource(boolean ignoreSource) { setField("IgnoreSourcePort", String.valueOf(ignoreSource)); } + public void setIgnoreSourcePort(boolean ignoreSourcePort) { + setField("IgnoreSourcePort", String.valueOf(ignoreSourcePort)); + } + } diff --git a/src/test/java/net/pterodactylus/fcp/ModifyPeerTest.java b/src/test/java/net/pterodactylus/fcp/ModifyPeerTest.java new file mode 100644 index 0000000..c8502f5 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/ModifyPeerTest.java @@ -0,0 +1,82 @@ +package net.pterodactylus.fcp; + +import org.hamcrest.Matcher; +import org.junit.Test; + +import java.util.function.BiConsumer; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; + +public class ModifyPeerTest { + + @Test + public void newModifyPeerMessageDoesNotContainAnyOfTheModifications() { + ModifyPeer modifyPeer = new ModifyPeer("identifier", "node-identifier"); + assertThat(modifyPeer.getFields(), allOf( + aMapWithSize(2), + hasEntry("Identifier", "identifier"), + hasEntry("NodeIdentifier", "node-identifier") + )); + } + + @Test + public void modifyPeerMessageWithEnabledTrueContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setEnabled, true, "IsDisabled", equalTo("false")); + } + + @Test + public void modifyPeerMessageWithEnabledFalseContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setEnabled, false, "IsDisabled", equalTo("true")); + } + + @Test + public void modifyPeerMessageWithAllowLocalAddressesTrueContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setAllowLocalAddresses, true, "AllowLocalAddresses", equalTo("true")); + } + + @Test + public void modifyPeerMessageWithAllowLocalAddressesFalseContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setAllowLocalAddresses, false, "AllowLocalAddresses", equalTo("false")); + } + + @Test + public void modifyPeerMessageWithListenOnlyTrueContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setListenOnly, true, "IsListenOnly", equalTo("true")); + } + + @Test + public void modifyPeerMessageWithListenOnlyFalseContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setListenOnly, false, "IsListenOnly", equalTo("false")); + } + + @Test + public void modifyPeerMessageWithBurstOnlyTrueContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setBurstOnly, true, "IsBurstOnly", equalTo("true")); + } + + @Test + public void modifyPeerMessageWithBurstOnlyFalseContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setBurstOnly, false, "IsBurstOnly", equalTo("false")); + } + + @Test + public void modifyPeerMessageWithIgnoreSourcePortTrueContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setIgnoreSourcePort, true, "IgnoreSourcePort", equalTo("true")); + } + + @Test + public void modifyPeerMessageWithIgnoreSourcePortFalseContainsCorrectField() { + setFeatureAndVerifyMessage(ModifyPeer::setIgnoreSourcePort, false, "IgnoreSourcePort", equalTo("false")); + } + + private void setFeatureAndVerifyMessage(BiConsumer featureSetter, boolean featureValue, String fieldName, Matcher fieldValue) { + ModifyPeer modifyPeer = new ModifyPeer("identifier", "node-identifier"); + featureSetter.accept(modifyPeer, featureValue); + assertThat(modifyPeer.getFields(), hasEntry(equalTo(fieldName), fieldValue)); + } + +} -- 2.7.4