package xyz.wbsite.conf;

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.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import xyz.wbsite.frame.auth.Token;
import xyz.wbsite.frame.base.SortType;
import xyz.wbsite.module.conf.mgr.MappingManager;
import xyz.wbsite.module.conf.req.MappingCreateRequest;
import xyz.wbsite.module.conf.req.MappingDeleteRequest;
import xyz.wbsite.module.conf.req.MappingFindRequest;
import xyz.wbsite.module.conf.req.MappingGetRequest;
import xyz.wbsite.module.conf.req.MappingUpdateRequest;
import xyz.wbsite.module.conf.rsp.MappingCreateResponse;
import xyz.wbsite.module.conf.rsp.MappingDeleteResponse;
import xyz.wbsite.module.conf.rsp.MappingFindResponse;
import xyz.wbsite.module.conf.rsp.MappingGetResponse;
import xyz.wbsite.module.conf.rsp.MappingUpdateResponse;

import static junit.framework.TestCase.assertTrue;

/**
 * HomeTest -  - 映射测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-03-18
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class HomeTest {

    @Autowired
    private Token token;

    @Autowired
    private MappingManager mappingManager;

    @Test
    @Rollback(false)
    public void testCreate() {
        MappingCreateRequest request = new MappingCreateRequest();
        request.setName("名称");
        request.setPort("端口");
        request.setPath("路径");
        request.setType("类型");
        request.setLocation("代理地址");
        request.setNote("备注");
        MappingCreateResponse response = mappingManager.create(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        MappingCreateRequest createRequest = new MappingCreateRequest();
        createRequest.setName("名称");
        createRequest.setPort("端口");
        createRequest.setPath("路径");
        createRequest.setType("类型");
        createRequest.setLocation("代理地址");
        createRequest.setNote("备注");

        MappingCreateResponse createResponse = mappingManager.create(createRequest,token);

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

        MappingDeleteResponse response = mappingManager.delete(request,token);

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

    @Test
    public void testUpdate() {
        //创建数据
        MappingCreateRequest createRequest = new MappingCreateRequest();
        createRequest.setName("名称");
        createRequest.setPort("端口");
        createRequest.setPath("路径");
        createRequest.setType("类型");
        createRequest.setLocation("代理地址");
        createRequest.setNote("备注");

        MappingCreateResponse createResponse = mappingManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        MappingUpdateRequest request = new MappingUpdateRequest();
        request.setId(createResponse.getId());
        request.setName("名称");
        request.setPort("端口");
        request.setPath("路径");
        request.setType("类型");
        request.setLocation("代理地址");
        request.setNote("备注");

        MappingUpdateResponse response = mappingManager.update(request,token);

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

    @Test
    public void testFind() {
        MappingFindRequest request = new MappingFindRequest();
        request.setName("名称");
        request.setPort("端口");
        request.setPath("路径");
        request.setType("类型");
        request.setLocation("代理地址");
        request.setPageSize(0);
        request.setSortKey("PORT");
        request.setSortType(SortType.ASC);

        MappingFindResponse response = mappingManager.find(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        MappingCreateRequest createRequest = new MappingCreateRequest();
        createRequest.setName("名称");
        createRequest.setPort("端口");
        createRequest.setPath("路径");
        createRequest.setType("类型");
        createRequest.setLocation("代理地址");
        createRequest.setNote("备注");

        MappingCreateResponse createResponse = mappingManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //获得数据
        MappingGetRequest request = new MappingGetRequest();
        request.setId(createResponse.getId());

        MappingGetResponse response = mappingManager.get(request,token);

        assertTrue(!response.hasError() && response.getMapping() != null);
    }
}
