package com.bonus.sharedstation.app.controller;

import com.bonus.sharedstation.InitHeader;
import com.bonus.sharedstation.web.dto.SharingToolsDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.nio.charset.StandardCharsets;

/**
 * FileName: SharingToolsAppControllerTest
 * @author tqzhang
 * Date: 2025/2/19 上午9:09
 * Description:
 */

@SpringBootTest
@AutoConfigureMockMvc // 自动配置 MockMvc
@ExtendWith(SpringExtension.class) // JUnit 5 annotation
class SharingToolsAppControllerTest {

    @Autowired
    private MockMvc mockMvc; // 直接注入 MockMvc

    private HttpHeaders httpHeaders;

    private final String BASE_URL = "/information/sharingtools/app";

    @BeforeEach
    public void init() {
        InitHeader initHeader = new InitHeader();
        httpHeaders = new HttpHeaders();
        httpHeaders.addAll( initHeader.getHeader());
    }

    @Test
    void getList() throws Exception {
        SharingToolsDTO sharingToolsDTO = new SharingToolsDTO();
        sharingToolsDTO.setCurrentPage(1);
        sharingToolsDTO.setLimit(10);
        sharingToolsDTO.setIsDeleted("0");
        sharingToolsDTO.setSharingAddress("");

        ObjectMapper objectMapper = new ObjectMapper();
        String roleDtoJson = objectMapper.writeValueAsString(sharingToolsDTO);

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(BASE_URL + "/getListApp")
                        .headers(httpHeaders)
                        .content(roleDtoJson)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        String contentString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        System.out.println(contentString);
    }

    @Test
    void selectInfoById() throws Exception {
        SharingToolsDTO sharingToolsDTO = new SharingToolsDTO();
        sharingToolsDTO.setId(22);

        ObjectMapper objectMapper = new ObjectMapper();
        String roleDtoJson = objectMapper.writeValueAsString(sharingToolsDTO);

        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(BASE_URL + "/selectById")
                        .headers(httpHeaders)
                        .content(roleDtoJson)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();

        String contentString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
        System.out.println(contentString);
    }
}