package xyz.wbsite.admin;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import xyz.wbsite.frame.base.Token;
import xyz.wbsite.module.admin.mgr.ConfigManager;
import xyz.wbsite.module.admin.req.ConfigCreateRequest;
import xyz.wbsite.module.admin.req.ConfigDeleteRequest;
import xyz.wbsite.module.admin.req.ConfigFindRequest;
import xyz.wbsite.module.admin.req.ConfigUpdateRequest;
import xyz.wbsite.module.admin.rsp.ConfigCreateResponse;
import xyz.wbsite.module.admin.rsp.ConfigDeleteResponse;
import xyz.wbsite.module.admin.rsp.ConfigFindResponse;
import xyz.wbsite.module.admin.rsp.ConfigUpdateResponse;

import static junit.framework.TestCase.assertTrue;

/**
 * ConfigTest -  - 配置预设测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-07
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ConfigTest {

    @Autowired
    private Token token;

    @Autowired
    private ConfigManager configManager;

    @Test
    public void testCreate() {
        ConfigCreateRequest request = new ConfigCreateRequest();
        request.setConfName("配置名称");
        request.setConfType("value");
        request.setConfValue("配置属值");
        request.setConfNote("配置备注");

        ConfigCreateResponse response = configManager.create(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        ConfigCreateRequest createRequest = new ConfigCreateRequest();
        createRequest.setConfName("配置名称");
        createRequest.setConfType("value");
        createRequest.setConfValue("配置属值");
        createRequest.setConfNote("配置备注");

        ConfigCreateResponse createResponse = configManager.create(createRequest,token);

        assertTrue(!createResponse.hasError() && createResponse.getId() > 0);
        //删除数据
        ConfigDeleteRequest request = new ConfigDeleteRequest();
        request.setId(createResponse.getId());

        ConfigDeleteResponse response = configManager.delete(request,token);

        assertTrue(!response.hasError() && response.getResult() == 1L);
    }

    @Test
    public void testUpdate() {
        //创建数据
        ConfigCreateRequest createRequest = new ConfigCreateRequest();
        createRequest.setConfName("配置名称");
        createRequest.setConfType("value");
        createRequest.setConfValue("配置属值");
        createRequest.setConfNote("配置备注");

        ConfigCreateResponse createResponse = configManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        ConfigUpdateRequest request = new ConfigUpdateRequest();
        request.setId(createResponse.getId());
        request.setConfName("配置名称");
        request.setConfType("value");
        request.setConfValue("配置属值");
        request.setConfNote("配置备注");

        ConfigUpdateResponse response = configManager.update(request,token);

        assertTrue(!response.hasError() && response.getResult() == 1L);
    }

    @Test
    public void testFind() {
        ConfigFindRequest request = new ConfigFindRequest();
        request.setConfName("配置名称");
        request.setConfType("value");

        ConfigFindResponse response = configManager.find(request,token);

        assertTrue(!response.hasError());
    }
}
