From 278f120b0477e67e542a0d0c1723718ef0c27b32 Mon Sep 17 00:00:00 2001 From: binsky Date: Mon, 14 Feb 2022 20:49:53 +0100 Subject: [PATCH 1/5] implement creating a new vault --- src/main/java/PasswordManager.java | 15 ++++++++++----- src/test/java/PasswordManagerTest.java | 7 +++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/PasswordManager.java b/src/main/java/PasswordManager.java index 29960fd..10cf125 100644 --- a/src/main/java/PasswordManager.java +++ b/src/main/java/PasswordManager.java @@ -12,6 +12,7 @@ public class PasswordManager implements PasswordManagerInterface { boolean running = true; InputStream inputStream = System.in; OutputStream outputStream = System.out; + ArrayList vaults = new ArrayList<>(); public static void main(String[] args) { PasswordManager pm = new PasswordManager(); @@ -26,11 +27,6 @@ public class PasswordManager implements PasswordManagerInterface { println("Hello World"); } - @Override - public void listVaults() { - println("Vaults:"); - } - private void println(String output) { try { outputStream.write((output + "\n").getBytes(StandardCharsets.UTF_8)); @@ -39,6 +35,15 @@ public class PasswordManager implements PasswordManagerInterface { } } + @Override + public void listVaults() { + println("Vaults:"); + } + + public void createNewVault() { + vaults.add(new Vault()); + } + @Override public void openVault() { diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index c96f747..c4d3fb6 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -88,6 +88,13 @@ class PasswordManagerTest { assertTrue(outputStream.toString().startsWith("\nciip Gruppe 8")); } + @Test + void createNewVault() { + assertEquals(0, pm.vaults.size()); + pm.createNewVault(); + assertEquals(1, pm.vaults.size()); + } + private ByteArrayInputStream getEmptyStringInputStream() { return new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); } From dc97162cbeec3f499805f88fccf26f40e403cd4f Mon Sep 17 00:00:00 2001 From: binsky Date: Mon, 14 Feb 2022 20:54:59 +0100 Subject: [PATCH 2/5] refactor createNewVault test --- src/test/java/PasswordManagerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index c4d3fb6..9eab2f3 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -90,9 +90,9 @@ class PasswordManagerTest { @Test void createNewVault() { - assertEquals(0, pm.vaults.size()); + int vaultCount = pm.vaults.size(); pm.createNewVault(); - assertEquals(1, pm.vaults.size()); + assertEquals(vaultCount + 1, pm.vaults.size()); } private ByteArrayInputStream getEmptyStringInputStream() { From f8ab3d1a06c92c8f70fc9519de1fad6ebeb44a0f Mon Sep 17 00:00:00 2001 From: binsky Date: Mon, 14 Feb 2022 20:58:09 +0100 Subject: [PATCH 3/5] implement menu navigation to create a new vault --- src/main/java/PasswordManager.java | 7 +++++-- src/test/java/PasswordManagerTest.java | 10 ++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/PasswordManager.java b/src/main/java/PasswordManager.java index 10cf125..d988522 100644 --- a/src/main/java/PasswordManager.java +++ b/src/main/java/PasswordManager.java @@ -58,7 +58,8 @@ public class PasswordManager implements PasswordManagerInterface { sb.append("\nciip Gruppe 8 - Password Manager\n\n"); sb.append("Menu:\n"); - sb.append("- list vaults: l:\n"); + sb.append("- list vaults: l\n"); + sb.append("- create new vault: v\n"); sb.append("- select vault: v x (replace x with vault id)\n"); sb.append("- exit: e\n"); @@ -70,7 +71,9 @@ public class PasswordManager implements PasswordManagerInterface { running = false; } else if (input.equals("l")) { listVaults(); - } else if (input.startsWith("v")) { + } else if (input.equals("v")) { + createNewVault(); + } else if (input.startsWith("v")) { String vaultAction = (input.replace('v', ' ')).trim(); int vaultId = -1; diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index 9eab2f3..d191cf1 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -49,6 +49,16 @@ class PasswordManagerTest { assertTrue(outputStream.toString().endsWith("Vaults:\n")); } + @Test + void menuNavigationCreateNewVault() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + pm.inputStream = new ByteArrayInputStream("v\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = outputStream; + int vaultCount = pm.vaults.size(); + pm.showMenu(); + assertEquals(vaultCount + 1, pm.vaults.size()); + } + @Test void menuNavigationOpenVault() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); From 02a62e789b41b63bd02b98d9bb4eb2ce8f01128a Mon Sep 17 00:00:00 2001 From: binsky Date: Mon, 14 Feb 2022 21:01:12 +0100 Subject: [PATCH 4/5] refactor list vaults --- src/main/java/PasswordManager.java | 3 +++ src/test/java/PasswordManagerTest.java | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/PasswordManager.java b/src/main/java/PasswordManager.java index d988522..7ce12df 100644 --- a/src/main/java/PasswordManager.java +++ b/src/main/java/PasswordManager.java @@ -38,6 +38,9 @@ public class PasswordManager implements PasswordManagerInterface { @Override public void listVaults() { println("Vaults:"); + for (int i = 0; i < vaults.size(); i++) { + println("- vault id: " + i); + } } public void createNewVault() { diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index d191cf1..c062cbe 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -46,7 +46,8 @@ class PasswordManagerTest { pm.inputStream = new ByteArrayInputStream("l\n".getBytes(StandardCharsets.UTF_8)); pm.outputStream = outputStream; pm.showMenu(); - assertTrue(outputStream.toString().endsWith("Vaults:\n")); + pm.createNewVault(); + assertTrue(outputStream.toString().contains("Vaults:\n- vault id: 0\n")); } @Test From feea89c7188bb3ab1c968d3a806c2112f92a140c Mon Sep 17 00:00:00 2001 From: binsky Date: Mon, 14 Feb 2022 21:45:45 +0100 Subject: [PATCH 5/5] implement menu navigation to select a vault operation --- src/main/java/PasswordManager.java | 38 +++++++++++++++++++++++--- src/test/java/PasswordManagerTest.java | 33 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/main/java/PasswordManager.java b/src/main/java/PasswordManager.java index 7ce12df..f17f6f1 100644 --- a/src/main/java/PasswordManager.java +++ b/src/main/java/PasswordManager.java @@ -1,6 +1,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; @@ -13,6 +14,8 @@ public class PasswordManager implements PasswordManagerInterface { InputStream inputStream = System.in; OutputStream outputStream = System.out; ArrayList vaults = new ArrayList<>(); + int selectedVaultId = -1; + ArrayList vaultOperationOptions = new ArrayList<>(); public static void main(String[] args) { PasswordManager pm = new PasswordManager(); @@ -63,7 +66,8 @@ public class PasswordManager implements PasswordManagerInterface { sb.append("Menu:\n"); sb.append("- list vaults: l\n"); sb.append("- create new vault: v\n"); - sb.append("- select vault: v x (replace x with vault id)\n"); + sb.append("- select vault and show operations: v x (replace x with vault id)\n"); + sb.append("- select vault operation: o x (replace o with a operation id for the selected vault)\n"); sb.append("- exit: e\n"); println(sb.toString()); @@ -87,14 +91,16 @@ public class PasswordManager implements PasswordManagerInterface { } if (vaultId >= 0) { + selectedVaultId = vaultId; + println("\nVault " + vaultId + " selected."); try { int internalOptionCounter = 0; - ArrayList options = new ArrayList<>(); + println("\nAvailable vault operations:"); for (Method m : Class.forName("Vault").getDeclaredMethods()) { // returns all declared methods including private or protected try { - if (Modifier.isPublic(this.getClass().getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod) - options.add(m.getName()); + if (Modifier.isPublic(Class.forName("Vault").getDeclaredMethod(m.getName()).getModifiers())) { // filter out methods that are not public (using Modifier) or have arguments (using getDeclaredMethod) + vaultOperationOptions.add(m.getName()); println(internalOptionCounter + " - " + m.getName()); internalOptionCounter++; } @@ -106,6 +112,30 @@ public class PasswordManager implements PasswordManagerInterface { println("-- vaults not implemented yet"); } } + } else if (input.startsWith("o")) { + String operation = (input.replace('o', ' ')).trim(); + int operationId = -1; + + try { + operationId = Integer.parseInt(operation); + } catch (NumberFormatException e) { + println("-- please enter a valid operation id"); + } + + if (selectedVaultId < 0) { + println("-- a vault needs to be selected"); + } else if (operationId >= 0 && operationId < vaultOperationOptions.size()) { + Vault vault = vaults.get(selectedVaultId); + try { + println("Run selected operation '" + vaultOperationOptions.get(operationId) + "':"); + vault.getClass().getDeclaredMethod(vaultOperationOptions.get(operationId)).invoke(vault); + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + e.printStackTrace(); + println("-- selected operation failed"); + } + } else { + println("-- please enter a valid operation id"); + } } } } diff --git a/src/test/java/PasswordManagerTest.java b/src/test/java/PasswordManagerTest.java index c062cbe..76bff98 100644 --- a/src/test/java/PasswordManagerTest.java +++ b/src/test/java/PasswordManagerTest.java @@ -69,6 +69,39 @@ class PasswordManagerTest { assertFalse(outputStream.toString().endsWith("-- vaults not implemented yet\n")); } + @Test + void menuNavigationSelectVaultOperation() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + + pm.inputStream = new ByteArrayInputStream("v\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = new ByteArrayOutputStream(); + pm.showMenu(); + + pm.inputStream = new ByteArrayInputStream("v 0\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = new ByteArrayOutputStream(); + pm.showMenu(); + + pm.inputStream = new ByteArrayInputStream("o 0\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = outputStream; + pm.showMenu(); + assertTrue(outputStream.toString().contains("Run selected operation")); + + outputStream = new ByteArrayOutputStream(); + + pm.inputStream = new ByteArrayInputStream("o foobert\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = outputStream; + pm.showMenu(); + assertTrue(outputStream.toString().endsWith("-- please enter a valid operation id\n")); + + outputStream = new ByteArrayOutputStream(); + + pm.inputStream = new ByteArrayInputStream("o 1000\n".getBytes(StandardCharsets.UTF_8)); + pm.outputStream = outputStream; + pm.showMenu(); + System.out.println(outputStream); + assertTrue(outputStream.toString().endsWith("-- please enter a valid operation id\n")); + } + @Test void showMenu() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream();