Browse Source

Merge commit '698cc4ae0063aab03758d622f4ded66f75939c4b' into HEAD

development projektabgabe
jenkins 3 years ago
parent
commit
650d1b875c
  1. 3
      src/main/java/Credential.java
  2. 4
      src/main/java/CredentialRepository.java
  3. 19
      src/main/java/Vault.java
  4. 15
      src/test/java/VaultTest.java

3
src/main/java/Credential.java

@ -3,7 +3,7 @@ import java.io.Serializable;
public class Credential implements Serializable { public class Credential implements Serializable {
private String name, password; private String name, password;
private int id;
private final int id;
public Credential(String name, String password, int id) throws Exception { public Credential(String name, String password, int id) throws Exception {
@ -12,6 +12,7 @@ public class Credential implements Serializable {
this.name = name; this.name = name;
this.password = password; this.password = password;
this.id = id;
} }
public String getName() public String getName()

4
src/main/java/CredentialRepository.java

@ -193,6 +193,10 @@ public class CredentialRepository implements CredentialRepositoryInterface{
return System.getProperty("user.dir"); return System.getProperty("user.dir");
} }
public CredentialList getCredentialList() {
return credentials;
}
} }

19
src/main/java/Vault.java

@ -118,14 +118,19 @@ public class Vault implements VaultInterface {
println("Type in ID or username"); println("Type in ID or username");
String str = scan.nextLine(); String str = scan.nextLine();
Credential credential = null;
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));
credential = credentialRepository.getCredentialsViaId(Integer.parseInt(str));
} else { } else {
println("Getting credential via name"); println("Getting credential via name");
credentialRepository.getCredentialsViaName(str);
credential = credentialRepository.getCredentialsViaName(str);
}
if (credential != null) {
println(credential.getName() + ": " + credential.getPassword());
} }
} }
@ -158,6 +163,11 @@ public class Vault implements VaultInterface {
} }
} }
public void listCredentials() {
for (Credential credential : credentialRepository.getCredentialList()) {
println(String.format("ID: %d - Username: %s", credential.getId(), credential.getName()));
}
}
public void getAsJson(){ public void getAsJson(){
@ -210,6 +220,7 @@ public class Vault implements VaultInterface {
sbcm.append("- add credential: a\n"); sbcm.append("- add credential: a\n");
sbcm.append("- show credential: c\n"); sbcm.append("- show credential: c\n");
sbcm.append("- edit credential: l\n"); sbcm.append("- edit credential: l\n");
sbcm.append("- list credentials: s\n");
sbcm.append("- exit: e\n"); sbcm.append("- exit: e\n");
println(sbcm.toString()); println(sbcm.toString());
@ -233,6 +244,10 @@ public class Vault implements VaultInterface {
case "l": case "l":
editCredential(); editCredential();
break; break;
case "s":
listCredentials();
break;
} }
} }
} }

15
src/test/java/VaultTest.java

@ -84,6 +84,8 @@ public class VaultTest {
@Test @Test
void showCredentialTest() { void showCredentialTest() {
addCredentialTest();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream; vlt.outputS = outputStream;
vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8)); vlt.inputS = new ByteArrayInputStream("1".getBytes(StandardCharsets.UTF_8));
@ -95,6 +97,19 @@ public class VaultTest {
vlt.credentialMenu(); vlt.credentialMenu();
assertTrue(outputStream.toString().contains("name")); assertTrue(outputStream.toString().contains("name"));
assertTrue(outputStream.toString().contains("peter: " + vlt.password));
}
@Test
void listCredentialsTest() {
addCredentialTest();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
vlt.outputS = outputStream;
vlt.inputS = new ByteArrayInputStream("s\n".getBytes(StandardCharsets.UTF_8));
vlt.credentialMenu();
assertTrue(outputStream.toString().contains("ID: 0 - Username: peter"));
} }
@Test @Test

Loading…
Cancel
Save