/*
 * Copyright (c) 2023 - present Dromara, All rights reserved.
 *
 *   https://visor.dromara.org
 *   https://visor.dromara.org.cn
 *   https://visor.orionsec.cn
 *
 * Members:
 *   Jiahang Li - ljh1553488six@139.com - author
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ${currentPackage};

import cn.orionsec.kit.lang.utils.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.framework.test.core.base.BaseUnitTest;
import org.dromara.visor.framework.test.core.utils.EntityRandoms;
import ${package.ServiceImpl}.*;
#foreach($pkg in ${customProviderFilePackages})
import ${pkg}.*;
#end
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * $!{table.comment} 对外服务单元测试
 *
 * @author ${author}
 * @version ${version}
 * @since ${date}
 */
@Slf4j
@Import({${type}ApiImpl.class, ${type}ServiceImpl.class})
public class ${type}ApiImplTests extends BaseUnitTest {

    @Resource
    private ${type}Api ${typeLower}Api;

    private static Long lastId;

    @Test
    @Order(1)
    public void create${type}Test() {
        ${type}CreateDTO req = EntityRandoms.random(${type}CreateDTO.class);
        lastId = ${typeLower}Api.create${type}(req);
        assertNotNull(lastId);
    }

    @Test
    @Order(2)
    public void update${type}ByIdTest() {
        ${type}UpdateDTO req = EntityRandoms.random(${type}UpdateDTO.class);
        req.setId(lastId);
        Integer effect = ${typeLower}Api.update${type}ById(req);
        assertEquals(effect, 1);
    }

    @Test
    @Order(3)
    public void update${type}Test() {
        ${type}QueryDTO query = new ${type}QueryDTO();
        query.setId(lastId);
        ${type}UpdateDTO req = EntityRandoms.random(${type}UpdateDTO.class);
        req.setId(null);
        Integer effect = ${typeLower}Api.update${type}(query, req);
        assertEquals(effect, 1);
    }

    @Test
    @Order(4)
    public void get${type}ByIdTest() {
        ${type}DTO row = ${typeLower}Api.get${type}ById(lastId);
        assertNotNull(row);
    }

    @Test
    @Order(5)
    public void get${type}ByIdListTest() {
        List<${type}DTO> rows = ${typeLower}Api.get${type}ByIdList(Lists.of(lastId));
        assertFalse(rows.isEmpty());
    }

    @Test
    @Order(6)
    public void get${type}ListTest() {
        List<${type}DTO> rows = ${typeLower}Api.get${type}List(new ${type}QueryDTO());
        assertFalse(rows.isEmpty());
    }

    @Test
    @Order(7)
    public void get${type}CountTest() {
        Long count = ${typeLower}Api.get${type}Count(new ${type}QueryDTO());
        assertEquals(count, 1L);
    }

    @Test
    @Order(8)
    public void delete${type}ByIdTest() {
        Integer effect = ${typeLower}Api.delete${type}ById(lastId);
        assertEquals(effect, 1);
    }

    @Test
    @Order(9)
    public void delete${type}ByIdListTest() {
        Integer effect = ${typeLower}Api.delete${type}ByIdList(Lists.of(lastId));
        assertEquals(effect, 0);
    }

    @Test
    @Order(10)
    public void delete${type}Test() {
        ${type}QueryDTO dto = new ${type}QueryDTO();
        Integer effect = ${typeLower}Api.delete${type}(dto);
        assertEquals(effect, 0);
    }
    #if($meta.enableCache)
    
    // -------------------- cache --------------------

    @Test
    @Order(11)
    public void get${type}ListByCacheTest() {
        this.create${type}Test();
        List<${type}DTO> list = ${typeLower}Api.get${type}ListByCache();
        assertEquals(list.size(), 1);
    }
    #end

}
