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.req.DictItemCreateRequest;
import ${basePackage}.module.system.req.DictItemDeleteRequest;
import ${basePackage}.module.system.req.DictItemFindRequest;
import ${basePackage}.module.system.req.DictItemGetRequest;
import ${basePackage}.module.system.req.DictItemUpdateRequest;
import ${basePackage}.module.system.rsp.DictItemCreateResponse;
import ${basePackage}.module.system.rsp.DictItemDeleteResponse;
import ${basePackage}.module.system.rsp.DictItemFindResponse;
import ${basePackage}.module.system.rsp.DictItemGetResponse;
import ${basePackage}.module.system.rsp.DictItemUpdateResponse;

import static org.junit.Assert.assertTrue;

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

    @Autowired
    private Token token;

    @Autowired
    private DictItemManager dictItemManager;

    @Test
    public void testCreate() {
        DictItemCreateRequest request = new DictItemCreateRequest();
        request.setDictName("字典名称");
        request.setKey("字典KEY");
        request.setValue("字典VALUE");
        request.setSort(1);
        request.setValid(true);

        DictItemCreateResponse response = dictItemManager.create(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        DictItemCreateRequest createRequest = new DictItemCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setKey("字典KEY");
        createRequest.setValue("字典VALUE");
        createRequest.setSort(1);
        createRequest.setValid(true);

        DictItemCreateResponse createResponse = dictItemManager.create(createRequest, token);

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

        DictItemDeleteResponse response = dictItemManager.delete(request, token);

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

    @Test
    public void testUpdate() {
        //创建数据
        DictItemCreateRequest createRequest = new DictItemCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setKey("字典KEY");
        createRequest.setValue("字典VALUE");
        createRequest.setSort(1);
        createRequest.setValid(true);

        DictItemCreateResponse createResponse = dictItemManager.create(createRequest, token);

        //更新数据
        DictItemUpdateRequest request = new DictItemUpdateRequest();
        request.setId(createResponse.getId());
        request.setKey("字典KEY");
        request.setValue("字典VALUE");
        request.setSort(1);
        request.setValid(true);

        DictItemUpdateResponse response = dictItemManager.update(request, token);

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

    @Test
    public void testFind() {
        DictItemFindRequest request = new DictItemFindRequest();
        request.setDictName("字典名称");
        request.setKey("字典KEY");
        request.setValue("字典VALUE");
        request.setSort(1);
        request.setValid(true);

        DictItemFindResponse response = dictItemManager.find(request, token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testGet() {
        //创建数据
        DictItemCreateRequest createRequest = new DictItemCreateRequest();
        createRequest.setDictName("字典名称");
        createRequest.setKey("字典KEY");
        createRequest.setValue("字典VALUE");
        createRequest.setSort(1);
        createRequest.setValid(true);

        DictItemCreateResponse createResponse = dictItemManager.create(createRequest, token);

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

        DictItemGetResponse response = dictItemManager.get(request, token);

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