package com.zd.laboratory.onemachine.controller;

import com.zd.laboratory.base.BaseControllerTest;
import com.zd.laboratory.service.ILabStudentsInfoService;
import com.zd.system.api.domain.LabStudentsInfo;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @Author: zhoupan
 * @Date: 2021/10/04/8:32
 * @Description:
 */
@DisplayName("一体机相关测试")
class OneMachineControllerTest extends BaseControllerTest {

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

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

    @Autowired
    ILabStudentsInfoService studentsInfoService;
    @Test
    @DisplayName("查询实验室信息-根据ID")
    void querySubInfoById() throws Exception {

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

    @Test
    @DisplayName("实验室传感器信息-根据ID")
    void querySubValList() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp+"/3/sensor/val/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("获取实验室一体机可控制设备-根据ID")
    void querySubHardwareValList() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp+"/3/controller/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 querySubLayer() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp+"/3/layer" ).headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("测试结果:" + mvcResult.getResponse().getContentAsString());
    }

    @Test
    @DisplayName("危险源信息")
    void queryHazardList() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp+"/3/Hazard/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 checkUser() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp+"/3/SignInCheck/张三" ).headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("测试结果:" + mvcResult.getResponse().getContentAsString());
    }



    @Test
    @DisplayName("签出验证")
    void checkOutUser() throws Exception {
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.get(cp+"/3/SignOutCheck/admin" ).headers(httpHeaders).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("测试结果:" + mvcResult.getResponse().getContentAsString());
    }

    @Test
    @DisplayName("签到提交")
    void postSignIn() throws Exception {
        LabStudentsInfo labStudentsInfo = studentsInfoService.selectLabStudentsInfoByJoinStudentsId(2L);

        byte[] faceFeature = labStudentsInfo.getFaceFeature();
        String s = new String(faceFeature);
        MvcResult mvcResult = (MvcResult) mockMvc
                .perform(MockMvcRequestBuilders.post(cp+"/02de967b020d40d1a295c107b73f0596_10/SignIn" ).headers(httpHeaders).content(faceFeature).accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200))
                .andReturn();
        logger.info("测试结果:" + mvcResult.getResponse().getContentAsString());
    }
    @Test
    void outSub() {
    }
}