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.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import ${basePackage}.frame.auth.Token;
import ${basePackage}.module.system.mgr.DeptManager;
import ${basePackage}.module.system.req.DeptCreateRequest;
import ${basePackage}.module.system.req.DeptDeleteRequest;
import ${basePackage}.module.system.req.DeptFindRequest;
import ${basePackage}.module.system.req.DeptGetRequest;
import ${basePackage}.module.system.req.DeptUpdateRequest;
import ${basePackage}.module.system.rsp.DeptCreateResponse;
import ${basePackage}.module.system.rsp.DeptDeleteResponse;
import ${basePackage}.module.system.rsp.DeptFindResponse;
import ${basePackage}.module.system.rsp.DeptGetResponse;
import ${basePackage}.module.system.rsp.DeptUpdateResponse;

import static org.junit.Assert.assertTrue;

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

    @Autowired
    private Token token;

    @Autowired
    private DeptManager deptManager;

    @Test
    @Rollback(false)
    public void testCreate() {
        {
            DeptCreateRequest request = new DeptCreateRequest();
            request.setDeptCode("A00");
            request.setDeptName("开发部");
            request.setDeptAlias("开发部");
            request.setValid(true);
            DeptCreateResponse response = deptManager.create(request, token);
            assertTrue(!response.hasError());
        }
        {
            DeptCreateRequest request = new DeptCreateRequest();
            request.setDeptCode("A00_B00");
            request.setDeptName("开发部开发一组");
            request.setDeptAlias("开发一组");
            request.setValid(true);
            DeptCreateResponse response = deptManager.create(request, token);
            assertTrue(!response.hasError());
        }
    }

    @Test
    public void testDelete() {

        //创建数据
        DeptCreateRequest createRequest = new DeptCreateRequest();
        createRequest.setDeptCode("A00");
        createRequest.setDeptName("开发部");
        createRequest.setDeptAlias("开发部");
        createRequest.setValid(true);

        DeptCreateResponse createResponse = deptManager.create(createRequest, token);

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

        DeptDeleteResponse response = deptManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        DeptCreateRequest createRequest = new DeptCreateRequest();
        createRequest.setDeptCode("A00");
        createRequest.setDeptName("开发部");
        createRequest.setDeptAlias("开发部");
        createRequest.setValid(true);

        DeptCreateResponse createResponse = deptManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        DeptUpdateRequest request = new DeptUpdateRequest();
        request.setId(createResponse.getId());
        request.setDeptCode("A00");
        request.setDeptName("开发部");
        request.setDeptAlias("开发部");
        request.setValid(true);

        DeptUpdateResponse response = deptManager.update(request, token);

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

    @Test
    public void testFind() {
        DeptFindRequest request = new DeptFindRequest();
        request.setDeptCode("A00");
        request.setDeptName("开发部");
        request.setDeptAlias("开发部");
        request.setValid(true);

        DeptFindResponse response = deptManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        DeptCreateRequest createRequest = new DeptCreateRequest();
        createRequest.setDeptCode("A00");
        createRequest.setDeptName("开发部");
        createRequest.setDeptAlias("开发部");
        createRequest.setValid(true);

        DeptCreateResponse createResponse = deptManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

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

        DeptGetResponse response = deptManager.get(request, token);

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