package com.ruoyi.common.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.test.util.ReflectionTestUtils;
import com.ruoyi.common.core.cache.InMemoryCacheStore;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;

class DictUtilsTest
{
    @AfterEach
    void clearSpringUtils()
    {
        ReflectionTestUtils.setField(SpringUtils.class, "beanFactory", null);
        ReflectionTestUtils.setField(SpringUtils.class, "applicationContext", null);
    }

    @Test
    void shouldReadDictListFromLocalCache()
    {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerSingleton("redisCache", new RedisCache(new InMemoryCacheStore()));
        ReflectionTestUtils.setField(SpringUtils.class, "beanFactory", beanFactory);

        SysDictData dictData = new SysDictData();
        dictData.setDictLabel("正常");
        dictData.setDictValue("0");

        DictUtils.setDictCache("sys_normal_disable", List.of(dictData));

        List<SysDictData> cached = DictUtils.getDictCache("sys_normal_disable");
        assertEquals(1, cached.size());
        assertEquals("正常", cached.get(0).getDictLabel());

        DictUtils.clearDictCache();
        assertNull(DictUtils.getDictCache("sys_normal_disable"));
    }
}
