diff --git a/src/main/java/Vault.java b/src/main/java/Vault.java index 5bb53d4..eac1932 100644 --- a/src/main/java/Vault.java +++ b/src/main/java/Vault.java @@ -17,17 +17,15 @@ public class Vault implements VaultInterface { public int newID; public String newString; public boolean decision; - public boolean isInt = false; - InputStream inputS = System.in; OutputStream outputS = System.out; private CredentialRepository credentialRepository = new CredentialRepository(); - public static void main (String args[]) { + /*public static void main (String args[]) { Vault vault = new Vault(); vault.credentialMenu(); - } + }*/ private void println(String output) { @@ -54,24 +52,34 @@ public class Vault implements VaultInterface { String input = scan.nextLine(); - if (input.equals("e")) { - config = false; - } else if (input.equals("l")) { - println("Set PW length:"); - String inputPw = scan.nextLine(); - setPWLength(inputPw); - } else if (input.equals("h")) { - println("Should your PW have capitals? Type in yes or no."); - String inputC = scan.nextLine(); - setCapital(inputC); - } else if (input.equals("s")){ - println("Should you PW have special characters? Type in yes or no."); - String inputS = scan.nextLine(); - setSpecialChar(inputS); - } else if (input.equals("n")) { - println("Should you PW have numbers? Type in yes or no."); - String inputN = scan.nextLine(); - setNumbers(inputN); + switch(input) { + case "e": + config = false; + break; + + case "l": + println("Set PW length:"); + String inputPw = scan.nextLine(); + setPWLength(inputPw); + break; + + case "h": + println("Should your PW have capitals? Type in yes or no."); + String inputC = scan.nextLine(); + setCapital(inputC); + break; + + case "s": + println("Should you PW have special characters? Type in yes or no."); + String inputS = scan.nextLine(); + setSpecialChar(inputS); + break; + + case "n": + println("Should you PW have numbers? Type in yes or no."); + String inputN = scan.nextLine(); + setNumbers(inputN); + break; } } @@ -86,11 +94,22 @@ public class Vault implements VaultInterface { } - public void addCredential(String userName, String password){ + public void addCredential(){ + Scanner scan = new Scanner(inputS); + println("Type in username"); + userName = scan.nextLine(); + + println("Type in password"); + password = scan.nextLine(); credentialRepository.createNewCredential(userName, password); } - public void showCredential(String str) { + public void showCredential() { + Scanner scan = new Scanner(inputS); + println("Type in ID or username"); + + String str = scan.nextLine(); + if(str.matches("[0-9]+")) { println("Getting credential via ID"); credentialRepository.getCredentialsViaId(Integer.parseInt(str)); @@ -101,6 +120,35 @@ public class Vault implements VaultInterface { } } + public void editCredential() { + Scanner scan = new Scanner(inputS); + + println("Type ID to select credential:"); + newID = Integer.parseInt(scan.nextLine()); + + println("-change username: u\n-change password: p"); + + String input = scan.nextLine(); + + if (input.equals("u")) { + decision = true; + } + + if(decision){ + + println("Type new username:"); + newString = scan.nextLine(); + + credentialRepository.updateUsername(newID, newString); + } else { + + println("Type new password:"); + newString = scan.nextLine(); + + credentialRepository.updatePassword(newID, newString); + } + } + public void getAsJson(){ @@ -117,7 +165,7 @@ public class Vault implements VaultInterface { println("Your PWs contain now capitals."); }else if(input.equals("no")){ haveCapitals = false; - println("Your PWs don't have capitals anymore."); + println("Your PWs don´t have capitals anymore."); } } @@ -129,7 +177,7 @@ public class Vault implements VaultInterface { println("Your PWs contain now special characters."); }else if(input.equals("no")){ hasSpecialChars = false; - println("Your PWs don't have special characters anymore."); + println("Your PWs don´t have special characters anymore."); } } @@ -140,7 +188,7 @@ public class Vault implements VaultInterface { println("Your PWs contains now numbers."); }else if(input.equals("no")){ hasNumbers = false; - println("Your PWs don't have numbers anymore."); + println("Your PWs don´t have numbers anymore."); } } @@ -159,40 +207,23 @@ public class Vault implements VaultInterface { String input = scan.nextLine(); + switch(input) { + case "e": + credentialM = false; + break; - if (input.equals("e")) { - credentialM = false; - } - - if (input.equals("a")) { - println("Type in username"); - userName = scan.nextLine(); - - println("Type in password"); - password = scan.nextLine(); - addCredential(userName, password); - } - - if (input.equals("c")) { - println("Type in ID or username"); + case "a": + println("Add credential:"); + addCredential(); + break; - String str = scan.nextLine(); - showCredential(str); + case "c": + showCredential(); + break; - } else if (input.equals("l")) { - println("Type ID to select credential:"); - newID = Integer.parseInt(scan.nextLine()); - println("-change username: u\n-change password: p"); - input = scan.nextLine(); - - if (input.equals("u")) { - decision = true; - }else{ - decision = false; - } - println("Type new:"); - newString = scan.nextLine(); - //editCredential(newID, newString, decision); + case "l": + editCredential(); + break; } } } diff --git a/src/main/java/VaultInterface.java b/src/main/java/VaultInterface.java index cb23755..9f1fc26 100644 --- a/src/main/java/VaultInterface.java +++ b/src/main/java/VaultInterface.java @@ -3,8 +3,8 @@ public interface VaultInterface { void configure(); - void addCredential(String userName, String password); - void showCredential(String str); + void addCredential(); + void showCredential(); void getAsJson(); void loadFromJson(); } diff --git a/src/test/java/VaultTest.java b/src/test/java/VaultTest.java index ae472e8..d37ec57 100644 --- a/src/test/java/VaultTest.java +++ b/src/test/java/VaultTest.java @@ -1,7 +1,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -12,8 +11,6 @@ import static org.junit.jupiter.api.Assertions.*; public class VaultTest { - //Vault vlt = new Vault(); - static Vault vlt; @BeforeAll @@ -44,29 +41,26 @@ public class VaultTest { assertFalse(vlt.credentialM); } - @Test + /* @Test void openAddCredentialMenuItem() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - vlt.inputS = new ByteArrayInputStream("a\npeter\npassword".getBytes(StandardCharsets.UTF_8)); + vlt.inputS = new ByteArrayInputStream("a".getBytes(StandardCharsets.UTF_8)); vlt.outputS = outputStream; vlt.credentialMenu(); - assertTrue(outputStream.toString().contains("username")); - assertEquals("peter", vlt.userName); - assertTrue(outputStream.toString().contains("password")); - assertEquals("password",vlt.password); - } + assertTrue(outputStream.toString().contains("Add")); + }*/ - @Test + /*@Test void openShowCredentialMenuItem() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); vlt.inputS = new ByteArrayInputStream("c\n1".getBytes(StandardCharsets.UTF_8)); vlt.outputS = outputStream; vlt.credentialMenu(); assertTrue(outputStream.toString().contains("Type")); - } + }*/ - @Test + /*@Test void openEditCredentialMenuItem() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); vlt.inputS = new ByteArrayInputStream("l\n1\nu\npeter".getBytes(StandardCharsets.UTF_8)); @@ -74,16 +68,30 @@ public class VaultTest { vlt.credentialMenu(); assertTrue(outputStream.toString().contains("Type")); assertEquals(1,vlt.newID); - assertEquals(true,vlt.decision); + assertTrue(vlt.decision); assertEquals("peter",vlt.newString); - } + }*/ - /*@Test + @Test void editCredentialTest() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + vlt.outputS = outputStream; + vlt.inputS = new ByteArrayInputStream("1\np\npassword".getBytes(StandardCharsets.UTF_8)); + vlt.editCredential(); - }*/ + assertEquals(1, vlt.newID); + assertEquals("password", vlt.newString); - /*@Test + + vlt.inputS = new ByteArrayInputStream("1\nu\npeter".getBytes(StandardCharsets.UTF_8)); + vlt.editCredential(); + + assertEquals(1, vlt.newID); + assertEquals("peter", vlt.newString); + + } + + @Test void addCredentialTest() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); vlt.outputS = outputStream; @@ -96,18 +104,20 @@ public class VaultTest { assertEquals("password",vlt.password); } - @Test + @Test void showCredentialTest() { - vlt.inputS = new ByteArrayInputStream("l\n1".getBytes(StandardCharsets.UTF_8)); - vlt.credentialMenu(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + vlt.outputS = outputStream; + vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8)); + vlt.showCredential(); - assertTrue(vlt.isInt); + assertTrue(outputStream.toString().contains("ID")); - vlt.inputS = new ByteArrayInputStream("l\npeter".getBytes(StandardCharsets.UTF_8)); + vlt.inputS = new ByteArrayInputStream("peter".getBytes(StandardCharsets.UTF_8)); vlt.credentialMenu(); - assertFalse(vlt.isInt); - }*/ + assertTrue(outputStream.toString().contains("name")); + } @Test void getAsJson() {vlt.getAsJson();} @@ -187,7 +197,7 @@ public class VaultTest { vlt.setCapital("no"); assertFalse(vlt.haveCapitals = false); - assertTrue(outputStream.toString().contains("don't")); + assertTrue(outputStream.toString().contains("anymore")); } @Test @@ -211,7 +221,7 @@ public class VaultTest { vlt.setSpecialChar("no"); assertFalse(vlt.hasSpecialChars = false); - assertTrue(outputStream.toString().contains("don't")); + assertTrue(outputStream.toString().contains("anymore")); } @Test @@ -236,7 +246,7 @@ public class VaultTest { vlt.setNumbers("no"); assertFalse(vlt.hasNumbers = false); - assertTrue(outputStream.toString().contains("don't")); + assertTrue(outputStream.toString().contains("anymore")); } private ByteArrayInputStream getEmptyStringInputStream() {