mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
整理及重构
This commit is contained in:
@@ -9,10 +9,6 @@ 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;
|
||||
@@ -23,12 +19,12 @@ public class ApiTestModule implements Module {
|
||||
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);
|
||||
WxXmlMpInMemoryConfigStorage config = fromXml(WxXmlMpInMemoryConfigStorage.class, is1);
|
||||
WxMpServiceImpl wxService = new WxMpServiceImpl();
|
||||
wxService.setWxMpConfigStorage(config);
|
||||
|
||||
binder.bind(WxServiceImpl.class).toInstance(wxService);
|
||||
binder.bind(WxConfigStorage.class).toInstance(config);
|
||||
binder.bind(WxMpServiceImpl.class).toInstance(wxService);
|
||||
binder.bind(WxMpConfigStorage.class).toInstance(config);
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -44,7 +40,7 @@ public class ApiTestModule implements Module {
|
||||
|
||||
@XmlRootElement(name = "xml")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public static class WxXmlConfigStorage extends WxInMemoryConfigStorage {
|
||||
public static class WxXmlMpInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
|
||||
|
||||
protected String openId;
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -19,10 +19,10 @@ import com.google.inject.Inject;
|
||||
public class WxBaseAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
public void testRefreshAccessToken() throws WxErrorException {
|
||||
WxConfigStorage configStorage = wxService.wxConfigStorage;
|
||||
WxMpConfigStorage configStorage = wxService.wxMpConfigStorage;
|
||||
String before = configStorage.getAccessToken();
|
||||
wxService.accessTokenRefresh();
|
||||
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
|
||||
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 me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -15,15 +15,15 @@ import com.google.inject.Inject;
|
||||
*/
|
||||
@Test(groups="customMessageAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCustomMessageAPITest {
|
||||
public class WxMpCustomMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
public void testSendCustomMessage() throws WxErrorException {
|
||||
ApiTestModule.WxXmlConfigStorage configStorage = (ApiTestModule.WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxCustomMessage message = new WxCustomMessage();
|
||||
message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
WxMpCustomMessage message = new WxMpCustomMessage();
|
||||
message.setMsgType(WxMpConsts.CUSTOM_MSG_TEXT);
|
||||
message.setToUser(configStorage.getOpenId());
|
||||
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||
|
||||
@@ -2,12 +2,12 @@ package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMpGroup;
|
||||
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 me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -18,24 +18,24 @@ import com.google.inject.Inject;
|
||||
*/
|
||||
@Test(groups = "groupAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxGroupAPITest {
|
||||
public class WxMpGroupAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
protected WxGroup group;
|
||||
protected WxMpGroup group;
|
||||
|
||||
public void testGroupCreate() throws WxErrorException {
|
||||
WxGroup res = wxService.groupCreate("测试分组1");
|
||||
WxMpGroup res = wxService.groupCreate("测试分组1");
|
||||
Assert.assertEquals(res.getName(), "测试分组1");
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods="testGroupCreate")
|
||||
public void testGroupGet() throws WxErrorException {
|
||||
List<WxGroup> groupList = wxService.groupGet();
|
||||
List<WxMpGroup> groupList = wxService.groupGet();
|
||||
Assert.assertNotNull(groupList);
|
||||
Assert.assertTrue(groupList.size() > 0);
|
||||
for (WxGroup g : groupList) {
|
||||
for (WxMpGroup g : groupList) {
|
||||
group = g;
|
||||
Assert.assertNotNull(g.getName());
|
||||
}
|
||||
@@ -3,20 +3,20 @@ package me.chanjar.weixin.mp.api;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpMassNews;
|
||||
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpMassVideo;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
|
||||
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 me.chanjar.weixin.mp.bean.WxMpMassNews.WxMassNewsArticle;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -27,21 +27,21 @@ import com.google.inject.Inject;
|
||||
*/
|
||||
@Test(groups = "massAPI", dependsOnGroups = { "baseAPI", "mediaAPI", "groupAPI"})
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMassMessageAPITest {
|
||||
public class WxMpMassMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
@Test
|
||||
public void testTextMassOpenIdsMessageSend() throws WxErrorException {
|
||||
// 发送群发消息
|
||||
ApiTestModule.WxXmlConfigStorage configProvider = (ApiTestModule.WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxMassOpenIdsMessage massMessage = new WxMassOpenIdsMessage();
|
||||
massMessage.setMsgType(WxConsts.MASS_MSG_TEXT);
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
|
||||
massMessage.setMsgType(WxMpConsts.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);
|
||||
WxMpMassSendResult massResult = wxService.massOpenIdsMessageSend(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsgId());
|
||||
}
|
||||
@@ -49,37 +49,37 @@ public class WxMassMessageAPITest {
|
||||
@Test(dataProvider="massMessages")
|
||||
public void testMediaMassOpenIdsMessageSend(String massMsgType, String mediaId) throws WxErrorException, IOException {
|
||||
// 发送群发消息
|
||||
ApiTestModule.WxXmlConfigStorage configProvider = (ApiTestModule.WxXmlConfigStorage) wxService.wxConfigStorage;
|
||||
WxMassOpenIdsMessage massMessage = new WxMassOpenIdsMessage();
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
|
||||
massMessage.setMsgType(massMsgType);
|
||||
massMessage.setMediaId(mediaId);
|
||||
massMessage.getToUsers().add(configProvider.getOpenId());
|
||||
|
||||
WxMassSendResult massResult = wxService.massOpenIdsMessageSend(massMessage);
|
||||
WxMpMassSendResult 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);
|
||||
WxMpMassGroupMessage massMessage = new WxMpMassGroupMessage();
|
||||
massMessage.setMsgtype(WxMpConsts.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);
|
||||
WxMpMassSendResult 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();
|
||||
WxMpMassGroupMessage massMessage = new WxMpMassGroupMessage();
|
||||
massMessage.setMsgtype(massMsgType);
|
||||
massMessage.setMediaId(mediaId);
|
||||
massMessage.setGroupId(wxService.groupGet().get(0).getId());
|
||||
|
||||
WxMassSendResult massResult = wxService.massGroupMessageSend(massMessage);
|
||||
WxMpMassSendResult massResult = wxService.massGroupMessageSend(massMessage);
|
||||
Assert.assertNotNull(massResult);
|
||||
Assert.assertNotNull(massResult.getMsgId());
|
||||
}
|
||||
@@ -93,39 +93,39 @@ public class WxMassMessageAPITest {
|
||||
{
|
||||
// 上传视频到媒体库
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp4");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_VIDEO, WxConsts.FILE_MP4, inputStream);
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_VIDEO, WxMpConsts.FILE_MP4, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
|
||||
// 把视频变成可被群发的媒体
|
||||
WxMassVideo video = new WxMassVideo();
|
||||
WxMpMassVideo video = new WxMpMassVideo();
|
||||
video.setTitle("测试标题");
|
||||
video.setDescription("测试描述");
|
||||
video.setMediaId(uploadMediaRes.getMediaId());
|
||||
WxMassUploadResult uploadResult = wxService.massVideoUpload(video);
|
||||
WxMpMassUploadResult uploadResult = wxService.massVideoUpload(video);
|
||||
Assert.assertNotNull(uploadResult);
|
||||
Assert.assertNotNull(uploadResult.getMediaId());
|
||||
messages[0] = new Object[] { WxConsts.MASS_MSG_VIDEO, uploadResult.getMediaId() };
|
||||
messages[0] = new Object[] { WxMpConsts.MASS_MSG_VIDEO, uploadResult.getMediaId() };
|
||||
}
|
||||
/**
|
||||
* 图片素材
|
||||
*/
|
||||
{
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_IMAGE, WxMpConsts.FILE_JPG, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
messages[1] = new Object[] { WxConsts.MASS_MSG_IMAGE, uploadMediaRes.getMediaId() };
|
||||
messages[1] = new Object[] { WxMpConsts.MASS_MSG_IMAGE, uploadMediaRes.getMediaId() };
|
||||
}
|
||||
/**
|
||||
* 语音素材
|
||||
*/
|
||||
{
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.mp3");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_VOICE, WxConsts.FILE_MP3, inputStream);
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_VOICE, WxMpConsts.FILE_MP3, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
messages[2] = new Object[] { WxConsts.MASS_MSG_VOICE, uploadMediaRes.getMediaId() };
|
||||
messages[2] = new Object[] { WxMpConsts.MASS_MSG_VOICE, uploadMediaRes.getMediaId() };
|
||||
}
|
||||
/**
|
||||
* 图文素材
|
||||
@@ -133,12 +133,12 @@ public class WxMassMessageAPITest {
|
||||
{
|
||||
// 上传照片到媒体库
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream("mm.jpeg");
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, inputStream);
|
||||
WxMediaUploadResult uploadMediaRes = wxService.mediaUpload(WxMpConsts.MEDIA_IMAGE, WxMpConsts.FILE_JPG, inputStream);
|
||||
Assert.assertNotNull(uploadMediaRes);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
|
||||
// 上传图文消息
|
||||
WxMassNews news = new WxMassNews();
|
||||
WxMpMassNews news = new WxMpMassNews();
|
||||
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");
|
||||
@@ -155,10 +155,10 @@ public class WxMassMessageAPITest {
|
||||
article2.setDigest("摘要2");
|
||||
news.addArticle(article2);
|
||||
|
||||
WxMassUploadResult massUploadResult = wxService.massNewsUpload(news);
|
||||
WxMpMassUploadResult massUploadResult = wxService.massNewsUpload(news);
|
||||
Assert.assertNotNull(massUploadResult);
|
||||
Assert.assertNotNull(uploadMediaRes.getMediaId());
|
||||
messages[3] = new Object[] { WxConsts.MASS_MSG_NEWS, massUploadResult.getMediaId() };
|
||||
messages[3] = new Object[] { WxMpConsts.MASS_MSG_NEWS, massUploadResult.getMediaId() };
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
@@ -5,13 +5,13 @@ import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
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 me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -22,10 +22,10 @@ import com.google.inject.Inject;
|
||||
*/
|
||||
@Test(groups="mediaAPI", dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMediaAPITest {
|
||||
public class WxMpMediaAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
private List<String> media_ids = new ArrayList<String>();
|
||||
|
||||
@@ -48,10 +48,10 @@ public class WxMediaAPITest {
|
||||
@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" }
|
||||
new Object[] { WxMpConsts.MEDIA_IMAGE, WxMpConsts.FILE_JPG, "mm.jpeg" },
|
||||
new Object[] { WxMpConsts.MEDIA_VOICE, WxMpConsts.FILE_MP3, "mm.mp3" },
|
||||
new Object[] { WxMpConsts.MEDIA_VIDEO, WxMpConsts.FILE_MP4, "mm.mp4" },
|
||||
new Object[] { WxMpConsts.MEDIA_THUMB, WxMpConsts.FILE_JPG, "mm.jpeg" }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ 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;
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
/**
|
||||
* 测试菜单
|
||||
@@ -20,10 +20,10 @@ import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
*/
|
||||
@Test(groups="menuAPI", dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMenuAPITest {
|
||||
public class WxMpMenuAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
@Test(dataProvider = "menu")
|
||||
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
|
||||
@@ -44,12 +44,12 @@ public class WxMenuAPITest {
|
||||
public Object[][] getMenu() throws JAXBException {
|
||||
WxMenu menu = new WxMenu();
|
||||
WxMenuButton button1 = new WxMenuButton();
|
||||
button1.setType(WxConsts.BUTTON_CLICK);
|
||||
button1.setType(WxMpConsts.BUTTON_CLICK);
|
||||
button1.setName("今日歌曲");
|
||||
button1.setKey("V1001_TODAY_MUSIC");
|
||||
|
||||
WxMenuButton button2 = new WxMenuButton();
|
||||
button2.setType(WxConsts.BUTTON_CLICK);
|
||||
button2.setType(WxMpConsts.BUTTON_CLICK);
|
||||
button2.setName("歌手简介");
|
||||
button2.setKey("V1001_TODAY_SINGER");
|
||||
|
||||
@@ -61,17 +61,17 @@ public class WxMenuAPITest {
|
||||
menu.getButtons().add(button3);
|
||||
|
||||
WxMenuButton button31 = new WxMenuButton();
|
||||
button31.setType(WxConsts.BUTTON_VIEW);
|
||||
button31.setType(WxMpConsts.BUTTON_VIEW);
|
||||
button31.setName("搜索");
|
||||
button31.setUrl("http://www.soso.com/");
|
||||
|
||||
WxMenuButton button32 = new WxMenuButton();
|
||||
button32.setType(WxConsts.BUTTON_VIEW);
|
||||
button32.setType(WxMpConsts.BUTTON_VIEW);
|
||||
button32.setName("视频");
|
||||
button32.setUrl("http://v.qq.com/");
|
||||
|
||||
WxMenuButton button33 = new WxMenuButton();
|
||||
button33.setType(WxConsts.BUTTON_CLICK);
|
||||
button33.setType(WxMpConsts.BUTTON_CLICK);
|
||||
button33.setName("赞一下我们");
|
||||
button33.setKey("V1001_GOOD");
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* 测试消息路由器
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public class WxMpMessageRouterTest {
|
||||
|
||||
@Test(enabled = false)
|
||||
public void prepare(boolean async, StringBuffer sb, WxMpMessageRouter router) {
|
||||
router
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxMpConsts.XML_MSG_TEXT).event(WxMpConsts.EVT_CLICK).eventKey("KEY_1").content("CONTENT_1")
|
||||
.handler(new WxEchoMpMessageHandler(sb, "COMBINE_4"))
|
||||
.end()
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxMpConsts.XML_MSG_TEXT).event(WxMpConsts.EVT_CLICK).eventKey("KEY_1")
|
||||
.handler(new WxEchoMpMessageHandler(sb, "COMBINE_3"))
|
||||
.end()
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxMpConsts.XML_MSG_TEXT).event(WxMpConsts.EVT_CLICK)
|
||||
.handler(new WxEchoMpMessageHandler(sb, "COMBINE_2"))
|
||||
.end()
|
||||
.rule().async(async).msgType(WxMpConsts.XML_MSG_TEXT).handler(new WxEchoMpMessageHandler(sb, WxMpConsts.XML_MSG_TEXT)).end()
|
||||
.rule().async(async).event(WxMpConsts.EVT_CLICK).handler(new WxEchoMpMessageHandler(sb, WxMpConsts.EVT_CLICK)).end()
|
||||
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMpMessageHandler(sb, "KEY_1")).end()
|
||||
.rule().async(async).content("CONTENT_1").handler(new WxEchoMpMessageHandler(sb, "CONTENT_1")).end()
|
||||
.rule().async(async).rContent(".*bc.*").handler(new WxEchoMpMessageHandler(sb, "abcd")).end()
|
||||
.rule().async(async).handler(new WxEchoMpMessageHandler(sb, "ALL")).end();
|
||||
;
|
||||
}
|
||||
|
||||
@Test(dataProvider="messages-1")
|
||||
public void testSync(WxMpXmlMessage message, String expected) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
WxMpMessageRouter router = new WxMpMessageRouter();
|
||||
prepare(false, sb, router);
|
||||
router.route(message);
|
||||
Assert.assertEquals(sb.toString(), expected);
|
||||
}
|
||||
|
||||
@Test(dataProvider="messages-1")
|
||||
public void testAsync(WxMpXmlMessage message, String expected) throws InterruptedException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
WxMpMessageRouter router = new WxMpMessageRouter();
|
||||
prepare(true, sb, router);
|
||||
router.route(message);
|
||||
Thread.sleep(500l);
|
||||
Assert.assertEquals(sb.toString(), expected);
|
||||
}
|
||||
|
||||
public void testConcurrency() throws InterruptedException {
|
||||
final WxMpMessageRouter router = new WxMpMessageRouter();
|
||||
router.rule().handler(new WxMpMessageHandler() {
|
||||
@Override
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context) {
|
||||
return null;
|
||||
}
|
||||
}).end();
|
||||
|
||||
final WxMpXmlMessage m = new WxMpXmlMessage();
|
||||
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() {
|
||||
WxMpXmlMessage message1 = new WxMpXmlMessage();
|
||||
message1.setMsgType(WxMpConsts.XML_MSG_TEXT);
|
||||
|
||||
WxMpXmlMessage message2 = new WxMpXmlMessage();
|
||||
message2.setEvent(WxMpConsts.EVT_CLICK);
|
||||
|
||||
WxMpXmlMessage message3 = new WxMpXmlMessage();
|
||||
message3.setEventKey("KEY_1");
|
||||
|
||||
WxMpXmlMessage message4 = new WxMpXmlMessage();
|
||||
message4.setContent("CONTENT_1");
|
||||
|
||||
WxMpXmlMessage message5 = new WxMpXmlMessage();
|
||||
message5.setContent("BLA");
|
||||
|
||||
WxMpXmlMessage message6 = new WxMpXmlMessage();
|
||||
message6.setContent("abcd");
|
||||
|
||||
WxMpXmlMessage c2 = new WxMpXmlMessage();
|
||||
c2.setMsgType(WxMpConsts.XML_MSG_TEXT);
|
||||
c2.setEvent(WxMpConsts.EVT_CLICK);
|
||||
|
||||
WxMpXmlMessage c3 = new WxMpXmlMessage();
|
||||
c3.setMsgType(WxMpConsts.XML_MSG_TEXT);
|
||||
c3.setEvent(WxMpConsts.EVT_CLICK);
|
||||
c3.setEventKey("KEY_1");
|
||||
|
||||
WxMpXmlMessage c4 = new WxMpXmlMessage();
|
||||
c4.setMsgType(WxMpConsts.XML_MSG_TEXT);
|
||||
c4.setEvent(WxMpConsts.EVT_CLICK);
|
||||
c4.setEventKey("KEY_1");
|
||||
c4.setContent("CONTENT_1");
|
||||
|
||||
return new Object[][] {
|
||||
new Object[] { message1, WxMpConsts.XML_MSG_TEXT + "," },
|
||||
new Object[] { message2, WxMpConsts.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 WxEchoMpMessageHandler implements WxMpMessageHandler {
|
||||
|
||||
private StringBuffer sb;
|
||||
private String echoStr;
|
||||
|
||||
public WxEchoMpMessageHandler(StringBuffer sb, String echoStr) {
|
||||
this.sb = sb;
|
||||
this.echoStr = echoStr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context) {
|
||||
sb.append(this.echoStr).append(',');
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,12 +2,12 @@ package me.chanjar.weixin.mp.api;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
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 me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -18,27 +18,27 @@ import com.google.inject.Inject;
|
||||
*/
|
||||
@Test(groups = "qrCodeAPI", dependsOnGroups = { "baseAPI" })
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxQrCodeAPITest {
|
||||
public class WxMpQrCodeAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
public void testQrCodeCreateTmpTicket() throws WxErrorException {
|
||||
WxQrCodeTicket ticket = wxService.qrCodeCreateTmpTicket(1, null);
|
||||
WxMpQrCodeTicket 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);
|
||||
WxMpQrCodeTicket 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);
|
||||
WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
|
||||
File file = wxService.qrCodePicture(ticket);
|
||||
Assert.assertNotNull(file);
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
@@ -15,10 +15,10 @@ import com.google.inject.Inject;
|
||||
*/
|
||||
@Test(groups = "shortURLAPI", dependsOnGroups = { "baseAPI" })
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxShortUrlAPITest {
|
||||
public class WxMpShortUrlAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxServiceImpl wxService;
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
public void testShortUrl() throws WxErrorException {
|
||||
String shortUrl = wxService.shortUrl("www.baidu.com");
|
||||
@@ -0,0 +1,55 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试用户相关的接口
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
@Test(groups = "userAPI", dependsOnGroups = { "baseAPI", "groupAPI" })
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMpUserAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxMpServiceImpl wxService;
|
||||
|
||||
public void testUserUpdateRemark() throws WxErrorException {
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
wxService.userUpdateRemark(configProvider.getOpenId(), "测试备注名");
|
||||
}
|
||||
|
||||
public void testUserInfo() throws WxErrorException {
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configProvider = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
WxMpUser user = wxService.userInfo(configProvider.getOpenId(), null);
|
||||
Assert.assertNotNull(user);
|
||||
}
|
||||
|
||||
public void testUserList() throws WxErrorException {
|
||||
WxMpUserList wxMpUserList = wxService.userList(null);
|
||||
Assert.assertNotNull(wxMpUserList);
|
||||
Assert.assertFalse(wxMpUserList.getCount() == -1);
|
||||
Assert.assertFalse(wxMpUserList.getTotal() == -1);
|
||||
Assert.assertFalse(wxMpUserList.getOpenIds().size() == -1);
|
||||
}
|
||||
|
||||
public void testGroupQueryUserGroup() throws WxErrorException {
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
long groupid = wxService.userGetGroup(configStorage.getOpenId());
|
||||
Assert.assertTrue(groupid != -1l);
|
||||
}
|
||||
|
||||
public void getGroupMoveUser() throws WxErrorException {
|
||||
ApiTestModule.WxXmlMpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlMpInMemoryConfigStorage) wxService.wxMpConfigStorage;
|
||||
wxService.userUpdateGroup(configStorage.getOpenId(), wxService.groupGet().get(3).getId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxAccessToken;
|
||||
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.getAccessToken(), "ACCESS_TOKEN");
|
||||
Assert.assertTrue(wxError.getExpiresIn() == 7200);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.result.WxError;
|
||||
|
||||
@Test
|
||||
public class WxErrorTest {
|
||||
|
||||
public void testFromJson() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrorCode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrorMsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson1() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\", \"media_id\": \"12323423dsfafsf232f\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrorCode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrorMsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson2() {
|
||||
|
||||
String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrorCode() == 0);
|
||||
Assert.assertEquals(wxError.getErrorMsg(), null);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMenu;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMenu.WxMenuButton;
|
||||
|
||||
@Test
|
||||
public class WxMenuTest {
|
||||
|
||||
@Test(dataProvider="wxReturnMenu")
|
||||
public void testFromJson(String json) {
|
||||
WxMenu menu = WxMenu.fromJson(json);
|
||||
Assert.assertEquals(menu.getButtons().size(), 3);
|
||||
}
|
||||
|
||||
@Test(dataProvider="wxPushMenu")
|
||||
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.getButtons().add(button1);
|
||||
menu.getButtons().add(button2);
|
||||
menu.getButtons().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.getSubButtons().add(button31);
|
||||
button3.getSubButtons().add(button32);
|
||||
button3.getSubButtons().add(button33);
|
||||
|
||||
Assert.assertEquals(menu.toJson(), json);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
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\":["
|
||||
+"{"
|
||||
+"\"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 }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +1,57 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxCustomMessage;
|
||||
import me.chanjar.weixin.mp.api.WxMpConsts;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.mp.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.WxCustomMessage.WxArticle;
|
||||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage.WxArticle;
|
||||
|
||||
@Test
|
||||
public class WxCustomMessageTest {
|
||||
public class WxMpCustomMessageTest {
|
||||
|
||||
public void testTextReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
WxMpCustomMessage reply = new WxMpCustomMessage();
|
||||
reply.setToUser("OPENID");
|
||||
reply.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
|
||||
reply.setMsgType(WxMpConsts.CUSTOM_MSG_TEXT);
|
||||
reply.setContent("sfsfdsdf");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
|
||||
}
|
||||
|
||||
public void testTextBuild() {
|
||||
WxCustomMessage reply = WxCustomMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
|
||||
WxMpCustomMessage reply = WxMpCustomMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
|
||||
}
|
||||
|
||||
public void testImageReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
WxMpCustomMessage reply = new WxMpCustomMessage();
|
||||
reply.setToUser("OPENID");
|
||||
reply.setMsgType(WxConsts.CUSTOM_MSG_IMAGE);
|
||||
reply.setMsgType(WxMpConsts.CUSTOM_MSG_IMAGE);
|
||||
reply.setMediaId("MEDIA_ID");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testImageBuild() {
|
||||
WxCustomMessage reply = WxCustomMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
||||
WxMpCustomMessage reply = WxMpCustomMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVoiceReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
WxMpCustomMessage reply = new WxMpCustomMessage();
|
||||
reply.setToUser("OPENID");
|
||||
reply.setMsgType(WxConsts.CUSTOM_MSG_VOICE);
|
||||
reply.setMsgType(WxMpConsts.CUSTOM_MSG_VOICE);
|
||||
reply.setMediaId("MEDIA_ID");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVoiceBuild() {
|
||||
WxCustomMessage reply = WxCustomMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
||||
WxMpCustomMessage reply = WxMpCustomMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVideoReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
WxMpCustomMessage reply = new WxMpCustomMessage();
|
||||
reply.setToUser("OPENID");
|
||||
reply.setMsgType(WxConsts.CUSTOM_MSG_VIDEO);
|
||||
reply.setMsgType(WxMpConsts.CUSTOM_MSG_VIDEO);
|
||||
reply.setMediaId("MEDIA_ID");
|
||||
reply.setThumbMediaId("MEDIA_ID");
|
||||
reply.setTitle("TITLE");
|
||||
@@ -61,14 +60,14 @@ public class WxCustomMessageTest {
|
||||
}
|
||||
|
||||
public void testVideoBuild() {
|
||||
WxCustomMessage reply = WxCustomMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
|
||||
WxMpCustomMessage reply = WxMpCustomMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
|
||||
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();
|
||||
WxMpCustomMessage reply = new WxMpCustomMessage();
|
||||
reply.setToUser("OPENID");
|
||||
reply.setMsgType(WxConsts.CUSTOM_MSG_MUSIC);
|
||||
reply.setMsgType(WxMpConsts.CUSTOM_MSG_MUSIC);
|
||||
reply.setThumbMediaId("MEDIA_ID");
|
||||
reply.setDescription("DESCRIPTION");
|
||||
reply.setTitle("TITLE");
|
||||
@@ -78,7 +77,7 @@ public class WxCustomMessageTest {
|
||||
}
|
||||
|
||||
public void testMusicBuild() {
|
||||
WxCustomMessage reply = WxCustomMessage.MUSIC()
|
||||
WxMpCustomMessage reply = WxMpCustomMessage.MUSIC()
|
||||
.toUser("OPENID")
|
||||
.title("TITLE")
|
||||
.thumbMediaId("MEDIA_ID")
|
||||
@@ -90,9 +89,9 @@ public class WxCustomMessageTest {
|
||||
}
|
||||
|
||||
public void testNewsReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
WxMpCustomMessage reply = new WxMpCustomMessage();
|
||||
reply.setToUser("OPENID");
|
||||
reply.setMsgType(WxConsts.CUSTOM_MSG_NEWS);
|
||||
reply.setMsgType(WxMpConsts.CUSTOM_MSG_NEWS);
|
||||
|
||||
WxArticle article1 = new WxArticle();
|
||||
article1.setUrl("URL");
|
||||
@@ -125,7 +124,7 @@ public class WxCustomMessageTest {
|
||||
article2.setDescription("Is Really A Happy Day");
|
||||
article2.setTitle("Happy Day");
|
||||
|
||||
WxCustomMessage reply = WxCustomMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
|
||||
WxMpCustomMessage reply = WxMpCustomMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
|
||||
|
||||
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\"}]}");
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlMessage;
|
||||
import me.chanjar.weixin.mp.api.WxMpConsts;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class WxXmlMessageTest {
|
||||
public class WxMpXmlMessageTest {
|
||||
|
||||
public void testFromXml() {
|
||||
|
||||
@@ -54,11 +53,11 @@ public class WxXmlMessageTest {
|
||||
+ " <Poiname><![CDATA[wo de poi]]></Poiname>\n"
|
||||
+ "</SendLocationInfo>"
|
||||
+ "</xml>";
|
||||
WxXmlMessage wxMessage = WxXmlMessage.fromXml(xml);
|
||||
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
|
||||
Assert.assertEquals(wxMessage.getToUserName(), "toUser");
|
||||
Assert.assertEquals(wxMessage.getFromUserName(), "fromUser");
|
||||
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
|
||||
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
|
||||
Assert.assertEquals(wxMessage.getMsgType(), WxMpConsts.XML_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");
|
||||
@@ -1,15 +1,13 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutImageMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class WxXmlOutImageMessageTest {
|
||||
public class WxMpXmlOutImageMessageTest {
|
||||
|
||||
public void test() {
|
||||
WxXmlOutImageMessage m = new WxXmlOutImageMessage();
|
||||
WxMpMpXmlOutImageMessage m = new WxMpMpXmlOutImageMessage();
|
||||
m.setMediaId("ddfefesfsdfef");
|
||||
m.setCreateTime(1122l);
|
||||
m.setFromUserName("from");
|
||||
@@ -28,7 +26,7 @@ public class WxXmlOutImageMessageTest {
|
||||
}
|
||||
|
||||
public void testBuild() {
|
||||
WxXmlOutImageMessage m = WxXmlOutMessage.IMAGE().mediaId("ddfefesfsdfef").fromUser("from").toUser("to").build();
|
||||
WxMpMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId("ddfefesfsdfef").fromUser("from").toUser("to").build();
|
||||
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
|
||||
+ "<xml>"
|
||||
+ "<ToUserName><![CDATA[to]]></ToUserName>"
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMusicMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -9,7 +7,7 @@ import org.testng.annotations.Test;
|
||||
public class WxXmlOutMusicMessageTest {
|
||||
|
||||
public void test() {
|
||||
WxXmlOutMusicMessage m = new WxXmlOutMusicMessage();
|
||||
WxMpXmlOutMusicMessage m = new WxMpXmlOutMusicMessage();
|
||||
m.setTitle("title");
|
||||
m.setDescription("ddfff");
|
||||
m.setHqMusicUrl("hQMusicUrl");
|
||||
@@ -38,7 +36,7 @@ public class WxXmlOutMusicMessageTest {
|
||||
}
|
||||
|
||||
public void testBuild() {
|
||||
WxXmlOutMusicMessage m = WxXmlOutMessage.MUSIC()
|
||||
WxMpXmlOutMusicMessage m = WxMpXmlOutMessage.MUSIC()
|
||||
.fromUser("fromUser")
|
||||
.toUser("toUser")
|
||||
.title("title")
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMewsMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -9,12 +7,12 @@ import org.testng.annotations.Test;
|
||||
public class WxXmlOutNewsMessageTest {
|
||||
|
||||
public void test() {
|
||||
WxXmlOutMewsMessage m = new WxXmlOutMewsMessage();
|
||||
WxMpXmlOutMewsMessage m = new WxMpXmlOutMewsMessage();
|
||||
m.setCreateTime(1122l);
|
||||
m.setFromUserName("fromUser");
|
||||
m.setToUserName("toUser");
|
||||
|
||||
WxXmlOutMewsMessage.Item item = new WxXmlOutMewsMessage.Item();
|
||||
WxMpXmlOutMewsMessage.Item item = new WxMpXmlOutMewsMessage.Item();
|
||||
item.setDescription("description");
|
||||
item.setPicUrl("picUrl");
|
||||
item.setTitle("title");
|
||||
@@ -48,13 +46,13 @@ public class WxXmlOutNewsMessageTest {
|
||||
}
|
||||
|
||||
public void testBuild() {
|
||||
WxXmlOutMewsMessage.Item item = new WxXmlOutMewsMessage.Item();
|
||||
WxMpXmlOutMewsMessage.Item item = new WxMpXmlOutMewsMessage.Item();
|
||||
item.setDescription("description");
|
||||
item.setPicUrl("picUrl");
|
||||
item.setTitle("title");
|
||||
item.setUrl("url");
|
||||
|
||||
WxXmlOutMewsMessage m = WxXmlOutMessage.NEWS()
|
||||
WxMpXmlOutMewsMessage m = WxMpXmlOutMessage.NEWS()
|
||||
.fromUser("fromUser")
|
||||
.toUser("toUser")
|
||||
.addArticle(item)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutTextMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -9,7 +7,7 @@ import org.testng.annotations.Test;
|
||||
public class WxXmlOutTextMessageTest {
|
||||
|
||||
public void test() {
|
||||
WxXmlOutTextMessage m = new WxXmlOutTextMessage();
|
||||
WxMpXmlOutTextMessage m = new WxMpXmlOutTextMessage();
|
||||
m.setContent("content");
|
||||
m.setCreateTime(1122l);
|
||||
m.setFromUserName("from");
|
||||
@@ -28,7 +26,7 @@ public class WxXmlOutTextMessageTest {
|
||||
}
|
||||
|
||||
public void testBuild() {
|
||||
WxXmlOutTextMessage m = WxXmlOutMessage.TEXT().content("content").fromUser("from").toUser("to").build();
|
||||
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content("content").fromUser("from").toUser("to").build();
|
||||
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
|
||||
+ "<xml>"
|
||||
+ "<ToUserName><![CDATA[to]]></ToUserName>"
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutVideoMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -9,7 +7,7 @@ import org.testng.annotations.Test;
|
||||
public class WxXmlOutVideoMessageTest {
|
||||
|
||||
public void test() {
|
||||
WxXmlOutVideoMessage m = new WxXmlOutVideoMessage();
|
||||
WxMpXmlOutVideoMessage m = new WxMpXmlOutVideoMessage();
|
||||
m.setMediaId("media_id");
|
||||
m.setTitle("title");
|
||||
m.setDescription("ddfff");
|
||||
@@ -34,7 +32,7 @@ public class WxXmlOutVideoMessageTest {
|
||||
}
|
||||
|
||||
public void testBuild() {
|
||||
WxXmlOutVideoMessage m = WxXmlOutMessage.VIDEO()
|
||||
WxMpXmlOutVideoMessage m = WxMpXmlOutMessage.VIDEO()
|
||||
.mediaId("media_id")
|
||||
.fromUser("fromUser")
|
||||
.toUser("toUser")
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package me.chanjar.weixin.mp.bean;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutVoiceMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@@ -9,7 +7,7 @@ import org.testng.annotations.Test;
|
||||
public class WxXmlOutVoiceMessageTest {
|
||||
|
||||
public void test() {
|
||||
WxXmlOutVoiceMessage m = new WxXmlOutVoiceMessage();
|
||||
WxMpXmlOutVoiceMessage m = new WxMpXmlOutVoiceMessage();
|
||||
m.setMediaId("ddfefesfsdfef");
|
||||
m.setCreateTime(1122l);
|
||||
m.setFromUserName("from");
|
||||
@@ -28,7 +26,7 @@ public class WxXmlOutVoiceMessageTest {
|
||||
}
|
||||
|
||||
public void testBuild() {
|
||||
WxXmlOutVoiceMessage m = WxXmlOutMessage.VOICE().mediaId("ddfefesfsdfef").fromUser("from").toUser("to").build();
|
||||
WxMpXmlOutVoiceMessage m = WxMpXmlOutMessage.VOICE().mediaId("ddfefesfsdfef").fromUser("from").toUser("to").build();
|
||||
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
|
||||
+ "<xml>"
|
||||
+ "<ToUserName><![CDATA[to]]></ToUserName>"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package me.chanjar.weixin.mp.demo;
|
||||
|
||||
import me.chanjar.weixin.mp.api.WxInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
@@ -16,7 +16,7 @@ import java.io.InputStream;
|
||||
*/
|
||||
@XmlRootElement(name = "xml")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
class WxTestConfigStorage extends WxInMemoryConfigStorage {
|
||||
class WxMpDemoInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -25,11 +25,11 @@ class WxTestConfigStorage extends WxInMemoryConfigStorage {
|
||||
}
|
||||
|
||||
|
||||
public static WxTestConfigStorage fromXml(InputStream is) throws JAXBException {
|
||||
Unmarshaller um = JAXBContext.newInstance(WxTestConfigStorage.class).createUnmarshaller();
|
||||
public static WxMpDemoInMemoryConfigStorage fromXml(InputStream is) throws JAXBException {
|
||||
Unmarshaller um = JAXBContext.newInstance(WxMpDemoInMemoryConfigStorage.class).createUnmarshaller();
|
||||
InputSource inputSource = new InputSource(is);
|
||||
inputSource.setEncoding("utf-8");
|
||||
return (WxTestConfigStorage) um.unmarshal(inputSource);
|
||||
return (WxMpDemoInMemoryConfigStorage) um.unmarshal(inputSource);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,14 +6,14 @@ import org.eclipse.jetty.servlet.ServletHandler;
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxTestServer {
|
||||
public class WxMpDemoServer {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Server server = new Server(8080);
|
||||
|
||||
ServletHandler handler = new ServletHandler();
|
||||
server.setHandler(handler);
|
||||
|
||||
handler.addServletWithMapping(WxTestServlet.class, "/*");
|
||||
handler.addServletWithMapping(WxMpDemoServlet.class, "/*");
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package me.chanjar.weixin.mp.demo;
|
||||
|
||||
import me.chanjar.weixin.mp.api.*;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxXmlOutTextMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
@@ -18,34 +18,34 @@ import java.util.Map;
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
public class WxTestServlet extends HttpServlet {
|
||||
public class WxMpDemoServlet extends HttpServlet {
|
||||
|
||||
protected WxService wxService;
|
||||
protected WxConfigStorage wxConfigStorage;
|
||||
protected WxMessageRouter wxMessageRouter;
|
||||
protected WxMpService wxMpService;
|
||||
protected WxMpConfigStorage wxMpConfigStorage;
|
||||
protected WxMpMessageRouter wxMpMessageRouter;
|
||||
|
||||
@Override public void init() throws ServletException {
|
||||
//
|
||||
super.init();
|
||||
try {
|
||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
|
||||
WxTestConfigStorage config = WxTestConfigStorage.fromXml(is1);
|
||||
WxMpDemoInMemoryConfigStorage config = WxMpDemoInMemoryConfigStorage.fromXml(is1);
|
||||
|
||||
wxConfigStorage = config;
|
||||
wxService = new WxServiceImpl();
|
||||
wxService.setWxConfigStorage(config);
|
||||
wxMpConfigStorage = config;
|
||||
wxMpService = new WxMpServiceImpl();
|
||||
wxMpService.setWxMpConfigStorage(config);
|
||||
|
||||
WxMessageHandler handler = new WxMessageHandler() {
|
||||
@Override public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context) {
|
||||
WxXmlOutTextMessage m
|
||||
= WxXmlOutMessage.TEXT().content("测试加密消息").fromUser(wxMessage.getToUserName())
|
||||
WxMpMessageHandler handler = new WxMpMessageHandler() {
|
||||
@Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context) {
|
||||
WxMpXmlOutTextMessage m
|
||||
= WxMpXmlOutMessage.TEXT().content("测试加密消息").fromUser(wxMessage.getToUserName())
|
||||
.toUser(wxMessage.getFromUserName()).build();
|
||||
return m;
|
||||
}
|
||||
};
|
||||
|
||||
wxMessageRouter = new WxMessageRouter();
|
||||
wxMessageRouter
|
||||
wxMpMessageRouter = new WxMpMessageRouter();
|
||||
wxMpMessageRouter
|
||||
.rule()
|
||||
.async(false)
|
||||
.content("哈哈") // 拦截内容为“哈哈”的消息
|
||||
@@ -67,7 +67,7 @@ public class WxTestServlet extends HttpServlet {
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
|
||||
if (!wxService.checkSignature(timestamp, nonce, signature)) {
|
||||
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
|
||||
// 消息签名不正确,说明不是公众平台发过来的消息
|
||||
response.getWriter().println("非法请求");
|
||||
return;
|
||||
@@ -84,30 +84,27 @@ public class WxTestServlet extends HttpServlet {
|
||||
"raw" :
|
||||
request.getParameter("encrypt_type");
|
||||
|
||||
WxXmlMessage inMessage = null;
|
||||
WxMpXmlMessage inMessage = null;
|
||||
|
||||
if ("raw".equals(encryptType)) {
|
||||
// 明文传输的消息
|
||||
inMessage = WxXmlMessage.fromXml(request.getInputStream());
|
||||
inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
|
||||
} else if ("aes".equals(encryptType)) {
|
||||
// 是aes加密的消息
|
||||
String msgSignature = request.getParameter("msg_signature");
|
||||
inMessage = WxXmlMessage.fromEncryptedXml(
|
||||
request.getInputStream(),
|
||||
wxConfigStorage,
|
||||
timestamp, nonce, msgSignature);
|
||||
inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), wxMpConfigStorage, timestamp, nonce, msgSignature);
|
||||
} else {
|
||||
response.getWriter().println("不可识别的加密类型");
|
||||
return;
|
||||
}
|
||||
|
||||
WxXmlOutMessage outMessage = wxMessageRouter.route(inMessage);
|
||||
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
|
||||
|
||||
if (outMessage != null) {
|
||||
if ("raw".equals(encryptType)) {
|
||||
response.getWriter().write(outMessage.toXml());
|
||||
} else if ("aes".equals(encryptType)) {
|
||||
response.getWriter().write(outMessage.toEncryptedXml(wxConfigStorage));
|
||||
response.getWriter().write(outMessage.toEncryptedXml(wxMpConfigStorage));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package me.chanjar.weixin.mp.util.crypto;
|
||||
|
||||
import me.chanjar.weixin.mp.util.crypto.WxCryptUtil;
|
||||
import org.testng.annotations.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
@Test
|
||||
public class WxCryptUtilTest {
|
||||
String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
|
||||
String token = "pamtest";
|
||||
String timestamp = "1409304348";
|
||||
String nonce = "xxxxxx";
|
||||
String appId = "wxb11529c136998cb6";
|
||||
String randomStr = "aaaabbbbccccdddd";
|
||||
|
||||
String xmlFormat = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
|
||||
String replyMsg = "我是中文abcd123";
|
||||
|
||||
String afterAesEncrypt = "jn1L23DB+6ELqJ+6bruv21Y6MD7KeIfP82D6gU39rmkgczbWwt5+3bnyg5K55bgVtVzd832WzZGMhkP72vVOfg==";
|
||||
|
||||
String replyMsg2 = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
|
||||
String afterAesEncrypt2 = "jn1L23DB+6ELqJ+6bruv23M2GmYfkv0xBh2h+XTBOKVKcgDFHle6gqcZ1cZrk3e1qjPQ1F4RsLWzQRG9udbKWesxlkupqcEcW7ZQweImX9+wLMa0GaUzpkycA8+IamDBxn5loLgZpnS7fVAbExOkK5DYHBmv5tptA9tklE/fTIILHR8HLXa5nQvFb3tYPKAlHF3rtTeayNf0QuM+UW/wM9enGIDIJHF7CLHiDNAYxr+r+OrJCmPQyTy8cVWlu9iSvOHPT/77bZqJucQHQ04sq7KZI27OcqpQNSto2OdHCoTccjggX5Z9Mma0nMJBU+jLKJ38YB1fBIz+vBzsYjrTmFQ44YfeEuZ+xRTQwr92vhA9OxchWVINGC50qE/6lmkwWTwGX9wtQpsJKhP+oS7rvTY8+VdzETdfakjkwQ5/Xka042OlUb1/slTwo4RscuQ+RdxSGvDahxAJ6+EAjLt9d8igHngxIbf6YyqqROxuxqIeIch3CssH/LqRs+iAcILvApYZckqmA7FNERspKA5f8GoJ9sv8xmGvZ9Yrf57cExWtnX8aCMMaBropU/1k+hKP5LVdzbWCG0hGwx/dQudYR/eXp3P0XxjlFiy+9DMlaFExWUZQDajPkdPrEeOwofJb";
|
||||
|
||||
public void testNormal() throws ParserConfigurationException, SAXException, IOException {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
String encryptedXml = pc.encrypt(replyMsg);
|
||||
|
||||
System.out.println(encryptedXml);
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml)));
|
||||
|
||||
Element root = document.getDocumentElement();
|
||||
String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent();
|
||||
String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent();
|
||||
String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent();
|
||||
String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent();
|
||||
|
||||
String messageText = String.format(xmlFormat, cipherText);
|
||||
|
||||
// 第三方收到公众号平台发送的消息
|
||||
String plainMessage = pc.decrypt(cipherText);
|
||||
|
||||
System.out.println(plainMessage);
|
||||
assertEquals(plainMessage, replyMsg);
|
||||
}
|
||||
|
||||
public void testAesEncrypt() {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
assertEquals(pc.encrypt(randomStr, replyMsg), afterAesEncrypt);
|
||||
}
|
||||
|
||||
public void testAesEncrypt2() {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
assertEquals(pc.encrypt(randomStr, replyMsg2), afterAesEncrypt2);
|
||||
}
|
||||
|
||||
public void testValidateSignatureError() throws ParserConfigurationException, SAXException,
|
||||
IOException {
|
||||
try {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
String afterEncrpt = pc.encrypt(replyMsg);
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
StringReader sr = new StringReader(afterEncrpt);
|
||||
InputSource is = new InputSource(sr);
|
||||
Document document = db.parse(is);
|
||||
|
||||
Element root = document.getDocumentElement();
|
||||
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
|
||||
|
||||
String encrypt = nodelist1.item(0).getTextContent();
|
||||
String fromXML = String.format(xmlFormat, encrypt);
|
||||
pc.decrypt("12345", timestamp, nonce, fromXML); // 这里签名错误
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals(e.getMessage(), "加密消息签名校验失败");
|
||||
return;
|
||||
}
|
||||
fail("错误流程不抛出异常???");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user