From: David ‘Bombe’ Roden Date: Wed, 8 Feb 2023 07:51:00 +0000 (+0100) Subject: ✅ Add tests for “AddPeer” X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cd6e14bf7d54abc28b28979ee28f13d378576e7a;p=jFCPlib.git ✅ Add tests for “AddPeer” This also added a couple of FIXMEs because I either need to set a default for both trust and visibility, or I need to extend the parameter list of the methods that create a new AddPeer command. --- diff --git a/src/main/java/net/pterodactylus/fcp/AddPeer.java b/src/main/java/net/pterodactylus/fcp/AddPeer.java index 45a5937..c2a0555 100644 --- a/src/main/java/net/pterodactylus/fcp/AddPeer.java +++ b/src/main/java/net/pterodactylus/fcp/AddPeer.java @@ -27,10 +27,22 @@ import java.net.URL; public class AddPeer extends FcpMessage { /** + * Represents the trust values for a peer. + */ + public enum Trust {LOW, NORMAL, HIGH} + + /** + * Represents the visibility values for a peer. + */ + public enum Visibility {NO, NAME_ONLY, YES} + + /** * Creates a new “AddPeer” request. */ - private AddPeer() { + private AddPeer(Trust trust, Visibility visibility) { super("AddPeer"); + setField("Trust", trust.name()); + setField("Visibility", visibility.name()); } /** @@ -40,13 +52,13 @@ public class AddPeer extends FcpMessage { * @param file * The file to read the noderef from */ - public AddPeer(String file) { - this(); + public AddPeer(Trust trust, Visibility visibility, String file) { + this(trust, visibility); setField("File", file); } - public AddPeer(String identifier, String file) { - this(file); + public AddPeer(Trust trust, Visibility visibility, String identifier, String file) { + this(trust, visibility, file); setField("Identifier", identifier); } @@ -57,13 +69,13 @@ public class AddPeer extends FcpMessage { * @param url * The URL to read the noderef from */ - public AddPeer(URL url) { - this(); + public AddPeer(Trust trust, Visibility visibility, URL url) { + this(trust, visibility); setField("URL", String.valueOf(url)); } - public AddPeer(String identifier, URL url) { - this(url); + public AddPeer(Trust trust, Visibility visibility, String identifier, URL url) { + this(trust, visibility, url); setField("Identifier", identifier); } @@ -73,13 +85,13 @@ public class AddPeer extends FcpMessage { * @param nodeRef * The noderef of the peer */ - public AddPeer(NodeRef nodeRef) { - this(); + public AddPeer(Trust trust, Visibility visibility, NodeRef nodeRef) { + this(trust, visibility); setNodeRef(nodeRef); } - public AddPeer(String identifier, NodeRef nodeRef) { - this(nodeRef); + public AddPeer(Trust trust, Visibility visibility, String identifier, NodeRef nodeRef) { + this(trust, visibility, nodeRef); setField("Identifier", identifier); } diff --git a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java index ced9995..2f25ddd 100644 --- a/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -583,7 +583,7 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public void addPeer(NodeRef nodeRef) throws IOException, FcpException { - addPeer(new AddPeer(nodeRef)); + addPeer(new AddPeer(null, null, nodeRef)); // FIXME – add parameters } /** @@ -597,7 +597,7 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public void addPeer(URL url) throws IOException, FcpException { - addPeer(new AddPeer(url)); + addPeer(new AddPeer(null, null, url)); // FIXME – add parameters } /** @@ -613,7 +613,7 @@ public class FcpClient implements Closeable { * if an FCP error occurs */ public void addPeer(String file) throws IOException, FcpException { - addPeer(new AddPeer(file)); + addPeer(new AddPeer(null, null, file)); // FIXME – add parameters } /** diff --git a/src/test/java/net/pterodactylus/fcp/AddPeerTest.java b/src/test/java/net/pterodactylus/fcp/AddPeerTest.java new file mode 100644 index 0000000..154d8d0 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/AddPeerTest.java @@ -0,0 +1,174 @@ +package net.pterodactylus.fcp; + +import java.net.URL; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.junit.Test; + +import static net.pterodactylus.fcp.AddPeer.Trust.HIGH; +import static net.pterodactylus.fcp.AddPeer.Trust.LOW; +import static net.pterodactylus.fcp.AddPeer.Trust.NORMAL; +import static net.pterodactylus.fcp.AddPeer.Visibility.NAME_ONLY; +import static net.pterodactylus.fcp.AddPeer.Visibility.NO; +import static net.pterodactylus.fcp.AddPeer.Visibility.YES; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; + +/** + * Unit test for {@link AddPeer}. + */ +public class AddPeerTest extends AbstractFcpMessageTest { + + @Test + public void canCreateAddPeerWithFile() throws Exception { + AddPeer addPeer = new AddPeer(LOW, NO, "file"); + assertThat(encodeMessage(addPeer), isMessage("AddPeer", + "File=file", + "Trust=LOW", + "Visibility=NO" + )); + } + + @Test + public void canCreateAddPeerWithIdentifierAndFile() throws Exception { + AddPeer addPeer = new AddPeer(LOW, NO, "identifier", "file"); + assertThat(encodeMessage(addPeer), isMessage("AddPeer", + "File=file", + "Identifier=identifier", + "Trust=LOW", + "Visibility=NO" + )); + } + + @Test + public void canCreateAddPeerWithUrl() throws Exception { + AddPeer addPeer = new AddPeer(NORMAL, NAME_ONLY, new URL("http://url")); + assertThat(encodeMessage(addPeer), isMessage("AddPeer", + "URL=http://url", + "Trust=NORMAL", + "Visibility=NAME_ONLY" + )); + } + + @Test + public void canCreateAddPeerWithIdentifierAndUrl() throws Exception { + AddPeer addPeer = new AddPeer(NORMAL, NAME_ONLY, "identifier", new URL("http://url")); + assertThat(encodeMessage(addPeer), isMessage("AddPeer", + "URL=http://url", + "Identifier=identifier", + "Trust=NORMAL", + "Visibility=NAME_ONLY" + )); + } + + @Test + public void canCreateAddPeerWithNodeRef() throws Exception { + NodeRef nodeRef = createNodeRef(); + AddPeer addPeer = new AddPeer(HIGH, YES, nodeRef); + assertThat(encodeMessage(addPeer), isMessage("AddPeer", + "lastGoodVersion=test,1.2.3,4.5,678", + "opennet=true", + "identity=identity", + "myName=test-node", + "location=0.1234", + "testnet=false", + "version=test,2.3.4,5.6,789", + "physical.udp=10.11.12.13", + "ark.pubURI=ark-public-uri", + "ark.number=1", + "dsaPubKey.y=dsa-public-key", + "dsaGroup.g=dsa-base", + "dsaGroup.p=dsa-prime", + "dsaGroup.q=dsa-subprime", + "auth.negTypes=1;2;3", + "sig=signature", + "Trust=HIGH", + "Visibility=YES" + )); + } + + @Test + public void canCreateAddPeerWithIdentifierAndNodeRef() throws Exception { + NodeRef nodeRef = createNodeRef(); + AddPeer addPeer = new AddPeer(HIGH, YES, "identifier", nodeRef); + assertThat(encodeMessage(addPeer), isMessage("AddPeer", + "Identifier=identifier", + "lastGoodVersion=test,1.2.3,4.5,678", + "opennet=true", + "identity=identity", + "myName=test-node", + "location=0.1234", + "testnet=false", + "version=test,2.3.4,5.6,789", + "physical.udp=10.11.12.13", + "ark.pubURI=ark-public-uri", + "ark.number=1", + "dsaPubKey.y=dsa-public-key", + "dsaGroup.g=dsa-base", + "dsaGroup.p=dsa-prime", + "dsaGroup.q=dsa-subprime", + "auth.negTypes=1;2;3", + "sig=signature", + "Trust=HIGH", + "Visibility=YES" + )); + } + + private static NodeRef createNodeRef() { + NodeRef nodeRef = new NodeRef(); + nodeRef.setLastGoodVersion(new Version("test,1.2.3,4.5,678")); + nodeRef.setOpennet(true); + nodeRef.setIdentity("identity"); + nodeRef.setName("test-node"); + nodeRef.setLocation(0.1234); + nodeRef.setTestnet(false); + nodeRef.setVersion(new Version("test,2.3.4,5.6,789")); + nodeRef.setPhysicalUDP("10.11.12.13"); + nodeRef.setARK(new ARK("ark-public-uri", "1")); + nodeRef.setDSAPublicKey("dsa-public-key"); + nodeRef.setDSAGroup(new DSAGroup("dsa-base", "dsa-prime", "dsa-subprime")); + nodeRef.setNegotiationTypes(new int[]{1, 2, 3}); + nodeRef.setSignature("signature"); + return nodeRef; + } + + private static Matcher> isMessage(String name, String... lines) { + return new TypeSafeDiagnosingMatcher>() { + @Override + protected boolean matchesSafely(Iterable encodedMessage, Description mismatchDescription) { + Iterator messageLines = encodedMessage.iterator(); + String messageName = messageLines.next(); + if (!messageName.equals(name)) { + mismatchDescription.appendText("name is ").appendValue(messageName); + return false; + } + List parameterLines = new ArrayList<>(); + while (true) { + String actualLine = messageLines.next(); + if (actualLine.contains("=")) { + parameterLines.add(actualLine); + } else { + if (!containsInAnyOrder(lines).matches(parameterLines)) { + containsInAnyOrder(lines).describeMismatch(parameterLines, mismatchDescription); + return false; + } + break; + } + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is message named ").appendValue(name) + .appendText(" with parameters ").appendValue(lines); + } + }; + } + +}