1
0
mirror of synced 2026-02-12 23:27:55 +08:00

优化重构企业号相关代码,修复了升级企业微信后出现的菜单问题和用户管理的问题

This commit is contained in:
Binary Wang
2017-06-25 18:35:21 +08:00
parent 0f0f7bb0a3
commit 9f4a7a7796
29 changed files with 1031 additions and 522 deletions

View File

@@ -31,7 +31,7 @@ public class ApiTestModule implements Module {
wxService.setWxCpConfigStorage(config);
binder.bind(WxCpService.class).toInstance(wxService);
binder.bind(WxCpConfigStorage.class).toInstance(config);
binder.bind(WxXmlCpInMemoryConfigStorage.class).toInstance(config);
} catch (IOException e) {
e.printStackTrace();
}

View File

@@ -1,77 +0,0 @@
package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 测试多媒体文件上传下载
*
* @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<>();
@Test(dataProvider = "uploadMedia")
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
try (InputStream inputStream = ClassLoader
.getSystemResourceAsStream(fileName);) {
WxMediaUploadResult res = this.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) {
this.media_ids.add(res.getMediaId());
}
if (res.getThumbMediaId() != null) {
this.media_ids.add(res.getThumbMediaId());
}
}
}
@DataProvider
public Object[][] uploadMedia() {
return new Object[][]{
new Object[]{WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_MP3, "mm.mp3"},
new Object[]{WxConsts.MEDIA_VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MEDIA_FILE, TestConstants.FILE_JPG, "mm.jpeg"}
};
}
@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
public void testDownloadMedia(String media_id) throws WxErrorException {
this.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;
}
}

View File

@@ -1,68 +0,0 @@
package me.chanjar.weixin.cp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
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 {
this.tagId = this.wxService.tagCreate("测试标签4");
System.out.println(this.tagId);
}
@Test(dependsOnMethods = "testTagCreate")
public void testTagUpdate() throws Exception {
this.wxService.tagUpdate(this.tagId, "测试标签-改名");
}
@Test(dependsOnMethods = "testTagUpdate")
public void testTagGet() throws Exception {
List<WxCpTag> tags = this.wxService.tagGet();
Assert.assertNotEquals(tags.size(), 0);
}
@Test(dependsOnMethods = "testTagGet")
public void testTagAddUsers() throws Exception {
List<String> userIds = new ArrayList<>();
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage) this.configStorage).getUserId());
this.wxService.tagAddUsers(this.tagId, userIds, null);
}
@Test(dependsOnMethods = "testTagAddUsers")
public void testTagGetUsers() throws Exception {
List<WxCpUser> users = this.wxService.tagGetUsers(this.tagId);
Assert.assertNotEquals(users.size(), 0);
}
@Test(dependsOnMethods = "testTagGetUsers")
public void testTagRemoveUsers() throws Exception {
List<String> userIds = new ArrayList<>();
userIds.add(((ApiTestModule.WxXmlCpInMemoryConfigStorage) this.configStorage).getUserId());
this.wxService.tagRemoveUsers(this.tagId, userIds);
}
@Test(dependsOnMethods = "testTagRemoveUsers")
public void testTagDelete() throws Exception {
this.wxService.tagDelete(this.tagId);
}
}

View File

