dev --- 3.8.0.A版本, openProject引用 ; formao-live --- 3.7.0.B 版本, formallProject引用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.5 KiB

  1. package chanjarster.weixin.api;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import javax.xml.bind.JAXBException;
  7. import javax.xml.bind.annotation.XmlAccessType;
  8. import javax.xml.bind.annotation.XmlAccessorType;
  9. import javax.xml.bind.annotation.XmlRootElement;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.testng.Assert;
  12. import org.testng.annotations.BeforeTest;
  13. import org.testng.annotations.DataProvider;
  14. import org.testng.annotations.Test;
  15. import chanjarster.weixin.bean.WxCustomMessage;
  16. import chanjarster.weixin.bean.WxMenu;
  17. import chanjarster.weixin.bean.WxMenu.WxMenuButton;
  18. import chanjarster.weixin.bean.result.WxUploadResult;
  19. import chanjarster.weixin.exception.WxErrorException;
  20. import chanjarster.weixin.util.xml.XmlTransformer;
  21. public class WxServiceTest {
  22. private WxServiceImpl wxService;
  23. private List<String> media_ids = new ArrayList<String>();
  24. @BeforeTest
  25. public void prepare() throws JAXBException {
  26. this.wxService = null;
  27. InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
  28. WxXmlConfigStorage config = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1);
  29. this.wxService = new WxServiceImpl();
  30. this.wxService.setWxConfigStorage(config);
  31. }
  32. @Test()
  33. public void testRefreshAccessToken() throws WxErrorException {
  34. WxConfigStorage configStorage = wxService.wxConfigStorage;
  35. String before = configStorage.getAccessToken();
  36. wxService.refreshAccessToken();
  37. String after = configStorage.getAccessToken();
  38. Assert.assertNotEquals(before, after);
  39. Assert.assertTrue(StringUtils.isNotBlank(after));
  40. }
  41. @Test(dependsOnMethods = "testRefreshAccessToken", enabled = true)
  42. public void sendCustomMessage() throws WxErrorException {
  43. WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
  44. WxCustomMessage message = new WxCustomMessage();
  45. message.setMsgtype(WxConsts.MSG_TEXT);
  46. message.setTouser(configProvider.getOpenId());
  47. message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
  48. wxService.sendCustomMessage(message);
  49. }
  50. @Test(dataProvider = "menu", dependsOnMethods = "testRefreshAccessToken", enabled = true)
  51. public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
  52. wxService.createMenu(wxMenu);
  53. }
  54. @Test(dependsOnMethods = { "testRefreshAccessToken" , "testCreateMenu"}, enabled = true)
  55. public void testGetMenu() throws WxErrorException {
  56. Assert.assertNotNull(wxService.getMenu());
  57. }
  58. @Test(dependsOnMethods = { "testRefreshAccessToken", "testGetMenu"}, enabled = true)
  59. public void testDeleteMenu() throws WxErrorException {
  60. wxService.deleteMenu();
  61. }
  62. @Test(dependsOnMethods = { "testRefreshAccessToken" }, dataProvider="uploadMedia", enabled = true)
  63. public void testUploadMedia1(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
  64. InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
  65. WxUploadResult res = wxService.uploadMedia(mediaType, fileType, inputStream);
  66. Assert.assertNotNull(res.getType());
  67. Assert.assertNotNull(res.getCreated_at());
  68. Assert.assertTrue(res.getMedia_id() != null || res.getThumb_media_id() != null);
  69. if (res.getMedia_id() != null) {
  70. media_ids.add(res.getMedia_id());
  71. }
  72. if (res.getThumb_media_id() != null) {
  73. media_ids.add(res.getThumb_media_id());
  74. }
  75. }
  76. @DataProvider
  77. public Object[][] uploadMedia() {
  78. return new Object[][] {
  79. new Object[] { WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, "mm.jpeg" },
  80. new Object[] { WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, "mm.mp3" },
  81. new Object[] { WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, "mm.mp4" },
  82. new Object[] { WxConsts.MEDIA_THUMB, WxConsts.FILE_JPG, "mm.jpeg" }
  83. };
  84. }
  85. @Test(dependsOnMethods = { "testUploadMedia1" }, dataProvider="downloadMedia")
  86. public void testDownloadMedia(String media_id) throws WxErrorException {
  87. wxService.downloadMedia(media_id);
  88. }
  89. @DataProvider
  90. public Object[][] downloadMedia() {
  91. Object[][] params = new Object[this.media_ids.size()][];
  92. for (int i = 0; i < this.media_ids.size(); i++) {
  93. params[i] = new Object[] { this.media_ids.get(i) };
  94. }
  95. return params;
  96. }
  97. @Test(enabled = true)
  98. public void testCheckSignature() throws WxErrorException {
  99. String timestamp = "23234235423246";
  100. String nonce = "y7didfkcmvnbd90sdofjkiefhsd";
  101. String signature = "77b6651628dfb9a64bfb0d3432ee053ac566a459";
  102. Assert.assertTrue(wxService.checkSignature(timestamp, nonce, signature));
  103. }
  104. @DataProvider(name="menu")
  105. public Object[][] getMenu() throws JAXBException {
  106. WxMenu menu = new WxMenu();
  107. WxMenuButton button1 = new WxMenuButton();
  108. button1.setType("click");
  109. button1.setName("今日歌曲");
  110. button1.setKey("V1001_TODAY_MUSIC");
  111. WxMenuButton button2 = new WxMenuButton();
  112. button2.setType("click");
  113. button2.setName("歌手简介");
  114. button2.setKey("V1001_TODAY_SINGER");
  115. WxMenuButton button3 = new WxMenuButton();
  116. button3.setName("菜单");
  117. menu.getButton().add(button1);
  118. menu.getButton().add(button2);
  119. menu.getButton().add(button3);
  120. WxMenuButton button31 = new WxMenuButton();
  121. button31.setType("view");
  122. button31.setName("搜索");
  123. button31.setUrl("http://www.soso.com/");
  124. WxMenuButton button32 = new WxMenuButton();
  125. button32.setType("view");
  126. button32.setName("视频");
  127. button32.setUrl("http://v.qq.com/");
  128. WxMenuButton button33 = new WxMenuButton();
  129. button33.setType("click");
  130. button33.setName("赞一下我们");
  131. button33.setKey("V1001_GOOD");
  132. button3.getSub_button().add(button31);
  133. button3.getSub_button().add(button32);
  134. button3.getSub_button().add(button33);
  135. return new Object[][] {
  136. new Object[] {
  137. menu
  138. }
  139. };
  140. }
  141. @XmlRootElement(name = "xml")
  142. @XmlAccessorType(XmlAccessType.FIELD)
  143. public static class WxXmlConfigStorage extends WxInMemoryConfigStorage {
  144. protected String openId;
  145. public String getOpenId() {
  146. return openId;
  147. }
  148. public void setOpenId(String openId) {
  149. this.openId = openId;
  150. }
  151. @Override
  152. public String toString() {
  153. return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken
  154. + ", expiresIn=" + expiresIn + ", token=" + token + ", openId=" + openId + "]";
  155. }
  156. }
  157. }