package com.ruoyi.web.modules.news.support;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.when;

import java.util.Collections;
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 org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.domain.vo.InformationCategoryItemVo;
import com.ruoyi.web.modules.category.service.IBizInformationCategoryService;
import com.ruoyi.web.modules.news.FarmingNewsTestSamples;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("养殖资讯 type 校验器")
class FarmingNewsTypeValidatorTest
{
    @Mock
    private IBizInformationCategoryService bizInformationCategoryService;

    @InjectMocks
    private FarmingNewsTypeValidator farmingNewsTypeValidator;

    @Test
    @DisplayName("合法 type 通过")
    void allowedType()
    {
        InformationCategoryItemVo item = new InformationCategoryItemVo();
        item.setCode(FarmingNewsTestSamples.TYPE_BREEDING);
        item.setName("繁育作业");
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("1")))
                .thenReturn(Collections.singletonList(item));
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("2")))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("3")))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("4")))
                .thenReturn(Collections.emptyList());
        assertDoesNotThrow(() -> farmingNewsTypeValidator.assertAllowed(FarmingNewsTestSamples.TYPE_BREEDING));
    }

    @Test
    @DisplayName("非法 type 拒绝")
    void invalidType()
    {
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("1")))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("2")))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("3")))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("001"), eq("4")))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("002"), isNull()))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren(eq("003"), isNull()))
                .thenReturn(Collections.emptyList());
        assertThrows(ServiceException.class, () -> farmingNewsTypeValidator.assertAllowed("999999"));
    }
}
