14 Commits

  1. 4
      src/main/java/Credential.java
  2. 4
      src/main/java/CredentialList.java
  3. 126
      src/main/java/CredentialRepository.java
  4. 35
      src/test/java/CredentialRepositoryTest.java

4
src/main/java/Credential.java

@ -1,4 +1,6 @@
public class Credential {
import java.io.Serializable;
public class Credential implements Serializable {
private String name, password; private String name, password;
private int id; private int id;

4
src/main/java/CredentialList.java

@ -0,0 +1,4 @@
import java.io.Serializable;
import java.util.ArrayList;
public class CredentialList extends ArrayList<Credential> implements Serializable{}

126
src/main/java/CredentialRepository.java

@ -1,15 +1,36 @@
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* Credential Repository for handling user credentials
* @author Claudia Metzler
*/
public class CredentialRepository implements CredentialRepositoryInterface{ public class CredentialRepository implements CredentialRepositoryInterface{
private int idCounter = 0; private int idCounter = 0;
private ArrayList<Credential> credentials;
private CredentialList credentials;
/**
* Konstruktor
*
* Initializes instance variable ArrayList
*/
public CredentialRepository() public CredentialRepository()
{ {
this.credentials = new ArrayList<Credential>();
this.credentials = new CredentialList();
} }
/**
* Create a new Set of User credentials
* @param name
* @param password
* @return size of repositoy - arraylist for checking
*/
public int createNewCredential(String name, String password) public int createNewCredential(String name, String password)
{ {
try { try {
@ -23,6 +44,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{
return this.getListSize(); return this.getListSize();
} }
/**
* Function to return a specific Credential via Name input
* @param needle [ aka to-search-for ]
* @return usercredential | NULL
*/
@Override @Override
public Credential getCredentialsViaName(String needle) { public Credential getCredentialsViaName(String needle) {
@ -38,6 +65,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{
} }
/**
* Function to return a specific Credential via ID
* @param id
* @return usercredential | NULL
*/
@Override @Override
public Credential getCredentialsViaId(int id) { public Credential getCredentialsViaId(int id) {
@ -51,13 +84,11 @@ public class CredentialRepository implements CredentialRepositoryInterface{
} }
/*
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
/**
* Function to update userpassword, assuming we already know the userid
* @param index
* @param newPassword
*/ */
@Override @Override
public void updatePassword(int index, String newPassword) { public void updatePassword(int index, String newPassword) {
@ -71,6 +102,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{
} }
/**
* Function to update username analoge to updatePassword
* @param index
* @param newUsername
*/
public void updateUsername(int index, String newUsername){ public void updateUsername(int index, String newUsername){
try{ try{
@ -82,6 +119,11 @@ public class CredentialRepository implements CredentialRepositoryInterface{
} }
} }
/**
* Deletion function for credentials
* Will immediately delete - double check for user permission
* @param index
*/
@Override @Override
public void delete(int index) { public void delete(int index) {
@ -89,28 +131,68 @@ public class CredentialRepository implements CredentialRepositoryInterface{
} }
/*
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
/**
* Function for serialization of all Credentials
* @param fileName
*/
public void serializeObject(String fileName)
{
if(fileName.equals("")) return;
private int mapIndexToListCounter(int needle) {
for(int c = 0; c<this.credentials.size(); c++)
try {
FileOutputStream file = new FileOutputStream(this.getWorkingDirectory() + fileName + ".claud11");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(this.credentials);
} catch (Exception fail) {
System.err.println("Serialization failed!");
fail.printStackTrace();
}
}
/**
* Function to load serialized Objects from hard drive by path
* @param fileName
*/
public void deserializeObjects(String fileName)
{
if(fileName.equals("")) return;
try {
FileInputStream file = new FileInputStream(this.getWorkingDirectory() + fileName + ".claud11");
ObjectInputStream in = new ObjectInputStream(file);
this.credentials = (CredentialList)in.readObject();
in.close();
file.close();
} catch(Exception fail)
{ {
if(this.credentials.get(c).getId() == needle)
{
return c;
}
System.err.println("Loading of CredentialRepository failed");
} }
return -1;
} }
*/
public int getListSize()
/**
* helper function to check list size after insertion / deletion
* @return
*/
private int getListSize()
{ {
return this.credentials.size(); return this.credentials.size();
} }
/**
* Helper function for serialization
* @return
*/
private String getWorkingDirectory()
{
return System.getProperty("user.dir");
}
} }

35
src/test/java/CredentialRepositoryTest.java

@ -81,4 +81,39 @@ class CredentialRepositoryTest {
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(0)); Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaId(0));
} }
@Test
void testSerializeObject(){
this.credentialRepository.createNewCredential("mock", "mock");
this.credentialRepository.serializeObject("testName");
}
@Test
void testLoadingOfSerializedObject(){
this.credentialRepository.createNewCredential("mock1", "mock1");
this.credentialRepository.createNewCredential("mock2", "mock2");
this.credentialRepository.createNewCredential("mock3", "mock3");
Credential temporary1 = this.credentialRepository.getCredentialsViaId(0);
Credential temporary2 = this.credentialRepository.getCredentialsViaId(1);
Credential temporary3 = this.credentialRepository.getCredentialsViaId(2);
this.credentialRepository.serializeObject("unitTest_Deserialization");
//delete entries to proove we really load something
this.credentialRepository.delete(0);
this.credentialRepository.delete(0);
Assertions.assertEquals(null, this.credentialRepository.getCredentialsViaName("mock2") );
//load and deserialize object and proove objects are back
this.credentialRepository.deserializeObjects("unitTest_Deserialization");
Assertions.assertEquals(temporary1.getName(), this.credentialRepository.getCredentialsViaName("mock1").getName());
Assertions.assertEquals(temporary1.getPassword(), this.credentialRepository.getCredentialsViaName("mock1").getPassword());
Assertions.assertEquals(temporary2.getName(), this.credentialRepository.getCredentialsViaName("mock2").getName());
Assertions.assertEquals(temporary2.getPassword(), this.credentialRepository.getCredentialsViaName("mock2").getPassword());
Assertions.assertEquals(temporary3.getName(), this.credentialRepository.getCredentialsViaName("mock3").getName());
Assertions.assertEquals(temporary3.getPassword(), this.credentialRepository.getCredentialsViaName("mock3").getPassword());
}
} }
Loading…
Cancel
Save