package ${basePackage}.system;

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 ${basePackage}.frame.auth.Token;
import ${basePackage}.module.system.mgr.ResManager;
import ${basePackage}.module.system.req.ResCreateRequest;
import ${basePackage}.module.system.req.ResDeleteRequest;
import ${basePackage}.module.system.req.ResFindRequest;
import ${basePackage}.module.system.req.ResGetRequest;
import ${basePackage}.module.system.req.ResUpdateRequest;
import ${basePackage}.module.system.rsp.ResCreateResponse;
import ${basePackage}.module.system.rsp.ResDeleteResponse;
import ${basePackage}.module.system.rsp.ResFindResponse;
import ${basePackage}.module.system.rsp.ResGetResponse;
import ${basePackage}.module.system.rsp.ResUpdateResponse;

import static org.junit.Assert.assertTrue;

/**
 * ResTest -  - 资源测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-01-05
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ResTest {

    @Autowired
    private Token token;

    @Autowired
    private ResManager resManager;

    @Test
    public void testCreate() {
        ResCreateRequest request = new ResCreateRequest();
        request.setResCode("资源代码");
        request.setResName("资源名称");
        request.setResType("code");
        request.setResValue("资源内容");
        request.setValid(true);

        ResCreateResponse response = resManager.create(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        ResCreateRequest createRequest = new ResCreateRequest();
        createRequest.setResCode("资源代码");
        createRequest.setResName("资源名称");
        createRequest.setResType("code");
        createRequest.setResValue("资源内容");
        createRequest.setValid(true);

        ResCreateResponse createResponse = resManager.create(createRequest, token);

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

        ResDeleteResponse response = resManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        ResCreateRequest createRequest = new ResCreateRequest();
        createRequest.setResCode("资源代码");
        createRequest.setResName("资源名称");
        createRequest.setResType("code");
        createRequest.setResValue("资源内容");
        createRequest.setValid(true);

        ResCreateResponse createResponse = resManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        ResUpdateRequest request = new ResUpdateRequest();
        request.setId(createResponse.getId());
        request.setResCode("资源代码");
        request.setResName("资源名称");
        request.setResType("code");
        request.setResValue("资源内容");
        request.setValid(true);

        ResUpdateResponse response = resManager.update(request, token);

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

    @Test
    public void testFind() {
        ResFindRequest request = new ResFindRequest();
        request.setResCode("资源代码");
        request.setResName("资源名称");
        request.setResType("code");
        request.setResValue("资源内容");
        request.setSupCode("上级代码");
        request.setSupName("上级名称");
        request.setValid(true);

        ResFindResponse response = resManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        ResCreateRequest createRequest = new ResCreateRequest();
        createRequest.setResCode("资源代码");
        createRequest.setResName("资源名称");
        createRequest.setResType("code");
        createRequest.setResValue("资源内容");
        createRequest.setValid(true);

        ResCreateResponse createResponse = resManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

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

        ResGetResponse response = resManager.get(request, token);

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