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.DictItemManager;
import ${basePackage}.module.system.mgr.DictManager;
import ${basePackage}.module.system.req.DictCreateRequest;
import ${basePackage}.module.system.req.DictDeleteRequest;
import ${basePackage}.module.system.req.DictFindRequest;
import ${basePackage}.module.system.req.DictGetRequest;
import ${basePackage}.module.system.req.DictItemCreateRequest;
import ${basePackage}.module.system.req.DictLoadRequest;
import ${basePackage}.module.system.req.DictUpdateRequest;
import ${basePackage}.module.system.rsp.DictCreateResponse;
import ${basePackage}.module.system.rsp.DictDeleteResponse;
import ${basePackage}.module.system.rsp.DictFindResponse;
import ${basePackage}.module.system.rsp.DictGetResponse;
import ${basePackage}.module.system.rsp.DictItemCreateResponse;
import ${basePackage}.module.system.rsp.DictLoadResponse;
import ${basePackage}.module.system.rsp.DictUpdateResponse;

import static org.junit.Assert.assertTrue;

/**
 * DictTest -  - 字典测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class DictTest {

    @Autowired
    private Token token;

    @Autowired
    private DictManager dictManager;
    @Autowired
    private DictItemManager dictItemManager;

    @Test
    public void testCreate() {
        DictCreateRequest request = new DictCreateRequest();
        request.setDictName("字典名称");
        request.setDictComment("字典描述");
        request.setVersion("字典版本号");
        request.setValid(true);

        DictCreateResponse response = dictManager.create(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        DictCreateRequest createRequest = new DictCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setDictComment("字典描述");
        createRequest.setVersion("字典版本号");
        createRequest.setValid(true);

        DictCreateResponse createResponse = dictManager.create(createRequest, token);

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

        DictDeleteResponse response = dictManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        DictCreateRequest createRequest = new DictCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setDictComment("字典描述");
        createRequest.setVersion("字典版本号");
        createRequest.setValid(true);

        DictCreateResponse createResponse = dictManager.create(createRequest, token);

        //更新数据
        DictUpdateRequest request = new DictUpdateRequest();
        request.setId(createResponse.getId());
        request.setDictName("字典名称");
        request.setDictComment("字典描述");
        request.setVersion("字典版本号");
        request.setValid(true);

        DictUpdateResponse response = dictManager.update(request, token);

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

    @Test
    public void testFind() {
        DictFindRequest request = new DictFindRequest();
        request.setDictName("字典名称");
        request.setDictComment("字典描述");
        request.setValid(true);

        DictFindResponse response = dictManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        DictCreateRequest createRequest = new DictCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setDictComment("字典描述");
        createRequest.setVersion("字典版本号");
        createRequest.setValid(true);

        DictCreateResponse createResponse = dictManager.create(createRequest, token);

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

        DictGetResponse response = dictManager.get(request, token);

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

    @Test
    public void testLoad() {
        //创建数据
        DictCreateRequest createRequest = new DictCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setDictComment("字典描述");
        createRequest.setVersion("字典版本号");
        createRequest.setValid(true);

        DictCreateResponse createResponse = dictManager.create(createRequest, token);
        assertTrue(!createResponse.hasError() && createResponse.getId() > 0);

        DictItemCreateRequest dictItemCreateRequest = new DictItemCreateRequest();
        dictItemCreateRequest.setDictName("字典名称");
        dictItemCreateRequest.setKey("1");
        dictItemCreateRequest.setValue("1");
        dictItemCreateRequest.setSort(1);
        dictItemCreateRequest.setValid(true);
        DictItemCreateResponse dictItemCreateResponse = dictItemManager.create(dictItemCreateRequest, token);
        assertTrue(!dictItemCreateResponse.hasError() && dictItemCreateResponse.getId() > 0);

        //获取字典及字典项数据
        DictLoadRequest request = new DictLoadRequest();
        request.setDictName("字典名称");

        DictLoadResponse response = dictManager.load(request, token);
        assertTrue(!response.hasError() && response.getDict() != null && response.getDictItems().size() > 0);
    }
}
