package com.sysu.admin.api.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Maps;
import com.sysu.admin.support.base.ServiceContext;
import com.xiesx.fastboot.core.token.JwtHelper;
import com.xiesx.fastboot.core.token.cfg.TokenCfg;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.Map;

/**
 * @author shu hao
 * @description:
 * @date 2022/1/5 10:05
 */
public class BaseTest extends ServiceContext {

    @Autowired
    WebApplicationContext wac;
    @Autowired
    SecurityManager securityManager;
    private MockMvc mockMvc;
    @Autowired
    public ServiceContext serviceContext;


    @Before
    public void brfore() {
        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
        SecurityUtils.setSecurityManager(securityManager);
    }

    public String getToken(String userId, String username){
        Map<String, Object> claims = Maps.newConcurrentMap();
        claims.put(TokenCfg.USERID, userId);
        claims.put(TokenCfg.USERNAME, username);
        String token = JwtHelper.create("zedu", "api", claims, JwtHelper.JWT_EXPIRE_D_7);
        return token;
    }

    public void printJsonString(Object o){
        System.out.println(JSON.toJSONString(o, SerializerFeature.PrettyFormat));
    }



}
