package com.zd.laboratory.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.utils.Assert;
import com.zd.laboratory.base.BaseControllerTest;
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 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/11/16:51
 * @Description:
 */
@DisplayName("实验室")
class LabSubjectControllerTest extends BaseControllerTest {


    /**
     * controller 前缀
     */
    private String cp = "/subject";

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

    @Test
    @DisplayName("根据用户IDS获取负责实验室")
    void getInfoByUserIds() throws Exception {

        //获取测试
        MvcResult get = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/count/1,2,3").headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.data").isArray())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("获取-测试结果:" + get.getResponse().getContentAsString());

    }

    @Test
    @DisplayName("查询未关联负责人的实验室列表")
    void listNoAdmin() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp + "/list/noadmin?pageNum=1&pageSize=10").headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.rows").isArray())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        JSONObject object = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
        object.getJSONArray("rows").forEach(a -> {
            Assert.isTrue(((JSONObject) a).getLong("adminId").equals(-1L), "查询未关联负责人的实验室列表错误");
        });
        logger.debug("查询未关联负责人的实验室列表-测试结果:" + mvcResult.getResponse().getContentAsString());
    }

    @Test
    @DisplayName("给实验室关联用户")
    void setAdmin() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.put(cp + "/6/1").headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        JSONObject object = JSONObject.parseObject(mvcResult.getResponse().getContentAsString());
        logger.debug("给实验室关联用户-测试结果:" + mvcResult.getResponse().getContentAsString());
    }
}
