1
0
mirror of synced 2025-12-18 22:08:01 +08:00

add menu support

This commit is contained in:
Daniel Qian
2014-08-21 22:13:13 +08:00
parent 0a3e136605
commit 189f285259
19 changed files with 444 additions and 145 deletions

View File

@@ -1,6 +1,48 @@
package chanjarster.weixin.api;
// TODO
import java.util.Map;
import org.testng.Assert;
import org.testng.annotations.Test;
import chanjarster.weixin.bean.WxXmlMessage;
@Test
public class WxMessageRouterTest {
public void testSimple() {
StringBuilder sb = new StringBuilder();
WxMessageRouter router = new WxMessageRouter();
router
.rule().msgType(WxConsts.MSG_TEXT).handler(new WxEchoMessageHandler(sb, WxConsts.MSG_TEXT)).end()
.rule().msgType(WxConsts.MSG_IMAGE).handler(new WxEchoMessageHandler(sb, WxConsts.MSG_IMAGE)).end()
;
WxXmlMessage message = new WxXmlMessage();
message.setMsgType(WxConsts.MSG_TEXT);
router.route(message);
Assert.assertEquals(sb.toString(), WxConsts.MSG_TEXT + ",");
}
public static class WxEchoMessageHandler implements WxMessageHandler {
private StringBuilder sb;
private String echoStr;
public WxEchoMessageHandler(StringBuilder sb, String echoStr) {
this.sb = sb;
this.echoStr = echoStr;
}
@Override
public void handle(WxXmlMessage wxMessage, Map<String, Object> context) {
sb.append(this.echoStr).append(',');
}
}
}

View File

