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

添加菜单API的支持

This commit is contained in:
Daniel Qian
2014-08-21 21:19:24 +08:00
parent 2f6003bae0
commit 0a3e136605
28 changed files with 527 additions and 176 deletions

View File

@@ -1,4 +1,4 @@
package chanjarster.weixin.service;
package chanjarster.weixin.api;
// TODO
public class WxMessageRouterTest {

View File

@@ -1,4 +1,4 @@
package chanjarster.weixin.service;
package chanjarster.weixin.api;
import java.io.InputStream;
@@ -12,14 +12,18 @@ import org.testng.Assert;
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.exception.WxErrorException;
import chanjarster.weixin.out.WxCustomMessage;
import chanjarster.weixin.util.XmlTransformer;
public class WxServiceTest {
@Test(dataProvider = "configs")
public void testRefreshAccessToken(WxConfigProvider config) throws WxErrorException {
public void testRefreshAccessToken(WxTestConfigProvider config) throws WxErrorException {
String before = config.getAccessToken();
WxService wxService = new WxServiceImpl();
@@ -33,12 +37,12 @@ public class WxServiceTest {
}
@Test(dataProvider = "configs")
public void sendCustomMessage(SimpleWxConfigProvider config) throws WxErrorException {
public void sendCustomMessage(WxTestConfigProvider config) throws WxErrorException {
WxService wxService = new WxServiceImpl();
wxService.setWxConfigProvider(config);
WxCustomMessage message = new WxCustomMessage();
message.setMsgtype(WxMsgType.TEXT);
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>");
@@ -56,7 +60,7 @@ public class WxServiceTest {
*/
// 没有access_token
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
SimpleWxConfigProvider config1 = XmlTransformer.fromXml(SimpleWxConfigProvider.class, is1);
WxTestConfigProvider config1 = XmlTransformer.fromXml(WxTestConfigProvider.class, is1);
return new Object[][] {
new Object[] {
config1
@@ -66,47 +70,10 @@ public class WxServiceTest {
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SimpleWxConfigProvider implements WxConfigProvider {
private String appId;
private String secret;
private String accessToken = "";
private Integer expiresIn;
private String token;
private String openId;
public void updateAccessToken(String accessToken, Integer expiresIn) {
this.accessToken = accessToken;
this.expiresIn = expiresIn;
}
public String getAccessToken() {
return accessToken;
}
public String getAppId() {
return appId;
}
public String getSecret() {
return secret;
}
public String getToken() {
return token;
}
public void setAppId(String appId) {
this.appId = appId;
}
public void setSecret(String secret) {
this.secret = secret;
}
public void setToken(String token) {
this.token = token;
}
public Integer getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Integer expiresIn) {
this.expiresIn = expiresIn;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public static class WxTestConfigProvider extends WxMemoryConfigProvider {
protected String openId;
public String getOpenId() {
return openId;
}

View File

@@ -0,0 +1,18 @@
package chanjarster.weixin.bean;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class WxAccessTokenTest {
public void testFromJson() {
String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
WxAccessToken wxError = WxAccessToken.fromJson(json);
Assert.assertEquals(wxError.getAccess_token(), "ACCESS_TOKEN");
Assert.assertTrue(wxError.getExpires_in() == 7200);
}
}

View File

@@ -1,18 +1,19 @@
package chanjarster.weixin.out;
package chanjarster.weixin.bean;
import org.testng.Assert;
import org.testng.annotations.Test;
import chanjarster.weixin.out.WxCustomMessage;
import chanjarster.weixin.out.WxCustomMessage.WxArticle;
import chanjarster.weixin.service.WxMsgType;
import chanjarster.weixin.api.WxConsts;
import chanjarster.weixin.bean.WxCustomMessage;
import chanjarster.weixin.bean.WxCustomMessage.WxArticle;
@Test
public class WxCustomMessageTest {
public void testTextReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxMsgType.TEXT);
reply.setMsgtype(WxConsts.TEXT);
reply.setContent("sfsfdsdf");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
}
@@ -20,7 +21,7 @@ public class WxCustomMessageTest {
public void testImageReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxMsgType.IMAGE);
reply.setMsgtype(WxConsts.IMAGE);
reply.setMedia_id("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
}
@@ -28,7 +29,7 @@ public class WxCustomMessageTest {
public void testVoiceReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxMsgType.VOICE);
reply.setMsgtype(WxConsts.VOICE);
reply.setMedia_id("MEDIA_ID");
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
}
@@ -36,7 +37,7 @@ public class WxCustomMessageTest {
public void testVideoReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxMsgType.VIDEO);
reply.setMsgtype(WxConsts.VIDEO);
reply.setMedia_id("MEDIA_ID");
reply.setThumb_media_id("MEDIA_ID");
reply.setTitle("TITLE");
@@ -47,7 +48,7 @@ public class WxCustomMessageTest {
public void testMusicReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxMsgType.MUSIC);
reply.setMsgtype(WxConsts.MUSIC);
reply.setThumb_media_id("MEDIA_ID");
reply.setDescription("DESCRIPTION");
reply.setTitle("TITLE");
@@ -59,7 +60,7 @@ public class WxCustomMessageTest {
public void testNewsReply() {
WxCustomMessage reply = new WxCustomMessage();
reply.setTouser("OPENID");
reply.setMsgtype(WxMsgType.NEWS);
reply.setMsgtype(WxConsts.NEWS);
WxArticle article1 = new WxArticle();
article1.setUrl("URL");

View File

@@ -1,9 +1,9 @@
package chanjarster.weixin.in;
package chanjarster.weixin.bean;
import org.testng.Assert;
import org.testng.annotations.Test;
import chanjarster.weixin.in.WxError;
import chanjarster.weixin.bean.WxError;
@Test
public class WxErrorTest {

View File

@@ -0,0 +1,104 @@
package chanjarster.weixin.bean;
import org.apache.http.util.Asserts;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import chanjarster.weixin.bean.WxMenu.WxMenuButton;
@Test
public class WxMenuTest {
@Test(dataProvider="json")
public void testFromJson(String json) {
WxMenu menu = WxMenu.fromJson(json);
Assert.assertEquals(menu.getButton().size(), 3);
}
@Test(dataProvider="json")
public void testToJson(String json) {
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);
System.out.println(menu.toJson());
Assert.assertEquals(menu.toJson(), json);
}
@DataProvider(name="json")
public Object[][] getMenuJson() {
String json =
"{"
+"\"button\":["
+"{"
+"\"type\":\"click\","
+"\"name\":\"今日歌曲\","
+"\"key\":\"V1001_TODAY_MUSIC\""
+"},"
+"{"
+"\"type\":\"click\","
+"\"name\":\"歌手简介\","
+"\"key\":\"V1001_TODAY_SINGER\""
+"},"
+"{"
+"\"name\":\"菜单\","
+"\"sub_button\":["
+"{"
+"\"type\":\"view\","
+"\"name\":\"搜索\","
+"\"url\":\"http://www.soso.com/\""
+"},"
+"{"
+"\"type\":\"view\","
+"\"name\":\"视频\","
+"\"url\":\"http://v.qq.com/\""
+"},"
+"{"
+"\"type\":\"click\","
+"\"name\":\"赞一下我们\","
+"\"key\":\"V1001_GOOD\""
+"}"
+"]"
+"}"
+"]"
+"}";
return new Object[][] {
new Object[] { json }
};
}
}

View File

@@ -1,13 +1,13 @@
package chanjarster.weixin.out;
package chanjarster.weixin.bean;
import org.testng.Assert;
import org.testng.annotations.Test;
import chanjarster.weixin.out.WxUserMessage;
import chanjarster.weixin.service.WxMsgType;
import chanjarster.weixin.api.WxConsts;
import chanjarster.weixin.bean.WxXmlMessage;
@Test
public class WxUserMessageTest {
public class WxXmlMessageTest {
public void testFromXml() {
@@ -36,11 +36,11 @@ public class WxUserMessageTest {
+ "<Longitude>113.352425</Longitude>"
+ "<Precision>119.385040</Precision>"
+ "</xml>";
WxUserMessage wxMessage = WxUserMessage.fromXml(xml);
WxXmlMessage wxMessage = WxXmlMessage.fromXml(xml);
Assert.assertEquals(wxMessage.getToUserName(), "toUser");
Assert.assertEquals(wxMessage.getFromUserName(), "fromUser");
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
Assert.assertEquals(wxMessage.getMsgType(), WxMsgType.TEXT);
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.TEXT);
Assert.assertEquals(wxMessage.getContent(), "this is a test");
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");
@@ -63,11 +63,11 @@ public class WxUserMessageTest {
}
public void testToXml() {
WxUserMessage wxMessage = new WxUserMessage();
WxXmlMessage wxMessage = new WxXmlMessage();
wxMessage.setToUserName("toUser");
wxMessage.setFromUserName("fromUser");
wxMessage.setCreateTime(new Long(1348831860l));
wxMessage.setMsgType(WxMsgType.TEXT);
wxMessage.setMsgType(WxConsts.TEXT);
wxMessage.setContent("this is a test");
wxMessage.setMsgId(new Long(1234567890123456l));
wxMessage.setPicUrl("this is a url");