Skip to content
Snippets Groups Projects
Commit 6d7cfd61 authored by Mads Lundegaard's avatar Mads Lundegaard
Browse files

Added more javadoc

parent 665d9496
No related branches found
No related tags found
1 merge request!50Fix/login
Pipeline #76382 passed
...@@ -18,6 +18,13 @@ import java.util.Optional; ...@@ -18,6 +18,13 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
/**
* Tests for {@link LoginRepository}
*
* @author madslun
* @version 1.0 17.03.20
*/
class LoginRepositoryTest { class LoginRepositoryTest {
...@@ -34,7 +41,10 @@ class LoginRepositoryTest { ...@@ -34,7 +41,10 @@ class LoginRepositoryTest {
private Login login2; private Login login2;
/**
* Sets up some testdata for thorough testing
* So much information has been added for making sure every part works as intended
*/
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest"); EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest");
...@@ -52,12 +62,18 @@ class LoginRepositoryTest { ...@@ -52,12 +62,18 @@ class LoginRepositoryTest {
loginRepository = new LoginRepository(entityManager); loginRepository = new LoginRepository(entityManager);
} }
/**
* Test that saving an entity returns the saved instance
*/
@Test @Test
void testSaveReturnsInstance() { void testSaveReturnsInstance() {
Optional<Login> optionalLogin = loginRepository.save(login1); Optional<Login> optionalLogin = loginRepository.save(login1);
assertTrue(optionalLogin.isPresent()); assertTrue(optionalLogin.isPresent());
} }
/**
* Test that finding all entities returns all entities
*/
@Test @Test
void testSaveReturnsAllSavedEntities() { void testSaveReturnsAllSavedEntities() {
...@@ -68,6 +84,9 @@ class LoginRepositoryTest { ...@@ -68,6 +84,9 @@ class LoginRepositoryTest {
assertEquals(2, foundLogins.size()); assertEquals(2, foundLogins.size());
} }
/**
* Test that saving invalid entity will fail and return empty optional
*/
@Test @Test
void testSaveInvalidEntityReturnsEmptyOptional() { void testSaveInvalidEntityReturnsEmptyOptional() {
Optional<Login> savedLogin = loginRepository.save(null); Optional<Login> savedLogin = loginRepository.save(null);
...@@ -75,6 +94,9 @@ class LoginRepositoryTest { ...@@ -75,6 +94,9 @@ class LoginRepositoryTest {
assertTrue(savedLogin.isEmpty()); assertTrue(savedLogin.isEmpty());
} }
/**
* Test that finding entity by id returns optional with the correct id
*/
@Test @Test
void testFindByIdReturnsOptionalWithEntityWithId() { void testFindByIdReturnsOptionalWithEntityWithId() {
...@@ -85,6 +107,9 @@ class LoginRepositoryTest { ...@@ -85,6 +107,9 @@ class LoginRepositoryTest {
assertEquals(id1, foundLogins.get().getUser().getUsername()); assertEquals(id1, foundLogins.get().getUser().getUsername());
} }
/**
* Test that deleting by id removes the given entity and returns empty optional
*/
@Test @Test
void testDeleteById() { void testDeleteById() {
loginRepository.save(login1); loginRepository.save(login1);
...@@ -96,6 +121,9 @@ class LoginRepositoryTest { ...@@ -96,6 +121,9 @@ class LoginRepositoryTest {
assertTrue(deletedLogin.isEmpty()); assertTrue(deletedLogin.isEmpty());
} }
/**
* Test that count returns correct amount of enities
*/
@Test @Test
void testCountReturnsAmountOfSavedEntities() { void testCountReturnsAmountOfSavedEntities() {
loginRepository.save(login1); loginRepository.save(login1);
...@@ -106,6 +134,9 @@ class LoginRepositoryTest { ...@@ -106,6 +134,9 @@ class LoginRepositoryTest {
assertEquals(2, loginCount); assertEquals(2, loginCount);
} }
/**
* Test that a created user can log in
*/
@Test @Test
void testLogin() { void testLogin() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
...@@ -117,6 +148,9 @@ class LoginRepositoryTest { ...@@ -117,6 +148,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(id1, password)); assertTrue(loginRepository.logIn(id1, password));
} }
/**
* Test that a created user can change the password
*/
@Test @Test
void testChangePassword() { void testChangePassword() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
...@@ -128,6 +162,9 @@ class LoginRepositoryTest { ...@@ -128,6 +162,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.changePassword(id1, password, newPassword)); assertTrue(loginRepository.changePassword(id1, password, newPassword));
} }
/**
* Test that a user can log in, change password then log in again
*/
@Test @Test
void testLoginWithNewPassword() { void testLoginWithNewPassword() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
...@@ -141,6 +178,9 @@ class LoginRepositoryTest { ...@@ -141,6 +178,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(id1, newPassword)); assertTrue(loginRepository.logIn(id1, newPassword));
} }
/**
* Test that trying to login with wrong password returns false
*/
@Test @Test
void testWrongPasswordDoesNotLogIn() { void testWrongPasswordDoesNotLogIn() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
...@@ -151,6 +191,9 @@ class LoginRepositoryTest { ...@@ -151,6 +191,9 @@ class LoginRepositoryTest {
assertFalse(loginRepository.logIn(id1, newPassword)); assertFalse(loginRepository.logIn(id1, newPassword));
} }
/**
* Test that trying to change password with wrong password returns false
*/
@Test @Test
void testWrongPasswordDoesNotChangePassword() { void testWrongPasswordDoesNotChangePassword() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
...@@ -162,6 +205,9 @@ class LoginRepositoryTest { ...@@ -162,6 +205,9 @@ class LoginRepositoryTest {
assertTrue(loginRepository.logIn(id1, password)); assertTrue(loginRepository.logIn(id1, password));
} }
/**
* Test that trying to login with null returns false
*/
@Test @Test
void testLoginWithNullReturnsFalse() { void testLoginWithNullReturnsFalse() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
...@@ -172,6 +218,9 @@ class LoginRepositoryTest { ...@@ -172,6 +218,9 @@ class LoginRepositoryTest {
assertFalse(loginRepository.logIn(id1, null)); assertFalse(loginRepository.logIn(id1, null));
} }
/**
* Test that trying to login with null returns false
*/
@Test @Test
void testChangeWithNullReturnsFalse() { void testChangeWithNullReturnsFalse() {
ArrayList<String> credentials = Authentication.setPassword(password); ArrayList<String> credentials = Authentication.setPassword(password);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment