|
| 1 | +package me.chanjar.weixin.mp.api.impl; |
| 2 | + |
| 3 | +import com.google.inject.Inject; |
| 4 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 5 | +import me.chanjar.weixin.mp.api.WxMpService; |
| 6 | +import me.chanjar.weixin.mp.api.test.ApiTestModule; |
| 7 | +import me.chanjar.weixin.mp.config.impl.WxMpMapConfigImpl; |
| 8 | +import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder; |
| 9 | +import org.testng.annotations.Guice; |
| 10 | +import org.testng.annotations.Test; |
| 11 | +import static org.testng.Assert.assertEquals; |
| 12 | + |
| 13 | +/** |
| 14 | + * 测试 ConcurrentHashMap 保存配置信息 |
| 15 | + * @author jimmyjimmy-sw |
| 16 | + */ |
| 17 | +@Test |
| 18 | +@Guice(modules = ApiTestModule.class) |
| 19 | +public class WxMpMapConfigImplTest { |
| 20 | + |
| 21 | + @Inject |
| 22 | + private WxMpService wxService; |
| 23 | + |
| 24 | + /** |
| 25 | + * 测试多租户保存 WxMpMapConfigImpl 到 WxMpService,切换之后能获取到租户各自AppId对应的token |
| 26 | + * @throws WxErrorException |
| 27 | + */ |
| 28 | + @Test |
| 29 | + public void testAppidSwitch() throws WxErrorException { |
| 30 | + // 保存租户A的配置信息,并获取token |
| 31 | + WxMpMapConfigImpl configAppA = new WxMpMapConfigImpl(); |
| 32 | + String appidA = "APPID_A"; |
| 33 | + configAppA.setAppId(appidA); |
| 34 | + configAppA.setSecret("APP_SECRET_A"); |
| 35 | + configAppA.useStableAccessToken(true); |
| 36 | + String tokenA = "TOKEN_A"; |
| 37 | + configAppA.updateAccessToken(tokenA,60 * 60 * 1); |
| 38 | + wxService.addConfigStorage(appidA, configAppA); |
| 39 | + WxMpConfigStorageHolder.set(appidA); |
| 40 | + assertEquals(this.wxService.getAccessToken(),tokenA); |
| 41 | + |
| 42 | + // 保存租户B的配置信息,并获取token |
| 43 | + WxMpMapConfigImpl configAppB = new WxMpMapConfigImpl(); |
| 44 | + String appidB = "APPID_B"; |
| 45 | + configAppB.setAppId(appidB); |
| 46 | + configAppB.setSecret("APP_SECRET_B"); |
| 47 | + configAppB.useStableAccessToken(true); |
| 48 | + String tokenB = "TOKEN_B"; |
| 49 | + configAppB.updateAccessToken(tokenB,60 * 60 * 1); |
| 50 | + wxService.addConfigStorage(appidB, configAppB); |
| 51 | + WxMpConfigStorageHolder.set(appidB); |
| 52 | + assertEquals(this.wxService.getAccessToken(),tokenB); |
| 53 | + |
| 54 | + // 上下文切换到租户A 获取租户A的token |
| 55 | + WxMpConfigStorageHolder.set(appidA); |
| 56 | + assertEquals(this.wxService.getAccessToken(),tokenA); |
| 57 | + } |
| 58 | +} |
0 commit comments