package com.usoftchina.bi.test.server.controller.dataSource;

import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.core.utils.JsonUtils;
import com.usoftchina.bi.server.Application;
import com.usoftchina.bi.server.model.po.TokenData;
import com.usoftchina.bi.server.model.vo.configVo.DataConnectorInfo;
import com.usoftchina.bi.server.model.vo.configVo.LoginInfo;
import com.usoftchina.bi.server.service.user.UserService;
import com.usoftchina.bi.starter.test.BaseControllerTest;
import com.usoftchina.bi.test.server.controller.chart.ChartsConfigControllerTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MvcResult;


/**
 * 测试数据源接口
 * @Author chenwei
 * @Date 2019-04-02
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class DataConnectorControllerTest extends BaseControllerTest {

    @Autowired
    private UserService userService;

    private static String token = null;
    private static final Logger logger = LoggerFactory.getLogger(ChartsConfigControllerTest.class);

    @Before
    public void init(){
        if (token == null) {
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setUserName("U0818");
            loginInfo.setPassWord("cw111111");
            RepEntity<TokenData> repEntity = userService.login(loginInfo);
            token = repEntity.getData().getToken();
            logger.info("token={}", token);
        }
    }

    /**
     * 保存数据源
     * @throws Exception
     */
    @Test
    public void testA_saveDataConnector() throws Exception {
        DataConnectorInfo dataConnectorInfo = new DataConnectorInfo();
        dataConnectorInfo.setCreateBy("陈炜");
        dataConnectorInfo.setDataName("未命名");
        dataConnectorInfo.setDbConId("151");
        dataConnectorInfo.setLoadObject("flow_chart");
        dataConnectorInfo.setNote("测试方法跑的数据");
        dataConnectorInfo.setType("database");

        //dataConnectorInfo.setColumnConfig();
        MvcResult mvcResult = mockMvc.perform(requestBody("/Connector/inputDataConnector", dataConnectorInfo)
                                        .header("token", token))
                                    .andExpect(isSuccess())
                                    .andReturn();
        logger.info("mvcResult={}", JsonUtils.toJsonString(mvcResult));
    }

    /**
     * 获取数据单个连接
     * @throws Exception
     */
    @Test
    public void testB_getDataConnector() throws Exception {
        MvcResult mvcResult = mockMvc.perform(get("/Connector/getDataConnector")
                                        .header("token", token)
                                        .param("id", "31"))
                                    .andReturn();
        logger.info("mvcResult={}", JsonUtils.toJsonString(mvcResult));
    }

    /**
     * 获取数据连接列表
     * @throws Exception
     */
    @Test
    public void testC_getDataConnectorList() throws Exception {
        MvcResult mvcResult = mockMvc.perform(get("/Connector/getListDataConnector")
                .header("token", token)
                .param("pageNum", "1")
                .param("pageSize", "10"))
                .andReturn();
        logger.info("mvcResult={}", JsonUtils.toJsonString(mvcResult));
    }

    @Test
    public void testD_updateDataConnector() throws Exception {
        DataConnectorInfo dataConnectorInfo = new DataConnectorInfo();
        dataConnectorInfo.setDataId(45);
        dataConnectorInfo.setCreateBy("陈炜");
        dataConnectorInfo.setDataName("未命名");
        dataConnectorInfo.setDbConId("151");
        dataConnectorInfo.setLoadObject("flow_chart");
        dataConnectorInfo.setNote("测试方法跑的数据");
        dataConnectorInfo.setType("database");

        //dataConnectorInfo.setColumnConfig();
        MvcResult mvcResult = mockMvc.perform(requestBody("/Connector/updateData", dataConnectorInfo)
                .header("token", token))
                .andExpect(isSuccess())
                .andReturn();
        logger.info("mvcResult={}", JsonUtils.toJsonString(mvcResult));
    }


}
