From: David ‘Bombe’ Roden Date: Fri, 6 Sep 2024 12:27:15 +0000 (+0200) Subject: ✅ Add tests for ModifyPeer, deprecate and replace a method X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=0376031ea129c2279f364d3e152990af58984d4a;p=jFCPlib.git ✅ Add tests for ModifyPeer, deprecate and replace a method --- 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)); + } + +}