mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
添加菜单API的支持
This commit is contained in:
18
src/test/java/chanjarster/weixin/bean/WxAccessTokenTest.java
Normal file
18
src/test/java/chanjarster/weixin/bean/WxAccessTokenTest.java
Normal 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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package chanjarster.weixin.bean;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
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(WxConsts.TEXT);
|
||||
reply.setContent("sfsfdsdf");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
|
||||
}
|
||||
|
||||
public void testImageReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxConsts.IMAGE);
|
||||
reply.setMedia_id("MEDIA_ID");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVoiceReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxConsts.VOICE);
|
||||
reply.setMedia_id("MEDIA_ID");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVideoReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxConsts.VIDEO);
|
||||
reply.setMedia_id("MEDIA_ID");
|
||||
reply.setThumb_media_id("MEDIA_ID");
|
||||
reply.setTitle("TITLE");
|
||||
reply.setDescription("DESCRIPTION");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
|
||||
}
|
||||
|
||||
public void testMusicReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxConsts.MUSIC);
|
||||
reply.setThumb_media_id("MEDIA_ID");
|
||||
reply.setDescription("DESCRIPTION");
|
||||
reply.setTitle("TITLE");
|
||||
reply.setMusicurl("MUSIC_URL");
|
||||
reply.setHqmusicurl("HQ_MUSIC_URL");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
|
||||
}
|
||||
|
||||
public void testNewsReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxConsts.NEWS);
|
||||
|
||||
WxArticle article1 = new WxArticle();
|
||||
article1.setUrl("URL");
|
||||
article1.setPicurl("PIC_URL");
|
||||
article1.setDescription("Is Really A Happy Day");
|
||||
article1.setTitle("Happy Day");
|
||||
reply.getArticles().add(article1);
|
||||
|
||||
WxArticle article2 = new WxArticle();
|
||||
article2.setUrl("URL");
|
||||
article2.setPicurl("PIC_URL");
|
||||
article2.setDescription("Is Really A Happy Day");
|
||||
article2.setTitle("Happy Day");
|
||||
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\"}]}");
|
||||
}
|
||||
|
||||
}
|
||||
38
src/test/java/chanjarster/weixin/bean/WxErrorTest.java
Normal file
38
src/test/java/chanjarster/weixin/bean/WxErrorTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package chanjarster.weixin.bean;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.bean.WxError;
|
||||
|
||||
@Test
|
||||
public class WxErrorTest {
|
||||
|
||||
public void testFromJson() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrcode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrmsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson1() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\", \"media_id\": \"12323423dsfafsf232f\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrcode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrmsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson2() {
|
||||
|
||||
String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrcode() == 0);
|
||||
Assert.assertEquals(wxError.getErrmsg(), null);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
104
src/test/java/chanjarster/weixin/bean/WxMenuTest.java
Normal file
104
src/test/java/chanjarster/weixin/bean/WxMenuTest.java
Normal 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 }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
119
src/test/java/chanjarster/weixin/bean/WxXmlMessageTest.java
Normal file
119
src/test/java/chanjarster/weixin/bean/WxXmlMessageTest.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package chanjarster.weixin.bean;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.api.WxConsts;
|
||||
import chanjarster.weixin.bean.WxXmlMessage;
|
||||
|
||||
@Test
|
||||
public class WxXmlMessageTest {
|
||||
|
||||
public void testFromXml() {
|
||||
|
||||
String xml = "<xml>"
|
||||
+ "<ToUserName><![CDATA[toUser]]></ToUserName>"
|
||||
+ "<FromUserName><![CDATA[fromUser]]></FromUserName> "
|
||||
+ "<CreateTime>1348831860</CreateTime>"
|
||||
+ "<MsgType><![CDATA[text]]></MsgType>"
|
||||
+ "<Content><![CDATA[this is a test]]></Content>"
|
||||
+ "<MsgId>1234567890123456</MsgId>"
|
||||
+ "<PicUrl><![CDATA[this is a url]]></PicUrl>"
|
||||
+ "<MediaId><![CDATA[media_id]]></MediaId>"
|
||||
+ "<Format><![CDATA[Format]]></Format>"
|
||||
+ "<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>"
|
||||
+ "<Location_X>23.134521</Location_X>"
|
||||
+ "<Location_Y>113.358803</Location_Y>"
|
||||
+ "<Scale>20</Scale>"
|
||||
+ "<Label><![CDATA[位置信息]]></Label>"
|
||||
+ "<Description><![CDATA[公众平台官网链接]]></Description>"
|
||||
+ "<Url><![CDATA[url]]></Url>"
|
||||
+ "<Title><![CDATA[公众平台官网链接]]></Title>"
|
||||
+ "<Event><![CDATA[subscribe]]></Event>"
|
||||
+ "<EventKey><![CDATA[qrscene_123123]]></EventKey>"
|
||||
+ "<Ticket><![CDATA[TICKET]]></Ticket>"
|
||||
+ "<Latitude>23.137466</Latitude>"
|
||||
+ "<Longitude>113.352425</Longitude>"
|
||||
+ "<Precision>119.385040</Precision>"
|
||||
+ "</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(), 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");
|
||||
Assert.assertEquals(wxMessage.getMediaId(), "media_id");
|
||||
Assert.assertEquals(wxMessage.getFormat(), "Format");
|
||||
Assert.assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
|
||||
Assert.assertEquals(wxMessage.getLocation_X(), new Double(23.134521d));
|
||||
Assert.assertEquals(wxMessage.getLocation_Y(), new Double(113.358803d));
|
||||
Assert.assertEquals(wxMessage.getScale(), new Double(20));
|
||||
Assert.assertEquals(wxMessage.getLabel(), "位置信息");
|
||||
Assert.assertEquals(wxMessage.getDescription(), "公众平台官网链接");
|
||||
Assert.assertEquals(wxMessage.getUrl(), "url");
|
||||
Assert.assertEquals(wxMessage.getTitle(), "公众平台官网链接");
|
||||
Assert.assertEquals(wxMessage.getEvent(), "subscribe");
|
||||
Assert.assertEquals(wxMessage.getEventKey(), "qrscene_123123");
|
||||
Assert.assertEquals(wxMessage.getTicket(), "TICKET");
|
||||
Assert.assertEquals(wxMessage.getLatitude(), new Double(23.137466));
|
||||
Assert.assertEquals(wxMessage.getLongitude(), new Double(113.352425));
|
||||
Assert.assertEquals(wxMessage.getPrecision(), new Double(119.385040));
|
||||
}
|
||||
|
||||
public void testToXml() {
|
||||
WxXmlMessage wxMessage = new WxXmlMessage();
|
||||
wxMessage.setToUserName("toUser");
|
||||
wxMessage.setFromUserName("fromUser");
|
||||
wxMessage.setCreateTime(new Long(1348831860l));
|
||||
wxMessage.setMsgType(WxConsts.TEXT);
|
||||
wxMessage.setContent("this is a test");
|
||||
wxMessage.setMsgId(new Long(1234567890123456l));
|
||||
wxMessage.setPicUrl("this is a url");
|
||||
wxMessage.setMediaId("media_id");
|
||||
wxMessage.setFormat("Format");
|
||||
wxMessage.setThumbMediaId("thumb_media_id");
|
||||
wxMessage.setLocation_X(new Double(23.134521d));
|
||||
wxMessage.setLocation_Y(new Double(113.358803d));
|
||||
wxMessage.setScale(new Double(20));
|
||||
wxMessage.setLabel("位置信息");
|
||||
wxMessage.setDescription("公众平台官网链接");
|
||||
wxMessage.setUrl("url");
|
||||
wxMessage.setTitle("公众平台官网链接");
|
||||
wxMessage.setEvent("subscribe");
|
||||
wxMessage.setEventKey("qrscene_123123");
|
||||
wxMessage.setTicket("TICKET");
|
||||
wxMessage.setLatitude(new Double(23.137466));
|
||||
wxMessage.setLongitude(new Double(113.352425));
|
||||
wxMessage.setPrecision(new Double(119.385040));
|
||||
|
||||
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
|
||||
+ "<xml>\n"
|
||||
+ " <ToUserName><![CDATA[toUser]]></ToUserName>\n"
|
||||
+ " <FromUserName><![CDATA[fromUser]]></FromUserName>\n"
|
||||
+ " <CreateTime>1348831860</CreateTime>\n"
|
||||
+ " <MsgType><![CDATA[text]]></MsgType>\n"
|
||||
+ " <Content><![CDATA[this is a test]]></Content>\n"
|
||||
+ " <MsgId>1234567890123456</MsgId>\n"
|
||||
+ " <PicUrl><![CDATA[this is a url]]></PicUrl>\n"
|
||||
+ " <MediaId><![CDATA[media_id]]></MediaId>\n"
|
||||
+ " <Format><![CDATA[Format]]></Format>\n"
|
||||
+ " <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>\n"
|
||||
+ " <Location_X>23.134521</Location_X>\n"
|
||||
+ " <Location_Y>113.358803</Location_Y>\n"
|
||||
+ " <Scale>20.0</Scale>\n"
|
||||
+ " <Label><![CDATA[位置信息]]></Label>\n"
|
||||
+ " <Title><![CDATA[公众平台官网链接]]></Title>\n"
|
||||
+ " <Description><![CDATA[公众平台官网链接]]></Description>\n"
|
||||
+ " <Url><![CDATA[url]]></Url>\n"
|
||||
+ " <Event><![CDATA[subscribe]]></Event>\n"
|
||||
+ " <EventKey><![CDATA[qrscene_123123]]></EventKey>\n"
|
||||
+ " <Ticket><![CDATA[TICKET]]></Ticket>\n"
|
||||
+ " <Latitude>23.137466</Latitude>\n"
|
||||
+ " <Longitude>113.352425</Longitude>\n"
|
||||
+ " <Precision>119.38504</Precision>\n"
|
||||
+ "</xml>\n";
|
||||
Assert.assertEquals(wxMessage.toXml(), xml);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user