tests #43

Merged
sebastian merged 41 commits from tests into master 2023-11-11 11:20:50 +01:00
3 changed files with 71 additions and 81 deletions
Showing only changes of commit 2b9e821cf2 - Show all commits

View File

@ -4,11 +4,10 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="3a869f59-290a-4ab2-b036-a878ce801bc4" name="Changes" comment="Test TaskRepository">
<change afterPath="$PROJECT_DIR$/src/test/java/core/taskgroups/TaskgroupServiceTest.java" afterDir="false" />
<list default="true" id="3a869f59-290a-4ab2-b036-a878ce801bc4" name="Changes" comment="Initialize Testing of Testservice">
<change afterPath="$PROJECT_DIR$/src/test/resources/userRepisotoryTestEntries.sql" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/resources/application.properties" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/core/users/UserRepositoryTests.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/core/users/UserRepositoryTests.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -87,81 +86,82 @@
<recent name="$PROJECT_DIR$/src/test/java/core/taskgroups" />
</key>
</component>
<component name="RunManager" selected="JUnit.TaskgroupServiceTest.getTaskgroupByIDAndUsername">
<configuration name="TaskRepositoryTest.findAllActive" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<component name="RunManager" selected="JUnit.UserRepositoryTests.test_countUsers">
<configuration name="UserRepositoryTests.test_countUsers" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.tasks.*" />
<option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="core.tasks" />
<option name="MAIN_CLASS_NAME" value="core.tasks.TaskRepositoryTest" />
<option name="METHOD_NAME" value="findAllActive" />
<option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="test_countUsers" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TaskRepositoryTest.findAllOverdue" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="UserRepositoryTests.test_deleteUserByName" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.tasks.*" />
<option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="core.tasks" />
<option name="MAIN_CLASS_NAME" value="core.tasks.TaskRepositoryTest" />
<option name="METHOD_NAME" value="findAllOverdue" />
<option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="test_deleteUserByName" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TaskRepositoryTest.findAllUpcoming" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="UserRepositoryTests.test_existsByMail" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.tasks.*" />
<option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="core.tasks" />
<option name="MAIN_CLASS_NAME" value="core.tasks.TaskRepositoryTest" />
<option name="METHOD_NAME" value="findAllUpcoming" />
<option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="test_existsByMail" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TaskgroupServiceTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="UserRepositoryTests.test_existsByUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.taskgroups.*" />
<option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="TEST_OBJECT" value="class" />
<option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="test_existsByUsername" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TaskgroupServiceTest.getTaskgroupByIDAndUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="UserRepositoryTests.test_findByUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.taskgroups.*" />
<option name="PATTERN" value="core.users.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="METHOD_NAME" value="getTaskgroupByIDAndUsername" />
<option name="PACKAGE_NAME" value="core.users" />
<option name="MAIN_CLASS_NAME" value="core.users.UserRepositoryTests" />
<option name="METHOD_NAME" value="test_findByUsername" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
@ -176,11 +176,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.TaskgroupServiceTest.getTaskgroupByIDAndUsername" />
<item itemvalue="JUnit.TaskgroupServiceTest" />
<item itemvalue="JUnit.TaskRepositoryTest.findAllActive" />
<item itemvalue="JUnit.TaskRepositoryTest.findAllUpcoming" />
<item itemvalue="JUnit.TaskRepositoryTest.findAllOverdue" />
<item itemvalue="JUnit.UserRepositoryTests.test_countUsers" />
<item itemvalue="JUnit.UserRepositoryTests.test_deleteUserByName" />
<item itemvalue="JUnit.UserRepositoryTests.test_existsByUsername" />
<item itemvalue="JUnit.UserRepositoryTests.test_existsByMail" />
<item itemvalue="JUnit.UserRepositoryTests.test_findByUsername" />
</list>
</recent_temporary>
</component>
@ -217,7 +217,7 @@
<workItem from="1699460935890" duration="5000" />
<workItem from="1699466564154" duration="6646000" />
<workItem from="1699473376129" duration="1423000" />
<workItem from="1699639316405" duration="5468000" />
<workItem from="1699639316405" duration="6148000" />
</task>
<task id="LOCAL-00001" summary="Structure Taskgroups in Hierarchies">
<option name="closed" value="true" />
@ -435,14 +435,21 @@
<option name="project" value="LOCAL" />
<updated>1699641346158</updated>
</task>
<option name="localTasksCounter" value="28" />
<task id="LOCAL-00028" summary="Initialize Testing of Testservice">
<option name="closed" value="true" />
<created>1699645467388</created>
<option name="number" value="00028" />
<option name="presentableId" value="LOCAL-00028" />
<option name="project" value="LOCAL" />
<updated>1699645467388</updated>
</task>
<option name="localTasksCounter" value="29" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Implementing edit route for tasks" />
<MESSAGE value="Delete and clear Tasks" />
<MESSAGE value="Delete and clear Tasks (Frontend)" />
<MESSAGE value="Define Entity BasicTaskSchedule and Taskcontroller" />
@ -467,7 +474,8 @@
<MESSAGE value="TaskgroupRepsitoryTest (+Delete)" />
<MESSAGE value="FindAlltasksByUser Test" />
<MESSAGE value="Test TaskRepository" />
<option name="LAST_COMMIT_MESSAGE" value="Test TaskRepository" />
<MESSAGE value="Initialize Testing of Testservice" />
<option name="LAST_COMMIT_MESSAGE" value="Initialize Testing of Testservice" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
@ -497,11 +505,6 @@
<line>57</line>
<option name="timeStamp" value="12" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/test/java/core/taskgroups/TaskgroupServiceTest.java</url>
<line>50</line>
<option name="timeStamp" value="21" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>

