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.ServicesManager;
import xyz.wbsite.module.admin.req.ServicesCreateRequest;
import xyz.wbsite.module.admin.req.ServicesDeleteRequest;
import xyz.wbsite.module.admin.req.ServicesFindRequest;
import xyz.wbsite.module.admin.req.ServicesUpdateRequest;
import xyz.wbsite.module.admin.rsp.ServicesCreateResponse;
import xyz.wbsite.module.admin.rsp.ServicesDeleteResponse;
import xyz.wbsite.module.admin.rsp.ServicesFindResponse;
import xyz.wbsite.module.admin.rsp.ServicesUpdateResponse;

import static junit.framework.TestCase.assertTrue;

/**
 * ServicesTest -  - 虚拟主机测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-07
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ServicesTest {

    @Autowired
    private Token token;

    @Autowired
    private ServicesManager servicesManager;

    @Test
    public void testCreate() {
        ServicesCreateRequest request = new ServicesCreateRequest();
        request.setTitle("主机标题");
        request.setDomain("主机域名");
        request.setType("value");
        request.setPort(1);
        request.setValid(true);
        request.setFilter(true);
        request.setFilterConf("过滤配置");

        ServicesCreateResponse response = servicesManager.create(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        ServicesCreateRequest createRequest = new ServicesCreateRequest();
        createRequest.setTitle("主机标题");
        createRequest.setDomain("主机域名");
        createRequest.setType("value");
        createRequest.setPort(1);
        createRequest.setValid(true);
        createRequest.setFilter(true);
        createRequest.setFilterConf("过滤配置");

        ServicesCreateResponse createResponse = servicesManager.create(createRequest,token);

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

        ServicesDeleteResponse response = servicesManager.delete(request,token);

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

    @Test
    public void testUpdate() {
        //创建数据
        ServicesCreateRequest createRequest = new ServicesCreateRequest();
        createRequest.setTitle("主机标题");
        createRequest.setDomain("主机域名");
        createRequest.setType("value");
        createRequest.setPort(1);
        createRequest.setValid(true);
        createRequest.setFilter(true);
        createRequest.setFilterConf("过滤配置");

        ServicesCreateResponse createResponse = servicesManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        ServicesUpdateRequest request = new ServicesUpdateRequest();
        request.setId(createResponse.getId());
        request.setTitle("主机标题");
        request.setDomain("主机域名");
        request.setType("value");
        request.setPort(1);
        request.setValid(true);
        request.setFilter(true);
        request.setFilterConf("过滤配置");

        ServicesUpdateResponse response = servicesManager.update(request,token);

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

    @Test
    public void testFind() {
        ServicesFindRequest request = new ServicesFindRequest();
        request.setTitle("主机标题");
        request.setDomain("主机域名");
        request.setType("value");
        request.setPort(1);
        request.setValid(true);
        request.setFilter(true);

        ServicesFindResponse response = servicesManager.find(request,token);

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