package com.estate.expert.admin.service; import com.estate.expert.admin.domain.standard.hierachy.HierarchyData; import com.estate.expert.admin.domain.standard.hierachy.HierarchyIds; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Assertions; 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; import java.util.Arrays; /** * @ClassName HierarchySystemTest * @Description * @Author daisyli * @Date 2023/9/8 **/ @Slf4j @SpringBootTest @ExtendWith(SpringExtension.class) @Transactional public class HierarchyServiceTest { @Autowired private HierarchyService service; @Test public void testRoot() { var root = service.root(HierarchyIds.SYSTEM); Assertions.assertNotNull(root); root = service.root(HierarchyIds.DEVICE); Assertions.assertNotNull(root); } @Test public void testGet() { var node = service.node(HierarchyIds.SYSTEM, "/", false); Assertions.assertEquals("综合体系-标准-层级", node.getName()); node = service.node(HierarchyIds.SYSTEM, "/0", false); Assertions.assertEquals("安全检查", node.getName()); node = service.node(HierarchyIds.SYSTEM, "/1", true); Assertions.assertEquals("管理行为", node.getName()); Assertions.assertFalse(node.getChildren().isEmpty()); node = service.node(HierarchyIds.DEVICE, "/1", false); Assertions.assertEquals("施工升降机", node.getName()); Assertions.assertTrue(node.getChildren().isEmpty()); } @Test public void testChildren() { var children = service.children(HierarchyIds.SYSTEM, "/0", null); Assertions.assertNotNull(children); Assertions.assertFalse(children.isEmpty()); children = service.children(HierarchyIds.DEVICE, "/0", null); Assertions.assertNotNull(children); Assertions.assertTrue(children.get(0).getChildren().isEmpty()); } @Test public void testAddAndDelete() { var child = service.addChild(HierarchyIds.SYSTEM, "/0", "测试3", new HierarchyData( 100f, 1.0f)); Assertions.assertNotNull(child); var path = child.getPath(); service.edit(HierarchyIds.SYSTEM, path, "测试2", new HierarchyData( 100f, 1.0f)); child = service.node(HierarchyIds.SYSTEM, path, false); Assertions.assertEquals("测试2", child.getName()); service.deleteNode(HierarchyIds.SYSTEM, path); var node = service.node(HierarchyIds.SYSTEM, path, false); Assertions.assertNull(node); } @Test public void testFindAllPaths(){ var paths0 = service.findAllPaths(HierarchyIds.SYSTEM, Arrays.asList("/2/1/2/1/0")); Assertions.assertFalse(paths0.isEmpty()); var paths1 = service.findAllPaths(HierarchyIds.SYSTEM, Arrays.asList("/2/1/2/1")); Assertions.assertTrue(paths1.size() > paths0.size()); var paths2 = service.findAllPaths(HierarchyIds.SYSTEM, Arrays.asList("/2")); Assertions.assertTrue(paths2.size() > paths1.size()); } }