From 78db2e5908fbb5347eb13ab1f2051c8c80f1fbc9 Mon Sep 17 00:00:00 2001 From: fdai5728 Date: Wed, 16 Feb 2022 17:29:22 +0100 Subject: [PATCH] Implements search function and test for search via index --- src/main/java/CredentialRepository.java | 14 +++++++++++--- .../java/CredentialRepositoryInterface.java | 2 +- src/test/java/CredentialRepositoryTest.java | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/main/java/CredentialRepository.java b/src/main/java/CredentialRepository.java index 2c49131..acc0c08 100644 --- a/src/main/java/CredentialRepository.java +++ b/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 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 diff --git a/src/main/java/CredentialRepositoryInterface.java b/src/main/java/CredentialRepositoryInterface.java index 85db2ed..19750c6 100644 --- a/src/main/java/CredentialRepositoryInterface.java +++ b/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(); diff --git a/src/test/java/CredentialRepositoryTest.java b/src/test/java/CredentialRepositoryTest.java index 36fe139..bb10bc6 100644 --- a/src/test/java/CredentialRepositoryTest.java +++ b/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)); + + } }