11 Commits

  1. 30
      src/main/java/Credential.java
  2. 11
      src/main/java/CredentialInterface.java
  3. 84
      src/main/java/CredentialRepository.java
  4. 10
      src/main/java/CredentialRepositoryInterface.java
  5. 61
      src/test/java/CredentialRepositoryTest.java

30
src/main/java/Credential.java

@ -0,0 +1,30 @@
public class Credential {
/**
* Value Object für das Tupel aus name + pw
*/
private String name, password;
private int id;
public Credential(String name, String password, int id) throws Exception {
if(name == null || password == null)
throw new Exception("kein leeres Objekt erstellen");
this.name = name;
this.password = password;
}
public String getName()
{
return this.name;
}
public String getPassword() { return this.password; }
public int getId() { return this.id; }
public void upDatePassword(String newPassword)
{
this.password = newPassword;
}
}

11
src/main/java/CredentialInterface.java

@ -1,11 +0,0 @@
public interface CredentialInterface {
public void show();
public void edit();
public void delete();
public void getCredentials();
public void setCredentials();
public void getAsJSON();
public void loadCredentialsFromJSON();
}

84
src/main/java/CredentialRepository.java

@ -0,0 +1,84 @@
import java.util.ArrayList;
public class CredentialRepository implements CredentialRepositoryInterface{
private int idCounter = 0;
private ArrayList<Credential> credentials;
public CredentialRepository()
{
this.credentials = new ArrayList<Credential>();
}
public int createNewCredential(String name, String password)
{
try {
this.credentials.add(new Credential(name, password, this.idCounter++));
}
catch(Exception fail)
{
System.err.println(fail.getMessage());
}
return this.getListSize();
}
@Override
public Credential getCredentialsViaName(String needle) {
for(int c = 0; c < this.getListSize(); c++)
{
Credential credential = this.credentials.get(c);
if(credential.getName().equals(needle))
return credential;
}
return null;
}
@Override
public Credential getCredentialsViaId(int id) {
try{
return this.credentials.get(id);
}
catch (IndexOutOfBoundsException outOfBoundsException)
{
return null;
}
}
/*
The next 3 functions assume you already have successfully
pulled a credential from the repository and now want to edit it.
Thus, these functions require you to pass the desired credential's index
*/
@Override
public void updatePassword(int index) {
}
public void updateUsername(int index){
}
@Override
public void delete(int index) {
}
private int getListSize()
{
return this.credentials.size();
}
}

10
src/main/java/CredentialRepositoryInterface.java

@ -0,0 +1,10 @@
public interface CredentialRepositoryInterface {
int createNewCredential(String name, String password);
Credential getCredentialsViaName(String needle);
Credential getCredentialsViaId(int id);
void updatePassword(int index);
void updateUsername(int index);
void delete(int index);
}

61
src/test/java/CredentialRepositoryTest.java

@ -0,0 +1,61 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
class CredentialRepositoryTest {
private CredentialRepository credentialRepository;
@BeforeEach
public void setUp()
{
credentialRepository = new CredentialRepository();
}
@Test
void createNewCredentialAndAddToList() {
Assertions.assertEquals(1, this.credentialRepository.createNewCredential("mock", "mock"));
Assertions.assertEquals(2, this.credentialRepository.createNewCredential("kek", "mate"));
}
@Test
void searchForUserInRepository() {
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.createNewCredential("kek", "mate");
Credential c = this.credentialRepository.getCredentialsViaName("kek");
Assertions.assertEquals("mate", c.getPassword());
}
@Test
void searchForIdInRepository() {
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.createNewCredential("kek", "mate");
Credential c = this.credentialRepository.getCredentialsViaId(1);
Assertions.assertEquals("mate", c.getPassword());
}
@Test
void testSearchWithInvalidIndex() {
this.credentialRepository.createNewCredential("mock", "mock");
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(2));
}
}
/*
Exception failed = Assertions.assertThrows(Exception.class, () -> {
this.credentialRepository.createNewCredential(null,null);
});
Assertions.assertEquals("kein leeres Objekt erstellen", failed.getMessage());
*/
Loading…
Cancel
Save