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}.frame.utils.IDgenerator;
import ${basePackage}.module.system.mgr.TokensManager;
import ${basePackage}.module.system.req.TokensCreateRequest;
import ${basePackage}.module.system.req.TokensFindRequest;
import ${basePackage}.module.system.req.TokensGetRequest;
import ${basePackage}.module.system.req.TokensUpdateRequest;
import ${basePackage}.module.system.rsp.TokensCreateResponse;
import ${basePackage}.module.system.rsp.TokensDeleteResponse;
import ${basePackage}.module.system.rsp.TokensFindResponse;
import ${basePackage}.module.system.rsp.TokensGetResponse;
import ${basePackage}.module.system.rsp.TokensUpdateResponse;

import java.util.Date;

import static org.junit.Assert.assertTrue;

/**
 * TokensTest -  - 通行证测试用例
 *
 * @author author
 * @version 0.0.1
 * @since 2017-01-01
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class TokensTest {

    @Autowired
    private Token token;

    @Autowired
    private TokensManager tokensManager;

    @Test
    public void testCreate() {
        TokensCreateRequest request = new TokensCreateRequest();
        request.setToken(IDgenerator.nextUUID());
        request.setUserId(1L);
        request.setUserName("用户名称");
        request.setLoginTime(new Date());
        request.setValidTime(new Date());
        request.setValid(true);
        request.setDeptId(1L);
        request.setDeptCode("部门代码");
        request.setDeptName("部门名称");
        request.setTerminalType("code");
        request.setTerminalIp("终端地址");
        request.setTerminalInfo("终端信息");

        TokensCreateResponse response = tokensManager.create(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        TokensCreateRequest createRequest = new TokensCreateRequest();
        createRequest.setToken(IDgenerator.nextUUID());
        createRequest.setUserId(1L);
        createRequest.setUserName("用户名称");
        createRequest.setLoginTime(new Date());
        createRequest.setValidTime(new Date());
        createRequest.setValid(true);
        createRequest.setDeptId(1L);
        createRequest.setDeptCode("部门代码");
        createRequest.setDeptName("部门名称");
        createRequest.setTerminalType("code");
        createRequest.setTerminalIp("终端地址");
        createRequest.setTerminalInfo("终端信息");

        TokensCreateResponse createResponse = tokensManager.create(createRequest, token);

        assertTrue(!createResponse.hasError() && createResponse.getId() > 0);
        //删除数据
        ${basePackage}.module.system.req.TokensDeleteRequest request = new ${basePackage}.module.system.req.TokensDeleteRequest();
        request.setToken(createRequest.getToken());

        TokensDeleteResponse response = tokensManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        TokensCreateRequest createRequest = new TokensCreateRequest();
        createRequest.setToken(IDgenerator.nextUUID());
        createRequest.setUserId(1L);
        createRequest.setUserName("用户名称");
        createRequest.setLoginTime(new Date());
        createRequest.setValidTime(new Date());
        createRequest.setValid(true);
        createRequest.setDeptId(1L);
        createRequest.setDeptCode("部门代码");
        createRequest.setDeptName("部门名称");
        createRequest.setTerminalType("code");
        createRequest.setTerminalIp("终端地址");
        createRequest.setTerminalInfo("终端信息");

        TokensCreateResponse createResponse = tokensManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        TokensUpdateRequest request = new TokensUpdateRequest();
        request.setId(createResponse.getId());
        request.setUserId(1L);
        request.setUserName("用户名称");
        request.setLoginTime(new Date());
        request.setValidTime(new Date());
        request.setValid(true);
        request.setDeptId(1L);
        request.setDeptCode("部门代码");
        request.setDeptName("部门名称");
        request.setTerminalType("code");
        request.setTerminalIp("终端地址");
        request.setTerminalInfo("终端信息");

        TokensUpdateResponse response = tokensManager.update(request, token);

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

    @Test
    public void testFind() {
        TokensFindRequest request = new TokensFindRequest();
        request.setUserId(1L);
        request.setUserName("用户名称");
        request.setLoginTime(new Date());
        request.setValidTime(new Date());
        request.setValid(true);
        request.setDeptId(1L);
        request.setDeptCode("部门代码");
        request.setDeptName("部门名称");
        request.setTerminalType("code");
        request.setTerminalIp("终端地址");
        request.setTerminalInfo("终端信息");

        TokensFindResponse response = tokensManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        TokensCreateRequest createRequest = new TokensCreateRequest();
        createRequest.setToken(IDgenerator.nextUUID());
        createRequest.setUserId(1L);
        createRequest.setUserName("用户名称");
        createRequest.setLoginTime(new Date());
        createRequest.setValidTime(new Date());
        createRequest.setValid(true);
        createRequest.setDeptId(1L);
        createRequest.setDeptCode("部门代码");
        createRequest.setDeptName("部门名称");
        createRequest.setTerminalType("code");
        createRequest.setTerminalIp("终端地址");
        createRequest.setTerminalInfo("终端信息");

        TokensCreateResponse createResponse = tokensManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

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

        TokensGetResponse response = tokensManager.get(request, token);

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