package com.ruoyi.web.core.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

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.diagnosis.mapper.BizMedicalResourceMapper;
import com.ruoyi.web.modules.farming.mapper.BizTechResourceMapper;

@ExtendWith(MockitoExtension.class)
@DisplayName("SysUserResourceReferenceChecker")
class SysUserResourceReferenceCheckerTest
{
    @Mock
    private BizMedicalResourceMapper bizMedicalResourceMapper;

    @Mock
    private BizTechResourceMapper bizTechResourceMapper;

    @InjectMocks
    private SysUserResourceReferenceChecker checker;

    @Test
    @DisplayName("无引用可删除")
    void deletable()
    {
        when(bizMedicalResourceMapper.countBySysUserId(1L)).thenReturn(0);
        when(bizTechResourceMapper.countBySysUserId(1L)).thenReturn(0);
        assertDoesNotThrow(() -> checker.assertDeletable(1L));
    }

    @Test
    @DisplayName("医疗资源引用不可删")
    void medicalReferenced()
    {
        when(bizMedicalResourceMapper.countBySysUserId(2L)).thenReturn(1);
        ServiceException ex = assertThrows(ServiceException.class, () -> checker.assertDeletable(2L));
        assertEquals("该用户已被畜牧医疗资源引用，不允许删除", ex.getMessage());
    }

    @Test
    @DisplayName("科技资源引用不可删")
    void techReferenced()
    {
        when(bizMedicalResourceMapper.countBySysUserId(3L)).thenReturn(0);
        when(bizTechResourceMapper.countBySysUserId(3L)).thenReturn(1);
        ServiceException ex = assertThrows(ServiceException.class, () -> checker.assertDeletable(3L));
        assertEquals("该用户已被畜牧科技资源引用，不允许删除", ex.getMessage());
    }
}
