Testcases (and failing test fixes) of TaskgroupService
Some checks failed
Java CI with Maven / build (push) Failing after 39s

This commit is contained in:
Sebastian Böckelmann 2023-11-11 07:54:13 +01:00
parent 2b9e821cf2
commit 542e04659b
5 changed files with 189 additions and 48 deletions

View File

@ -4,10 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<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" />
<list default="true" id="3a869f59-290a-4ab2-b036-a878ce801bc4" name="Changes" comment="Use @Sql instead of persit()">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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" />
<change beforePath="$PROJECT_DIR$/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupFieldInfo.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/core/api/models/timemanager/taskgroup/TaskgroupFieldInfo.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/core/entities/timemanager/Taskgroup.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/core/entities/timemanager/Taskgroup.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/core/services/TaskgroupService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/core/services/TaskgroupService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/core/taskgroups/TaskgroupServiceTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/core/taskgroups/TaskgroupServiceTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -86,82 +88,82 @@
<recent name="$PROJECT_DIR$/src/test/java/core/taskgroups" />
</key>
</component>
<component name="RunManager" selected="JUnit.UserRepositoryTests.test_countUsers">
<configuration name="UserRepositoryTests.test_countUsers" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<component name="RunManager" selected="JUnit.TaskgroupServiceTest.deleteTaskgroupByUser">
<configuration name="TaskgroupServiceTest.addTaskgroup" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.users.*" />
<option name="PATTERN" value="core.taskgroups.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<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="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="METHOD_NAME" value="addTaskgroup" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="UserRepositoryTests.test_deleteUserByName" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="TaskgroupServiceTest.deleteTaskgroup" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.users.*" />
<option name="PATTERN" value="core.taskgroups.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<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="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="METHOD_NAME" value="deleteTaskgroup" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="UserRepositoryTests.test_existsByMail" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="TaskgroupServiceTest.deleteTaskgroupByUser" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.users.*" />
<option name="PATTERN" value="core.taskgroups.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<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="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="METHOD_NAME" value="deleteTaskgroupByUser" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="UserRepositoryTests.test_existsByUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="TaskgroupServiceTest.editTaskgroup" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.users.*" />
<option name="PATTERN" value="core.taskgroups.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<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="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="METHOD_NAME" value="editTaskgroup" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="UserRepositoryTests.test_findByUsername" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<configuration name="TaskgroupServiceTest.getTopTaskgroupsByUser" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="demo" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="core.users.*" />
<option name="PATTERN" value="core.taskgroups.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<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="PACKAGE_NAME" value="core.taskgroups" />
<option name="MAIN_CLASS_NAME" value="core.taskgroups.TaskgroupServiceTest" />
<option name="METHOD_NAME" value="getTopTaskgroupsByUser" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
@ -176,11 +178,11 @@
</configuration>
<recent_temporary>
<list>
<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" />
<item itemvalue="JUnit.TaskgroupServiceTest.deleteTaskgroupByUser" />
<item itemvalue="JUnit.TaskgroupServiceTest.getTopTaskgroupsByUser" />
<item itemvalue="JUnit.TaskgroupServiceTest.deleteTaskgroup" />
<item itemvalue="JUnit.TaskgroupServiceTest.editTaskgroup" />
<item itemvalue="JUnit.TaskgroupServiceTest.addTaskgroup" />
</list>
</recent_temporary>
</component>
@ -217,7 +219,8 @@
<workItem from="1699460935890" duration="5000" />
<workItem from="1699466564154" duration="6646000" />
<workItem from="1699473376129" duration="1423000" />
<workItem from="1699639316405" duration="6148000" />
<workItem from="1699639316405" duration="9267000" />
<workItem from="1699684493731" duration="1121000" />
</task>
<task id="LOCAL-00001" summary="Structure Taskgroups in Hierarchies">
<option name="closed" value="true" />
@ -443,14 +446,21 @@
<option name="project" value="LOCAL" />
<updated>1699645467388</updated>
</task>
<option name="localTasksCounter" value="29" />
<task id="LOCAL-00029" summary="Use @Sql instead of persit()">
<option name="closed" value="true" />
<created>1699646291964</created>
<option name="number" value="00029" />
<option name="presentableId" value="LOCAL-00029" />
<option name="project" value="LOCAL" />
<updated>1699646291964</updated>
</task>
<option name="localTasksCounter" value="30" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Delete and clear Tasks" />
<MESSAGE value="Delete and clear Tasks (Frontend)" />
<MESSAGE value="Define Entity BasicTaskSchedule and Taskcontroller" />
<MESSAGE value="Delete Schedules" />
@ -475,16 +485,12 @@
<MESSAGE value="FindAlltasksByUser Test" />
<MESSAGE value="Test TaskRepository" />
<MESSAGE value="Initialize Testing of Testservice" />
<option name="LAST_COMMIT_MESSAGE" value="Initialize Testing of Testservice" />
<MESSAGE value="Use @Sql instead of persit()" />
<option name="LAST_COMMIT_MESSAGE" value="Use @Sql instead of persit()" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/core/services/TaskgroupService.java</url>
<line>52</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/core/services/TaskScheduleService.java</url>
<line>87</line>

