package com.ruoyi.web.controller.front;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictTypeService;

public class FrontSourceControllerTest
{
    private FrontSourceController controller;

    @Mock
    private ISysDictTypeService dictTypeService;

    @Before
    public void setUp() throws Exception
    {
        MockitoAnnotations.initMocks(this);
        controller = new FrontSourceController();
        setField("dictTypeService", dictTypeService);
    }

    @Test
    public void categoriesReturnsSourceProjectCategoryDictData()
    {
        SysDictData data = new SysDictData();
        data.setDictLabel("Enterprise Systems");
        data.setDictValue("enterprise");
        data.setDictType("source_project_category");
        when(dictTypeService.selectDictDataByType("source_project_category")).thenReturn(Collections.singletonList(data));

        AjaxResult result = controller.categories();

        List<?> categories = (List<?>) result.get(AjaxResult.DATA_TAG);
        SysDictData category = (SysDictData) categories.get(0);
        assertEquals("enterprise", category.getDictValue());
        assertEquals("Enterprise Systems", category.getDictLabel());
    }

    private void setField(String name, Object value) throws Exception
    {
        Field field = FrontSourceController.class.getDeclaredField(name);
        field.setAccessible(true);
        field.set(controller, value);
    }
}
