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.UserManager;
import ${basePackage}.module.system.req.UserCreateRequest;
import ${basePackage}.module.system.req.UserDeleteRequest;
import ${basePackage}.module.system.req.UserFindRequest;
import ${basePackage}.module.system.req.UserGetRequest;
import ${basePackage}.module.system.req.UserUpdateRequest;
import ${basePackage}.module.system.rsp.UserCreateResponse;
import ${basePackage}.module.system.rsp.UserDeleteResponse;
import ${basePackage}.module.system.rsp.UserFindResponse;
import ${basePackage}.module.system.rsp.UserGetResponse;
import ${basePackage}.module.system.rsp.UserUpdateResponse;

import static org.junit.Assert.assertTrue;

/**
 * UserTest -  - 用户测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2019-12-27
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class UserTest {

    @Autowired
    private Token token;

    @Autowired
    private UserManager userManager;

    @Test
    public void testCreate() {
        UserCreateRequest request = new UserCreateRequest();
        request.setUserName("test");
        request.setUserCode("");
        request.setUserAlias("test");
        request.setUserPwd("123456");
        request.setUserStatus("code");
        request.setDeptCode("A01");

        UserCreateResponse response = userManager.create(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        UserCreateRequest createRequest = new UserCreateRequest();
        createRequest.setUserName("test");
        createRequest.setUserCode("");
        createRequest.setUserAlias("test");
        createRequest.setUserPwd("123456");
        createRequest.setUserStatus("code");
        createRequest.setDeptCode("A01");

        UserCreateResponse createResponse = userManager.create(createRequest, token);

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

        UserDeleteResponse response = userManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        UserCreateRequest createRequest = new UserCreateRequest();
        createRequest.setUserName("test");
        createRequest.setUserCode("");
        createRequest.setUserAlias("test");
        createRequest.setUserPwd("123456");
        createRequest.setUserStatus("code");
        createRequest.setDeptCode("A01");

        UserCreateResponse createResponse = userManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        UserUpdateRequest request = new UserUpdateRequest();
        request.setId(createResponse.getId());
        request.setUserAlias("test");
        request.setUserStatus("code");
        request.setDeptCode("A01");

        UserUpdateResponse response = userManager.update(request, token);

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

    @Test
    public void testFind() {
        UserFindRequest request = new UserFindRequest();
        request.setUserName("test");
        request.setUserCode("");
        request.setUserAlias("test");
        request.setUserStatus("code");
        request.setDeptCode("A01");

        UserFindResponse response = userManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        UserCreateRequest createRequest = new UserCreateRequest();
        createRequest.setUserName("test");
        createRequest.setUserCode("");
        createRequest.setUserAlias("test");
        createRequest.setUserPwd("123456");
        createRequest.setUserStatus("code");
        createRequest.setDeptCode("A01");

        UserCreateResponse createResponse = userManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

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

        UserGetResponse response = userManager.get(request, token);

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