@@ -9,68 +9,117 @@ import javax.xml.bind.annotation.XmlRootElement;
import org.apache.commons.lang3.StringUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import chanjarster.weixin.api.WxConsts;
import chanjarster.weixin.api.WxMemoryConfigProvider;
import chanjarster.weixin.api.WxService;
import chanjarster.weixin.api.WxServiceImpl;
import chanjarster.weixin.bean.WxCustomMessage;
import chanjarster.weixin.bean.WxMenu;
import chanjarster.weixin.bean.WxMenu.WxMenuButton;
import chanjarster.weixin.exception.WxErrorException;
import chanjarster.weixin.util.XmlTransformer;
public class WxServiceTest {
@Test(dataProvider = "configs")
public void testRefreshAccessToken(WxTestConfigProvider config) throws WxErrorException {
String before = config.getAccessToken();
WxService wxService = new WxServiceImpl();
wxService.setWxConfigProvider(config);
private WxServiceImpl wxService;
@BeforeTest
public void prepare() throws JAXBException {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxXmlConfigStorage config1 = XmlTransformer.fromXml(WxXmlConfigStorage.class, is1);
this.wxService = new WxServiceImpl();
this.wxService.setWxConfigProvider(config1);
}
@Test
public void testRefreshAccessToken() throws WxErrorException {
WxConfigStorage configProvider = wxService.wxConfigProvider;
String before = configProvider.getAccessToken();
wxService.refreshAccessToken();
String after = config.getAccessToken();
String after = configProvider.getAccessToken();
Assert.assertNotEquals(before, after);
Assert.assertTrue(StringUtils.isNotBlank(after));
}
@Test(dataProvider = "configs")
public void sendCustomMessage(WxTestConfigProvider config) throws WxErrorException {
WxService wxService = new WxServiceImpl();
wxService.setWxConfigProvider(config);
@Test(dependsOnMethods = "testRefreshAccessToken", enabled = false)
public void sendCustomMessage() throws WxErrorException {
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigProvider;
WxCustomMessage message = new WxCustomMessage();
message.setMsgtype(WxConsts.TEXT);
message.setTouser(config.getOpenId());
message.setContent("欢迎使用教务系统微信公众号\n下面\n<a href=\"http://192.168.1.249:9180/eams-rc/login.action\">Hello World</a>");
message.setMsgtype(WxConsts.MSG_TEXT);
message.setTouser(configProvider.getOpenId());
message.setContent("欢迎使用教务系统微信公众号\n下面\n<a href=\"http://www.baidu.com\">Hello World</a>");
wxService.sendCustomMessage(message);
}
/**
* 返回新的access_token
* @return
* @throws JAXBException
*/
@DataProvider(name = "configs")
public Object[][] getConfig() throws JAXBException {
/**
* 将 src/test/resources/test-config.sample.xml 改成 test-config.xml 并设置appId, secret, 一个过期的accessToken
*/
// 没有access_token
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxTestConfigProvider config1 = XmlTransformer.fromXml(WxTestConfigProvider.class, is1);
@Test(dataProvider = "menu", enabled = true, dependsOnMethods = "testRefreshAccessToken")
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
wxService.createMenu(wxMenu);
}
@Test(dependsOnMethods = { "testRefreshAccessToken" , "testCreateMenu"})
public void testGetMenu() throws WxErrorException {
Assert.assertNotNull(wxService.getMenu());
}
@Test(dependsOnMethods = { "testRefreshAccessToken", "testGetMenu" }, enabled = false)
public void testDeleteMenu() throws WxErrorException {
wxService.deleteMenu();
}
@DataProvider(name="menu")
public Object[][] getMenu() throws JAXBException {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType("click");
button1.setName("今日歌曲");
button1.setKey("V1001_TODAY_MUSIC");
WxMenuButton button2 = new WxMenuButton();
button2.setType("click");
button2.setName("歌手简介");
button2.setKey("V1001_TODAY_SINGER");
WxMenuButton button3 = new WxMenuButton();
button3.setName("菜单");
menu.getButton().add(button1);
menu.getButton().add(button2);
menu.getButton().add(button3);
WxMenuButton button31 = new WxMenuButton();
button31.setType("view");
button31.setName("搜索");
button31.setUrl("http://www.soso.com/");
WxMenuButton button32 = new WxMenuButton();
button32.setType("view");
button32.setName("视频");
button32.setUrl("http://v.qq.com/");
WxMenuButton button33 = new WxMenuButton();
button33.setType("click");
button33.setName("赞一下我们");
button33.setKey("V1001_GOOD");
button3.getSub_button().add(button31);
button3.getSub_button().add(button32);
button3.getSub_button().add(button33);
return new Object[][] {
new Object[] {
config1
menu
}
};
}
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public static class WxTestConfigProvider extends WxMemoryConfigProvider {
public static class WxXmlConfigStorage extends WxInMemoryConfigStorage {
protected String openId;

View File

@@ -13,7 +13,7 @@ public class WxCustomMessageTest {
public void testTextReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxConsts.TEXT);
reply.setMsgtype(WxConsts.MSG_TEXT);
reply.setContent("sfsfdsdf");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
}
@@ -21,7 +21,7 @@ public class WxCustomMessageTest {
public void testImageReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxConsts.IMAGE);
reply.setMsgtype(WxConsts.MSG_IMAGE);
reply.setMedia_id("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
}
@@ -29,7 +29,7 @@ public class WxCustomMessageTest {
public void testVoiceReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxConsts.VOICE);
reply.setMsgtype(WxConsts.MSG_VOICE);
reply.setMedia_id("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
}
@@ -37,7 +37,7 @@ public class WxCustomMessageTest {
public void testVideoReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxConsts.VIDEO);
reply.setMsgtype(WxConsts.MSG_VIDEO);
reply.setMedia_id("MEDIA_ID");
reply.setThumb_media_id("MEDIA_ID");
reply.setTitle("TITLE");
@@ -48,7 +48,7 @@ public class WxCustomMessageTest {
public void testMusicReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxConsts.MUSIC);
reply.setMsgtype(WxConsts.MSG_MUSIC);
reply.setThumb_media_id("MEDIA_ID");
reply.setDescription("DESCRIPTION");
reply.setTitle("TITLE");
@@ -60,7 +60,7 @@ public class WxCustomMessageTest {
public void testNewsReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxConsts.NEWS);
reply.setMsgtype(WxConsts.MSG_NEWS);
WxArticle article1 = new WxArticle();
article1.setUrl("URL");
@@ -77,7 +77,6 @@ public class WxCustomMessageTest {
reply.getArticles().add(article2);
System.out.println(reply.toJson());
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}");
}

View File

@@ -1,6 +1,5 @@
package chanjarster.weixin.bean;
import org.apache.http.util.Asserts;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -10,13 +9,13 @@ import chanjarster.weixin.bean.WxMenu.WxMenuButton;
@Test
public class WxMenuTest {
@Test(dataProvider="json")
@Test(dataProvider="wxReturnMenu")
public void testFromJson(String json) {
WxMenu menu = WxMenu.fromJson(json);
Assert.assertEquals(menu.getButton().size(), 3);
}
@Test(dataProvider="json")
@Test(dataProvider="wxPushMenu")
public void testToJson(String json) {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
@@ -55,12 +54,20 @@ public class WxMenuTest {
button3.getSub_button().add(button32);
button3.getSub_button().add(button33);
System.out.println(menu.toJson());
Assert.assertEquals(menu.toJson(), json);
}
@DataProvider(name="json")
public Object[][] getMenuJson() {
@Test(dataProvider="wxReturnMenu")
public Object[][] wxReturnMenu() {
Object[][] res = menuJson();
String json = "{ \"menu\" : " + res[0][0] + " }";
return new Object[][] {
new Object[] { json }
};
}
@DataProvider(name="wxPushMenu")
public Object[][] menuJson() {
String json =
"{"
+"\"button\":["

View File

@@ -40,7 +40,7 @@ public class WxXmlMessageTest {
Assert.assertEquals(wxMessage.getToUserName(), "toUser");
Assert.assertEquals(wxMessage.getFromUserName(), "fromUser");
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.TEXT);
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.MSG_TEXT);
Assert.assertEquals(wxMessage.getContent(), "this is a test");
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");
@@ -67,7 +67,7 @@ public class WxXmlMessageTest {
wxMessage.setToUserName("toUser");
wxMessage.setFromUserName("fromUser");
wxMessage.setCreateTime(new Long(1348831860l));
wxMessage.setMsgType(WxConsts.TEXT);
wxMessage.setMsgType(WxConsts.MSG_TEXT);
wxMessage.setContent("this is a test");
wxMessage.setMsgId(new Long(1234567890123456l));
wxMessage.setPicUrl("this is a url");