package com.zd.laboratory.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zd.laboratory.base.BaseControllerTest;
import com.zd.laboratory.domain.dto.LabHazardJoinSubDTO;
import com.zd.laboratory.domain.vo.LabHazardSubVO;
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.util.Arrays;

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/26/15:54
 * @Description:
 */
@DisplayName("危险源管理")
class LabHazardControllerTest extends BaseControllerTest {
    /**
     * controller 前缀
     */
    private String cp = "/hazard";

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

    @Test
    @DisplayName("查询实验室已有危险源关系")
    void queryLabById() throws Exception {


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

    }

    @Test
    @DisplayName("维护危险源与实验室关系")
    void addRelation() throws Exception {
        Long id = 4L;
        //修改
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/" + id + "/list").headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andExpect(MockMvcResultMatchers.jsonPath("$.data").isArray())
                .andReturn();

        JSONObject object = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
        JSONArray data = object.getJSONArray("data");
        JSONObject v = (JSONObject) data.get(0);
        v.remove("hazardType");
        LabHazardSubVO labHazardSubVO = JSON.parseObject(v.toJSONString(), LabHazardSubVO.class);
        labHazardSubVO.setxPoint(100L);
        labHazardSubVO.setyPoint(100L);
        LabHazardJoinSubDTO labHazardJoinSubDTO = new LabHazardJoinSubDTO();
        labHazardJoinSubDTO.setSubId(id);
        labHazardJoinSubDTO.setHazardSubVOS(Arrays.asList(labHazardSubVO));

        MvcResult mvcResult1 = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.post(cp + "/addRelation").headers(httpHeaders).contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(labHazardJoinSubDTO)).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("维护危险源与实验室关系-测试结果:" + mvcResult1.getResponse().getContentAsString());


    }
}
