tests #43
@ -103,11 +103,11 @@ public class User {
 | 
				
			|||||||
        if (this == o) return true;
 | 
					        if (this == o) return true;
 | 
				
			||||||
        if (o == null || getClass() != o.getClass()) return false;
 | 
					        if (o == null || getClass() != o.getClass()) return false;
 | 
				
			||||||
        User user = (User) o;
 | 
					        User user = (User) o;
 | 
				
			||||||
        return Objects.equals(id, user.id) && Objects.equals(username, user.username) && Objects.equals(email, user.email) && Objects.equals(password, user.password) && Objects.equals(roles, user.roles);
 | 
					        return Objects.equals(username, user.username) && Objects.equals(email, user.email) && Objects.equals(password, user.password) && Objects.equals(roles, user.roles);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public int hashCode() {
 | 
					    public int hashCode() {
 | 
				
			||||||
        return Objects.hash(id, username, email, password, roles);
 | 
					        return Objects.hash(username, email, password, roles);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -12,6 +12,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.Optional;
 | 
					import java.util.Optional;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
 | 
				
			||||||
import static org.junit.jupiter.api.Assertions.*;
 | 
					import static org.junit.jupiter.api.Assertions.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@DataJpaTest
 | 
					@DataJpaTest
 | 
				
			||||||
@ -27,11 +28,75 @@ public class UserRepositoryTests {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    void test_findByUsername() {
 | 
					    void test_findByUsername() {
 | 
				
			||||||
 | 
					        //Situation 1: No user is present in the database, so no user should be found by a given username
 | 
				
			||||||
        assertTrue(userRepository.findByUsername("FawKes100").isEmpty());
 | 
					        assertTrue(userRepository.findByUsername("FawKes100").isEmpty());
 | 
				
			||||||
        userRepository.save(new User(1L,"FawKes100", "mail@fawkes100.de", "123456"));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //persist a user to the database, so a user is present
 | 
				
			||||||
 | 
					        testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 2: User with name "FawKes100" is present
 | 
				
			||||||
        Optional<User> findResult = userRepository.findByUsername("FawKes100");
 | 
					        Optional<User> findResult = userRepository.findByUsername("FawKes100");
 | 
				
			||||||
        assertTrue(findResult.isPresent());
 | 
					        assertTrue(findResult.isPresent());
 | 
				
			||||||
        assertEquals(new User(1L, "FawKes100", "mail@fawkes100.de", "123456"),findResult.get());
 | 
					        assertEquals(new User("FawKes100", "mail@fawkes100.de", "123456"),findResult.get());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 3: No user with the required name is present
 | 
				
			||||||
 | 
					        findResult = userRepository.findByUsername("fawkes1001");
 | 
				
			||||||
 | 
					        assertFalse(findResult.isPresent());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    void test_existsByMail() {
 | 
				
			||||||
 | 
					        //Situation 1: invalid email format should not matter
 | 
				
			||||||
 | 
					        assertFalse(userRepository.existsByEmail("FawKes100"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 2: No user exists with such a mail
 | 
				
			||||||
 | 
					        assertFalse(userRepository.existsByEmail("mail@fawkes100.de"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //persist a user to the database, so a user is present
 | 
				
			||||||
 | 
					        testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 3: User with this mail exists
 | 
				
			||||||
 | 
					        assertTrue(userRepository.existsByEmail("mail@fawkes100.de"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 4: No user exists with such a mail (non-empty db)
 | 
				
			||||||
 | 
					        assertFalse(userRepository.existsByEmail("mail2@fawkes100.de"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    void test_existsByUsername() {
 | 
				
			||||||
 | 
					        //Situation 1: Empty database
 | 
				
			||||||
 | 
					        assertFalse(userRepository.existsByUsername("FawKes100"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //persist a user to the database, so a user is present
 | 
				
			||||||
 | 
					        testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 3: User with this name exists
 | 
				
			||||||
 | 
					        assertTrue(userRepository.existsByUsername("FawKes100"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //Situation 4: No user exists with such a mail (non-empty db)
 | 
				
			||||||
 | 
					        assertFalse(userRepository.existsByUsername("FawKes101"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    void test_deleteUserByName() {
 | 
				
			||||||
 | 
					        //persist a user to the database, so a user is present
 | 
				
			||||||
 | 
					        User user = testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
 | 
				
			||||||
 | 
					        User user1 = testEntityManager.persist(new User("FawKes101", "mail@fawkes101.de", "123456"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        userRepository.deleteByUsername("FawKes100");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertThat(testEntityManager.find(User.class, user.getId())).isNull();
 | 
				
			||||||
 | 
					        assertThat(testEntityManager.find(User.class, user1.getId())).isNotNull();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    void test_countUsers() {
 | 
				
			||||||
 | 
					        assertEquals(0, userRepository.countUsers());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
 | 
				
			||||||
 | 
					        assertEquals(1, userRepository.countUsers());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user