package com.ruoyi.web.modules.trading.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.trading.mapper.BizSupplierMapper;
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;

@ExtendWith(MockitoExtension.class)
@DisplayName("SupplierCodeGenerator")
class SupplierCodeGeneratorTest
{
    @Mock
    private BizSupplierMapper mapper;

    @InjectMocks
    private SupplierCodeGenerator generator;

    @Test
    @DisplayName("首条编号为01")
    void firstCode()
    {
        when(mapper.selectMaxSupplierCodeForUpdate()).thenReturn(null);
        assertEquals("01", generator.allocateNextCode());
    }

    @Test
    @DisplayName("递增编号")
    void nextCode()
    {
        when(mapper.selectMaxSupplierCodeForUpdate()).thenReturn("08");
        assertEquals("09", generator.allocateNextCode());
    }

    @Test
    @DisplayName("编号用尽")
    void codeExhausted()
    {
        when(mapper.selectMaxSupplierCodeForUpdate()).thenReturn("99");
        ServiceException ex = assertThrows(ServiceException.class, () -> generator.allocateNextCode());
        assertTrue(ex.getMessage().contains("上限"));
    }
}
