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.

142 line
4.4 KiB

  1. package chanjarster.weixin.api;
  2. import java.io.InputStream;
  3. import javax.xml.bind.JAXBException;
  4. import javax.xml.bind.annotation.XmlAccessType;
  5. import javax.xml.bind.annotation.XmlAccessorType;
  6. import javax.xml.bind.annotation.XmlRootElement;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.testng.Assert;
  9. import org.testng.annotations.BeforeTest;
  10. import org.testng.annotations.DataProvider;
  11. import org.testng.annotations.Test;
  12. import chanjarster.weixin.bean.WxCustomMessage;
  13. import chanjarster.weixin.bean.WxMenu;
  14. import chanjarster.weixin.bean.WxMenu.WxMenuButton;
  15. import chanjarster.weixin.exception.WxErrorException;
  16. import chanjarster.weixin.util.XmlTransformer;
  17. public class WxServiceTest {
  18. private WxServiceImpl wxService;
  19. @BeforeTest
  20. public void prepare() throws JAXBException {
  21. InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
  22. WxXmlConfigStorage config1 = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1);
  23. this.wxService = new WxServiceImpl();
  24. this.wxService.setWxConfigProvider(config1);
  25. }
  26. @Test
  27. public void testRefreshAccessToken() throws WxErrorException {
  28. WxConfigStorage configProvider = wxService.wxConfigProvider;
  29. String before = configProvider.getAccessToken();
  30. wxService.refreshAccessToken();
  31. String after = configProvider.getAccessToken();
  32. Assert.assertNotEquals(before, after);
  33. Assert.assertTrue(StringUtils.isNotBlank(after));
  34. }
  35. @Test(dependsOnMethods = "testRefreshAccessToken", enabled = false)
  36. public void sendCustomMessage() throws WxErrorException {
  37. WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigProvider;
  38. WxCustomMessage message = new WxCustomMessage();
  39. message.setMsgtype(WxConsts.MSG_TEXT);
  40. message.setTouser(configProvider.getOpenId());
  41. message.setContent("欢迎使用教务系统微信公众号\n下面\n<a href=\"http://www.baidu.com\">Hello World</a>");
  42. wxService.sendCustomMessage(message);
  43. }
  44. @Test(dataProvider = "menu", enabled = true, dependsOnMethods = "testRefreshAccessToken")
  45. public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
  46. wxService.createMenu(wxMenu);
  47. }
  48. @Test(dependsOnMethods = { "testRefreshAccessToken" , "testCreateMenu"})
  49. public void testGetMenu() throws WxErrorException {
  50. Assert.assertNotNull(wxService.getMenu());
  51. }
  52. @Test(dependsOnMethods = { "testRefreshAccessToken", "testGetMenu" }, enabled = false)
  53. public void testDeleteMenu() throws WxErrorException {
  54. wxService.deleteMenu();
  55. }
  56. @DataProvider(name="menu")
  57. public Object[][] getMenu() throws JAXBException {
  58. WxMenu menu = new WxMenu();
  59. WxMenuButton button1 = new WxMenuButton();
  60. button1.setType("click");
  61. button1.setName("今日歌曲");
  62. button1.setKey("V1001_TODAY_MUSIC");
  63. WxMenuButton button2 = new WxMenuButton();
  64. button2.setType("click");
  65. button2.setName("歌手简介");
  66. button2.setKey("V1001_TODAY_SINGER");
  67. WxMenuButton button3 = new WxMenuButton();
  68. button3.setName("菜单");
  69. menu.getButton().add(button1);
  70. menu.getButton().add(button2);
  71. menu.getButton().add(button3);
  72. WxMenuButton button31 = new WxMenuButton();
  73. button31.setType("view");
  74. button31.setName("搜索");
  75. button31.setUrl("http://www.soso.com/");
  76. WxMenuButton button32 = new WxMenuButton();
  77. button32.setType("view");
  78. button32.setName("视频");
  79. button32.setUrl("http://v.qq.com/");
  80. WxMenuButton button33 = new WxMenuButton();
  81. button33.setType("click");
  82. button33.setName("赞一下我们");
  83. button33.setKey("V1001_GOOD");
  84. button3.getSub_button().add(button31);
  85. button3.getSub_button().add(button32);
  86. button3.getSub_button().add(button33);
  87. return new Object[][] {
  88. new Object[] {
  89. menu
  90. }
  91. };
  92. }
  93. @XmlRootElement(name = "xml")
  94. @XmlAccessorType(XmlAccessType.FIELD)
  95. public static class WxXmlConfigStorage extends WxInMemoryConfigStorage {
  96. protected String openId;
  97. public String getOpenId() {
  98. return openId;
  99. }
  100. public void setOpenId(String openId) {
  101. this.openId = openId;
  102. }
  103. @Override
  104. public String toString() {
  105. return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken
  106. + ", expiresIn=" + expiresIn + ", token=" + token + ", openId=" + openId + "]";
  107. }
  108. }
  109. }