Repository: urlaubsverwaltung/urlaubsverwaltung (java)

Issue:
Abteilungen werden nicht alphabetisch sortiert im Konto
![grafik](https://github.com/user-attachments/assets/ac5d03f5-67e2-49d2-9a13-e50996c80b92)

Pull request:
4791 sort departments
closes #4791


Patch:
diff --git a/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentService.java b/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentService.java
index ef7227aca..0233333ea 100644
--- a/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentService.java
+++ b/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentService.java
@@ -72,7 +72,7 @@ public interface DepartmentService {
      * Finds all departments the given person is member of.
      *
      * @param member to get the departments of
-     * @return list of departments the given person is assigned to
+     * @return list of departments the given person is assigned to ordered by the department name
      */
     List<Department> getAssignedDepartmentsOfMember(Person member);
 
@@ -80,7 +80,7 @@ public interface DepartmentService {
      * Finds all departments the given person is set as department head.
      *
      * @param departmentHead to get the departments of
-     * @return list of departments the department head manages
+     * @return list of departments the department head manages ordered by the department name
      */
     List<Department> getManagedDepartmentsOfDepartmentHead(Person departmentHead);
 
@@ -88,7 +88,7 @@ public interface DepartmentService {
      * Finds all departments the given person is set as second stage authority.
      *
      * @param secondStageAuthority to get the departments of
-     * @return list of departments the second stage authority manages
+     * @return list of departments the second stage authority manages ordered by the department name
      */
     List<Department> getManagedDepartmentsOfSecondStageAuthority(Person secondStageAuthority);
 
diff --git a/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImpl.java b/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImpl.java
index f6027a616..77d07defc 100644
--- a/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImpl.java
+++ b/src/main/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImpl.java
@@ -278,6 +278,7 @@ class DepartmentServiceImpl implements DepartmentService {
     public List<Department> getAssignedDepartmentsOfMember(Person member) {
         return departmentRepository.findByMembersPerson(member).stream()
             .map(this::mapToDepartment)
+            .sorted(departmentComparator())
             .collect(toList());
     }
 
@@ -285,6 +286,7 @@ class DepartmentServiceImpl implements DepartmentService {
     public List<Department> getManagedDepartmentsOfDepartmentHead(Person departmentHead) {
         return departmentRepository.findByDepartmentHeads(departmentHead).stream()
             .map(this::mapToDepartment)
+            .sorted(departmentComparator())
             .collect(toList());
     }
 
@@ -292,6 +294,7 @@ class DepartmentServiceImpl implements DepartmentService {
     public List<Department> getManagedDepartmentsOfSecondStageAuthority(Person secondStageAuthority) {
         return departmentRepository.findBySecondStageAuthorities(secondStageAuthority).stream()
             .map(this::mapToDepartment)
+            .sorted(departmentComparator())
             .collect(toList());
     }
 


Test patch:
diff --git a/src/test/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImplTest.java b/src/test/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImplTest.java
index 8e289272f..5d04fd6ab 100644
--- a/src/test/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImplTest.java
+++ b/src/test/java/org/synyx/urlaubsverwaltung/department/DepartmentServiceImplTest.java
@@ -1164,32 +1164,98 @@ class DepartmentServiceImplTest {
 
     @Test
     void ensureGetManagedDepartmentsOfDepartmentHeadCallCorrectDAOMethod() {
+        final Person person = new Person();
+        sut.getManagedDepartmentsOfDepartmentHead(person);
+        verify(departmentRepository).findByDepartmentHeads(person);
+    }
+
+    @Test
+    void ensureGetManagedDepartmentsOfDepartmentHeadDepartmentSorted() {
+
+        final DepartmentEntity departmentEntityA = new DepartmentEntity();
+        departmentEntityA.setId(1L);
+        departmentEntityA.setName("Department A");
+        final DepartmentEntity departmentEntityB = new DepartmentEntity();
+        departmentEntityB.setId(2L);
+        departmentEntityB.setName("Department B");
+
+        final Department departmentA = new Department();
+        departmentA.setId(1L);
+        final Department departmentB = new Department();
+        departmentB.setId(2L);
 
         final Person person = new Person();
 
-        sut.getManagedDepartmentsOfDepartmentHead(person);
+        when(departmentRepository.findByDepartmentHeads(person)).thenReturn(List.of(departmentEntityB, departmentEntityA));
 
-        verify(departmentRepository).findByDepartmentHeads(person);
+        final List<Department> allDepartments = sut.getManagedDepartmentsOfDepartmentHead(person);
+        assertThat(allDepartments)
+            .hasSize(2)
+            .containsExactly(departmentA, departmentB);
     }
 
     @Test
     void ensureGetManagedDepartmentsOfSecondStageAuthorityCallCorrectDAOMethod() {
+        final Person person = new Person();
+        sut.getManagedDepartmentsOfSecondStageAuthority(person);
+        verify(departmentRepository).findBySecondStageAuthorities(person);
+    }
+
+    @Test
+    void ensureGetManagedDepartmentsOfSecondStageAuthorityDepartmentSorted() {
+
+        final DepartmentEntity departmentEntityA = new DepartmentEntity();
+        departmentEntityA.setId(1L);
+        departmentEntityA.setName("Department A");
+        final DepartmentEntity departmentEntityB = new DepartmentEntity();
+        departmentEntityB.setId(2L);
+        departmentEntityB.setName("Department B");
+
+        final Department departmentA = new Department();
+        departmentA.setId(1L);
+        final Department departmentB = new Department();
+        departmentB.setId(2L);
 
         final Person person = new Person();
 
-        sut.getManagedDepartmentsOfSecondStageAuthority(person);
+        when(departmentRepository.findBySecondStageAuthorities(person)).thenReturn(List.of(departmentEntityB, departmentEntityA));
 
-        verify(departmentRepository).findBySecondStageAuthorities(person);
+        final List<Department> allDepartments = sut.getManagedDepartmentsOfSecondStageAuthority(person);
+        assertThat(allDepartments)
+            .hasSize(2)
+            .containsExactly(departmentA, departmentB);
     }
 
     @Test
     void ensureGetAssignedDepartmentsOfMemberCallCorrectDAOMethod() {
+        final Person person = new Person();
+        sut.getAssignedDepartmentsOfMember(person);
+        verify(departmentRepository).findByMembersPerson(person);
+    }
+
+    @Test
+    void ensureGetAssignedDepartmentsOfMemberDepartmentSorted() {
+
+        final DepartmentEntity departmentEntityA = new DepartmentEntity();
+        departmentEntityA.setId(1L);
+        departmentEntityA.setName("Department A");
+        final DepartmentEntity departmentEntityB = new DepartmentEntity();
+        departmentEntityB.setId(2L);
+        departmentEntityB.setName("Department B");
+
+        final Department departmentA = new Department();
+        departmentA.setId(1L);
+        final Department departmentB = new Department();
+        departmentB.setId(2L);
 
         final Person person = new Person();
 
-        sut.getAssignedDepartmentsOfMember(person);
+        when(departmentRepository.findByMembersPerson(person)).thenReturn(List.of(departmentEntityB, departmentEntityA));
 
-        verify(departmentRepository).findByMembersPerson(person);
+        final List<Department> allDepartments = sut.getAssignedDepartmentsOfMember(person);
+        assertThat(allDepartments)
+            .hasSize(2)
+            .containsExactly(departmentA, departmentB);
     }
 
     @Test