View File

@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
@ -16,7 +17,6 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
@DirtiesContext
@ExtendWith(SpringExtension.class)
public class UserRepositoryTests {
@ -27,76 +27,61 @@ public class UserRepositoryTests {
private TestEntityManager testEntityManager;
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
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());
//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");
//Situation 1: User with name "FawKes100" is present
User referenceUser = testEntityManager.find(User.class, 1L);
Optional<User> findResult = userRepository.findByUsername(referenceUser.getUsername());
assertTrue(findResult.isPresent());
assertEquals(new User("FawKes100", "mail@fawkes100.de", "123456"),findResult.get());
assertEquals(referenceUser,findResult.get());
//Situation 3: No user with the required name is present
//Situation 2: No user with the required name is present
findResult = userRepository.findByUsername("fawkes1001");
assertFalse(findResult.isPresent());
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_existsByMail() {
User referenceUser = testEntityManager.find(User.class, 1L);
//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"));
assertTrue(userRepository.existsByEmail(referenceUser.getEmail()));
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_existsByUsername() {
//Situation 1: Empty database
User referenceUser = testEntityManager.find(User.class, 1L);
//Situation 1: No such user
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"));
assertTrue(userRepository.existsByUsername(referenceUser.getUsername()));
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
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"));
User referenceUser1 = testEntityManager.find(User.class, 1L);
User referenceUser2 = testEntityManager.find(User.class, 2L);
userRepository.deleteByUsername("FawKes100");
userRepository.deleteByUsername(referenceUser2.getUsername());
assertThat(testEntityManager.find(User.class, user.getId())).isNull();
assertThat(testEntityManager.find(User.class, user1.getId())).isNotNull();
assertThat(testEntityManager.find(User.class, referenceUser2.getId())).isNull();
assertThat(testEntityManager.find(User.class, referenceUser1.getId())).isNotNull();
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_countUsers() {
assertEquals(0, userRepository.countUsers());
testEntityManager.persist(new User("FawKes100", "mail@fawkes100.de", "123456"));
assertEquals(1, userRepository.countUsers());
assertEquals(2, userRepository.countUsers());
}
}

View File

@ -0,0 +1,2 @@
INSERT INTO users (id, username, email, password) VALUES (1, 'Testuser1', 'test1@test.com', '123456');
INSERT INTO users (id, username, email, password) VALUES (2, 'Testuser2', 'test2@test.com', '123456');