package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.apiservice.auth.realm.UserAuthRealm;
import cn.estsh.i3plus.pojo.base.shirotoken.UserToken;
import cn.estsh.i3plus.pojo.platform.bean.SessionUser;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import cn.estsh.impp.framework.run.ImppGo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.session.mgt.DefaultSessionContext;
import org.apache.shiro.session.mgt.SessionContext;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Description :
 * @Reference :
 * @Author : Adair Peng
 * @CreateDate : 2018-12-13 10:00
 * @Modify:
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ImppGo.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class TestBase {

    public static final String JSON_SESSION_USER = "{\"languageCode\":\"zh\",\"user\":{\"ascOrDesc\":1,\"id\":400001,\"isValid\":1,\"menuList\":[],\"orderByParam\":\"\",\"roleList\":[],\"userEmail\":\"wei.peng@estsh.com\",\"userInfoId\":300001,\"userLoginLastDateTime\":\"2018-12-13 14:52:09\",\"userLoginName\":\"admin\",\"userLoginNum\":170,\"userLoginPassword\":\"e10adc3949ba59abbe56e057f20f883e\",\"userName\":\"系统管理员\",\"userPhone\":\"18507256660\",\"userStatus\":1,\"userType\":1},\"userCode\":\"100001\",\"userId\":300001,\"userInfo\":{\"ascOrDesc\":1,\"department\":{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:52:38\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10001\",\"id\":1072036353472073728,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:52:38\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行一部\",\"orderByParam\":\"\",\"organizeId\":1072033924571598848,\"organizeNameRdd\":\"闵行车轮工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},\"departmentId\":1072036353472073728,\"departmentList\":[{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:52:38\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10001\",\"id\":1072036353472073728,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:52:38\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行一部\",\"orderByParam\":\"\",\"organizeId\":1072033924571598848,\"organizeNameRdd\":\"闵行车轮工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:52:52\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10002\",\"id\":1072036412234272768,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:52:52\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行二部\",\"orderByParam\":\"\",\"organizeId\":1072033995035906048,\"organizeNameRdd\":\"闵行车门工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:53:07\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10003\",\"id\":1072036474448384000,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:53:07\",\"modifyUser\":\"系统管理员\",\"name\":\"浦东一部\",\"orderByParam\":\"\",\"organizeId\":1072034176548605952,\"organizeNameRdd\":\"浦东车窗工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:53:26\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10004\",\"id\":1072036552160448512,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:53:26\",\"modifyUser\":\"系统管理员\",\"name\":\"浦东二部\",\"orderByParam\":\"\",\"organizeId\":1072034276020719616,\"organizeNameRdd\":\"浦东发动机工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:54:42\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10005\",\"id\":1072036873070841856,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:54:52\",\"modifyUser\":\"系统管理员\",\"name\":\"浙江AD部\",\"orderByParam\":\"\",\"organizeId\":1072034411442212864,\"organizeNameRdd\":\"浙江AD钙工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:55:05\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10006\",\"id\":1072036970013790208,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:55:05\",\"modifyUser\":\"系统管理员\",\"name\":\"江苏一部\",\"orderByParam\":\"\",\"organizeId\":1072034525309177856,\"organizeNameRdd\":\"江苏爽歪歪工厂\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:55:48\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10007\",\"id\":1072037153732694016,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:55:48\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行一部一组\",\"orderByParam\":\"\",\"organizeId\":1072033924571598848,\"organizeNameRdd\":\"闵行车轮工厂\",\"parentId\":1072036353472073728,\"parentNameRdd\":\"闵行一部\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:56:08\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10008\",\"id\":1072037232132624384,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:56:08\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行二部一组\",\"orderByParam\":\"\",\"organizeId\":1072033995035906048,\"organizeNameRdd\":\"闵行车门工厂\",\"parentId\":1072036412234272768,\"parentNameRdd\":\"闵行二部\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 20:43:11\",\"createUser\":\"系统管理员\",\"departmentCode\":\"10010\",\"id\":1072109473407045632,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 20:43:11\",\"modifyUser\":\"系统管理员\",\"name\":\"东尚总部\",\"orderByParam\":\"\",\"organizeId\":1072028266539388928,\"organizeNameRdd\":\"东尚集团\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 20:43:37\",\"createUser\":\"系统管理员\",\"departmentCode\":\"20010\",\"id\":1072109580340826112,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 20:43:37\",\"modifyUser\":\"系统管理员\",\"name\":\"娃哈哈总部\",\"orderByParam\":\"\",\"organizeId\":1072033577438416896,\"organizeNameRdd\":\"娃哈哈集团\",\"parentId\":-1,\"parentNameRdd\":\"顶级部门\"}],\"departmentNameRdd\":\"闵行一部\",\"departmentNamesRdd\":\"闵行一部,闵行二部,浦东一部,浦东二部,浙江AD部,江苏一部,闵行一部一组,闵行二部一组,东尚总部,娃哈哈总部\",\"id\":300001,\"isValid\":1,\"modifyDatetime\":\"2018-12-12 16:34:45\",\"modifyUser\":\"系统管理员\",\"name\":\"系统管理员\",\"orderByParam\":\"\",\"organize\":{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:42:59\",\"createUser\":\"系统管理员\",\"id\":1072033924571598848,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:42:59\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行车轮工厂\",\"orderByParam\":\"\",\"organizeCode\":\"10004\",\"organizeType\":3,\"parentId\":1072032375745155072,\"parentNameRdd\":\"闵行公司\"},\"organizeId\":1072033924571598848,\"organizeList\":[{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:42:59\",\"createUser\":\"系统管理员\",\"id\":1072033924571598848,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:42:59\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行车轮工厂\",\"orderByParam\":\"\",\"organizeCode\":\"10004\",\"organizeType\":3,\"parentId\":1072032375745155072,\"parentNameRdd\":\"闵行公司\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:43:16\",\"createUser\":\"系统管理员\",\"id\":1072033995035906048,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:43:16\",\"modifyUser\":\"系统管理员\",\"name\":\"闵行车门工厂\",\"orderByParam\":\"\",\"organizeCode\":\"10005\",\"organizeType\":3,\"parentId\":1072032375745155072,\"parentNameRdd\":\"闵行公司\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:43:59\",\"createUser\":\"系统管理员\",\"id\":1072034176548605952,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:43:59\",\"modifyUser\":\"系统管理员\",\"name\":\"浦东车窗工厂\",\"orderByParam\":\"\",\"organizeCode\":\"10006\",\"organizeType\":3,\"parentId\":1072033520731426816,\"parentNameRdd\":\"浦东公司\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:44:23\",\"createUser\":\"系统管理员\",\"id\":1072034276020719616,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 15:44:23\",\"modifyUser\":\"系统管理员\",\"name\":\"浦东发动机工厂\",\"orderByParam\":\"\",\"organizeCode\":\"10007\",\"organizeType\":3,\"parentId\":1072033520731426816,\"parentNameRdd\":\"浦东公司\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:44:55\",\"createUser\":\"系统管理员\",\"id\":1072034411442212864,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-12 15:47:10\",\"modifyUser\":\"系统管理员\",\"name\":\"浙江AD钙工厂\",\"orderByParam\":\"\",\"organizeCode\":\"20004\",\"organizeSort\":999999999,\"organizeType\":3,\"parentId\":1072033722552946688,\"parentNameRdd\":\"浙江娃哈哈有限公司\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 15:45:22\",\"createUser\":\"系统管理员\",\"id\":1072034525309177856,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-12 15:47:57\",\"modifyUser\":\"系统管理员\",\"name\":\"江苏爽歪歪工厂\",\"orderByParam\":\"\",\"organizeCode\":\"20005\",\"organizeSort\":222222222,\"organizeType\":3,\"parentId\":1072033806300614656,\"parentNameRdd\":\"江苏娃哈哈有限公司\"}],\"organizeNameRdd\":\"闵行车轮工厂\",\"position\":{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 16:07:07\",\"createUser\":\"系统管理员\",\"id\":1072039995872776192,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-12 15:22:50\",\"modifyUser\":\"系统管理员\",\"name\":\"Java开发工程师\",\"orderByParam\":\"\",\"parentId\":1072039995872776192,\"parentNameRdd\":\"Java开发工程师\",\"positionCode\":\"10001\"},\"positionId\":1072039995872776192,\"positionList\":[{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 16:20:17\",\"createUser\":\"系统管理员\",\"id\":1072043310375374848,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 16:20:17\",\"modifyUser\":\"系统管理员\",\"name\":\"运维工程师\",\"orderByParam\":\"\",\"parentId\":-1,\"parentNameRdd\":\"顶级岗位\",\"positionCode\":\"10002\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 16:20:31\",\"createUser\":\"系统管理员\",\"id\":1072043370869821440,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-12 15:22:34\",\"modifyUser\":\"系统管理员\",\"name\":\"Android 开发工程师\",\"orderByParam\":\"\",\"parentId\":-1,\"parentNameRdd\":\"顶级岗位\",\"positionCode\":\"10003\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 16:20:50\",\"createUser\":\"系统管理员\",\"id\":1072043447055159296,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 16:20:50\",\"modifyUser\":\"系统管理员\",\"name\":\"数据库运维\",\"orderByParam\":\"\",\"parentId\":1072043310375374848,\"parentNameRdd\":\"运维工程师\",\"positionCode\":\"10004\"},{\"ascOrDesc\":1,\"createDatetime\":\"2018-12-10 16:21:38\",\"createUser\":\"系统管理员\",\"id\":1072043651225489408,\"isDeleted\":2,\"isValid\":1,\"modifyDatetime\":\"2018-12-10 16:21:38\",\"modifyUser\":\"系统管理员\",\"name\":\"部署工程师\",\"orderByParam\":\"\",\"parentId\":1072043310375374848,\"parentNameRdd\":\"运维工程师\",\"positionCode\":\"10006\"}],\"positionNameRdd\":\"Java开发工程师\",\"positionNamesRdd\":\"运维工程师,Android 开发工程师,数据库运维,部署工程师\",\"user\":{\"$ref\":\"$.user\"},\"userAge\":110,\"userEmpNo\":\"100001\",\"userImageId\":1072104117033373696,\"userInfoStatus\":1,\"userLoginLastDateTime\":\"2018-12-13 14:52:09\",\"userLoginNum\":170},\"userName\":\"系统管理员\",\"userType\":\"普通用户\"}\n";

    @Before
    public void before() {
        System.out.println("----------------->> 注入登录用户 <<-----------------");
        SessionContext sessionContext = new DefaultSessionContext();
        sessionContext.setHost("localhost");

        UserAuthRealm userAuthRealm = (UserAuthRealm) SpringContextsUtil.getBean(UserAuthRealm.class);
        org.apache.shiro.mgt.SecurityManager securityManager = new DefaultSecurityManager(userAuthRealm);
        SecurityUtils.setSecurityManager(securityManager);
        securityManager.start(sessionContext);

//        UserToken token = new UserToken("login_name", "login_pwd", "zh");
//        SessionUser sessionUser = AuthUtil.login(token);
//        AuthUtil.setSessionUser(sessionUser);
        System.out.println("----------------->> 开始测试 <<-----------------");
    }

    @After
    public void after() {
        System.out.println("----------------->> 测试结束 <<-----------------");

    }

}
//
//@Component
//class UserAuthRealm extends AuthorizingRealm {
//
//    public UserAuthRealm() {
//        //添加支持的token
//        this.setAuthenticationTokenClass(UserToken.class);
//    }
//
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        return new SimpleAuthorizationInfo();
//    }
//
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        SessionUser user = JSON.parseObject(TestBase.JSON_SESSION_USER, SessionUser.class);
//        return new SimpleAuthenticationInfo(user, ((UserToken) authenticationToken).getLoginPwd(), this.getName());
//    }
//
//}
//

