Browse Source

Merge commit 'eb3439281fe93753a321aedcf3baf7e75bd318fc' into HEAD

development
jenkins 3 years ago
parent
commit
380d31bca4
  1. 2
      src/main/java/CredentialRepository.java
  2. 83
      src/main/java/Vault.java
  3. 4
      src/main/java/VaultInterface.java
  4. 2
      src/test/java/PasswordManagerTest.java
  5. 64
      src/test/java/VaultTest.java

2
src/main/java/CredentialRepository.java

@ -106,7 +106,7 @@ public class CredentialRepository implements CredentialRepositoryInterface{
return -1;
}
*/
private int getListSize()
public int getListSize()
{
return this.credentials.size();
}

83
src/main/java/Vault.java

@ -14,10 +14,22 @@ public class Vault implements VaultInterface {
public boolean credentialM;
public String userName;
public String password;
public int newID;
public String newString;
public boolean decision;
public boolean isInt = false;
//InputStream inputS = System.in;
InputStream inputS = System.in;
OutputStream outputS = System.out;
private CredentialRepository credentialRepository = new CredentialRepository();
public static void main (String args[]) {
Vault vault = new Vault();
vault.credentialMenu();
}
private void println(String output) {
try {
outputS.write((output + "\n").getBytes(StandardCharsets.UTF_8));
@ -28,7 +40,7 @@ public class Vault implements VaultInterface {
public void configure(){
StringBuilder sbcm = new StringBuilder();
Scanner scan = new Scanner(System.in);
Scanner scan = new Scanner(inputS);
config = true;
sbcm.append("Configure:\n");
@ -74,32 +86,21 @@ public class Vault implements VaultInterface {
}
public void addCredential(){
Scanner scan = new Scanner(System.in);
println("Type in username");
userName = scan.nextLine();
println("Type in password");
password = scan.nextLine();
//createNewCredential(userName, password);
public void addCredential(String userName, String password){
credentialRepository.createNewCredential(userName, password);
}
public void showCredential(){
Scanner scan = new Scanner(System.in);
println("Type in ID or username");
String str = scan.nextLine();
public void showCredential(String str) {
if(str.matches("[0-9]+")) {
isInt = true;
//getCredentialsViaId(Integer.parseInt(str));
println("Getting credential via ID");
credentialRepository.getCredentialsViaId(Integer.parseInt(str));
} else {
isInt = false;
//getCredentialsViaName(str);
}
println("Getting credential via name");
credentialRepository.getCredentialsViaName(str);
}
}
public void getAsJson(){
@ -145,7 +146,7 @@ public class Vault implements VaultInterface {
public void credentialMenu() {
StringBuilder sbcm = new StringBuilder();
Scanner scan = new Scanner(System.in);
Scanner scan = new Scanner(inputS);
credentialM = true;
sbcm.append("Configure credentials:\n");
@ -158,14 +159,40 @@ public class Vault implements VaultInterface {
String input = scan.nextLine();
if (input.equals("e")) {
credentialM = false;
} else if (input.equals("a")) {
println("Type username:");
} else if (input.equals("c")) {
println("Type username or ID to show credential:");
}
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");
String str = scan.nextLine();
showCredential(str);
} else if (input.equals("l")) {
println("Type username or ID to select credential:");
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);
}
}
}

4
src/main/java/VaultInterface.java

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

2
src/test/java/PasswordManagerTest.java

@ -80,9 +80,11 @@ class PasswordManagerTest {
pm.inputStream = new ByteArrayInputStream("v 0\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = new ByteArrayOutputStream();
pm.showMenu();
System.out.println(pm.outputStream.toString());
pm.inputStream = new ByteArrayInputStream("o 0\n".getBytes(StandardCharsets.UTF_8));
pm.outputStream = outputStream;
pm.vaults.get(0).inputS = new ByteArrayInputStream("e\n".getBytes(StandardCharsets.UTF_8));
pm.showMenu();
assertTrue(outputStream.toString().contains("Run selected operation"));

64
src/test/java/VaultTest.java

@ -24,10 +24,10 @@ public class VaultTest {
@BeforeEach
void reset() {
vlt.outputS = System.out;
//vlt.inputS = System.in;
vlt.inputS = getEmptyStringInputStream();
}
/* @Test
@Test
void openCredentialMenu() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
@ -47,16 +47,20 @@ public class VaultTest {
@Test
void openAddCredentialMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("a".getBytes(StandardCharsets.UTF_8));
vlt.inputS = new ByteArrayInputStream("a\npeter\npassword".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type"));
assertTrue(outputStream.toString().contains("username"));
assertEquals("peter", vlt.userName);
assertTrue(outputStream.toString().contains("password"));
assertEquals("password",vlt.password);
}
@Test
void openShowCredentialMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("c".getBytes(StandardCharsets.UTF_8));
vlt.inputS = new ByteArrayInputStream("c\n1".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type"));
@ -65,13 +69,21 @@ public class VaultTest {
@Test
void openEditCredentialMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("l".getBytes(StandardCharsets.UTF_8));
vlt.inputS = new ByteArrayInputStream("l\n1\nu\npeter".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type"));
assertEquals(1,vlt.newID);
assertEquals(true,vlt.decision);
assertEquals("peter",vlt.newString);
}
@Test
/*@Test
void editCredentialTest() {
}*/
/*@Test
void addCredentialTest() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
@ -86,19 +98,14 @@ public class VaultTest {
@Test
void showCredentialTest() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8));
vlt.showCredential();
vlt.inputS = new ByteArrayInputStream("l\n1".getBytes(StandardCharsets.UTF_8));
vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type"));
assertTrue(vlt.isInt);
vlt.outputS = outputStream;
vlt.inputS = new ByteArrayInputStream("peter".getBytes(StandardCharsets.UTF_8));
vlt.showCredential();
vlt.inputS = new ByteArrayInputStream("l\npeter".getBytes(StandardCharsets.UTF_8));
vlt.credentialMenu();
assertTrue(outputStream.toString().contains("Type"));
assertFalse(vlt.isInt);
}*/
@ -108,7 +115,7 @@ public class VaultTest {
@Test
void loadFromJson() {vlt.loadFromJson();}
/* @Test
@Test
void openConfigureMenu() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
@ -122,9 +129,9 @@ public class VaultTest {
vlt.inputS = new ByteArrayInputStream("e".getBytes(StandardCharsets.UTF_8));
vlt.configure();
assertFalse(vlt.config);
}*/
}
/* @Test
@Test
void doNotExitConfigAfterWrongInput() {
vlt.inputS = new ByteArrayInputStream("__\n".getBytes(StandardCharsets.UTF_8));
vlt.configure();
@ -136,9 +143,9 @@ public class VaultTest {
assertThrowsExactly(NoSuchElementException.class, () -> vlt.configure());
assertTrue(outputStream.toString().startsWith("Configure:"));
}*/
}
/* @Test
@Test
void openSetPWLengthMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("l\n7".getBytes(StandardCharsets.UTF_8));
@ -146,7 +153,7 @@ public class VaultTest {
vlt.configure();
assertEquals(7, vlt.PWLength);
assertTrue(outputStream.toString().contains("Set"));
}*/
}
@Test
void setPWLength() {
@ -158,14 +165,14 @@ public class VaultTest {
assertTrue(outputStream.toString().contains("now:"));
}
/*@Test
@Test
void openSetCapitalsMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.configure();
assertTrue(vlt.haveCapitals = true);
}*/
}
@Test
void setCapitals() {
@ -181,17 +188,16 @@ public class VaultTest {
assertFalse(vlt.haveCapitals = false);
assertTrue(outputStream.toString().contains("don't"));
System.out.println(outputStream.toString());
}
/*@Test
@Test
void openSetSpecialCharsMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.configure();
assertTrue(vlt.hasSpecialChars = true);
}*/
}
@Test
void setSpecialCharTest() {
@ -208,14 +214,14 @@ public class VaultTest {
assertTrue(outputStream.toString().contains("don't"));
}
/*@Test
@Test
void openSetNumbersMenuItem() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.inputS = new ByteArrayInputStream("h\nyes".getBytes(StandardCharsets.UTF_8));
vlt.outputS = outputStream;
vlt.configure();
assertTrue(vlt.hasNumbers = true);
}*/
}
@Test
void setNumbersTest() {

Loading…
Cancel
Save