|
1 | 1 | package me.chanjar.weixin.cp.api.impl;
|
2 | 2 |
|
| 3 | +import com.google.inject.Inject; |
| 4 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 5 | +import me.chanjar.weixin.cp.api.ApiTestModule; |
3 | 6 | import me.chanjar.weixin.cp.api.WxCpAgentService;
|
4 | 7 | import me.chanjar.weixin.cp.api.WxCpService;
|
5 | 8 | import me.chanjar.weixin.cp.bean.WxCpAgent;
|
6 | 9 | import org.testng.Assert;
|
| 10 | +import org.testng.annotations.Guice; |
7 | 11 | import org.testng.annotations.Test;
|
8 | 12 |
|
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
9 | 16 | import static org.mockito.Mockito.mock;
|
10 | 17 | import static org.mockito.Mockito.when;
|
| 18 | +import static org.testng.Assert.assertEquals; |
11 | 19 |
|
12 | 20 |
|
13 | 21 | /**
|
|
18 | 26 | *
|
19 | 27 | * @author <a href="https://github.com/huansinho">huansinho</a>
|
20 | 28 | */
|
| 29 | +@Guice(modules = ApiTestModule.class) |
21 | 30 | public class WxCpAgentServiceImplTest {
|
22 |
| - |
23 |
| - protected WxCpService wxService = mock(WxCpService.class); |
| 31 | + @Inject |
| 32 | + private WxCpService wxCpService; |
24 | 33 |
|
25 | 34 | @Test
|
26 | 35 | public void testGet() throws Exception {
|
27 |
| - String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}"; |
28 |
| - when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson); |
29 |
| - when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService)); |
| 36 | + final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId(); |
| 37 | + WxCpAgent wxCpAgent = this.wxCpService.getAgentService().get(agentId); |
| 38 | + |
| 39 | + assertThat(wxCpAgent.getAgentId()).isEqualTo(agentId); |
| 40 | + |
| 41 | + assertThat(wxCpAgent.getAllowUserInfos().getUsers().toArray()).isNotEmpty(); |
| 42 | + assertThat(wxCpAgent.getAllowParties().getPartyIds().toArray()).isNotEmpty(); |
| 43 | + assertThat(wxCpAgent.getAllowTags().getTagIds().toArray()).isNotEmpty(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testSet() throws WxErrorException { |
| 48 | + final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId(); |
| 49 | + |
| 50 | + this.wxCpService.getAgentService().set(WxCpAgent.builder() |
| 51 | + .description("abcddd") |
| 52 | + .logoMediaId("aaaaaaaaaaaaaa") |
| 53 | + .agentId(agentId) |
| 54 | + .build()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testList() throws WxErrorException { |
| 59 | + List<WxCpAgent> list = this.wxCpService.getAgentService().list(); |
| 60 | + |
| 61 | + assertThat(list).isNotEmpty(); |
| 62 | + |
| 63 | + assertThat(list.get(0).getAgentId()).isNotNull(); |
| 64 | + assertThat(list.get(0).getName()).isNotEmpty(); |
| 65 | + assertThat(list.get(0).getSquareLogoUrl()).isNotEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + public static class MockTest { |
| 69 | + private WxCpService wxService = mock(WxCpService.class); |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testGet() throws Exception { |
| 73 | + String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}"; |
| 74 | + when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson); |
| 75 | + when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService)); |
| 76 | + |
| 77 | + WxCpAgentService wxAgentService = this.wxService.getAgentService(); |
| 78 | + WxCpAgent wxCpAgent = wxAgentService.get(9); |
30 | 79 |
|
31 |
| - WxCpAgentService wxAgentService = this.wxService.getAgentService(); |
32 |
| - WxCpAgent wxCpAgent = wxAgentService.get(9); |
| 80 | + assertEquals(9, wxCpAgent.getAgentId().intValue()); |
33 | 81 |
|
34 |
| - Assert.assertEquals(9, wxCpAgent.getAgentId().intValue()); |
| 82 | + assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray()); |
35 | 83 |
|
36 |
| - Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray()); |
| 84 | + assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray()); |
37 | 85 |
|
38 |
| - Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray()); |
| 86 | + } |
39 | 87 |
|
40 | 88 | }
|
41 | 89 |
|
|
0 commit comments