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.RulesManager;
import xyz.wbsite.module.admin.req.RulesCreateRequest;
import xyz.wbsite.module.admin.req.RulesDeleteRequest;
import xyz.wbsite.module.admin.req.RulesFindRequest;
import xyz.wbsite.module.admin.req.RulesUpdateRequest;
import xyz.wbsite.module.admin.rsp.RulesCreateResponse;
import xyz.wbsite.module.admin.rsp.RulesDeleteResponse;
import xyz.wbsite.module.admin.rsp.RulesFindResponse;
import xyz.wbsite.module.admin.rsp.RulesUpdateResponse;

import static junit.framework.TestCase.assertTrue;

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

    @Autowired
    private Token token;

    @Autowired
    private RulesManager rulesManager;

    @Test
    public void testCreate() {
        RulesCreateRequest request = new RulesCreateRequest();
        request.setRuleFor("value");
        request.setRuleTitle("规则标题");
        request.setRuleName("规则名称");
        request.setRuleValue("规则属值");
        request.setValueDefault("默认属值");
        request.setValueRegex("属值正则");
        request.setRuleNote("规则描述");

        RulesCreateResponse response = rulesManager.create(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        RulesCreateRequest createRequest = new RulesCreateRequest();
        createRequest.setRuleFor("value");
        createRequest.setRuleTitle("规则标题");
        createRequest.setRuleName("规则名称");
        createRequest.setRuleValue("规则属值");
        createRequest.setValueDefault("默认属值");
        createRequest.setValueRegex("属值正则");
        createRequest.setRuleNote("规则描述");

        RulesCreateResponse createResponse = rulesManager.create(createRequest,token);

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

        RulesDeleteResponse response = rulesManager.delete(request,token);

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

    @Test
    public void testUpdate() {
        //创建数据
        RulesCreateRequest createRequest = new RulesCreateRequest();
        createRequest.setRuleFor("value");
        createRequest.setRuleTitle("规则标题");
        createRequest.setRuleName("规则名称");
        createRequest.setRuleValue("规则属值");
        createRequest.setValueDefault("默认属值");
        createRequest.setValueRegex("属值正则");
        createRequest.setRuleNote("规则描述");

        RulesCreateResponse createResponse = rulesManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        RulesUpdateRequest request = new RulesUpdateRequest();
        request.setId(createResponse.getId());
        request.setRuleFor("value");
        request.setRuleTitle("规则标题");
        request.setRuleName("规则名称");
        request.setRuleValue("规则属值");
        request.setValueDefault("默认属值");
        request.setValueRegex("属值正则");
        request.setRuleNote("规则描述");

        RulesUpdateResponse response = rulesManager.update(request,token);

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

    @Test
    public void testFind() {
        RulesFindRequest request = new RulesFindRequest();
        request.setRuleFor("value");
        request.setRuleTitle("规则标题");
        request.setRuleName("规则名称");
        request.setRuleValue("规则属值");
        request.setValueDefault("默认属值");
        request.setValueRegex("属值正则");

        RulesFindResponse response = rulesManager.find(request,token);

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