From 88196de42bde3e3a420c40ea78ad8057dab29363 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 14 Oct 2015 21:40:14 +0200 Subject: [PATCH] Refactor GetNode command tests --- .../fcp/quelaton/DefaultFcpClientTest.java | 177 ++++++++------------- 1 file changed, 65 insertions(+), 112 deletions(-) diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index ed416e2..f7823d6 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -196,118 +196,6 @@ public class DefaultFcpClientTest { }; } - @Test - public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException { - Future nodeData = fcpClient.getNode().execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "GetNode", - "Identifier=" + identifier, - "GiveOpennetRef=false", - "WithPrivate=false", - "WithVolatile=false" - )); - fcpServer.writeLine( - "NodeData", - "Identifier=" + identifier, - "ark.pubURI=SSK@3YEf.../ark", - "ark.number=78", - "auth.negTypes=2", - "version=Fred,0.7,1.0,1466", - "lastGoodVersion=Fred,0.7,1.0,1466", - "EndMessage" - ); - assertThat(nodeData.get(), notNullValue()); - } - - @Test - public void defaultFcpClientCanGetNodeInformationWithOpennetRef() - throws InterruptedException, ExecutionException, IOException { - Future nodeData = fcpClient.getNode().opennetRef().execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "GetNode", - "Identifier=" + identifier, - "GiveOpennetRef=true", - "WithPrivate=false", - "WithVolatile=false" - )); - fcpServer.writeLine( - "NodeData", - "Identifier=" + identifier, - "opennet=true", - "ark.pubURI=SSK@3YEf.../ark", - "ark.number=78", - "auth.negTypes=2", - "version=Fred,0.7,1.0,1466", - "lastGoodVersion=Fred,0.7,1.0,1466", - "EndMessage" - ); - assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466")); - } - - @Test - public void defaultFcpClientCanGetNodeInformationWithPrivateData() - throws InterruptedException, ExecutionException, IOException { - Future nodeData = fcpClient.getNode().includePrivate().execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "GetNode", - "Identifier=" + identifier, - "GiveOpennetRef=false", - "WithPrivate=true", - "WithVolatile=false" - )); - fcpServer.writeLine( - "NodeData", - "Identifier=" + identifier, - "opennet=false", - "ark.pubURI=SSK@3YEf.../ark", - "ark.number=78", - "auth.negTypes=2", - "version=Fred,0.7,1.0,1466", - "lastGoodVersion=Fred,0.7,1.0,1466", - "ark.privURI=SSK@XdHMiRl", - "EndMessage" - ); - assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl")); - } - - @Test - public void defaultFcpClientCanGetNodeInformationWithVolatileData() - throws InterruptedException, ExecutionException, IOException { - Future nodeData = fcpClient.getNode().includeVolatile().execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - assertThat(lines, matchesFcpMessage( - "GetNode", - "Identifier=" + identifier, - "GiveOpennetRef=false", - "WithPrivate=false", - "WithVolatile=true" - )); - fcpServer.writeLine( - "NodeData", - "Identifier=" + identifier, - "opennet=false", - "ark.pubURI=SSK@3YEf.../ark", - "ark.number=78", - "auth.negTypes=2", - "version=Fred,0.7,1.0,1466", - "lastGoodVersion=Fred,0.7,1.0,1466", - "volatile.freeJavaMemory=205706528", - "EndMessage" - ); - assertThat(nodeData.get().getVolatile("freeJavaMemory"), is("205706528")); - } - private List lines; private String identifier; @@ -1833,4 +1721,69 @@ public class DefaultFcpClientTest { } + public class NodeInformation { + + @Test + public void defaultFcpClientCanGetNodeInformation() throws InterruptedException, ExecutionException, IOException { + Future nodeData = fcpClient.getNode().execute(); + connectAndAssert(() -> matchesGetNode(false, false, false)); + replyWithNodeData(); + assertThat(nodeData.get(), notNullValue()); + assertThat(nodeData.get().getNodeRef().isOpennet(), is(false)); + } + + @Test + public void defaultFcpClientCanGetNodeInformationWithOpennetRef() + throws InterruptedException, ExecutionException, IOException { + Future nodeData = fcpClient.getNode().opennetRef().execute(); + connectAndAssert(() -> matchesGetNode(true, false, false)); + replyWithNodeData("opennet=true"); + assertThat(nodeData.get().getVersion().toString(), is("Fred,0.7,1.0,1466")); + assertThat(nodeData.get().getNodeRef().isOpennet(), is(true)); + } + + @Test + public void defaultFcpClientCanGetNodeInformationWithPrivateData() + throws InterruptedException, ExecutionException, IOException { + Future nodeData = fcpClient.getNode().includePrivate().execute(); + connectAndAssert(() -> matchesGetNode(false, true, false)); + replyWithNodeData("ark.privURI=SSK@XdHMiRl"); + assertThat(nodeData.get().getARK().getPrivateURI(), is("SSK@XdHMiRl")); + } + + @Test + public void defaultFcpClientCanGetNodeInformationWithVolatileData() + throws InterruptedException, ExecutionException, IOException { + Future nodeData = fcpClient.getNode().includeVolatile().execute(); + connectAndAssert(() -> matchesGetNode(false, false, true)); + replyWithNodeData("volatile.freeJavaMemory=205706528"); + assertThat(nodeData.get().getVolatile("freeJavaMemory"), is("205706528")); + } + + private Matcher> matchesGetNode(boolean withOpennetRef, boolean withPrivate, boolean withVolatile) { + return matchesFcpMessage( + "GetNode", + "Identifier=" + identifier, + "GiveOpennetRef=" + withOpennetRef, + "WithPrivate=" + withPrivate, + "WithVolatile=" + withVolatile + ); + } + + private void replyWithNodeData(String... additionalLines) throws IOException { + fcpServer.writeLine( + "NodeData", + "Identifier=" + identifier, + "ark.pubURI=SSK@3YEf.../ark", + "ark.number=78", + "auth.negTypes=2", + "version=Fred,0.7,1.0,1466", + "lastGoodVersion=Fred,0.7,1.0,1466" + ); + fcpServer.writeLine(additionalLines); + fcpServer.writeLine("EndMessage"); + } + + } + } -- 2.7.4