@@ -0,0 +1,78 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.TestConstants;
import me.chanjar.weixin.cp.api.WxCpService;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
/**
* <pre>
*
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
@Guice(modules = ApiTestModule.class)
public class WxCpMediaServiceImplTest {
@Inject
private WxCpService wxService;
private List<String> mediaIds = new ArrayList<>();
@DataProvider
public Object[][] mediaData() {
return new Object[][]{
new Object[]{WxConsts.MEDIA_IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_MP3, "mm.mp3"},
new Object[]{WxConsts.MEDIA_VOICE, TestConstants.FILE_AMR, "mm.amr"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
new Object[]{WxConsts.MEDIA_VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
new Object[]{WxConsts.MEDIA_FILE, TestConstants.FILE_JPG, "mm.jpeg"}
};
}
@Test(dataProvider = "mediaData")
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)) {
WxMediaUploadResult res = this.wxService.getMediaService().upload(mediaType, fileType, inputStream);
assertNotNull(res.getType());
assertNotNull(res.getCreatedAt());
assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
if (res.getMediaId() != null) {
this.mediaIds.add(res.getMediaId());
}
if (res.getThumbMediaId() != null) {
this.mediaIds.add(res.getThumbMediaId());
}
}
}
@DataProvider
public Object[][] downloadMedia() {
Object[][] params = new Object[this.mediaIds.size()][];
for (int i = 0; i < this.mediaIds.size(); i++) {
params[i] = new Object[]{this.mediaIds.get(i)};
}
return params;
}
@Test(dependsOnMethods = {"testUploadMedia"}, dataProvider = "downloadMedia")
public void testDownloadMedia(String media_id) throws WxErrorException {
File file = this.wxService.getMediaService().download(media_id);
assertNotNull(file);
System.out.println(file);
}
}

View File

@@ -1,45 +1,29 @@
package me.chanjar.weixin.cp.api;
package me.chanjar.weixin.cp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import org.testng.annotations.*;
import static org.testng.Assert.*;
/**
* 测试菜单
* <pre>
*
* @author Daniel Qian
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
@Test(groups = "menuAPI", dependsOnGroups = "baseAPI")
@Guice(modules = ApiTestModule.class)
public class WxMenuAPITest {
public class WxCpMenuServiceImplTest {
@Inject
protected WxCpServiceImpl wxService;
protected WxCpService wxService;
@Test(dataProvider = "menu")
public void testCreateMenu(WxMenu wxMenu) throws WxErrorException {
this.wxService.menuCreate(wxMenu);
}
@Test(dependsOnMethods = {"testCreateMenu"})
public void testGetMenu() throws WxErrorException {
Assert.assertNotNull(this.wxService.menuGet());
}
@Test(dependsOnMethods = {"testGetMenu"})
public void testDeleteMenu() throws WxErrorException {
this.wxService.menuDelete();
}
@DataProvider(name = "menu")
public Object[][] getMenu() {
@DataProvider
public Object[][] menuData() {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxConsts.BUTTON_CLICK);
@@ -85,5 +69,21 @@ public class WxMenuAPITest {
}
@Test(dataProvider = "menuData")
public void testCreate(WxMenu wxMenu) throws Exception {
this.wxService.getMenuService().create(wxMenu);
}
@Test(dependsOnMethods = "testCreate")
public void testGet() throws Exception {
WxMenu menu = this.wxService.getMenuService().get();
assertNotNull(menu);
System.out.println(menu.toJson());
}
@Test(dependsOnMethods = {"testGet", "testCreate"})
public void testDelete() throws Exception {
this.wxService.getMenuService().delete();
}
}

View File

@@ -0,0 +1,75 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.common.base.Splitter;
import com.google.inject.Inject;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpTag;
import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.testng.annotations.*;
import java.util.List;
import static org.testng.Assert.*;
/**
* <pre>
*
* Created by Binary Wang on 2017-6-25.
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* </pre>
*/
@Guice(modules = ApiTestModule.class)
public class WxCpTagServiceImplTest {
@Inject
protected WxCpService wxService;
@Inject
protected ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage;
protected String tagId;
@Test
public void testCreate() throws Exception {
this.tagId = this.wxService.getTagService().create("测试标签" + System.currentTimeMillis());
System.out.println(this.tagId);
}
@Test(dependsOnMethods = "testCreate")
public void testUpdate() throws Exception {
this.wxService.getTagService().update(this.tagId, "测试标签-改名" + System.currentTimeMillis());
}
@Test(dependsOnMethods = {"testUpdate", "testCreate"})
public void testListAll() throws Exception {
List<WxCpTag> tags = this.wxService.getTagService().listAll();
assertNotEquals(tags.size(), 0);
}
@Test(dependsOnMethods = {"testListAll", "testUpdate", "testCreate"})
public void testAddUsers2Tag() throws Exception {
List<String> userIds = Splitter.on("|").splitToList(this.configStorage.getUserId());
WxCpTagAddOrRemoveUsersResult result = this.wxService.getTagService().addUsers2Tag(this.tagId, userIds, null);
assertEquals(result.getErrCode(), Integer.valueOf(0));
}
@Test(dependsOnMethods = {"testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"})
public void testListUsersByTagId() throws Exception {
List<WxCpUser> users = this.wxService.getTagService().listUsersByTagId(this.tagId);
assertNotEquals(users.size(), 0);
}
@Test(dependsOnMethods = {"testListUsersByTagId", "testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"})
public void testRemoveUsersFromTag() throws Exception {
List<String> userIds = Splitter.on("|").splitToList(this.configStorage.getUserId());
WxCpTagAddOrRemoveUsersResult result = this.wxService.getTagService().removeUsersFromTag(this.tagId, userIds);
assertEquals(result.getErrCode(), Integer.valueOf(0));
}
@Test(dependsOnMethods = {"testRemoveUsersFromTag", "testListUsersByTagId", "testAddUsers2Tag", "testListAll", "testUpdate", "testCreate"})
public void testDelete() throws Exception {
this.wxService.getTagService().delete(this.tagId);
}
}

