diff --git a/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java b/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java index 6e01e3d5807dc857ea1821f911974d9a315ba9ef..72bab67e12a33c555e3ec101b13b3c4a15344231 100644 --- a/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java +++ b/src/test/java/NTNU/IDATT1002/repository/LoginRepositoryTest.java @@ -18,6 +18,13 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; +/** + * Tests for {@link LoginRepository} + * + * @author madslun + * @version 1.0 17.03.20 + */ + class LoginRepositoryTest { @@ -34,7 +41,10 @@ class LoginRepositoryTest { 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 public void setUp() { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("ImageApplicationTest"); @@ -52,12 +62,18 @@ class LoginRepositoryTest { loginRepository = new LoginRepository(entityManager); } + /** + * Test that saving an entity returns the saved instance + */ @Test void testSaveReturnsInstance() { Optional<Login> optionalLogin = loginRepository.save(login1); assertTrue(optionalLogin.isPresent()); } + /** + * Test that finding all entities returns all entities + */ @Test void testSaveReturnsAllSavedEntities() { @@ -68,6 +84,9 @@ class LoginRepositoryTest { assertEquals(2, foundLogins.size()); } + /** + * Test that saving invalid entity will fail and return empty optional + */ @Test void testSaveInvalidEntityReturnsEmptyOptional() { Optional<Login> savedLogin = loginRepository.save(null); @@ -75,6 +94,9 @@ class LoginRepositoryTest { assertTrue(savedLogin.isEmpty()); } + /** + * Test that finding entity by id returns optional with the correct id + */ @Test void testFindByIdReturnsOptionalWithEntityWithId() { @@ -85,6 +107,9 @@ class LoginRepositoryTest { assertEquals(id1, foundLogins.get().getUser().getUsername()); } + /** + * Test that deleting by id removes the given entity and returns empty optional + */ @Test void testDeleteById() { loginRepository.save(login1); @@ -96,6 +121,9 @@ class LoginRepositoryTest { assertTrue(deletedLogin.isEmpty()); } + /** + * Test that count returns correct amount of enities + */ @Test void testCountReturnsAmountOfSavedEntities() { loginRepository.save(login1); @@ -106,6 +134,9 @@ class LoginRepositoryTest { assertEquals(2, loginCount); } + /** + * Test that a created user can log in + */ @Test void testLogin() { ArrayList<String> credentials = Authentication.setPassword(password); @@ -117,6 +148,9 @@ class LoginRepositoryTest { assertTrue(loginRepository.logIn(id1, password)); } + /** + * Test that a created user can change the password + */ @Test void testChangePassword() { ArrayList<String> credentials = Authentication.setPassword(password); @@ -128,6 +162,9 @@ class LoginRepositoryTest { assertTrue(loginRepository.changePassword(id1, password, newPassword)); } + /** + * Test that a user can log in, change password then log in again + */ @Test void testLoginWithNewPassword() { ArrayList<String> credentials = Authentication.setPassword(password); @@ -141,6 +178,9 @@ class LoginRepositoryTest { assertTrue(loginRepository.logIn(id1, newPassword)); } + /** + * Test that trying to login with wrong password returns false + */ @Test void testWrongPasswordDoesNotLogIn() { ArrayList<String> credentials = Authentication.setPassword(password); @@ -151,6 +191,9 @@ class LoginRepositoryTest { assertFalse(loginRepository.logIn(id1, newPassword)); } + /** + * Test that trying to change password with wrong password returns false + */ @Test void testWrongPasswordDoesNotChangePassword() { ArrayList<String> credentials = Authentication.setPassword(password); @@ -162,6 +205,9 @@ class LoginRepositoryTest { assertTrue(loginRepository.logIn(id1, password)); } + /** + * Test that trying to login with null returns false + */ @Test void testLoginWithNullReturnsFalse() { ArrayList<String> credentials = Authentication.setPassword(password); @@ -172,6 +218,9 @@ class LoginRepositoryTest { assertFalse(loginRepository.logIn(id1, null)); } + /** + * Test that trying to login with null returns false + */ @Test void testChangeWithNullReturnsFalse() { ArrayList<String> credentials = Authentication.setPassword(password);