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.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 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.AgriculturalCourseTestSamples;

@ExtendWith(MockitoExtension.class)
@DisplayName("AgriculturalCourseTypeValidator")
class AgriculturalCourseTypeValidatorTest
{
    @Mock
    private IBizInformationCategoryService bizInformationCategoryService;

    @InjectMocks
    private AgriculturalCourseTypeValidator validator;

    @Test
    @DisplayName("视频课程主题编码通过")
    void videoTopicAllowed()
    {
        when(bizInformationCategoryService.selectChildren("005", "8"))
                .thenReturn(Collections.singletonList(
                        new InformationCategoryItemVo(AgriculturalCourseTestSamples.TYPE_VIDEO_TOPIC, "基础养殖")));
        assertDoesNotThrow(() -> validator.assertAllowed(AgriculturalCourseTestSamples.TYPE_VIDEO_TOPIC));
    }

    @Test
    @DisplayName("实战培训状态编码通过")
    void trainingStatusAllowed()
    {
        when(bizInformationCategoryService.selectChildren("005", "8"))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren("006", null))
                .thenReturn(Collections.singletonList(
                        new InformationCategoryItemVo(AgriculturalCourseTestSamples.TYPE_TRAINING_ENROLLING, "报名中")));
        assertDoesNotThrow(() -> validator.assertAllowed(AgriculturalCourseTestSamples.TYPE_TRAINING_ENROLLING));
    }

    @Test
    @DisplayName("非法类型拒绝")
    void invalidType()
    {
        when(bizInformationCategoryService.selectChildren("005", "8"))
                .thenReturn(Collections.emptyList());
        when(bizInformationCategoryService.selectChildren("006", null))
                .thenReturn(Collections.emptyList());
        assertThrows(ServiceException.class, () -> validator.assertAllowed("004008"));
    }
}
