添加客服消息接口
This commit is contained in:
38
src/test/java/chanjarster/weixin/in/WxErrorTest.java
Normal file
38
src/test/java/chanjarster/weixin/in/WxErrorTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package chanjarster.weixin.in;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.in.WxError;
|
||||
|
||||
@Test
|
||||
public class WxErrorTest {
|
||||
|
||||
public void testFromJson() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrcode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrmsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson1() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\", \"media_id\": \"12323423dsfafsf232f\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrcode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrmsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson2() {
|
||||
|
||||
String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrcode() == 0);
|
||||
Assert.assertEquals(wxError.getErrmsg(), null);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package chanjarster.weixin.out;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.out.WxCustomMessage;
|
||||
import chanjarster.weixin.out.WxCustomMessage.WxArticle;
|
||||
import chanjarster.weixin.service.WxMsgType;
|
||||
@Test
|
||||
public class WxCustomMessageTest {
|
||||
|
||||
public void testTextReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxMsgType.TEXT);
|
||||
reply.setContent("sfsfdsdf");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
|
||||
}
|
||||
|
||||
public void testImageReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxMsgType.IMAGE);
|
||||
reply.setMedia_id("MEDIA_ID");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVoiceReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxMsgType.VOICE);
|
||||
reply.setMedia_id("MEDIA_ID");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
|
||||
}
|
||||
|
||||
public void testVideoReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxMsgType.VIDEO);
|
||||
reply.setMedia_id("MEDIA_ID");
|
||||
reply.setThumb_media_id("MEDIA_ID");
|
||||
reply.setTitle("TITLE");
|
||||
reply.setDescription("DESCRIPTION");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
|
||||
}
|
||||
|
||||
public void testMusicReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxMsgType.MUSIC);
|
||||
reply.setThumb_media_id("MEDIA_ID");
|
||||
reply.setDescription("DESCRIPTION");
|
||||
reply.setTitle("TITLE");
|
||||
reply.setMusicurl("MUSIC_URL");
|
||||
reply.setHqmusicurl("HQ_MUSIC_URL");
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
|
||||
}
|
||||
|
||||
public void testNewsReply() {
|
||||
WxCustomMessage reply = new WxCustomMessage();
|
||||
reply.setTouser("OPENID");
|
||||
reply.setMsgtype(WxMsgType.NEWS);
|
||||
|
||||
WxArticle article1 = new WxArticle();
|
||||
article1.setUrl("URL");
|
||||
article1.setPicurl("PIC_URL");
|
||||
article1.setDescription("Is Really A Happy Day");
|
||||
article1.setTitle("Happy Day");
|
||||
reply.getArticles().add(article1);
|
||||
|
||||
WxArticle article2 = new WxArticle();
|
||||
article2.setUrl("URL");
|
||||
article2.setPicurl("PIC_URL");
|
||||
article2.setDescription("Is Really A Happy Day");
|
||||
article2.setTitle("Happy Day");
|
||||
reply.getArticles().add(article2);
|
||||
|
||||
|
||||
System.out.println(reply.toJson());
|
||||
Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}");
|
||||
}
|
||||
|
||||
}
|
||||
119
src/test/java/chanjarster/weixin/out/WxUserMessageTest.java
Normal file
119
src/test/java/chanjarster/weixin/out/WxUserMessageTest.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package chanjarster.weixin.out;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.out.WxUserMessage;
|
||||
import chanjarster.weixin.service.WxMsgType;
|
||||
|
||||
@Test
|
||||
public class WxUserMessageTest {
|
||||
|
||||
public void testFromXml() {
|
||||
|
||||
String xml = "<xml>"
|
||||
+ "<ToUserName><![CDATA[toUser]]></ToUserName>"
|
||||
+ "<FromUserName><![CDATA[fromUser]]></FromUserName> "
|
||||
+ "<CreateTime>1348831860</CreateTime>"
|
||||
+ "<MsgType><![CDATA[text]]></MsgType>"
|
||||
+ "<Content><![CDATA[this is a test]]></Content>"
|
||||
+ "<MsgId>1234567890123456</MsgId>"
|
||||
+ "<PicUrl><![CDATA[this is a url]]></PicUrl>"
|
||||
+ "<MediaId><![CDATA[media_id]]></MediaId>"
|
||||
+ "<Format><![CDATA[Format]]></Format>"
|
||||
+ "<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>"
|
||||
+ "<Location_X>23.134521</Location_X>"
|
||||
+ "<Location_Y>113.358803</Location_Y>"
|
||||
+ "<Scale>20</Scale>"
|
||||
+ "<Label><![CDATA[位置信息]]></Label>"
|
||||
+ "<Description><![CDATA[公众平台官网链接]]></Description>"
|
||||
+ "<Url><![CDATA[url]]></Url>"
|
||||
+ "<Title><![CDATA[公众平台官网链接]]></Title>"
|
||||
+ "<Event><![CDATA[subscribe]]></Event>"
|
||||
+ "<EventKey><![CDATA[qrscene_123123]]></EventKey>"
|
||||
+ "<Ticket><![CDATA[TICKET]]></Ticket>"
|
||||
+ "<Latitude>23.137466</Latitude>"
|
||||
+ "<Longitude>113.352425</Longitude>"
|
||||
+ "<Precision>119.385040</Precision>"
|
||||
+ "</xml>";
|
||||
WxUserMessage wxMessage = WxUserMessage.fromXml(xml);
|
||||
Assert.assertEquals(wxMessage.getToUserName(), "toUser");
|
||||
Assert.assertEquals(wxMessage.getFromUserName(), "fromUser");
|
||||
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
|
||||
Assert.assertEquals(wxMessage.getMsgType(), WxMsgType.TEXT);
|
||||
Assert.assertEquals(wxMessage.getContent(), "this is a test");
|
||||
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
|
||||
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");
|
||||
Assert.assertEquals(wxMessage.getMediaId(), "media_id");
|
||||
Assert.assertEquals(wxMessage.getFormat(), "Format");
|
||||
Assert.assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
|
||||
Assert.assertEquals(wxMessage.getLocation_X(), new Double(23.134521d));
|
||||
Assert.assertEquals(wxMessage.getLocation_Y(), new Double(113.358803d));
|
||||
Assert.assertEquals(wxMessage.getScale(), new Double(20));
|
||||
Assert.assertEquals(wxMessage.getLabel(), "位置信息");
|
||||
Assert.assertEquals(wxMessage.getDescription(), "公众平台官网链接");
|
||||
Assert.assertEquals(wxMessage.getUrl(), "url");
|
||||
Assert.assertEquals(wxMessage.getTitle(), "公众平台官网链接");
|
||||
Assert.assertEquals(wxMessage.getEvent(), "subscribe");
|
||||
Assert.assertEquals(wxMessage.getEventKey(), "qrscene_123123");
|
||||
Assert.assertEquals(wxMessage.getTicket(), "TICKET");
|
||||
Assert.assertEquals(wxMessage.getLatitude(), new Double(23.137466));
|
||||
Assert.assertEquals(wxMessage.getLongitude(), new Double(113.352425));
|
||||
Assert.assertEquals(wxMessage.getPrecision(), new Double(119.385040));
|
||||
}
|
||||
|
||||
public void testToXml() {
|
||||
WxUserMessage wxMessage = new WxUserMessage();
|
||||
wxMessage.setToUserName("toUser");
|
||||
wxMessage.setFromUserName("fromUser");
|
||||
wxMessage.setCreateTime(new Long(1348831860l));
|
||||
wxMessage.setMsgType(WxMsgType.TEXT);
|
||||
wxMessage.setContent("this is a test");
|
||||
wxMessage.setMsgId(new Long(1234567890123456l));
|
||||
wxMessage.setPicUrl("this is a url");
|
||||
wxMessage.setMediaId("media_id");
|
||||
wxMessage.setFormat("Format");
|
||||
wxMessage.setThumbMediaId("thumb_media_id");
|
||||
wxMessage.setLocation_X(new Double(23.134521d));
|
||||
wxMessage.setLocation_Y(new Double(113.358803d));
|
||||
wxMessage.setScale(new Double(20));
|
||||
wxMessage.setLabel("位置信息");
|
||||
wxMessage.setDescription("公众平台官网链接");
|
||||
wxMessage.setUrl("url");
|
||||
wxMessage.setTitle("公众平台官网链接");
|
||||
wxMessage.setEvent("subscribe");
|
||||
wxMessage.setEventKey("qrscene_123123");
|
||||
wxMessage.setTicket("TICKET");
|
||||
wxMessage.setLatitude(new Double(23.137466));
|
||||
wxMessage.setLongitude(new Double(113.352425));
|
||||
wxMessage.setPrecision(new Double(119.385040));
|
||||
|
||||
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
|
||||
+ "<xml>\n"
|
||||
+ " <ToUserName><![CDATA[toUser]]></ToUserName>\n"
|
||||
+ " <FromUserName><![CDATA[fromUser]]></FromUserName>\n"
|
||||
+ " <CreateTime>1348831860</CreateTime>\n"
|
||||
+ " <MsgType><![CDATA[text]]></MsgType>\n"
|
||||
+ " <Content><![CDATA[this is a test]]></Content>\n"
|
||||
+ " <MsgId>1234567890123456</MsgId>\n"
|
||||
+ " <PicUrl><![CDATA[this is a url]]></PicUrl>\n"
|
||||
+ " <MediaId><![CDATA[media_id]]></MediaId>\n"
|
||||
+ " <Format><![CDATA[Format]]></Format>\n"
|
||||
+ " <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>\n"
|
||||
+ " <Location_X>23.134521</Location_X>\n"
|
||||
+ " <Location_Y>113.358803</Location_Y>\n"
|
||||
+ " <Scale>20.0</Scale>\n"
|
||||
+ " <Label><![CDATA[位置信息]]></Label>\n"
|
||||
+ " <Title><![CDATA[公众平台官网链接]]></Title>\n"
|
||||
+ " <Description><![CDATA[公众平台官网链接]]></Description>\n"
|
||||
+ " <Url><![CDATA[url]]></Url>\n"
|
||||
+ " <Event><![CDATA[subscribe]]></Event>\n"
|
||||
+ " <EventKey><![CDATA[qrscene_123123]]></EventKey>\n"
|
||||
+ " <Ticket><![CDATA[TICKET]]></Ticket>\n"
|
||||
+ " <Latitude>23.137466</Latitude>\n"
|
||||
+ " <Longitude>113.352425</Longitude>\n"
|
||||
+ " <Precision>119.38504</Precision>\n"
|
||||
+ "</xml>\n";
|
||||
Assert.assertEquals(wxMessage.toXml(), xml);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package chanjarster.weixin.service;
|
||||
|
||||
// TODO
|
||||
public class WxMessageRouterTest {
|
||||
|
||||
}
|
||||
125
src/test/java/chanjarster/weixin/service/WxServiceTest.java
Normal file
125
src/test/java/chanjarster/weixin/service/WxServiceTest.java
Normal file
@@ -0,0 +1,125 @@
|
||||
package chanjarster.weixin.service;
|
||||
|
||||
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 org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import chanjarster.weixin.exception.WxErrorException;
|
||||
import chanjarster.weixin.out.WxCustomMessage;
|
||||
import chanjarster.weixin.util.XmlTransformer;
|
||||
|
||||
public class WxServiceTest {
|
||||
|
||||
@Test(dataProvider = "configs")
|
||||
public void testRefreshAccessToken(WxConfigProvider config) throws WxErrorException {
|
||||
String before = config.getAccessToken();
|
||||
|
||||
WxService wxService = new WxServiceImpl();
|
||||
wxService.setWxConfigProvider(config);
|
||||
wxService.refreshAccessToken();
|
||||
|
||||
String after = config.getAccessToken();
|
||||
|
||||
Assert.assertNotEquals(before, after);
|
||||
Assert.assertTrue(StringUtils.isNotBlank(after));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "configs")
|
||||
public void sendCustomMessage(SimpleWxConfigProvider config) throws WxErrorException {
|
||||
WxService wxService = new WxServiceImpl();
|
||||
wxService.setWxConfigProvider(config);
|
||||
|
||||
WxCustomMessage message = new WxCustomMessage();
|
||||
message.setMsgtype(WxMsgType.TEXT);
|
||||
message.setTouser(config.getOpenId());
|
||||
message.setContent("欢迎使用教务系统微信公众号\n下面\n<a href=\"http://192.168.1.249:9180/eams-rc/login.action\">Hello World</a>");
|
||||
|
||||
wxService.sendCustomMessage(message);
|
||||
}
|
||||
/**
|
||||
* 返回新的access_token
|
||||
* @return
|
||||
* @throws JAXBException
|
||||
*/
|
||||
@DataProvider(name = "configs")
|
||||
public Object[][] getConfig() throws JAXBException {
|
||||
/**
|
||||
* 将 src/test/resources/test-config.sample.xml 改成 test-config.xml 并设置appId, secret, 一个过期的accessToken
|
||||
*/
|
||||
// 没有access_token
|
||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
|
||||
SimpleWxConfigProvider config1 = XmlTransformer.fromXml(SimpleWxConfigProvider.class, is1);
|
||||
return new Object[][] {
|
||||
new Object[] {
|
||||
config1
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@XmlRootElement(name = "xml")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public static class SimpleWxConfigProvider implements WxConfigProvider {
|
||||
private String appId;
|
||||
private String secret;
|
||||
private String accessToken = "";
|
||||
private Integer expiresIn;
|
||||
private String token;
|
||||
private String openId;
|
||||
public void updateAccessToken(String accessToken, Integer expiresIn) {
|
||||
this.accessToken = accessToken;
|
||||
this.expiresIn = expiresIn;
|
||||
}
|
||||
public String getAccessToken() {
|
||||
return accessToken;
|
||||
}
|
||||
public String getAppId() {
|
||||
return appId;
|
||||
}
|
||||
public String getSecret() {
|
||||
return secret;
|
||||
}
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
public void setAppId(String appId) {
|
||||
this.appId = appId;
|
||||
}
|
||||
public void setSecret(String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
public Integer getExpiresIn() {
|
||||
return expiresIn;
|
||||
}
|
||||
public void setExpiresIn(Integer expiresIn) {
|
||||
this.expiresIn = expiresIn;
|
||||
}
|
||||
public void setAccessToken(String accessToken) {
|
||||
this.accessToken = accessToken;
|
||||
}
|
||||
public 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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user