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 root = new TreeNode("总公司"); 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 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()); } }