package com.uas.platform.b2b;

import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.support.SystemSession;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
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.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import java.util.HashSet;
import java.util.Set;

/**
 * Spring MVC 测试基类，所有测试类继承自这个类就可以直接写单元测试
 * @author stg
 * @date 2017年11月8日16:48:18
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/*.xml")
@WebAppConfiguration("")
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class BaseJunitTest {

    @Autowired
    protected WebApplicationContext wac;

    protected MockMvc mockMvc;

    /**
     * 前置防范，配置MVCMock
     */
    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    /**
     * 初始化用户信息
     * @param userUU
     * @param enUU
     */
    protected void initSystemSession(Long userUU, Long enUU) {
        User user = new User();
        user.setUserUU(userUU);
        Enterprise enterprise = new Enterprise();
        enterprise.setUu(enUU);
        Set<Enterprise> enterprises = new HashSet<>();
        enterprises.add(enterprise);
        user.setEnterprises(enterprises);
        user.setEnterprise(enterprise);
        SystemSession.setUser(user);
    }
}
