package com.persagy.dmp.define.test;

import com.persagy.dmp.common.constant.ResponseCode;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ObjectTypeDefineControllerTest {

    @Autowired
    private WebApplicationContext context;
    private MockMvc mvc;

    @Before
    public void setup() {
        mvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    /**
     * head参数
     * @return
     */
    private MultiValueMap<String, String> getHeadParam() {
        MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
        param.add("groupCode", "WD");
        param.add("projectId", null);
        param.add("appId", "BDTP");
        param.add("userId", "0");
        return param;
    }

    @Test
    public void query() throws Exception {
        String body = "{\n" +
                "    \"criteria\": {\n" +
                "        \"objType\":{\n" +
                "            \"$like\":\"comp\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        mvc.perform(MockMvcRequestBuilders.post("/define/type/query")
                .params(getHeadParam())
                .contentType(MediaType.APPLICATION_JSON)
                .content(body))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.respCode").value(ResponseCode.A00000.getCode()));
    }

    @Test
    public void create() throws Exception {
        String body = "{\n" +
                "    \"code\": \"TEXT-TTTTTTTT\",\n" +
                "    \"name\": \"基础桩\",\n" +
                "    \"aliasCode\": \"CFCSFDPL\",\n" +
                "    \"aliasName\": \"基础桩\",\n" +
                "    \"objType\": \"component\",\n" +
                "    \"projectId\": \"0\",\n" +
                "    \"majorCode\": \"CF\",\n" +
                "    \"systemCode\": \"CFCS\",\n" +
                "    \"equipmentCode\": \"CFCSFD\",\n" +
                "    \"parentCode\": \"component\",\n" +
                "    \"version\": null\n" +
                "}";
        mvc.perform(MockMvcRequestBuilders.post("/define/type/create")
                .params(getHeadParam())
                .contentType(MediaType.APPLICATION_JSON)
                .content(body))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.respCode").value(ResponseCode.A00000.getCode()));
    }

    @Test
    public void update() throws Exception {
        String body = "{\n" +
                "    \"id\": \"1413018374673371138\",\n" +
                "    \"code\": \"TEXT-TTTTTTTT\",\n" +
                "    \"name\": \"基础桩\",\n" +
                "    \"aliasCode\": \"CFCSFDPL\",\n" +
                "    \"aliasName\": \"基础桩\",\n" +
                "    \"objType\": \"component\",\n" +
                "    \"projectId\": \"0\",\n" +
                "    \"majorCode\": \"CF\",\n" +
                "    \"systemCode\": \"CFCS\",\n" +
                "    \"equipmentCode\": \"CFCSFD\",\n" +
                "    \"parentCode\": \"component\",\n" +
                "    \"version\": null\n" +
                "}";
        mvc.perform(MockMvcRequestBuilders.post("/define/type/update")
                .params(getHeadParam())
                .contentType(MediaType.APPLICATION_JSON)
                .content(body))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.respCode").value(ResponseCode.A00000.getCode()));
    }

    @Test
    public void delete() throws Exception {
        String body = "1413018374673371138";
        mvc.perform(MockMvcRequestBuilders.post("/define/type/delete")
                .params(getHeadParam())
                .contentType(MediaType.APPLICATION_JSON)
                .content(body))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.respCode").value(ResponseCode.A00000.getCode()));
    }
}
