package com.ruoyi.web.modules.industryservice.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.dto.YakAssetBundleDto;
import com.ruoyi.web.modules.industryservice.domain.vo.YakAssetSyncResultVo;
import com.ruoyi.web.modules.industryservice.support.ThirdPartyYakClient;

@ExtendWith(MockitoExtension.class)
@DisplayName("YakAssetSyncService")
class YakAssetSyncServiceTest
{
    @Mock
    private ThirdPartyYakClient thirdPartyYakClient;

    @Mock
    private YakAssetBundleSyncTxService bundleSyncTxService;

    @InjectMocks
    private YakAssetSyncService yakAssetSyncService;

    @Test
    @DisplayName("ZCZX-MYNZDA-UT-007 第三方失败不调用落库")
    void ut007_thirdPartyFail()
    {
        when(thirdPartyYakClient.fetchAll()).thenThrow(new ServiceException("超时"));
        org.junit.jupiter.api.Assertions.assertThrows(ServiceException.class,
                () -> yakAssetSyncService.syncFromThirdParty());
        verify(bundleSyncTxService, never()).persistBundle(any(), any());
    }

    @Test
    @DisplayName("ZCZX-MYNZDA-UT-005 新增计数")
    void ut005_insertCount()
    {
        YakAssetBundleDto bundle = new YakAssetBundleDto();
        when(thirdPartyYakClient.fetchAll()).thenReturn(Collections.singletonList(bundle));
        when(bundleSyncTxService.persistBundle(any(), any())).thenReturn(true);
        YakAssetSyncResultVo result = yakAssetSyncService.syncFromThirdParty();
        assertEquals(1, result.getInsertCount());
        assertEquals(0, result.getUpdateCount());
    }
}
