添加群发消息单元测试
This commit is contained in:
@@ -3,8 +3,10 @@ package chanjarster.weixin.api;
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import chanjarster.weixin.api.WxBaseAPITest.WxXmlConfigStorage;
|
||||
import chanjarster.weixin.util.xml.XmlTransformer;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
@@ -26,4 +28,24 @@ public class ApiTestModule implements Module {
|
||||
}
|
||||
}
|
||||
|
||||
@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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,113 +1,42 @@
|
||||
package chanjarster.weixin.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.bean.result.WxMediaUploadResult;
|
||||
import chanjarster.weixin.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@Test(groups="baseAPI")
|
||||
/**
|
||||
* 基础API测试
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxBaseAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
private List<String> media_ids = new ArrayList<String>();
|
||||
|
||||
@Test
|
||||
public void testRefreshAccessToken() throws WxErrorException {
|
||||
WxConfigStorage configStorage = wxService.wxConfigStorage;
|
||||
String before = configStorage.getAccessToken();
|
||||
wxService.refreshAccessToken();
|
||||
|
||||
|
||||
String after = configStorage.getAccessToken();
|
||||
|
||||
|
||||
Assert.assertNotEquals(before, after);
|
||||
Assert.assertTrue(StringUtils.isNotBlank(after));
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testRefreshAccessToken" }, dataProvider="uploadMedia", enabled = true)
|
||||
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
|
||||
WxMediaUploadResult res = wxService.uploadMedia(mediaType, fileType, inputStream);
|
||||
Assert.assertNotNull(res.getType());
|
||||
Assert.assertNotNull(res.getCreated_at());
|
||||
Assert.assertTrue(res.getMedia_id() != null || res.getThumb_media_id() != null);
|
||||
|
||||
if (res.getMedia_id() != null) {
|
||||
media_ids.add(res.getMedia_id());
|
||||
}
|
||||
if (res.getThumb_media_id() != null) {
|
||||
media_ids.add(res.getThumb_media_id());
|
||||
}
|
||||
}
|
||||
|
||||
@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.downloadMedia(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;
|
||||
}
|
||||
|
||||
@Test(enabled = true)
|
||||
|
||||
public void testCheckSignature() throws WxErrorException {
|
||||
String timestamp = "23234235423246";
|
||||
String nonce = "y7didfkcmvnbd90sdofjkiefhsd";
|
||||
String signature = "77b6651628dfb9a64bfb0d3432ee053ac566a459";
|
||||
Assert.assertTrue(wxService.checkSignature(timestamp, nonce, signature));
|
||||
}
|
||||
|
||||
@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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,20 +3,24 @@ package chanjarster.weixin.api;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import chanjarster.weixin.api.WxBaseAPITest.WxXmlConfigStorage;
|
||||
import chanjarster.weixin.api.ApiTestModule.WxXmlConfigStorage;
|
||||
import chanjarster.weixin.bean.WxCustomMessage;
|
||||
import chanjarster.weixin.exception.WxErrorException;
|
||||
|
||||
@Test(dependsOnGroups="baseAPI")
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/***
|
||||
* 测试发送客服消息
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups="customMessageAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCustomMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
@Test(enabled = true)
|
||||
|
||||
public void testSendCustomMessage() throws WxErrorException {
|
||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxCustomMessage message = new WxCustomMessage();
|
||||
@@ -26,5 +30,5 @@ public class WxCustomMessageAPITest {
|
||||
|
||||
wxService.sendCustomMessage(message);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
142
src/test/java/chanjarster/weixin/api/WxMassMessageAPITest.java
Normal file
142
src/test/java/chanjarster/weixin/api/WxMassMessageAPITest.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package chanjarster.weixin.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 chanjarster.weixin.api.ApiTestModule.WxXmlConfigStorage;
|
||||
import chanjarster.weixin.bean.WxMassNews;
|
||||
import chanjarster.weixin.bean.WxMassNews.WxMassNewsArticle;
|
||||
import chanjarster.weixin.bean.WxMassOpenIdsMessage;
|
||||
import chanjarster.weixin.bean.WxMassVideo;
|
||||
import chanjarster.weixin.bean.result.WxMassSendResult;
|
||||
import chanjarster.weixin.bean.result.WxMassUploadResult;
|
||||
import chanjarster.weixin.bean.result.WxMediaUploadResult;
|
||||
import chanjarster.weixin.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试群发消息
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups = "massAPI", dependsOnGroups = { "baseAPI", "mediaAPI"})
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMassMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
|
||||
@Test(enabled = false)
|
||||
public void testSendMassTextByOpenIds() throws WxErrorException {
|
||||
// 发送群发消息
|
||||
WxXmlConfigStorage configProvider = (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.getTouser().add(configProvider.getOpenId());
|
||||
|
||||
WxMassSendResult massResult = wxService.sendMassMessageByOpenIds(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsg_id());
|
||||
}
|
||||
|
||||
@Test(enabled = true, dataProvider="massMessages")
|
||||
public void testSendMassByOpenIds(String massMsgType, String mediaId) throws WxErrorException, IOException {
|
||||
// 发送群发消息
|
||||
WxXmlConfigStorage configProvider = (WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxMassOpenIdsMessage massMessage = new WxMassOpenIdsMessage();
|
||||
massMessage.setMsgtype(massMsgType);
|
||||
massMessage.setMedia_id(mediaId);
|
||||
massMessage.getTouser().add(configProvider.getOpenId());
|
||||
|
||||
WxMassSendResult massResult = wxService.sendMassMessageByOpenIds(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsg_id());
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] massMessages() throws WxErrorException, IOException {
|
||||
Object[][] messages = new Object[4][];
|
||||
/*
|
||||
* 视频素材
|
||||
*/
|
||||
{
|
||||
// 上传视频到媒体库
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp4");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.uploadMedia(WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMedia_id());
|
||||
|
||||
// 把视频变成可被群发的媒体
|
||||
WxMassVideo video = new WxMassVideo();
|
||||
video.setTitle("测试标题");
|
||||
video.setDescription("测试描述");
|
||||
video.setMedia_id(uploadMediaRes.getMedia_id());
|
||||
WxMassUploadResult uploadResult = wxService.uploadMassVideo(video);
|
||||
Assert.assertNotNull(uploadResult);
|
||||
Assert.assertNotNull(uploadResult.getMedia_id());
|
||||
messages[0] = new Object[] { WxConsts.MASS_MSG_VIDEO, uploadResult.getMedia_id() };
|
||||
}
|
||||
/**
|
||||
* 图片素材
|
||||
*/
|
||||
{
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.uploadMedia(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMedia_id());
|
||||
messages[1] = new Object[] { WxConsts.MASS_MSG_IMAGE, uploadMediaRes.getMedia_id() };
|
||||
}
|
||||
/**
|
||||
* 语音素材
|
||||
*/
|
||||
{
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp3");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.uploadMedia(WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMedia_id());
|
||||
messages[2] = new Object[] { WxConsts.MASS_MSG_VOICE, uploadMediaRes.getMedia_id() };
|
||||
}
|
||||
/**
|
||||
* 图文素材
|
||||
*/
|
||||
{
|
||||
// 上传照片到媒体库
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.uploadMedia(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMedia_id());
|
||||
|
||||
// 上传图文消息
|
||||
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.setThumb_media_id(uploadMediaRes.getMedia_id());
|
||||
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.setThumb_media_id(uploadMediaRes.getMedia_id());
|
||||
article2.setShow_cover_pic(true);
|
||||
article2.setAuthor("作者2");
|
||||
article2.setContent_source_url("www.baidu.com");
|
||||
article2.setDigest("摘要2");
|
||||
news.addArticle(article2);
|
||||
|
||||
WxMassUploadResult massUploadResult = wxService.uploadMassNews(news);
|
||||
Assert.assertNotNull(massUploadResult);
|
||||
Assert.assertNotNull(uploadMediaRes.getMedia_id());
|
||||
messages[3] = new Object[] { WxConsts.MASS_MSG_VIDEO, massUploadResult.getMedia_id() };
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
}
|
||||
72
src/test/java/chanjarster/weixin/api/WxMediaAPITest.java
Normal file
72
src/test/java/chanjarster/weixin/api/WxMediaAPITest.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package chanjarster.weixin.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 chanjarster.weixin.bean.result.WxMediaUploadResult;
|
||||
import chanjarster.weixin.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", enabled = true)
|
||||
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
|
||||
WxMediaUploadResult res = wxService.uploadMedia(mediaType, fileType, inputStream);
|
||||
Assert.assertNotNull(res.getType());
|
||||
Assert.assertNotNull(res.getCreated_at());
|
||||
Assert.assertTrue(res.getMedia_id() != null || res.getThumb_media_id() != null);
|
||||
|
||||
if (res.getMedia_id() != null) {
|
||||
media_ids.add(res.getMedia_id());
|
||||
}
|
||||
if (res.getThumb_media_id() != null) {
|
||||
media_ids.add(res.getThumb_media_id());
|
||||
}
|
||||
}
|
||||
|
||||
@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.downloadMedia(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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,11 @@ import chanjarster.weixin.bean.WxMenu;
|
||||
import chanjarster.weixin.bean.WxMenu.WxMenuButton;
|
||||
import chanjarster.weixin.exception.WxErrorException;
|
||||
|
||||
/**
|
||||
* 测试菜单
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMenuAPITest {
|
||||
|
||||
@@ -9,6 +9,11 @@ import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.bean.WxXmlMessage;
|
||||
|
||||
/**
|
||||
* 测试消息路由器
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public class WxMessageRouterTest {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user