package com.ruoyi.web.modules.category.service.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.domain.BizInformationCategory;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryTreeVo;
import com.ruoyi.web.modules.category.mapper.BizInformationCategoryMapper;
import com.ruoyi.web.modules.category.support.InformationCategoryRules;

@ExtendWith(MockitoExtension.class)
@DisplayName("BizInformationCategoryServiceImpl")
class BizInformationCategoryServiceImplTest
{
    @Mock
    private BizInformationCategoryMapper bizInformationCategoryMapper;

    @InjectMocks
    private BizInformationCategoryServiceImpl service;

    @Test
    @DisplayName("按 parentCode 与 categoryType 查下级")
    void selectChildren_withType()
    {
        BizInformationCategory row = new BizInformationCategory();
        row.setCategoryCode("001001");
        row.setCategoryName("繁育作业");
        row.setParentCode("001");
        row.setCategoryType("1");
        when(bizInformationCategoryMapper.selectChildrenByParent(any())).thenReturn(Arrays.asList(row));

        InformationCategoryItemVo item = service.selectChildren("001", "1").get(0);
        assertEquals("001001", item.getCode());
        assertEquals("繁育作业", item.getName());
    }

    @Test
    @DisplayName("树查询走可见有效数据")
    void selectCategoryTree_visibleOnly()
    {
        BizInformationCategory root = new BizInformationCategory();
        root.setCategoryCode("001");
        root.setCategoryName("养殖标准");
        root.setVisible(InformationCategoryRules.VISIBLE_YES);
        when(bizInformationCategoryMapper.selectVisibleActiveForTree(any())).thenReturn(Arrays.asList(root));

        InformationCategoryTreeVo node = service.selectCategoryTree(new BizInformationCategory()).get(0);
        assertEquals("001", node.getCode());
        assertEquals("养殖标准", node.getName());
    }

    @Test
    @DisplayName("缺 parentCode 拒绝")
    void selectChildren_parentRequired()
    {
        ServiceException ex = assertThrows(ServiceException.class, () -> service.selectChildren("", "1"));
        assertEquals(InformationCategoryRules.MSG_PARENT_CODE_REQUIRED, ex.getMessage());
    }
}
