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.ConfigDataManager;
import xyz.wbsite.module.admin.req.ConfigDataCreateRequest;
import xyz.wbsite.module.admin.req.ConfigDataDeleteRequest;
import xyz.wbsite.module.admin.req.ConfigDataFindRequest;
import xyz.wbsite.module.admin.req.ConfigDataUpdateRequest;
import xyz.wbsite.module.admin.rsp.ConfigDataCreateResponse;
import xyz.wbsite.module.admin.rsp.ConfigDataDeleteResponse;
import xyz.wbsite.module.admin.rsp.ConfigDataFindResponse;
import xyz.wbsite.module.admin.rsp.ConfigDataUpdateResponse;

import static junit.framework.TestCase.assertTrue;

/**
 * ConfigDataTest -  - 配置数据测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-08
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ConfigDataTest {

    @Autowired
    private Token token;

    @Autowired
    private ConfigDataManager configDataManager;

    @Test
    public void testCreate() {
        ConfigDataCreateRequest request = new ConfigDataCreateRequest();
        request.setTargetId(1L);
        request.setRuleId(1L);
        request.setConfName("配置名称");
        request.setConfValue("配置属值");
        request.setConfNote("配置备注");

        ConfigDataCreateResponse response = configDataManager.create(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        ConfigDataCreateRequest createRequest = new ConfigDataCreateRequest();
        createRequest.setTargetId(1L);
        createRequest.setRuleId(1L);
        createRequest.setConfName("配置名称");
        createRequest.setConfValue("配置属值");
        createRequest.setConfNote("配置备注");

        ConfigDataCreateResponse createResponse = configDataManager.create(createRequest,token);

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

        ConfigDataDeleteResponse response = configDataManager.delete(request,token);

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

    @Test
    public void testUpdate() {
        //创建数据
        ConfigDataCreateRequest createRequest = new ConfigDataCreateRequest();
        createRequest.setTargetId(1L);
        createRequest.setRuleId(1L);
        createRequest.setConfName("配置名称");
        createRequest.setConfValue("配置属值");
        createRequest.setConfNote("配置备注");

        ConfigDataCreateResponse createResponse = configDataManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        ConfigDataUpdateRequest request = new ConfigDataUpdateRequest();
        request.setId(createResponse.getId());
        request.setTargetId(1L);
        request.setRuleId(1L);
        request.setConfName("配置名称");
        request.setConfValue("配置属值");
        request.setConfNote("配置备注");

        ConfigDataUpdateResponse response = configDataManager.update(request,token);

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

    @Test
    public void testFind() {
        ConfigDataFindRequest request = new ConfigDataFindRequest();
        request.setTargetId(1L);
        request.setConfName("配置名称");

        ConfigDataFindResponse response = configDataManager.find(request,token);

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