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

import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetReqVO;
import com.cf.imes.module.report.controller.admin.dataset.vo.ReportDatasetSaveReqVO;
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.dataset.ReportDatasetDO;
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.service.dataset.ReportDatasetService;
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.DATASET_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * @author Gqr
 * @since 2024/7/8 14:22
 */
@SpringBootTest
public class ReportDatasetServiceImplTest extends ReportCommonServiceImplTest{

    @Resource
    private ReportDatasetService reportDatasetService;

    @Resource
    private ReportTemplateService reportTemplateService;

    @Resource
    private ReportDatasourceService reportDatasourceService;


    @Test
    public void testCreateDataset_success() {
        // 1、上下文添加组织id
        setOrganId();

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

        // 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);

        // 1、准备dataset参数
        // 2、插入dataset、断言自增id
        // 3、用id查dataset、断言对象
        ReportDatasetSaveReqVO datasetSaveReqVO = prepareDatasetReqVO(null, datasourceId);
        Long datasetId = reportDatasetService.createDataset(datasetSaveReqVO);
        assertNotNull(datasetId);
        ReportDatasetDO dataset = reportDatasetService.getDataset(datasetId);
        assertNotNull(dataset);

        //准备更新参数
        ReportDatasetSaveReqVO updateReqVO = prepareDatasetReqVO(datasetId, datasourceId);
        // 更新
        reportDatasetService.updateDataset(updateReqVO);
        // 校验是否更新正确
        ReportDatasetDO updateDataset = reportDatasetService.getDataset(updateReqVO.getId()); // 获取最新的
        isPojoEquals(updateDataset, updateReqVO);


        // 列表查询参数准备
        ReportDatasetReqVO reqVO = new ReportDatasetReqVO();
        reqVO.setName(updateReqVO.getName());
        reqVO.setDatasourceId(updateReqVO.getDatasourceId());
        List<ReportDatasetDO> datasetList = reportDatasetService.getDatasetList(reqVO);

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

        // 删除
        reportTemplateService.deleteReportTemplate(templateId);
        reportDatasourceService.deleteDatasource(datasourceId);
        reportDatasetService.deleteDataset(datasetId);
    }


    @Test
    public void testUpdateDataset_notExists() {
        setOrganId();
        // 准备参数
        ReportDatasetSaveReqVO updateReqVO = prepareDatasetReqVO(RandomUtils.randomLongId(), RandomUtils.randomLongId());
        // 调用, 并断言异常
        assertServiceException(() -> reportDatasetService.updateDataset(updateReqVO), DATASET_NOT_EXISTS);
    }

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

        // 调用, 并断言异常
        assertServiceException(() -> reportDatasetService.deleteDataset(id), DATASET_NOT_EXISTS);
    }
}
