package com.zd.laboratory.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.zd.laboratory.base.BaseControllerTest;
import com.zd.laboratory.domain.dto.LabPhotoNoteADD;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
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.io.UnsupportedEncodingException;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @Author: zhoupan
 * @Date: 2021/09/10/18:46
 * @Description:
 */
@DisplayName("随手拍记录")
class LabPhotoNoteControllerTest extends BaseControllerTest {
    /**
     * controller 前缀
     */
    private String cp = "/photonote";

    /**
     * 测试前置方法
     *
     * @throws Exception
     */
    @BeforeAll
    void init() throws Exception {
        //使用账号登陆
        beforeAll("admin", "admin123");
    }

    @Test
    @DisplayName("获取随手拍列表")
    void list() throws Exception {

        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/list").headers(httpHeaders))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.rows").isArray())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("测试结果:" + mvcResult.getResponse().getContentAsString());

    }

    @Test
    @DisplayName("新增一个随手拍并且查询，处理")
    void addAndGetInfo() throws Exception {
        //新增测试
        LabPhotoNoteADD add = new LabPhotoNoteADD();
        add.setSubjectId(1L);
        add.setImgUrl("ssss");
        add.setDescribe("测试");
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.post(cp).headers(httpHeaders).contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(add)).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("新增-测试结果:" + mvcResult.getResponse().getContentAsString());
        JSONObject jsonObject = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
        //新增后获取
        //获取测试
        MvcResult get = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/" + jsonObject.getLong("data")).headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.data.imgUrl").value(add.getImgUrl()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.data.describe").value(add.getDescribe()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.data.handleStatus").value(0))
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("获取-测试结果:" + get.getResponse().getContentAsString());
        //处理-通过
        MvcResult handle = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.post(cp + "/" + jsonObject.getLong("data")).headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("处理通过-测试结果:" + handle.getResponse().getContentAsString());

        //获取测试
        MvcResult get1 = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/" + jsonObject.getLong("data")).headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.data.imgUrl").value(add.getImgUrl()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.data.describe").value(add.getDescribe()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.data.handleStatus").value(1))
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("处理通过验证-测试结果:" + get1.getResponse().getContentAsString());

    }

    @Test
    @DisplayName("我的列表")
    void myList() throws Exception {

        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/my/list").headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.rows").isArray())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("测试结果:" + mvcResult.getResponse().getContentAsString());

    }

}