View File

@ -15,6 +15,14 @@ public class TaskgroupFieldInfo {
private long parentID;
public TaskgroupFieldInfo(String name, long parentID) {
this.name = name;
this.parentID = parentID;
}
public TaskgroupFieldInfo() {
}
public String getName() {
return name;
}

View File

@ -11,7 +11,7 @@ import java.util.*;
public class Taskgroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long taskgroupID;
@NotBlank

View File

@ -80,10 +80,6 @@ public class TaskgroupService {
taskgroupRepository.delete(taskgroup);
}
public List<Taskgroup> getTaskgroupsByUser(String username) {
return taskgroupRepository.findAllByUser(username);
}
public List<Taskgroup> getTopTaskgroupsByUser(String username) {
return taskgroupRepository.findAllTopTaskgroupsByUser(username);
}

View File

@ -1,11 +1,14 @@
package core.taskgroups;
import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo;
import core.entities.User;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.UserRepository;
import core.repositories.timemanager.TaskgroupRepository;
import core.services.PermissionResult;
import core.services.ServiceExitCode;
import core.services.ServiceResult;
import core.services.TaskgroupService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -23,6 +26,9 @@ import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@ -35,8 +41,11 @@ public class TaskgroupServiceTest {
private TaskgroupRepository taskgroupRepository;
@Autowired
private TaskgroupService taskgroupService;
@Autowired
private EntityManager entityManager;
private static final String username = "Testuser1";
private static final String username2 = "Testuser2";
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@ -65,4 +74,126 @@ public class TaskgroupServiceTest {
assertFalse(invalid_user.isHasPermissions());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void addTaskgroup() {
TaskgroupFieldInfo taskgroupFieldInfo = new TaskgroupFieldInfo("Taskgroup", -1);
//Situation 1: No such user
assertThrows(NoSuchElementException.class, () -> taskgroupService.addTaskgroup(taskgroupFieldInfo, "Rotzbakke"));
//Situation 2: Taskgroup already exists
Taskgroup taskgroup = taskgroupRepository.findById(1L).get();
ServiceResult<Taskgroup> creationResult = taskgroupService.addTaskgroup(new TaskgroupFieldInfo(taskgroup.getTaskgroupName(), -1), username);
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, creationResult.getExitCode());
assertThat(creationResult.getResult()).isNull();
//Situation 3: Taskgroup does not exist, no parent
ServiceResult<Taskgroup> creationResult_3 = taskgroupService.addTaskgroup(taskgroupFieldInfo, username);
assertEquals(ServiceExitCode.OK, creationResult_3.getExitCode());
assertTrue(taskgroupRepository.existsById(creationResult_3.getResult().getTaskgroupID()));
//Situation 4: Taskgroup does not exist, with parent; invalid parent (no existent)
ServiceResult<Taskgroup> creationResult_4 = taskgroupService.addTaskgroup(new TaskgroupFieldInfo("Situation 4", 100L), username);
assertEquals(ServiceExitCode.MISSING_ENTITY, creationResult_4.getExitCode());
//Situation 5: Taskgroup does not exist, parent exist
ServiceResult<Taskgroup> creationResult_5 = taskgroupService.addTaskgroup(new TaskgroupFieldInfo("Situation 5", 2L), username);
assertEquals(ServiceExitCode.OK, creationResult_5.getExitCode());
assertTrue(taskgroupRepository.existsById(creationResult_5.getResult().getTaskgroupID()));
//Situation 6: taskgroup exist on another user
ServiceResult<Taskgroup> creationResult_6 = taskgroupService.addTaskgroup(new TaskgroupFieldInfo(taskgroup.getTaskgroupName(), -1), username2);
assertEquals(ServiceExitCode.OK, creationResult_6.getExitCode());
assertTrue(taskgroupRepository.existsById(creationResult_6.getResult().getTaskgroupID()));
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void editTaskgroup() {
Taskgroup taskgroup = taskgroupRepository.findById(1L).get();
//Situation 1: Nothing changed
ServiceExitCode exitCode_1 = taskgroupService.editTaskgroup(taskgroup, new TaskgroupFieldInfo(taskgroup.getTaskgroupName(),-1));
assertEquals(ServiceExitCode.OK, exitCode_1);
//Situation 2: Name is already taken
ServiceExitCode exitCode_2 = taskgroupService.editTaskgroup(taskgroup, new TaskgroupFieldInfo("Taskgroup 1", -1));
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, exitCode_2);
//Situation 3: All fine
ServiceExitCode exitCode_3 = taskgroupService.editTaskgroup(taskgroup, new TaskgroupFieldInfo("Situation 3", -1));
assertEquals(ServiceExitCode.OK, exitCode_3);
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void deleteTaskgroup() {
taskgroupService.deleteTaskgroup(entityManager.find(Taskgroup.class, 1L));
assertThat(entityManager.find(Taskgroup.class, 1L)).isNull();
taskgroupService.deleteTaskgroup(entityManager.find(Taskgroup.class, 2L));
assertThat(entityManager.find(Taskgroup.class, 2L)).isNull();
assertThat(entityManager.find(Taskgroup.class, 3L)).isNull();
for(long i=1; i<=14; i++) {
assertThat(entityManager.find(Task.class, i)).isNull();
}
taskgroupService.deleteTaskgroup(entityManager.find(Taskgroup.class, 5L));
assertThat(entityManager.find(Taskgroup.class, 5L)).isNull();
assertThat(entityManager.find(Taskgroup.class, 6L)).isNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void getTopTaskgroupsByUser() {
//Situation 1: User without taskgroups
List<Taskgroup> taskgroups_user2 = taskgroupService.getTopTaskgroupsByUser(username2);
assertEquals(0, taskgroups_user2.size());
//Situation 2: user with taskgroups
List<Taskgroup> taskgroups_user1 = taskgroupService.getTopTaskgroupsByUser(username);
assertEquals(3, taskgroups_user1.size());
assertTrue(taskgroups_user1.contains(entityManager.find(Taskgroup.class, 1L)));
assertTrue(taskgroups_user1.contains(entityManager.find(Taskgroup.class, 2L)));
assertTrue(taskgroups_user1.contains(entityManager.find(Taskgroup.class, 5L)));
//Situation 3: No existent username
assertEquals(0, taskgroupService.getTopTaskgroupsByUser("Rotzbakke").size());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void deleteTaskgroupByUser() {
User referenceUser1 = entityManager.find(User.class, 1L);
User referenceUser2 = entityManager.find(User.class, 2L);
taskgroupService.deleteTaskgroupByUser(referenceUser2);
for(long i=1; i<=8; i++) {
assertThat(entityManager.find(Taskgroup.class, i)).isNotNull();
}
taskgroupService.deleteTaskgroupByUser(referenceUser1);
for(long i=1; i<=8; i++) {
assertThat(entityManager.find(Taskgroup.class, i)).isNull();
}
for(long i=1; i<=14; i++) {
assertThat(entityManager.find(Task.class, i)).isNull();
}
}
}