package com.ruoyi.web.controller.monitor;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.ruoyi.common.core.cache.InMemoryCacheStore;
import com.ruoyi.common.core.redis.RedisCache;

class CacheControllerTest
{
    @Test
    void shouldReturnInMemoryCacheSummary() throws Exception
    {
        RedisCache redisCache = new RedisCache(new InMemoryCacheStore());
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new CacheController(redisCache)).build();

        mockMvc.perform(get("/monitor/cache"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.info.cache_type").value("IN_MEMORY"))
                .andExpect(jsonPath("$.data.info.cache_mode").value("single-instance"));
    }

    @Test
    void shouldClearCacheKeysByPrefix() throws Exception
    {
        RedisCache redisCache = new RedisCache(new InMemoryCacheStore());
        redisCache.setCacheObject("login_tokens:a", "A");
        redisCache.setCacheObject("login_tokens:b", "B");

        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new CacheController(redisCache)).build();

        mockMvc.perform(delete("/monitor/cache/clearCacheName/login_tokens:"))
                .andExpect(status().isOk());

        mockMvc.perform(get("/monitor/cache/getKeys/login_tokens:"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data").isEmpty());
    }
}
