package com.prolog.cs.biz.inbound.service.impl;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.prolog.cs.biz.inbound.Application;
import com.prolog.cs.biz.inbound.constant.BizConstant;
import com.prolog.cs.biz.inbound.dto.rcv.RcvDTO;
import com.prolog.cs.biz.inbound.service.rcv.RcvHeadersService;
import com.prolog.framework.authority.util.HystrixVarUtil;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.nio.charset.Charset;


/**
 * 基础测试类
 *
 * @author tianhl
 * @Date 2017/5/21 16:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
//@Transactional //打开的话测试之后数据可自动回滚
public class BlankRcvJunit {

    @Autowired
    WebApplicationContext webApplicationContext;

    protected MockMvc mockMvc;

    @Autowired
    private RcvHeadersService rcvHeadersService;

    @Value("classpath:rcvDTO.json")
    private Resource rcvDTO;

    @Before
    public void setupMockMvc(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        HystrixVarUtil.setUserKey("kzywmstest");
    }

    //空入入库
    @Test
    public void blankRcv(){
        try {
            String areaData =  IOUtils.toString(rcvDTO.getInputStream(), Charset.forName("UTF-8"));
            RcvDTO rcvDTO= JSON.parseObject(areaData, RcvDTO.class);
            System.out.println("-----------------"+JSON.toJSONString(rcvDTO));
            rcvDTO.getRcvHeadersDTO().setRcvType(BizConstant.BLANK_IN);
            rcvHeadersService.addWithRelated(rcvDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
