package com.cf.imes.module.executor.service.plan;

import org.junit.jupiter.api.Test;

import jakarta.annotation.Resource;

import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;

import com.cf.imes.module.executor.controller.admin.plan.vo.*;
import com.cf.imes.module.executor.dal.dataobject.plan.PlanDO;
import com.cf.imes.module.executor.dal.mysql.plan.PlanMapper;

import org.springframework.context.annotation.Import;

import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;
import static com.cf.imes.framework.test.core.util.AssertUtils.*;
import static com.cf.imes.framework.test.core.util.RandomUtils.*;
/**
 * {@link PlanServiceImpl} 的单元测试类
 *
 * @author 晨丰科技
 */
@Import(PlanServiceImpl.class)
public class PlanServiceImplTest extends BaseDbUnitTest {

    @Resource
    private PlanServiceImpl planService;

    @Resource
    private PlanMapper planMapper;

    @Test
    public void testUpdatePlan_success() {
        // mock 数据
        PlanDO dbPlan = randomPojo(PlanDO.class);
        planMapper.insert(dbPlan);// @Sql: 先插入出一条存在的数据
        // 准备参数
        PlanSaveReqVO updateReqVO = randomPojo(PlanSaveReqVO.class, o -> {
            o.setId(dbPlan.getId()); // 设置更新的 ID
        });

        // 调用
        planService.updatePlan(updateReqVO);
        // 校验是否更新正确
        PlanDO plan = planMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, plan);
    }

    @Test
    public void testUpdatePlan_notExists() {
        // 准备参数
        PlanSaveReqVO updateReqVO = randomPojo(PlanSaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> planService.updatePlan(updateReqVO), PLAN_NOT_EXISTS);
    }

}
