package com.zd.test;

import com.ruoyi.RuoYiApplication;
import com.ruoyi.bar.BarCommonController;
import com.ruoyi.common.utils.AsyncManager;
import com.ruoyi.framework.config.ServerConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.ruoyi.common.config.RuoYiConfig;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@SpringBootTest(classes = RuoYiApplication.class)
@TestPropertySource(locations = "classpath:application-dev.yml")
public class BarCommonControllerTest {

    @InjectMocks
    private BarCommonController barCommonController;

    @Mock
    private ServerConfig serverConfig;

    @Autowired
    private RuoYiConfig ruoyiConfig;

    private MockMvc mockMvc;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(barCommonController).build();

        when(serverConfig.getUrl()).thenReturn("http://localhost:9090");
    }

    @Test
    public void testUploadFile() throws Exception {
        // 本地图片文件路径
        String localFilePath = "D:/images/0d3c93931f7fbfdac312a60c25ec0a10.jpg";

        // 读取本地图片文件内容
        File localFile = new File(localFilePath);
        byte[] fileContent = Files.readAllBytes(localFile.toPath());

        // 创建 MockMultipartFile 对象
        MockMultipartFile file = new MockMultipartFile(
                "file",
                localFile.getName(),
                MediaType.IMAGE_JPEG_VALUE,
                fileContent
        );

        // 发起上传请求
        mockMvc.perform(multipart("/bar/common/upload").file(file))
                .andExpect(status().isOk())
                .andReturn();

        // 获取上传路径
        // 获取上传路径
        String uploadPath = ruoyiConfig.getUploadPath();

        // 动态生成日期时间文件夹路径
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String dateFolder = currentDate.format(formatter);

        // 构建存储文件的完整路径
        Path storedFilePath = Paths.get(uploadPath, dateFolder, localFile.getName());

        boolean readable = Files.isReadable(storedFilePath);
        boolean exists = Files.exists(storedFilePath);
        log.info("Readable: {} ,Exists: {}" , readable , exists);
        // 验证文件是否存储在正确的路径
        if (Files.exists(storedFilePath) || Files.isReadable(storedFilePath)) {
            throw new RuntimeException("Could not read file: " + storedFilePath);
        }

        // 验证文件内容是否正确
        byte[] storedFileContent = Files.readAllBytes(Paths.get("D:" + storedFilePath));
        log.info("Stored file content: " + storedFileContent);

        // 关闭异步任务管理器
        AsyncManager.me().shutdown();
    }
}

