6 Commits

  1. 119
      src/main/java/Vault.java
  2. 4
      src/main/java/VaultInterface.java
  3. 58
      src/test/java/VaultTest.java

119
src/main/java/Vault.java

@ -17,17 +17,15 @@ public class Vault implements VaultInterface {
public int newID; public int newID;
public String newString; public String newString;
public boolean decision; public boolean decision;
public boolean isInt = false;
InputStream inputS = System.in; InputStream inputS = System.in;
OutputStream outputS = System.out; OutputStream outputS = System.out;
private CredentialRepository credentialRepository = new CredentialRepository(); private CredentialRepository credentialRepository = new CredentialRepository();
public static void main (String args[]) {
/*public static void main (String args[]) {
Vault vault = new Vault(); Vault vault = new Vault();
vault.credentialMenu(); vault.credentialMenu();
}
}*/
private void println(String output) { private void println(String output) {
@ -54,24 +52,34 @@ public class Vault implements VaultInterface {
String input = scan.nextLine(); String input = scan.nextLine();
if (input.equals("e")) {
switch(input) {
case "e":
config = false; config = false;
} else if (input.equals("l")) {
break;
case "l":
println("Set PW length:"); println("Set PW length:");
String inputPw = scan.nextLine(); String inputPw = scan.nextLine();
setPWLength(inputPw); setPWLength(inputPw);
} else if (input.equals("h")) {
break;
case "h":
println("Should your PW have capitals? Type in yes or no."); println("Should your PW have capitals? Type in yes or no.");
String inputC = scan.nextLine(); String inputC = scan.nextLine();
setCapital(inputC); setCapital(inputC);
} else if (input.equals("s")){
break;
case "s":
println("Should you PW have special characters? Type in yes or no."); println("Should you PW have special characters? Type in yes or no.");
String inputS = scan.nextLine(); String inputS = scan.nextLine();
setSpecialChar(inputS); setSpecialChar(inputS);
} else if (input.equals("n")) {
break;
case "n":
println("Should you PW have numbers? Type in yes or no."); println("Should you PW have numbers? Type in yes or no.");
String inputN = scan.nextLine(); String inputN = scan.nextLine();
setNumbers(inputN); 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); 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]+")) { if(str.matches("[0-9]+")) {
println("Getting credential via ID"); println("Getting credential via ID");
credentialRepository.getCredentialsViaId(Integer.parseInt(str)); 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(){ public void getAsJson(){
@ -117,7 +165,7 @@ public class Vault implements VaultInterface {
println("Your PWs contain now capitals."); println("Your PWs contain now capitals.");
}else if(input.equals("no")){ }else if(input.equals("no")){
haveCapitals = false; 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."); println("Your PWs contain now special characters.");
}else if(input.equals("no")){ }else if(input.equals("no")){
hasSpecialChars = false; 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."); println("Your PWs contains now numbers.");
}else if(input.equals("no")){ }else if(input.equals("no")){
hasNumbers = false; 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(); String input = scan.nextLine();
if (input.equals("e")) {
switch(input) {
case "e":
credentialM = false; credentialM = false;
}
break;
if (input.equals("a")) {
println("Type in username");
userName = scan.nextLine();
case "a":
println("Add credential:");
addCredential();
break;
println("Type in password");
password = scan.nextLine();
addCredential(userName, password);
}
if (input.equals("c")) {
println("Type in ID or username");
String str = scan.nextLine();
showCredential(str);
} 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();
case "c":
showCredential();
break;
if (input.equals("u")) {
decision = true;
}else{
decision = false;
}
println("Type new:");
newString = scan.nextLine();
//editCredential(newID, newString, decision);
case "l":
editCredential();
break;
} }
} }
} }

4
src/main/java/VaultInterface.java

@ -3,8 +3,8 @@
public interface VaultInterface { public interface VaultInterface {
void configure(); void configure();
void addCredential(String userName, String password);
void showCredential(String str);
void addCredential();
void showCredential();
void getAsJson(); void getAsJson();
void loadFromJson(); void loadFromJson();
} }

58
src/test/java/VaultTest.java

@ -1,7 +1,6 @@
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -12,8 +11,6 @@ import static org.junit.jupiter.api.Assertions.*;
public class VaultTest { public class VaultTest {
//Vault vlt = new Vault();
static Vault vlt; static Vault vlt;
@BeforeAll @BeforeAll
@ -44,29 +41,26 @@ public class VaultTest {
assertFalse(vlt.credentialM); assertFalse(vlt.credentialM);
} }
@Test
/* @Test
void openAddCredentialMenuItem() { void openAddCredentialMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 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.outputS = outputStream;
vlt.credentialMenu(); 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() { void openShowCredentialMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("c\n1".getBytes(StandardCharsets.UTF_8)); vlt.inputS = new ByteArrayInputStream("c\n1".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream; vlt.outputS = outputStream;
vlt.credentialMenu(); vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type")); assertTrue(outputStream.toString().contains("Type"));
}
}*/
@Test
/*@Test
void openEditCredentialMenuItem() { void openEditCredentialMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("l\n1\nu\npeter".getBytes(StandardCharsets.UTF_8)); vlt.inputS = new ByteArrayInputStream("l\n1\nu\npeter".getBytes(StandardCharsets.UTF_8));
@ -74,16 +68,30 @@ public class VaultTest {
vlt.credentialMenu(); vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type")); assertTrue(outputStream.toString().contains("Type"));
assertEquals(1,vlt.newID); assertEquals(1,vlt.newID);
assertEquals(true,vlt.decision);
assertTrue(vlt.decision);
assertEquals("peter",vlt.newString); assertEquals("peter",vlt.newString);
}
}*/
/*@Test
@Test
void editCredentialTest() { 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() { void addCredentialTest() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream; vlt.outputS = outputStream;
@ -98,16 +106,18 @@ public class VaultTest {
@Test @Test
void showCredentialTest() { 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(); vlt.credentialMenu();
assertFalse(vlt.isInt);
}*/
assertTrue(outputStream.toString().contains("name"));
}
@Test @Test
void getAsJson() {vlt.getAsJson();} void getAsJson() {vlt.getAsJson();}

Loading…
Cancel
Save