View File

@@ -6,9 +6,7 @@ import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpUser;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import org.testng.annotations.*;
import java.util.List;
@@ -25,19 +23,21 @@ import static org.testng.Assert.*;
public class WxCpUserServiceImplTest {
@Inject
private WxCpService wxCpService;
private String userId = "someone" + System.currentTimeMillis();
@Test
public void testAuthenticate() throws Exception {
this.wxCpService.getUserService().authenticate("abc");
}
@Test
public void testCreate() throws Exception {
WxCpUser user = new WxCpUser();
user.setUserId("some.woman");
user.setUserId(userId);
user.setName("Some Woman");
user.setDepartIds(new Integer[]{9, 8});
user.setDepartIds(new Integer[]{2});
user.setEmail("none@none.com");
user.setGender("");
user.setGender(WxCpUser.Gender.FEMAIL);
user.setMobile("13560084979");
user.setPosition("woman");
user.setTelephone("3300393");
@@ -48,20 +48,20 @@ public class WxCpUserServiceImplTest {
@Test(dependsOnMethods = "testCreate")
public void testUpdate() throws Exception {
WxCpUser user = new WxCpUser();
user.setUserId("some.woman");
user.setUserId(userId);
user.setName("Some Woman");
user.addExtAttr("爱好", "table2");
this.wxCpService.getUserService().update(user);
}
@Test
@Test(dependsOnMethods = {"testCreate", "testUpdate"})
public void testDelete() throws Exception {
this.wxCpService.getUserService().delete("some.woman");
this.wxCpService.getUserService().delete(userId);
}
@Test(dependsOnMethods = "testUpdate")
public void testGetById() throws Exception {
WxCpUser user = this.wxCpService.getUserService().getById("some.woman");
WxCpUser user = this.wxCpService.getUserService().getById(userId);
assertNotNull(user);
}

View File

@@ -1,14 +1,14 @@
package me.chanjar.weixin.cp.demo;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
@@ -69,8 +69,7 @@ public class WxCpDemoServer {
Map<String, Object> context, WxCpService wxService,
WxSessionManager sessionManager) {
String href = "<a href=\""
+ wxService.oauth2buildAuthorizationUrl(
wxCpConfigStorage.getOauth2redirectUri(), null)
+ wxService.getOauth2Service().buildAuthorizationUrl(wxCpConfigStorage.getOauth2redirectUri(), null)
+ "\">测试oauth2</a>";
return WxCpXmlOutMessage.TEXT().content(href)
.fromUser(wxMessage.getToUserName())

View File

@@ -30,7 +30,7 @@ public class WxCpOAuth2Servlet extends HttpServlet {
response.getWriter().println("<h1>code</h1>");
response.getWriter().println(code);
String[] res = this.wxCpService.oauth2getUserInfo(code);
String[] res = this.wxCpService.getOauth2Service().getUserInfo(code);
response.getWriter().println("<h1>result</h1>");
response.getWriter().println(Arrays.toString(res));
} catch (WxErrorException e) {

Binary file not shown.

View File

@@ -6,12 +6,7 @@
<class name="me.chanjar.weixin.cp.api.WxCpBusyRetryTest"/>
<class name="me.chanjar.weixin.cp.api.WxCpBaseAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpMessageAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxMenuAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpDepartAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpMediaAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpMessageRouterTest"/>
<class name="me.chanjar.weixin.cp.api.WxCpTagAPITest"/>
<class name="me.chanjar.weixin.cp.api.WxCpUserAPITest"/>
</classes>
</test>