From 8f35cd09d399a05dca296bb3c37368bc88cce228 Mon Sep 17 00:00:00 2001 From: zargess Date: Sun, 2 Feb 2025 22:14:00 +0100 Subject: [PATCH] * Improved conversation name creator * Improved names of encryption tests * Added trello task link to todo in inputworker --- fagiServer/src/main/java/com/fagi/model/Data.java | 12 +++++++++--- .../src/main/java/com/fagi/worker/InputWorker.java | 1 + .../java/com/fagi/encryption/EncryptionTests.java | 12 ++++++------ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/fagiServer/src/main/java/com/fagi/model/Data.java b/fagiServer/src/main/java/com/fagi/model/Data.java index bb9bb8f..9ff8d6d 100644 --- a/fagiServer/src/main/java/com/fagi/model/Data.java +++ b/fagiServer/src/main/java/com/fagi/model/Data.java @@ -20,7 +20,9 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.StringJoiner; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -47,10 +49,14 @@ public synchronized Conversation createConversation(List participants) { .reduce( "", (a, b) -> { - if (!a.isEmpty()) { - return a + ", " + b; + var joiner = new StringJoiner(", "); + if (Objects.nonNull(a) && !a.isEmpty()) { + joiner.add(a); } - return b; + if (Objects.nonNull(b) && !b.isEmpty()) { + joiner.add(b); + } + return joiner.toString(); } ); diff --git a/fagiServer/src/main/java/com/fagi/worker/InputWorker.java b/fagiServer/src/main/java/com/fagi/worker/InputWorker.java index 41ce1a8..d6b1ec2 100644 --- a/fagiServer/src/main/java/com/fagi/worker/InputWorker.java +++ b/fagiServer/src/main/java/com/fagi/worker/InputWorker.java @@ -37,6 +37,7 @@ public InputWorker( Data data) throws IOException { this.data = data; // TODO: This sysout does not make sense. Should be in the run method or where the thread is started. + // https://trello.com/c/SVazRIgj/58-inputworker-should-not-print-starting-an-input-thread-in-its-constructor System.out.println("Starting an input thread"); objIn = new ObjectInputStream(socket.getInputStream()); this.out = out; diff --git a/fagiServer/src/test/java/com/fagi/encryption/EncryptionTests.java b/fagiServer/src/test/java/com/fagi/encryption/EncryptionTests.java index cb94a1f..109e302 100644 --- a/fagiServer/src/test/java/com/fagi/encryption/EncryptionTests.java +++ b/fagiServer/src/test/java/com/fagi/encryption/EncryptionTests.java @@ -30,7 +30,7 @@ void tearDown() { } @Test - public void constructorLoadKeyPairThrowsExceptionIOExceptionWhenLoadKeyThrowsThat() throws Exception { + public void whenLoadKeyPairThrowsIOException_ThenConstructorPrintsStacktrace() throws Exception { try (var mockedKeyStorage = Mockito.mockStatic(KeyStorage.class)) { mockedKeyStorage .when(() -> KeyStorage.LoadKeyPair("RSA")) @@ -49,7 +49,7 @@ public void constructorLoadKeyPairThrowsExceptionIOExceptionWhenLoadKeyThrowsTha } @Test - public void constructorLoadKeyPairThrowsNoSuchAlgorithmExceptionWhenLoadKeyThrowsThat() throws Exception { + public void whenLoadKeyPairThrowsNoSuchAlgorithmException_ThenConstructorPrintsStacktrace() throws Exception { try (var mockedKeyStorage = Mockito.mockStatic(KeyStorage.class)) { mockedKeyStorage .when(() -> KeyStorage.LoadKeyPair("RSA")) @@ -68,7 +68,7 @@ public void constructorLoadKeyPairThrowsNoSuchAlgorithmExceptionWhenLoadKeyThrow } @Test - public void constructorLoadKeyPairThrowsInvalidKeySpecExceptionWhenLoadKeyThrowsThat() throws Exception { + public void whenLoadKeyPairThrowsInvalidKeySpecException_ThenConstructorPrintsStacktrace() throws Exception { try (var mockedKeyStorage = Mockito.mockStatic(KeyStorage.class)) { mockedKeyStorage .when(() -> KeyStorage.LoadKeyPair("RSA")) @@ -87,7 +87,7 @@ public void constructorLoadKeyPairThrowsInvalidKeySpecExceptionWhenLoadKeyThrows } @Test - void constructorSaveKeyPairThrowsIOExceptionWhenSaveKeyPairThrowsThat() { + void whenSaveKeyPairThrowsIOException_ThenConstructorPrintsStacktrace() { try (var mockedKeyStorage = Mockito.mockStatic(KeyStorage.class)) { mockedKeyStorage .when(() -> KeyStorage.SaveKeyPair(any())) @@ -104,7 +104,7 @@ void constructorSaveKeyPairThrowsIOExceptionWhenSaveKeyPairThrowsThat() { } @Test - void constructorCreatesRSAKeyAndTriesToStoreIt() { + void givenNoKeyPairFileExists_WhenConstructingEncryption_ThenConstructorAttemptsToStoreRSAKeyPair() { try (var mockedStatic = Mockito.mockStatic(KeyStorage.class)) { var encryption = new Encryption(PUBLIC_KEY_PATH); mockedStatic.verify(() -> KeyStorage.SaveKeyPair(eq((KeyPair) encryption @@ -115,7 +115,7 @@ void constructorCreatesRSAKeyAndTriesToStoreIt() { } @Test - void constructorCanLoadRSAKeyFromDisk() throws IOException { + void givenKeyPairFileExists_WhenConstructingEncryption_ThenConstructorLoadsKeyPairFromDisk() throws IOException { var rsa = new RSA(1024); KeyStorage.SaveKeyPair((KeyPair) rsa .getKey()