Browse Source

implement menu navigation to select a vault operation

feature-create-new-vault
binsky 3 years ago
parent
commit
feea89c718
  1. 38
      src/main/java/PasswordManager.java
  2. 33
      src/test/java/PasswordManagerTest.java

38
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<Vault> vaults = new ArrayList<>();
int selectedVaultId = -1;
ArrayList<String> 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<String> 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");
}
}
}
}

33
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();

Loading…
Cancel
Save