Browse Source

Implements search function and test for search via index

feature-serializer
fdai5728 3 years ago
committed by fdai6352
parent
commit
c131eac34a
  1. 12
      src/main/java/CredentialRepository.java
  2. 2
      src/main/java/CredentialRepositoryInterface.java
  3. 18
      src/test/java/CredentialRepositoryTest.java

12
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,11 +49,19 @@ 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;
}
}
@Override
public void setCredentials() {

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