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.UserRoleManager;
import ${basePackage}.module.system.req.UserRoleCreateRequest;
import ${basePackage}.module.system.req.UserRoleDeleteRequest;
import ${basePackage}.module.system.req.UserRoleFindRequest;
import ${basePackage}.module.system.req.UserRoleGetRequest;
import ${basePackage}.module.system.req.UserRoleUpdateRequest;
import ${basePackage}.module.system.rsp.UserRoleCreateResponse;
import ${basePackage}.module.system.rsp.UserRoleDeleteResponse;
import ${basePackage}.module.system.rsp.UserRoleFindResponse;
import ${basePackage}.module.system.rsp.UserRoleGetResponse;
import ${basePackage}.module.system.rsp.UserRoleUpdateResponse;

import static org.junit.Assert.assertTrue;

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

    @Autowired
    private Token token;

    @Autowired
    private UserRoleManager userRoleManager;

    @Test
    public void testCreate() {
        UserRoleCreateRequest request = new UserRoleCreateRequest();
        request.setUserId(1L);
        request.setRoleId(1L);

        UserRoleCreateResponse response = userRoleManager.create(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        UserRoleCreateRequest createRequest = new UserRoleCreateRequest();
        createRequest.setUserId(1L);
        createRequest.setRoleId(1L);

        UserRoleCreateResponse createResponse = userRoleManager.create(createRequest, token);

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

        UserRoleDeleteResponse response = userRoleManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        UserRoleCreateRequest createRequest = new UserRoleCreateRequest();
        createRequest.setUserId(1L);
        createRequest.setRoleId(1L);

        UserRoleCreateResponse createResponse = userRoleManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        UserRoleUpdateRequest request = new UserRoleUpdateRequest();
        request.setId(createResponse.getId());
        request.setUserId(1L);
        request.setRoleId(1L);

        UserRoleUpdateResponse response = userRoleManager.update(request, token);

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

    @Test
    public void testFind() {
        UserRoleFindRequest request = new UserRoleFindRequest();
        request.setUserId(1L);
        request.setRoleId(1L);

        UserRoleFindResponse response = userRoleManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        UserRoleCreateRequest createRequest = new UserRoleCreateRequest();
        createRequest.setUserId(1L);
        createRequest.setRoleId(1L);

        UserRoleCreateResponse createResponse = userRoleManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

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

        UserRoleGetResponse response = userRoleManager.get(request, token);

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