package cn.iocoder.yudao.module.flow.service.outboundgoods;

import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.flow.controller.admin.outboundgoods.vo.OutboundGoodsCreateReqVO;
import cn.iocoder.yudao.module.flow.controller.admin.outboundgoods.vo.OutboundGoodsUpdateReqVO;
import cn.iocoder.yudao.module.flow.dal.dataobject.outboundgoods.OutboundGoodsDO;
import cn.iocoder.yudao.module.flow.dal.mysql.outboundgoods.OutboundGoodsMapper;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.flow.enums.ErrorCodeConstants.OUTBOUND_GOODS_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* {@link OutboundGoodsServiceImpl} 的单元测试类
*
* @author 芋道源码
*/
@Import(OutboundGoodsServiceImpl.class)
public class OutboundGoodsServiceImplTest extends BaseDbUnitTest {

    @Resource
    private OutboundGoodsServiceImpl outboundGoodsService;

    @Resource
    private OutboundGoodsMapper outboundGoodsMapper;

    @Test
    public void testCreateOutboundGoods_success() {
        // 准备参数
        OutboundGoodsCreateReqVO reqVO = randomPojo(OutboundGoodsCreateReqVO.class);

        // 调用
        Long outboundGoodsId = outboundGoodsService.createOutboundGoods(reqVO);
        // 断言
        assertNotNull(outboundGoodsId);
        // 校验记录的属性是否正确
        OutboundGoodsDO outboundGoods = outboundGoodsMapper.selectById(outboundGoodsId);
        assertPojoEquals(reqVO, outboundGoods);
    }

    @Test
    public void testUpdateOutboundGoods_success() {
        // mock 数据
        OutboundGoodsDO dbOutboundGoods = randomPojo(OutboundGoodsDO.class);
        outboundGoodsMapper.insert(dbOutboundGoods);// @Sql: 先插入出一条存在的数据
        // 准备参数
        OutboundGoodsUpdateReqVO reqVO = randomPojo(OutboundGoodsUpdateReqVO.class, o -> {
            o.setId(dbOutboundGoods.getId()); // 设置更新的 ID
        });

        // 调用
        outboundGoodsService.updateOutboundGoods(reqVO);
        // 校验是否更新正确
        OutboundGoodsDO outboundGoods = outboundGoodsMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, outboundGoods);
    }

    @Test
    public void testUpdateOutboundGoods_notExists() {
        // 准备参数
        OutboundGoodsUpdateReqVO reqVO = randomPojo(OutboundGoodsUpdateReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> outboundGoodsService.updateOutboundGoods(reqVO), OUTBOUND_GOODS_NOT_EXISTS);
    }

    @Test
    public void testDeleteOutboundGoods_success() {
        // mock 数据
        OutboundGoodsDO dbOutboundGoods = randomPojo(OutboundGoodsDO.class);
        outboundGoodsMapper.insert(dbOutboundGoods);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbOutboundGoods.getId();

        // 调用
        outboundGoodsService.deleteOutboundGoods(id);
       // 校验数据不存在了
       assertNull(outboundGoodsMapper.selectById(id));
    }

    @Test
    public void testDeleteOutboundGoods_notExists() {
        // 准备参数
        Long id = randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> outboundGoodsService.deleteOutboundGoods(id), OUTBOUND_GOODS_NOT_EXISTS);
    }
}
