package xyz.wbsite.admin;

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 xyz.wbsite.frame.base.Token;
import xyz.wbsite.module.admin.mgr.LocationsManager;
import xyz.wbsite.module.admin.req.LocationsCreateRequest;
import xyz.wbsite.module.admin.req.LocationsDeleteRequest;
import xyz.wbsite.module.admin.req.LocationsFindRequest;
import xyz.wbsite.module.admin.req.LocationsUpdateRequest;
import xyz.wbsite.module.admin.rsp.LocationsCreateResponse;
import xyz.wbsite.module.admin.rsp.LocationsDeleteResponse;
import xyz.wbsite.module.admin.rsp.LocationsFindResponse;
import xyz.wbsite.module.admin.rsp.LocationsUpdateResponse;

import static junit.framework.TestCase.assertTrue;

/**
 * LocationsTest -  - 路径配置测试用例
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2021-02-07
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class LocationsTest {

    @Autowired
    private Token token;

    @Autowired
    private LocationsManager locationsManager;

    @Test
    public void testCreate() {
        LocationsCreateRequest request = new LocationsCreateRequest();
        request.setServicesId(1L);
        request.setLocalTitle("配置标题");
        request.setLocalNote("配置备注");
        request.setLocalPath("配置路径");
        request.setLocalValid(true);
        request.setFilter(true);
        request.setFilterConf("过滤配置");

        LocationsCreateResponse response = locationsManager.create(request,token);

        assertTrue(!response.hasError());
    }

    @Test
    public void testDelete() {

        //创建数据
        LocationsCreateRequest createRequest = new LocationsCreateRequest();
        createRequest.setServicesId(1L);
        createRequest.setLocalTitle("配置标题");
        createRequest.setLocalNote("配置备注");
        createRequest.setLocalPath("配置路径");
        createRequest.setLocalValid(true);
        createRequest.setFilter(true);
        createRequest.setFilterConf("过滤配置");

        LocationsCreateResponse createResponse = locationsManager.create(createRequest,token);

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

        LocationsDeleteResponse response = locationsManager.delete(request,token);

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

    @Test
    public void testUpdate() {
        //创建数据
        LocationsCreateRequest createRequest = new LocationsCreateRequest();
        createRequest.setServicesId(1L);
        createRequest.setLocalTitle("配置标题");
        createRequest.setLocalNote("配置备注");
        createRequest.setLocalPath("配置路径");
        createRequest.setLocalValid(true);
        createRequest.setFilter(true);
        createRequest.setFilterConf("过滤配置");

        LocationsCreateResponse createResponse = locationsManager.create(createRequest, token);

        assertTrue(!createResponse.hasError());

        //更新数据
        LocationsUpdateRequest request = new LocationsUpdateRequest();
        request.setId(createResponse.getId());
        request.setServicesId(1L);
        request.setLocalTitle("配置标题");
        request.setLocalNote("配置备注");
        request.setLocalPath("配置路径");
        request.setLocalValid(true);
        request.setFilter(true);
        request.setFilterConf("过滤配置");

        LocationsUpdateResponse response = locationsManager.update(request,token);

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

    @Test
    public void testFind() {
        LocationsFindRequest request = new LocationsFindRequest();
        request.setServicesId(1L);
        request.setLocalTitle("配置标题");
        request.setLocalPath("配置路径");
        request.setLocalValid(true);
        request.setFilter(true);

        LocationsFindResponse response = locationsManager.find(request,token);

        assertTrue(!response.hasError());
    }
}
