mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
微信企业号
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import me.chanjar.weixin.mp.api.WxConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxServiceImpl;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
import com.google.inject.Module;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class ApiTestModule implements Module {
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder) {
|
||||
try {
|
||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
|
||||
WxXmlConfigStorage config = fromXml(WxXmlConfigStorage.class, is1);
|
||||
WxServiceImpl wxService = new WxServiceImpl();
|
||||
wxService.setWxConfigStorage(config);
|
||||
|
||||
binder.bind(WxServiceImpl.class).toInstance(wxService);
|
||||
binder.bind(WxConfigStorage.class).toInstance(config);
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
|
||||
Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
|
||||
InputSource inputSource = new InputSource(is);
|
||||
inputSource.setEncoding("utf-8");
|
||||
T object = (T) um.unmarshal(inputSource);
|
||||
return object;
|
||||
}
|
||||
|
||||
@XmlRootElement(name = "xml")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public static class WxXmlConfigStorage extends WxInMemoryConfigStorage {
|
||||
|
||||
protected String openId;
|
||||
|
||||
public String getOpenId() {
|
||||
return openId;
|
||||
}
|
||||
public void setOpenId(String openId) {
|
||||
this.openId = openId;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleWxConfigProvider [appId=" + appId + ", secret=" + secret + ", accessToken=" + accessToken
|
||||
+ ", expiresIn=" + expiresIn + ", token=" + token + ", openId=" + openId + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 基础API测试
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxBaseAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
public void testRefreshAccessToken() throws WxErrorException {
|
||||
WxConfigStorage configStorage = wxService.wxConfigStorage;
|
||||
String before = configStorage.getAccessToken();
|
||||
wxService.accessTokenRefresh();
|
||||
|
||||
String after = configStorage.getAccessToken();
|
||||
|
||||
Assert.assertNotEquals(before, after);
|
||||
Assert.assertTrue(StringUtils.isNotBlank(after));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxCustomMessage;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/***
|
||||
* 测试发送客服消息
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups="customMessageAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCustomMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
public void testSendCustomMessage() throws WxErrorException {
|
||||
ApiTestModule.WxXmlConfigStorage configStorage = (ApiTestModule.WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxCustomMessage message = new WxCustomMessage();
|
||||
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
|
||||
message.setToUser(configStorage.getOpenId());
|
||||
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||
|
||||
wxService.customMessageSend(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxGroup;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试分组接口
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
@Test(groups = "groupAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxGroupAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
protected WxGroup group;
|
||||
|
||||
public void testGroupCreate() throws WxErrorException {
|
||||
WxGroup res = wxService.groupCreate("测试分组1");
|
||||
Assert.assertEquals(res.getName(), "测试分组1");
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods="testGroupCreate")
|
||||
public void testGroupGet() throws WxErrorException {
|
||||
List<WxGroup> groupList = wxService.groupGet();
|
||||
Assert.assertNotNull(groupList);
|
||||
Assert.assertTrue(groupList.size() > 0);
|
||||
for (WxGroup g : groupList) {
|
||||
group = g;
|
||||
Assert.assertNotNull(g.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods={"testGroupGet", "testGroupCreate"})
|
||||
public void getGroupUpdate() throws WxErrorException {
|
||||
group.setName("分组改名");
|
||||
wxService.groupUpdate(group);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMassGroupMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMassNews;
|
||||
import me.chanjar.weixin.mp.bean.WxMassNews.WxMassNewsArticle;
|
||||
import me.chanjar.weixin.mp.bean.WxMassOpenIdsMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMassVideo;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMassSendResult;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMassUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试群发消息
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups = "massAPI", dependsOnGroups = { "baseAPI", "mediaAPI", "groupAPI"})
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMassMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
@Test
|
||||
public void testTextMassOpenIdsMessageSend() throws WxErrorException {
|
||||
// 发送群发消息
|
||||
ApiTestModule.WxXmlConfigStorage configProvider = (ApiTestModule.WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxMassOpenIdsMessage massMessage = new WxMassOpenIdsMessage();
|
||||
massMessage.setMsgType(WxConsts.MASS_MSG_TEXT);
|
||||
massMessage.setContent("测试群发消息\n欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||
massMessage.getToUsers().add(configProvider.getOpenId());
|
||||
|
||||
WxMassSendResult massResult = wxService.massOpenIdsMessageSend(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsgId());
|
||||
}
|
||||
|
||||
@Test(dataProvider="massMessages")
|
||||
public void testMediaMassOpenIdsMessageSend(String massMsgType, String mediaId) throws WxErrorException, IOException {
|
||||
// 发送群发消息
|
||||
ApiTestModule.WxXmlConfigStorage configProvider = (ApiTestModule.WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxMassOpenIdsMessage massMessage = new WxMassOpenIdsMessage();
|
||||
massMessage.setMsgType(massMsgType);
|
||||
massMessage.setMediaId(mediaId);
|
||||
massMessage.getToUsers().add(configProvider.getOpenId());
|
||||
|
||||
WxMassSendResult massResult = wxService.massOpenIdsMessageSend(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsgId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTextMassGroupMessageSend() throws WxErrorException {
|
||||
WxMassGroupMessage massMessage = new WxMassGroupMessage();
|
||||
massMessage.setMsgtype(WxConsts.MASS_MSG_TEXT);
|
||||
massMessage.setContent("测试群发消息\n欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||
massMessage.setGroupId(wxService.groupGet().get(0).getId());
|
||||
|
||||
WxMassSendResult massResult = wxService.massGroupMessageSend(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsgId());
|
||||
}
|
||||
|
||||
@Test(dataProvider="massMessages")
|
||||
public void testMediaMassGroupMessageSend(String massMsgType, String mediaId) throws WxErrorException, IOException {
|
||||
WxMassGroupMessage massMessage = new WxMassGroupMessage();
|
||||
massMessage.setMsgtype(massMsgType);
|
||||
massMessage.setMediaId(mediaId);
|
||||
massMessage.setGroupId(wxService.groupGet().get(0).getId());
|
||||
|
||||
WxMassSendResult massResult = wxService.massGroupMessageSend(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsgId());
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] massMessages() throws WxErrorException, IOException {
|
||||
Object[][] messages = new Object[4][];
|
||||
/*
|
||||
* 视频素材
|
||||
*/
|
||||
{
|
||||
// 上传视频到媒体库
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp4");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
|
||||
// 把视频变成可被群发的媒体
|
||||
WxMassVideo video = new WxMassVideo();
|
||||
video.setTitle("测试标题");
|
||||
video.setDescription("测试描述");
|
||||
video.setMediaId(uploadMediaRes.getMediaId());
|
||||
WxMassUploadResult uploadResult = wxService.massVideoUpload(video);
|
||||
Assert.assertNotNull(uploadResult);
|
||||
Assert.assertNotNull(uploadResult.getMediaId());
|
||||
messages[0] = new Object[] { WxConsts.MASS_MSG_VIDEO, uploadResult.getMediaId() };
|
||||
}
|
||||
/**
|
||||
* 图片素材
|
||||
*/
|
||||
{
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
messages[1] = new Object[] { WxConsts.MASS_MSG_IMAGE, uploadMediaRes.getMediaId() };
|
||||
}
|
||||
/**
|
||||
* 语音素材
|
||||
*/
|
||||
{
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp3");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
messages[2] = new Object[] { WxConsts.MASS_MSG_VOICE, uploadMediaRes.getMediaId() };
|
||||
}
|
||||
/**
|
||||
* 图文素材
|
||||
*/
|
||||
{
|
||||
// 上传照片到媒体库
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
|
||||
// 上传图文消息
|
||||
WxMassNews news = new WxMassNews();
|
||||
WxMassNewsArticle article1 = new WxMassNewsArticle();
|
||||
article1.setTitle("标题1");
|
||||
article1.setContent("内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1内容1");
|
||||
article1.setThumbMediaId(uploadMediaRes.getMediaId());
|
||||
news.addArticle(article1);
|
||||
|
||||
WxMassNewsArticle article2 = new WxMassNewsArticle();
|
||||
article2.setTitle("标题2");
|
||||
article2.setContent("内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2内容2");
|
||||
article2.setThumbMediaId(uploadMediaRes.getMediaId());
|
||||
article2.setShowCoverPic(true);
|
||||
article2.setAuthor("作者2");
|
||||
article2.setContentSourceUrl("www.baidu.com");
|
||||
article2.setDigest("摘要2");
|
||||
news.addArticle(article2);
|
||||
|
||||
WxMassUploadResult massUploadResult = wxService.massNewsUpload(news);
|
||||
Assert.assertNotNull(massUploadResult);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
messages[3] = new Object[] { WxConsts.MASS_MSG_NEWS, massUploadResult.getMediaId() };
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试多媒体文件上传下载
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups="mediaAPI", dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMediaAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
private List<String> media_ids = new ArrayList<String>();
|
||||
|
||||
@Test(dataProvider="uploadMedia")
|
||||
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
|
||||
WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream);
|
||||
Assert.assertNotNull(res.getType());
|
||||
Assert.assertNotNull(res.getCreatedAt());
|
||||
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
|
||||
|
||||
if (res.getMediaId() != null) {
|
||||
media_ids.add(res.getMediaId());
|
||||
}
|
||||
if (res.getThumbMediaId() != null) {
|
||||
media_ids.add(res.getThumbMediaId());
|
||||
}
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] uploadMedia() {
|
||||
return new Object[][] {
|
||||
new Object[] { WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, "mm.jpeg" },
|
||||
new Object[] { WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, "mm.mp3" },
|
||||
new Object[] { WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, "mm.mp4" },
|
||||
new Object[] { WxConsts.MEDIA_THUMB, WxConsts.FILE_JPG, "mm.jpeg" }
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia")
|
||||
public void testDownloadMedia(String media_id) throws WxErrorException {
|
||||
wxService.mediaDownload(media_id);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] downloadMedia() {
|
||||
Object[][] params = new Object[this.media_ids.size()][];
|
||||
for (int i = 0; i < this.media_ids.size(); i++) {
|
||||
params[i] = new Object[] { this.media_ids.get(i) };
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMenu;
|
||||
import me.chanjar.weixin.mp.bean.WxMenu.WxMenuButton;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
/**
|
||||
* 测试菜单
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups="menuAPI", dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMenuAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
@Test(dataProvider = "menu")
|
||||
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
|
||||
wxService.menuCreate(wxMenu);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testCreateMenu"})
|
||||
public void testGetMenu() throws WxErrorException {
|
||||
Assert.assertNotNull(wxService.menuGet());
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testGetMenu"})
|
||||
public void testDeleteMenu() throws WxErrorException {
|
||||
wxService.menuDelete();
|
||||
}
|
||||
|
||||
@DataProvider(name="menu")
|
||||
public Object[][] getMenu() throws JAXBException {
|
||||
WxMenu menu = new WxMenu();
|
||||
WxMenuButton button1 = new WxMenuButton();
|
||||
button1.setType(WxConsts.BUTTON_CLICK);
|
||||
button1.setName("今日歌曲");
|
||||
button1.setKey("V1001_TODAY_MUSIC");
|
||||
|
||||
WxMenuButton button2 = new WxMenuButton();
|
||||
button2.setType(WxConsts.BUTTON_CLICK);
|
||||
button2.setName("歌手简介");
|
||||
button2.setKey("V1001_TODAY_SINGER");
|
||||
|
||||
WxMenuButton button3 = new WxMenuButton();
|
||||
button3.setName("菜单");
|
||||
|
||||
menu.getButtons().add(button1);
|
||||
menu.getButtons().add(button2);
|
||||
menu.getButtons().add(button3);
|
||||
|
||||
WxMenuButton button31 = new WxMenuButton();
|
||||
button31.setType(WxConsts.BUTTON_VIEW);
|
||||
button31.setName("搜索");
|
||||
button31.setUrl("http://www.soso.com/");
|
||||
|
||||
WxMenuButton button32 = new WxMenuButton();
|
||||
button32.setType(WxConsts.BUTTON_VIEW);
|
||||
button32.setName("视频");
|
||||
button32.setUrl("http://v.qq.com/");
|
||||
|
||||
WxMenuButton button33 = new WxMenuButton();
|
||||
button33.setType(WxConsts.BUTTON_CLICK);
|
||||
button33.setName("赞一下我们");
|
||||
button33.setKey("V1001_GOOD");
|
||||
|
||||
button3.getSubButtons().add(button31);
|
||||
button3.getSubButtons().add(button32);
|
||||
button3.getSubButtons().add(button33);
|
||||
|
||||
return new Object[][] {
|
||||
new Object[] {
|
||||
menu
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import me.chanjar.weixin.mp.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.api.WxMessageHandler;
|
||||
import me.chanjar.weixin.mp.api.WxMessageRouter;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
|
||||
/**
|
||||
* 测试消息路由器
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public class WxMessageRouterTest {
|
||||
|
||||
@Test(enabled = false)
|
||||
public void prepare(boolean async, StringBuffer sb, WxMessageRouter router) {
|
||||
router
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK).eventKey("KEY_1").content("CONTENT_1")
|
||||
.handler(new WxEchoMessageHandler(sb, "COMBINE_4"))
|
||||
.end()
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK).eventKey("KEY_1")
|
||||
.handler(new WxEchoMessageHandler(sb, "COMBINE_3"))
|
||||
.end()
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxConsts.XML_MSG_TEXT).event(WxConsts.EVT_CLICK)
|
||||
.handler(new WxEchoMessageHandler(sb, "COMBINE_2"))
|
||||
.end()
|
||||
.rule().async(async).msgType(WxConsts.XML_MSG_TEXT).handler(new WxEchoMessageHandler(sb, WxConsts.XML_MSG_TEXT)).end()
|
||||
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoMessageHandler(sb, WxConsts.EVT_CLICK)).end()
|
||||
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMessageHandler(sb, "KEY_1")).end()
|
||||
.rule().async(async).content("CONTENT_1").handler(new WxEchoMessageHandler(sb, "CONTENT_1")).end()
|
||||
.rule().async(async).rContent(".*bc.*").handler(new WxEchoMessageHandler(sb, "abcd")).end()
|
||||
.rule().async(async).handler(new WxEchoMessageHandler(sb, "ALL")).end();
|
||||
;
|
||||
}
|
||||
|
||||
@Test(dataProvider="messages-1")
|
||||
public void testSync(WxXmlMessage message, String expected) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
WxMessageRouter router = new WxMessageRouter();
|
||||
prepare(false, sb, router);
|
||||
router.route(message);
|
||||
Assert.assertEquals(sb.toString(), expected);
|
||||
}
|
||||
|
||||
@Test(dataProvider="messages-1")
|
||||
public void testAsync(WxXmlMessage message, String expected) throws InterruptedException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
WxMessageRouter router = new WxMessageRouter();
|
||||
prepare(true, sb, router);
|
||||
router.route(message);
|
||||
Thread.sleep(500l);
|
||||
Assert.assertEquals(sb.toString(), expected);
|
||||
}
|
||||
|
||||
public void testConcurrency() throws InterruptedException {
|
||||
final WxMessageRouter router = new WxMessageRouter();
|
||||
router.rule().handler(new WxMessageHandler() {
|
||||
@Override
|
||||
public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context) {
|
||||
return null;
|
||||
}
|
||||
}).end();
|
||||
|
||||
final WxXmlMessage m = new WxXmlMessage();
|
||||
Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
router.route(m);
|
||||
try {
|
||||
Thread.sleep(1000l);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < 10; i++) {
|
||||
new Thread(r).start();
|
||||
}
|
||||
|
||||
Thread.sleep(1000l * 2);
|
||||
}
|
||||
@DataProvider(name="messages-1")
|
||||
public Object[][] messages2() {
|
||||
WxXmlMessage message1 = new WxXmlMessage();
|
||||
message1.setMsgType(WxConsts.XML_MSG_TEXT);
|
||||
|
||||
WxXmlMessage message2 = new WxXmlMessage();
|
||||
message2.setEvent(WxConsts.EVT_CLICK);
|
||||
|
||||
WxXmlMessage message3 = new WxXmlMessage();
|
||||
message3.setEventKey("KEY_1");
|
||||
|
||||
WxXmlMessage message4 = new WxXmlMessage();
|
||||
message4.setContent("CONTENT_1");
|
||||
|
||||
WxXmlMessage message5 = new WxXmlMessage();
|
||||
message5.setContent("BLA");
|
||||
|
||||
WxXmlMessage message6 = new WxXmlMessage();
|
||||
message6.setContent("abcd");
|
||||
|
||||
WxXmlMessage c2 = new WxXmlMessage();
|
||||
c2.setMsgType(WxConsts.XML_MSG_TEXT);
|
||||
c2.setEvent(WxConsts.EVT_CLICK);
|
||||
|
||||
WxXmlMessage c3 = new WxXmlMessage();
|
||||
c3.setMsgType(WxConsts.XML_MSG_TEXT);
|
||||
c3.setEvent(WxConsts.EVT_CLICK);
|
||||
c3.setEventKey("KEY_1");
|
||||
|
||||
WxXmlMessage c4 = new WxXmlMessage();
|
||||
c4.setMsgType(WxConsts.XML_MSG_TEXT);
|
||||
c4.setEvent(WxConsts.EVT_CLICK);
|
||||
c4.setEventKey("KEY_1");
|
||||
c4.setContent("CONTENT_1");
|
||||
|
||||
return new Object[][] {
|
||||
new Object[] { message1, WxConsts.XML_MSG_TEXT + "," },
|
||||
new Object[] { message2, WxConsts.EVT_CLICK + "," },
|
||||
new Object[] { message3, "KEY_1," },
|
||||
new Object[] { message4, "CONTENT_1," },
|
||||
new Object[] { message5, "ALL," },
|
||||
new Object[] { message6, "abcd," },
|
||||
new Object[] { c2, "COMBINE_2," },
|
||||
new Object[] { c3, "COMBINE_3," },
|
||||
new Object[] { c4, "COMBINE_4," }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public static class WxEchoMessageHandler implements WxMessageHandler {
|
||||
|
||||
private StringBuffer sb;
|
||||
private String echoStr;
|
||||
|
||||
public WxEchoMessageHandler(StringBuffer sb, String echoStr) {
|
||||
this.sb = sb;
|
||||
this.echoStr = echoStr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context) {
|
||||
sb.append(this.echoStr).append(',');
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.result.WxQrCodeTicket;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试用户相关的接口
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
@Test(groups = "qrCodeAPI", dependsOnGroups = { "baseAPI" })
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxQrCodeAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
public void testQrCodeCreateTmpTicket() throws WxErrorException {
|
||||
WxQrCodeTicket ticket = wxService.qrCodeCreateTmpTicket(1, null);
|
||||
Assert.assertNotNull(ticket.getUrl());
|
||||
Assert.assertNotNull(ticket.getTicket());
|
||||
Assert.assertTrue(ticket.getExpire_seconds() != -1);
|
||||
}
|
||||
|
||||
public void testQrCodeCreateLastTicket() throws WxErrorException {
|
||||
WxQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
|
||||
Assert.assertNotNull(ticket.getUrl());
|
||||
Assert.assertNotNull(ticket.getTicket());
|
||||
Assert.assertTrue(ticket.getExpire_seconds() == -1);
|
||||
}
|
||||
|
||||
public void testQrCodePicture() throws WxErrorException {
|
||||
WxQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
|
||||
File file = wxService.qrCodePicture(ticket);
|
||||
Assert.assertNotNull(file);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试短连接
|
||||
*
|
||||
* @author chanjarster
|
||||
*/
|
||||
@Test(groups = "shortURLAPI", dependsOnGroups = { "baseAPI" })
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxShortUrlAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
public void testShortUrl() throws WxErrorException {
|
||||
String shortUrl = wxService.shortUrl("www.baidu.com");
|
||||
Assert.assertNotNull(shortUrl);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.api.ApiTestModule.WxXmlConfigStorage;
|
||||
import me.chanjar.weixin.mp.bean.result.WxUser;
|
||||
import me.chanjar.weixin.mp.bean.result.WxUserList;
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试用户相关的接口
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups = "userAPI", dependsOnGroups = { "baseAPI", "groupAPI" })
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxUserAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
public void testUserUpdateRemark() throws WxErrorException {
|
||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
wxService.userUpdateRemark(configProvider.getOpenId(), "测试备注名");
|
||||
}
|
||||
|
||||
public void testUserInfo() throws WxErrorException {
|
||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxUser user = wxService.userInfo(configProvider.getOpenId(), null);
|
||||
Assert.assertNotNull(user);
|
||||
}
|
||||
|
||||
public void testUserList() throws WxErrorException {
|
||||
WxUserList wxUserList = wxService.userList(null);
|
||||
Assert.assertNotNull(wxUserList);
|
||||
Assert.assertFalse(wxUserList.getCount() == -1);
|
||||
Assert.assertFalse(wxUserList.getTotal() == -1);
|
||||
Assert.assertFalse(wxUserList.getOpenIds().size() == -1);
|
||||
}
|
||||
|
||||
public void testGroupQueryUserGroup() throws WxErrorException {
|
||||
WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
long groupid = wxService.userGetGroup(configStorage.getOpenId());
|
||||
Assert.assertTrue(groupid != -1l);
|
||||
}
|
||||
|
||||
public void getGroupMoveUser() throws WxErrorException {
|
||||
WxXmlConfigStorage configStorage = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
wxService.userUpdateGroup(configStorage.getOpenId(), wxService.groupGet().get(3).getId());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user