package com.cf.imes.module.report.service.template;

import com.bstek.designer.bean.ReportDefinitionWrapper;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceReqVO;
import com.cf.imes.module.report.controller.admin.datasource.vo.ReportDatasourceSaveReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateRespVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateSaveReqVO;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import com.cf.imes.module.report.dal.dataobject.template.ReportTemplateDO;
import com.cf.imes.module.report.enums.datasource.ReportDatasourceTypeEnum;
import com.cf.imes.module.report.service.datasource.ReportDatasourceService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import jakarta.annotation.Resource;

import java.util.List;

import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceException;
import static com.cf.imes.framework.test.core.util.AssertUtils.isPojoEquals;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Gqr
 * @since 2024/7/5 17:01
 */
@SpringBootTest
public class ReportDatasourceServiceImplTest extends ReportCommonServiceImplTest {
    @Resource
    ReportDatasourceService reportDatasourceService;

    @Resource
    ReportTemplateService reportTemplateService;

    @Test
    public void testCreateTemplate_success() {
        // 1、上下文添加组织id
        setOrganId();
        Long organId = OrganContextHolder.getOrganId();

        // 1、准备template参数
        // 2、插入template、断言自增id
        // 3、用id查template、断言对象
        ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
        ReportTemplateRespVO reportTemplate = reportTemplateService.createReportTemplate(createReqVO);
        Long templateId = reportTemplate.getId();
        assertNotNull(templateId);
        ReportDefinitionWrapper reportDefinitionWrapper = reportTemplateService.getReportTemplateDefinition(templateId, organId);
        assertNotNull(reportDefinitionWrapper);

        // 1、准备datasource参数
        // 2、插入datasource、断言自增id
        // 3、用id查datasource、断言对象
        ReportDatasourceSaveReqVO datasourceSaveReqVO = prepareDatasourceReqVO(null, templateId);
        Long datasourceId = reportDatasourceService.createDatasource(datasourceSaveReqVO);
        assertNotNull(datasourceId);
        ReportDatasourceDO datasource = reportDatasourceService.getDatasource(datasourceId);
        assertNotNull(datasource);


        //准备更新参数
        ReportDatasourceSaveReqVO updateReqVO = prepareDatasourceReqVO(datasourceId, templateId);
        // 更新
        reportDatasourceService.updateDatasource(updateReqVO);
        // 校验是否更新正确
        ReportDatasourceDO updateDatasouce = reportDatasourceService.getDatasource(updateReqVO.getId()); // 获取最新的
        isPojoEquals(updateDatasouce, updateReqVO);


        // 列表查询参数准备
        ReportDatasourceReqVO reqVO = new ReportDatasourceReqVO();
        reqVO.setTemplateId(templateId);
        reqVO.setType(ReportDatasourceTypeEnum.JDBC.getCode());
        reqVO.setName(updateReqVO.getName());
        List<ReportDatasourceDO> templateDatasourceList = reportDatasourceService.getDatasourceList(null);

        assertNotNull(templateDatasourceList);
        assertNotEquals(0, templateDatasourceList.size());

        // 删除
        reportTemplateService.deleteReportTemplate(templateId, organId);
        reportDatasourceService.deleteDatasource(datasourceId);
    }

    @Test
    public void testUpdateTemplate_notExists() {
        setOrganId();
        // 准备参数
        ReportDatasourceSaveReqVO updateReqVO = prepareDatasourceReqVO(RandomUtils.randomLongId(), RandomUtils.randomLongId());
        // 调用, 并断言异常
        assertServiceException(() -> reportDatasourceService.updateDatasource(updateReqVO), DATASOURCE_NOT_EXISTS);
    }

    @Test
    public void testDeleteTemplate_notExists() {
        setOrganId();
        // 准备参数
        Long id = RandomUtils.randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> reportDatasourceService.deleteDatasource(id), DATASOURCE_NOT_EXISTS);
    }
}
