liujie
2026-04-01 4b33796be3887c81fa40fba2f55dce4489cff041
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.estate.expert.admin.service;
 
import com.estate.expert.admin.domain.standard.hierachy.Hierarchy;
import com.estate.expert.admin.domain.standard.hierachy.HierarchyData;
import com.estate.expert.admin.utils.tree.SimpleTreeRoot;
import com.estate.expert.admin.utils.tree.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
 
/**
 * @author ligf
 * @Description
 * @date 2023-09-05
 **/
@Slf4j
@SpringBootTest
@ExtendWith(SpringExtension.class)
@Transactional
public class
DepartmentServiceTest {
 
    @Autowired
    private HierarchyService departmentService;
 
    private Hierarchy hierarchy;
 
    @BeforeEach
    public void setUp() {
        TreeNode<HierarchyData> root = new TreeNode<HierarchyData>("总公司");
        root.setName("总公司");
 
        var c0 = root.addChild("子公司0");
        var c00 = c0.addChild("子公司0-0");
        var c01 = c0.addChild("子公司0-1");
        var c1 = root.addChild("子公司1");
        var c2 = root.addChild("子公司2");
        var c20 = c2.addChild("子公司2-1");
 
        hierarchy = new Hierarchy();
        hierarchy.setContent(new SimpleTreeRoot<>(root));
 
        departmentService.add(null, hierarchy);
    }
 
    @Test
    public void testRoot() {
        TreeNode<HierarchyData> root = departmentService.root(hierarchy.getId()).root();
        Assertions.assertEquals("总公司", root.getName());
        Assertions.assertEquals("/", root.getPath());
        Assertions.assertEquals(3, root.getChildren().size());
    }
 
    @Test
    public void testGet() {
        var department = departmentService.node(hierarchy.getId(),"/0", true);
        Assertions.assertEquals("子公司0", department.getName());
    }
 
    @Test
    public void testEdit() {
        departmentService.edit(hierarchy.getId(),"/0/0", "子公司0-0-X",null);
        var department = departmentService.node(hierarchy.getId(),"/0", true);
        Assertions.assertEquals("子公司0-0-X", department.getName());
    }
 
    @Test
    public void testAddChild() {
        departmentService.addChild(hierarchy.getId(),"/0", "子公司0-2",null);
        var department = departmentService.node(hierarchy.getId(),"/0/2", true);
        Assertions.assertEquals("子公司0-2", department.getName());
    }
 
    @Test
    public void testDeleteNode() {
        departmentService.deleteNode( hierarchy.getId(),"/0/0");
        var department = departmentService.node(hierarchy.getId(), "/0/0", true);
        Assertions.assertNull(department);
    }
 
    @Test
    public void testGetLeafNodes() {
        var root = departmentService.root(hierarchy.getId()).root();
        var leafNodes = root.getLeafNodes();
        Assertions.assertEquals(4, leafNodes.size());
    }
 
}