Browse Source

Merge commit '8fe5cec4110e84885910952cae72fcd55d883650' into HEAD

feature-refactor-password-validator-tests
jenkins 3 years ago
parent
commit
5a9eb32422
  1. 31
      src/main/java/Credential.java
  2. 11
      src/main/java/CredentialInterface.java
  3. 116
      src/main/java/CredentialRepository.java
  4. 10
      src/main/java/CredentialRepositoryInterface.java
  5. 38
      src/main/java/Credentials.java
  6. 84
      src/test/java/CredentialRepositoryTest.java

31
src/main/java/Credential.java

@ -0,0 +1,31 @@
public class Credential {
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;
}
public void updateUsername(String newUsername)
{
this.name = newUsername;
}
}

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();
}

116
src/main/java/CredentialRepository.java

@ -0,0 +1,116 @@
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, String newPassword) {
try{
this.credentials.get(index).upDatePassword(newPassword);
}
catch (IndexOutOfBoundsException outOfBoundsException)
{
System.err.println("No Credential with Index" + index + " found");
}
}
public void updateUsername(int index, String newUsername){
try{
this.credentials.get(index).updateUsername(newUsername);
}
catch (IndexOutOfBoundsException outOfBoundsException)
{
System.err.println("No Credential with Index" + index + " found");
}
}
@Override
public void delete(int index) {
this.credentials.remove(index);
}
/*
this function will be required in the future for catching the left-shift in the arrayList
after one entry was deleted. welp, maybe it won't. can't tell and i've no time atm to
finish this
private int mapIndexToListCounter(int needle) {
for(int c = 0; c<this.credentials.size(); c++)
{
if(this.credentials.get(c).getId() == needle)
{
return c;
}
}
return -1;
}
*/
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, String newPassword);
void updateUsername(int indexl, String newUsername);
void delete(int index);
}

38
src/main/java/Credentials.java

@ -1,38 +0,0 @@
public class Credentials implements CredentialInterface{
@Override
public void show() {
}
@Override
public void edit() {
}
@Override
public void delete() {
}
@Override
public void getCredentials() {
}
@Override
public void setCredentials() {
}
@Override
public void getAsJSON() {
}
@Override
public void loadCredentialsFromJSON() {
}
}

84
src/test/java/CredentialRepositoryTest.java

@ -0,0 +1,84 @@
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));
}
@Test
void testUpdatePassword(){
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.updatePassword(0, "newPassword");
Credential c = this.credentialRepository.getCredentialsViaId(0);
Assertions.assertEquals("newPassword", c.getPassword());
}
@Test
void testUpdateUsername(){
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.updateUsername(0, "newUsername");
Credential c = this.credentialRepository.getCredentialsViaId(0);
Assertions.assertEquals("newUsername", c.getName());
}
@Test
void testDeleteEntry(){
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.delete(0);
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(0));
}
}
Loading…
Cancel
Save