mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
整理及重构
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
import com.google.inject.Module;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class ApiTestModule implements Module {
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder) {
|
||||
try {
|
||||
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
|
||||
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, is1);
|
||||
WxCpServiceImpl wxService = new WxCpServiceImpl();
|
||||
wxService.setWxCpConfigStorage(config);
|
||||
|
||||
binder.bind(WxCpServiceImpl.class).toInstance(wxService);
|
||||
binder.bind(WxCpConfigStorage.class).toInstance(config);
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
|
||||
Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
|
||||
InputSource inputSource = new InputSource(is);
|
||||
inputSource.setEncoding("utf-8");
|
||||
T object = (T) um.unmarshal(inputSource);
|
||||
return object;
|
||||
}
|
||||
|
||||
@XmlRootElement(name = "xml")
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public static class WxXmlCpInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
|
||||
|
||||
protected String userId;
|
||||
|
||||
protected String departmentId;
|
||||
|
||||
protected String tagId;
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getDepartmentId() {
|
||||
return departmentId;
|
||||
}
|
||||
|
||||
public void setDepartmentId(String departmentId) {
|
||||
this.departmentId = departmentId;
|
||||
}
|
||||
|
||||
public String getTagId() {
|
||||
return tagId;
|
||||
}
|
||||
|
||||
public void setTagId(String tagId) {
|
||||
this.tagId = tagId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + " > WxXmlCpConfigStorage{" +
|
||||
"userId='" + userId + '\'' +
|
||||
", departmentId='" + departmentId + '\'' +
|
||||
", tagId='" + tagId + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 基础API测试
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
@Test(groups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpBaseAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
public void testRefreshAccessToken() throws WxErrorException {
|
||||
WxCpConfigStorage configStorage = wxService.wxCpConfigStorage;
|
||||
String before = configStorage.getAccessToken();
|
||||
wxService.accessTokenRefresh();
|
||||
|
||||
String after = configStorage.getAccessToken();
|
||||
|
||||
Assert.assertNotEquals(before, after);
|
||||
Assert.assertTrue(StringUtils.isNotBlank(after));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试部门接口
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Test(groups = "departAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpDepartAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
protected WxCpDepart depart;
|
||||
|
||||
public void testDepartCreate() throws WxErrorException {
|
||||
WxCpDepart depart = new WxCpDepart();
|
||||
depart.setName("子部门" + System.currentTimeMillis());
|
||||
depart.setParentId(1);
|
||||
depart.setOrder(1);
|
||||
Integer departId = wxService.departCreate(depart);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testDepartCreate")
|
||||
public void testDepartGet() throws WxErrorException {
|
||||
System.out.println("=================获取部门");
|
||||
List<WxCpDepart> departList = wxService.departGet();
|
||||
Assert.assertNotNull(departList);
|
||||
Assert.assertTrue(departList.size() > 0);
|
||||
for (WxCpDepart g : departList) {
|
||||
depart = g;
|
||||
System.out.println(depart.getId() + ":" + depart.getName());
|
||||
Assert.assertNotNull(g.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testDepartGet", "testDepartCreate" })
|
||||
public void testDepartUpdate() throws WxErrorException {
|
||||
System.out.println("=================更新部门");
|
||||
depart.setName("子部门改名" + System.currentTimeMillis());
|
||||
wxService.departUpdate(depart);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testDepartUpdate")
|
||||
public void testDepartDelete() throws WxErrorException {
|
||||
System.out.println("=================删除部门");
|
||||
System.out.println(depart.getId() + ":" + depart.getName());
|
||||
wxService.departDelete(depart.getId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import java.io.IOException;
|
||||
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.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* 测试多媒体文件上传下载
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
//@Test(groups="mediaAPI", dependsOnGroups="baseAPI")
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpMediaAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
private List<String> media_ids = new ArrayList<String>();
|
||||
|
||||
@Test(dataProvider="uploadMedia")
|
||||
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
|
||||
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
|
||||
WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream);
|
||||
Assert.assertNotNull(res.getType());
|
||||
Assert.assertNotNull(res.getCreatedAt());
|
||||
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
|
||||
|
||||
if (res.getMediaId() != null) {
|
||||
media_ids.add(res.getMediaId());
|
||||
}
|
||||
if (res.getThumbMediaId() != null) {
|
||||
media_ids.add(res.getThumbMediaId());
|
||||
}
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] uploadMedia() {
|
||||
return new Object[][] {
|
||||
new Object[] { WxCpConsts.MEDIA_IMAGE, WxCpConsts.FILE_JPG, "mm.jpeg" },
|
||||
new Object[] { WxCpConsts.MEDIA_VOICE, WxCpConsts.FILE_MP3, "mm.mp3" },
|
||||
new Object[] { WxCpConsts.MEDIA_VIDEO, WxCpConsts.FILE_MP4, "mm.mp4" },
|
||||
new Object[] { WxCpConsts.MEDIA_FILE, WxCpConsts.FILE_JPG, "mm.jpeg" }
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia")
|
||||
public void testDownloadMedia(String media_id) throws WxErrorException {
|
||||
wxService.mediaDownload(media_id);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] downloadMedia() {
|
||||
Object[][] params = new Object[this.media_ids.size()][];
|
||||
for (int i = 0; i < this.media_ids.size(); i++) {
|
||||
params[i] = new Object[] { this.media_ids.get(i) };
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/***
|
||||
* 测试发送消息
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
@Test(groups="customMessageAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpMessageAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
public void testSendCustomMessage() throws WxErrorException {
|
||||
ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) wxService.wxCpConfigStorage;
|
||||
WxCpMessage message1 = new WxCpMessage();
|
||||
message1.setAgentId(configStorage.getAgentId());
|
||||
message1.setMsgType(WxCpConsts.CUSTOM_MSG_TEXT);
|
||||
message1.setToUser(configStorage.getUserId());
|
||||
message1.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||
wxService.messageSend(message1);
|
||||
|
||||
WxCpMessage message2 = WxCpMessage
|
||||
.TEXT()
|
||||
.agentId(configStorage.getAgentId())
|
||||
.toUser(configStorage.getUserId())
|
||||
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
|
||||
.build();
|
||||
wxService.messageSend(message2);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
|
||||
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* 测试消息路由器
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public class WxCpMessageRouterTest {
|
||||
|
||||
@Test(enabled = false)
|
||||
public void prepare(boolean async, StringBuffer sb, WxCpMessageRouter router) {
|
||||
router
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxCpConsts.XML_MSG_TEXT).event(WxCpConsts.EVT_CLICK).eventKey("KEY_1").content("CONTENT_1")
|
||||
.handler(new WxEchoCpMessageHandler(sb, "COMBINE_4"))
|
||||
.end()
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxCpConsts.XML_MSG_TEXT).event(WxCpConsts.EVT_CLICK).eventKey("KEY_1")
|
||||
.handler(new WxEchoCpMessageHandler(sb, "COMBINE_3"))
|
||||
.end()
|
||||
.rule()
|
||||
.async(async)
|
||||
.msgType(WxCpConsts.XML_MSG_TEXT).event(WxCpConsts.EVT_CLICK)
|
||||
.handler(new WxEchoCpMessageHandler(sb, "COMBINE_2"))
|
||||
.end()
|
||||
.rule().async(async).msgType(WxCpConsts.XML_MSG_TEXT).handler(new WxEchoCpMessageHandler(sb, WxCpConsts.XML_MSG_TEXT)).end()
|
||||
.rule().async(async).event(WxCpConsts.EVT_CLICK).handler(new WxEchoCpMessageHandler(sb, WxCpConsts.EVT_CLICK)).end()
|
||||
.rule().async(async).eventKey("KEY_1").handler(new WxEchoCpMessageHandler(sb, "KEY_1")).end()
|
||||
.rule().async(async).content("CONTENT_1").handler(new WxEchoCpMessageHandler(sb, "CONTENT_1")).end()
|
||||
.rule().async(async).rContent(".*bc.*").handler(new WxEchoCpMessageHandler(sb, "abcd")).end()
|
||||
.rule().async(async).handler(new WxEchoCpMessageHandler(sb, "ALL")).end();
|
||||
;
|
||||
}
|
||||
|
||||
@Test(dataProvider="messages-1")
|
||||
public void testSync(WxCpXmlMessage message, String expected) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
WxCpMessageRouter router = new WxCpMessageRouter();
|
||||
prepare(false, sb, router);
|
||||
router.route(message);
|
||||
Assert.assertEquals(sb.toString(), expected);
|
||||
}
|
||||
|
||||
@Test(dataProvider="messages-1")
|
||||
public void testAsync(WxCpXmlMessage message, String expected) throws InterruptedException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
WxCpMessageRouter router = new WxCpMessageRouter();
|
||||
prepare(true, sb, router);
|
||||
router.route(message);
|
||||
Thread.sleep(500l);
|
||||
Assert.assertEquals(sb.toString(), expected);
|
||||
}
|
||||
|
||||
public void testConcurrency() throws InterruptedException {
|
||||
final WxCpMessageRouter router = new WxCpMessageRouter();
|
||||
router.rule().handler(new WxCpMessageHandler() {
|
||||
@Override
|
||||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
|
||||
return null;
|
||||
}
|
||||
}).end();
|
||||
|
||||
final WxCpXmlMessage m = new WxCpXmlMessage();
|
||||
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() {
|
||||
WxCpXmlMessage message1 = new WxCpXmlMessage();
|
||||
message1.setMsgType(WxCpConsts.XML_MSG_TEXT);
|
||||
|
||||
WxCpXmlMessage message2 = new WxCpXmlMessage();
|
||||
message2.setEvent(WxCpConsts.EVT_CLICK);
|
||||
|
||||
WxCpXmlMessage message3 = new WxCpXmlMessage();
|
||||
message3.setEventKey("KEY_1");
|
||||
|
||||
WxCpXmlMessage message4 = new WxCpXmlMessage();
|
||||
message4.setContent("CONTENT_1");
|
||||
|
||||
WxCpXmlMessage message5 = new WxCpXmlMessage();
|
||||
message5.setContent("BLA");
|
||||
|
||||
WxCpXmlMessage message6 = new WxCpXmlMessage();
|
||||
message6.setContent("abcd");
|
||||
|
||||
WxCpXmlMessage c2 = new WxCpXmlMessage();
|
||||
c2.setMsgType(WxCpConsts.XML_MSG_TEXT);
|
||||
c2.setEvent(WxCpConsts.EVT_CLICK);
|
||||
|
||||
WxCpXmlMessage c3 = new WxCpXmlMessage();
|
||||
c3.setMsgType(WxCpConsts.XML_MSG_TEXT);
|
||||
c3.setEvent(WxCpConsts.EVT_CLICK);
|
||||
c3.setEventKey("KEY_1");
|
||||
|
||||
WxCpXmlMessage c4 = new WxCpXmlMessage();
|
||||
c4.setMsgType(WxCpConsts.XML_MSG_TEXT);
|
||||
c4.setEvent(WxCpConsts.EVT_CLICK);
|
||||
c4.setEventKey("KEY_1");
|
||||
c4.setContent("CONTENT_1");
|
||||
|
||||
return new Object[][] {
|
||||
new Object[] { message1, WxCpConsts.XML_MSG_TEXT + "," },
|
||||
new Object[] { message2, WxCpConsts.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 WxEchoCpMessageHandler implements WxCpMessageHandler {
|
||||
|
||||
private StringBuffer sb;
|
||||
private String echoStr;
|
||||
|
||||
public WxEchoCpMessageHandler(StringBuffer sb, String echoStr) {
|
||||
this.sb = sb;
|
||||
this.echoStr = echoStr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
|
||||
sb.append(this.echoStr).append(',');
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.cp.bean.WxCpTag;
|
||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Test(groups = "departAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpTagAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
@Inject
|
||||
protected WxCpConfigStorage configStorage;
|
||||
|
||||
protected String tagId;
|
||||
|
||||
public void testTagCreate() throws Exception {
|
||||
tagId = wxService.tagCreate("测试标签4");
|
||||
System.out.println(tagId);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testTagCreate")
|
||||
public void testTagUpdate() throws Exception {
|
||||
wxService.tagUpdate(tagId, "测试标签-改名");
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testTagUpdate")
|
||||
public void testTagGet() throws Exception {
|
||||
List<WxCpTag> tags = wxService.tagGet();
|
||||
Assert.assertNotEquals(tags.size(), 0);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testTagGet")
|
||||
public void testTagAddUsers() throws Exception {
|
||||
List<String> userIds = new ArrayList<String>();
|
||||
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage)configStorage).getUserId());
|
||||
wxService.tagAddUsers(tagId, userIds);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testTagAddUsers")
|
||||
public void testTagGetUsers() throws Exception {
|
||||
List<WxCpUser> users = wxService.tagGetUsers(tagId);
|
||||
Assert.assertNotEquals(users.size(), 0);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testTagGetUsers")
|
||||
public void testTagRemoveUsers() throws Exception {
|
||||
List<String> userIds = new ArrayList<String>();
|
||||
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage)configStorage).getUserId());
|
||||
wxService.tagRemoveUsers(tagId, userIds);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testTagRemoveUsers")
|
||||
public void testTagDelete() throws Exception {
|
||||
wxService.tagDelete(tagId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 测试用户接口
|
||||
*
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@Test(groups = "userAPI", dependsOnGroups = "baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpUserAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
protected WxCpDepart depart;
|
||||
|
||||
public void testUserCreate() throws WxErrorException {
|
||||
WxCpUser user = new WxCpUser();
|
||||
user.setUserId("xiaohe.yang");
|
||||
user.setName("杨宝");
|
||||
user.setDepartIds(new Integer[] { 9, 8 });
|
||||
user.setEmail("yangxiaohe@ddd.com");
|
||||
user.setGender("女");
|
||||
user.setMobile("13564684979");
|
||||
user.setPosition("老婆");
|
||||
user.setTel("3300393");
|
||||
user.addExtAttr("爱好", "老公");
|
||||
wxService.userCreate(user);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testUserCreate")
|
||||
public void testUserUpdate() throws WxErrorException {
|
||||
WxCpUser user = new WxCpUser();
|
||||
user.setUserId("xiaohe.yang");
|
||||
user.setName("杨宝");
|
||||
user.addExtAttr("爱好", "老公2");
|
||||
wxService.userUpdate(user);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testUserUpdate")
|
||||
public void testUserGet() throws WxErrorException {
|
||||
WxCpUser user = wxService.userGet("xiaohe.yang");
|
||||
Assert.assertNotNull(user);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testUserGet")
|
||||
public void testDepartGetUsers() throws WxErrorException {
|
||||
List<WxCpUser> users = wxService.departGetUsers(1, true, 0);
|
||||
Assert.assertNotEquals(users.size(), 0);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = "testDepartGetUsers")
|
||||
public void testUserDelete() throws WxErrorException {
|
||||
wxService.userDelete("xiaohe.yang");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
|
||||
/**
|
||||
* 测试菜单
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
@Test(groups="menuAPI", dependsOnGroups="baseAPI")
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxMenuAPITest {
|
||||
|
||||
@Inject
|
||||
protected WxCpServiceImpl wxService;
|
||||
|
||||
@Test(dataProvider = "menu")
|
||||
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
|
||||
wxService.menuCreate(wxMenu);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testCreateMenu"})
|
||||
public void testGetMenu() throws WxErrorException {
|
||||
Assert.assertNotNull(wxService.menuGet());
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = { "testGetMenu"})
|
||||
public void testDeleteMenu() throws WxErrorException {
|
||||
wxService.menuDelete();
|
||||
}
|
||||
|
||||
@DataProvider(name="menu")
|
||||
public Object[][] getMenu() throws JAXBException {
|
||||
WxMenu menu = new WxMenu();
|
||||
WxMenuButton button1 = new WxMenuButton();
|
||||
button1.setType(WxCpConsts.BUTTON_CLICK);
|
||||
button1.setName("今日歌曲");
|
||||
button1.setKey("V1001_TODAY_MUSIC");
|
||||
|
||||
WxMenuButton button2 = new WxMenuButton();
|
||||
button2.setType(WxCpConsts.BUTTON_CLICK);
|
||||
button2.setName("歌手简介");
|
||||
button2.setKey("V1001_TODAY_SINGER");
|
||||
|
||||
WxMenuButton button3 = new WxMenuButton();
|
||||
button3.setName("菜单");
|
||||
|
||||
menu.getButtons().add(button1);
|
||||
menu.getButtons().add(button2);
|
||||
menu.getButtons().add(button3);
|
||||
|
||||
WxMenuButton button31 = new WxMenuButton();
|
||||
button31.setType(WxCpConsts.BUTTON_VIEW);
|
||||
button31.setName("搜索");
|
||||
button31.setUrl("http://www.soso.com/");
|
||||
|
||||
WxMenuButton button32 = new WxMenuButton();
|
||||
button32.setType(WxCpConsts.BUTTON_VIEW);
|
||||
button32.setName("视频");
|
||||
button32.setUrl("http://v.qq.com/");
|
||||
|
||||
WxMenuButton button33 = new WxMenuButton();
|
||||
button33.setType(WxCpConsts.BUTTON_CLICK);
|
||||
button33.setName("赞一下我们");
|
||||
button33.setKey("V1001_GOOD");
|
||||
|
||||
button3.getSubButtons().add(button31);
|
||||
button3.getSubButtons().add(button32);
|
||||
button3.getSubButtons().add(button33);
|
||||
|
||||
return new Object[][] {
|
||||
new Object[] {
|
||||
menu
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user