From a395e37c0d5db8f766a128130f32ec13c33912e8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 24 Aug 2015 06:40:21 +0200 Subject: [PATCH] Refactor connection tests a bit more --- .../fcp/quelaton/DefaultFcpClientTest.java | 146 +++++++++------------ 1 file changed, 62 insertions(+), 84 deletions(-) diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index 428ee04..f95f9b2 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -115,67 +115,10 @@ public class DefaultFcpClientTest { .orElse(""); } - @Test - public void defaultFcpClientReusesConnection() throws InterruptedException, ExecutionException, IOException { - Future keyPair = fcpClient.generateKeypair().execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - fcpServer.writeLine( - "SSKKeypair", - "InsertURI=" + INSERT_URI + "", - "RequestURI=" + REQUEST_URI + "", - "Identifier=" + identifier, - "EndMessage" - ); - keyPair.get(); - keyPair = fcpClient.generateKeypair().execute(); - lines = fcpServer.collectUntil(is("EndMessage")); - identifier = extractIdentifier(lines); - fcpServer.writeLine( - "SSKKeypair", - "InsertURI=" + INSERT_URI + "", - "RequestURI=" + REQUEST_URI + "", - "Identifier=" + identifier, - "EndMessage" - ); - keyPair.get(); - } - - @Test - public void defaultFcpClientCanReconnectAfterConnectionHasBeenClosed() - throws InterruptedException, ExecutionException, IOException { - Future keyPair = fcpClient.generateKeypair().execute(); - connectNode(); - fcpServer.collectUntil(is("EndMessage")); - fcpServer.close(); - try { - keyPair.get(); - Assert.fail(); - } catch (ExecutionException e) { - } - keyPair = fcpClient.generateKeypair().execute(); - connectNode(); - List lines = fcpServer.collectUntil(is("EndMessage")); - String identifier = extractIdentifier(lines); - fcpServer.writeLine( - "SSKKeypair", - "InsertURI=" + INSERT_URI + "", - "RequestURI=" + REQUEST_URI + "", - "Identifier=" + identifier, - "EndMessage" - ); - keyPair.get(); - } - private Matcher> matchesFcpMessage(String name, String... requiredLines) { return matchesFcpMessageWithTerminator(name, "EndMessage", requiredLines); } - private Matcher> matchesDataMessage(String name, String... requiredLines) { - return matchesFcpMessageWithTerminator(name, "Data", requiredLines); - } - private Matcher> hasHead(String firstElement) { return new TypeSafeDiagnosingMatcher>() { @Override @@ -914,39 +857,74 @@ public class DefaultFcpClientTest { assertThat(lines, requestMatcher.get()); } - public class Connections { + public class ConnectionsAndKeyPairs { - @Test(expected = ExecutionException.class) - public void throwsExceptionOnFailure() throws IOException, ExecutionException, InterruptedException { - Future keyPairFuture = fcpClient.generateKeypair().execute(); - connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); - fcpServer.writeLine( - "CloseConnectionDuplicateClientName", - "EndMessage" - ); - keyPairFuture.get(); - } + public class Connections { + + @Test(expected = ExecutionException.class) + public void throwsExceptionOnFailure() throws IOException, ExecutionException, InterruptedException { + Future keyPairFuture = fcpClient.generateKeypair().execute(); + connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); + fcpServer.writeLine( + "CloseConnectionDuplicateClientName", + "EndMessage" + ); + keyPairFuture.get(); + } + + @Test(expected = ExecutionException.class) + public void throwsExceptionIfConnectionIsClosed() throws IOException, ExecutionException, InterruptedException { + Future keyPairFuture = fcpClient.generateKeypair().execute(); + connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); + fcpServer.close(); + keyPairFuture.get(); + } + + @Test + public void connectionIsReused() throws InterruptedException, ExecutionException, IOException { + Future keyPair = fcpClient.generateKeypair().execute(); + connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); + replyWithKeyPair(); + keyPair.get(); + keyPair = fcpClient.generateKeypair().execute(); + readMessage(() -> matchesFcpMessage("GenerateSSK")); + identifier = extractIdentifier(lines); + replyWithKeyPair(); + keyPair.get(); + } + + @Test + public void defaultFcpClientCanReconnectAfterConnectionHasBeenClosed() + throws InterruptedException, ExecutionException, IOException { + Future keyPair = fcpClient.generateKeypair().execute(); + connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); + fcpServer.close(); + try { + keyPair.get(); + Assert.fail(); + } catch (ExecutionException e) { + } + keyPair = fcpClient.generateKeypair().execute(); + connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); + replyWithKeyPair(); + keyPair.get(); + } - @Test(expected = ExecutionException.class) - public void throwsExceptionIfConnectionIsClosed() throws IOException, ExecutionException, InterruptedException { - Future keyPairFuture = fcpClient.generateKeypair().execute(); - connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); - fcpServer.close(); - keyPairFuture.get(); } - } + public class GenerateKeyPair { - public class GenerateKeyPair { + @Test + public void defaultFcpClientCanGenerateKeypair() + throws ExecutionException, InterruptedException, IOException { + Future keyPairFuture = fcpClient.generateKeypair().execute(); + connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); + replyWithKeyPair(); + FcpKeyPair keyPair = keyPairFuture.get(); + assertThat(keyPair.getPublicKey(), is(REQUEST_URI)); + assertThat(keyPair.getPrivateKey(), is(INSERT_URI)); + } - @Test - public void defaultFcpClientCanGenerateKeypair() throws ExecutionException, InterruptedException, IOException { - Future keyPairFuture = fcpClient.generateKeypair().execute(); - connectAndAssert(() -> matchesFcpMessage("GenerateSSK")); - replyWithKeyPair(); - FcpKeyPair keyPair = keyPairFuture.get(); - assertThat(keyPair.getPublicKey(), is(REQUEST_URI)); - assertThat(keyPair.getPrivateKey(), is(INSERT_URI)); } private void replyWithKeyPair() throws IOException { -- 2.7.4