package com.tax.token.job;

import com.tax.token.job.dto.TokenAdjustPriorityDto;
import com.tax.token.model.TkToken;
import lombok.extern.slf4j.Slf4j;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author caosongqing
 * @date 2022/7/7
 * @description
 */
@EnableAutoConfiguration
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SynGetTokenTaskTest.class})
@TestPropertySource(properties = {"spring.config.location=classpath:application.yml"})
@MapperScan("com.tax.token.mapper")
@ComponentScan(basePackages = {
        "com.tax.token.service",
        "com.tax.token.thirdService",
        "com.tax.token.job"
})
@Slf4j
public class SynGetTokenTaskTest extends H2Server {

    @Autowired
    private SynGetTokenTask synGetTokenTask;

    @BeforeClass
    public static void beforeAll() {
        EmbedZookeeperServer.start(6181);
    }

    @Test
    public void testGetTokenList() {
        int limit = 2;
        int totalCount = 5;
        int[] itemArr = {0, 1, 2, 3, 4};
        int[] expectedArr = {2, 2, 2, 2, 2};
        for (int i = 0; i < expectedArr.length; i++) {
            List<TkToken> tkTokenList = synGetTokenTask.getTokenList(totalCount, itemArr[i], limit);
            log.info("tkToken list size:{}", tkTokenList.size());
            assertEquals(expectedArr[i], tkTokenList.size());
        }
    }

    @Test
    public void testGetTokenListLimit() {
        int limit = 3;
        int totalCount = 5;
        int[] itemArr = {0, 1, 2, 3, 4};
        int[] expectedArr = {2, 3, 3, 3, 3};
        for (int i = 0; i < expectedArr.length; i++) {
            List<TkToken> tkTokenList = synGetTokenTask.getTokenList(totalCount, itemArr[i], limit);
            log.info("tkToken list size:{}", tkTokenList.size());
            assertEquals(expectedArr[i], tkTokenList.size());
        }
    }

    @Test
    public void testGetTokenListLimitOther() {
        int limit = 5;
        int totalCount = 3;
        int[] itemArr = {0, 1, 2};
        int[] expectedArr = {4, 5, 5};
        for (int i = 0; i < expectedArr.length; i++) {
            List<TkToken> tkTokenList = synGetTokenTask.getTokenList(totalCount, itemArr[i], limit);
            log.info("tkToken list size:{}", tkTokenList.size());
            assertEquals(expectedArr[i], tkTokenList.size());
        }
    }

    @Test
    public void testGetTokenListEmpty() {
        int limit = 5;
        int totalCount = 3;
        int[] itemArr = {4, 5, 6};
        int[] expectedArr = {0, 0, 0};
        for (int i = 0; i < expectedArr.length; i++) {
            List<TkToken> tkTokenList = synGetTokenTask.getTokenList(totalCount, itemArr[i], limit);
            log.info("tkToken list size:{}", tkTokenList.size());
            assertEquals(expectedArr[i], tkTokenList.size());
        }
    }

    @Test
    public void testGetBehindTokenList() {
        int validHours = 24;
        int delayHours = 1;
        int limit = 10;
        TokenAdjustPriorityDto tokenAdjustPriorityDto = new TokenAdjustPriorityDto();
        tokenAdjustPriorityDto.setDelayHours(delayHours);
        tokenAdjustPriorityDto.setValidHours(validHours);
        tokenAdjustPriorityDto.setLimit(limit);
        List<TkToken> tkTokenList = synGetTokenTask.getBehindTokenList(tokenAdjustPriorityDto);
        log.info("token list size:{}", tkTokenList.size());
        assertEquals(0, tkTokenList.size());
    }

    @Test
    public void testCountToken() {
        int count = synGetTokenTask.countToken();
        log.info("count:{}", count);
        assertEquals(9, count);
    }
}