Browse Source

Implements search function and test for search via index

feature-credentialneu
fdai5728 3 years ago
parent
commit
78db2e5908
  1. 14
      src/main/java/CredentialRepository.java
  2. 2
      src/main/java/CredentialRepositoryInterface.java
  3. 18
      src/test/java/CredentialRepositoryTest.java

14
src/main/java/CredentialRepository.java

@ -2,7 +2,7 @@ import java.util.ArrayList;
public class CredentialRepository implements CredentialRepositoryInterface{
private int idCounter = 0;
private int idCounter = 1;
private ArrayList<Credential> credentials;
public CredentialRepository()
@ -49,9 +49,17 @@ public class CredentialRepository implements CredentialRepositoryInterface{
@Override
public Credential getCredentialsViaId() {
public Credential getCredentialsViaId(int id) {
try{
return this.credentials.get(id);
}
catch (IndexOutOfBoundsException outOfBoundsException)
{
return null;
}
return null;
}
@Override

2
src/main/java/CredentialRepositoryInterface.java

@ -4,7 +4,7 @@ public interface CredentialRepositoryInterface {
void edit();
void delete();
Credential getCredentialsViaName(String needle);
Credential getCredentialsViaId();
Credential getCredentialsViaId(int id);
void setCredentials();
void getAsJSON();
void loadCredentialsFromJSON();

18
src/test/java/CredentialRepositoryTest.java

@ -30,6 +30,24 @@ class CredentialRepositoryTest {
Assertions.assertEquals("mate", c.getPassword());
}
@Test
void searchForIdInRepository() {
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.createNewCredential("kek", "mate");
Credential c = this.credentialRepository.getCredentialsViaId(2);
Assertions.assertEquals("mate", c.getPassword());
}
@Test
void testSearchWithInvalidIndex() {
this.credentialRepository.createNewCredential("mock", "mock");
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(2));
}
}

Loading…
Cancel
Save