package com.cf.imes.framework.ip.service;

import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.ip.config.ChenfengIPAutoConfiguration;
import com.cf.imes.framework.ip.core.service.IPQueryService;
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import jakarta.annotation.Resource;

import static com.cf.imes.framework.test.core.util.RandomUtils.randomString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * {@link IPQueryService} 的单元测试
 *
 * @author gaoqr
 */
@SpringBootTest(classes = {ChenfengIPAutoConfiguration.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件
public class IPQueryServiceTest {
    @Resource
    private IPQueryService ipQueryService;


    @Test
    public void testQuerySourceFail() {
        assertThrows(ServiceException.class, () -> ipQueryService.querySource(randomString()));
    }

    @Test
    public void testQuerySourceSuccess() {
        IPQueryDataRespDTO ipQueryDataRespDTO = ipQueryService.querySource("220.250.48.74");
        assertNotNull(ipQueryDataRespDTO);
        assertEquals(ipQueryDataRespDTO.getProv(), "福建");
        assertEquals(ipQueryDataRespDTO.getCity(), "福州");
    }
}
