🎨 优化企业微信消息发送接口代码,引入moco模拟测试组件,方便测试代码
This commit is contained in:
@@ -1,22 +1,23 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
import com.google.inject.Module;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Slf4j
|
||||
public class ApiTestModule implements Module {
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
private static final String TEST_CONFIG_XML = "test-config.xml";
|
||||
protected WxXmlCpInMemoryConfigStorage config;
|
||||
|
||||
private static <T> T fromXml(Class<T> clazz, InputStream is) {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
@@ -32,70 +33,27 @@ public class ApiTestModule implements Module {
|
||||
throw new RuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成");
|
||||
}
|
||||
|
||||
WxXmlCpInMemoryConfigStorage config = fromXml(WxXmlCpInMemoryConfigStorage.class, inputStream);
|
||||
config = fromXml(WxXmlCpInMemoryConfigStorage.class, inputStream);
|
||||
WxCpService wxService = new WxCpServiceImpl();
|
||||
wxService.setWxCpConfigStorage(config);
|
||||
|
||||
binder.bind(WxCpService.class).toInstance(wxService);
|
||||
binder.bind(WxXmlCpInMemoryConfigStorage.class).toInstance(config);
|
||||
} catch (IOException e) {
|
||||
this.log.error(e.getMessage(), e);
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@XStreamAlias("xml")
|
||||
public static class WxXmlCpInMemoryConfigStorage extends WxCpDefaultConfigImpl {
|
||||
private static final long serialVersionUID = -4521839921547374822L;
|
||||
|
||||
protected String userId;
|
||||
|
||||
protected String departmentId;
|
||||
|
||||
protected String tagId;
|
||||
|
||||
protected String externalUserId;
|
||||
|
||||
public String getUserId() {
|
||||
return this.userId;
|
||||
}
|
||||
|
||||
public void setUserId(String userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getDepartmentId() {
|
||||
return this.departmentId;
|
||||
}
|
||||
|
||||
public void setDepartmentId(String departmentId) {
|
||||
this.departmentId = departmentId;
|
||||
}
|
||||
|
||||
public String getTagId() {
|
||||
return this.tagId;
|
||||
}
|
||||
|
||||
public void setTagId(String tagId) {
|
||||
this.tagId = tagId;
|
||||
}
|
||||
|
||||
public String getExternalUserId() {
|
||||
return externalUserId;
|
||||
}
|
||||
|
||||
public void setExternalUserId(String externalUserId) {
|
||||
this.externalUserId = externalUserId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + " > WxXmlCpConfigStorage{" +
|
||||
"userId='" + this.userId + '\'' +
|
||||
", departmentId='" + this.departmentId + '\'' +
|
||||
", tagId='" + this.tagId + '\'' +
|
||||
", externalUserId='" + this.externalUserId + '\'' +
|
||||
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import com.google.inject.Binder;
|
||||
|
||||
/**
|
||||
* 带mock server 的test module.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-08-30
|
||||
*/
|
||||
public class ApiTestModuleWithMockServer extends ApiTestModule {
|
||||
public static final int mockServerPort = 8080;
|
||||
|
||||
@Override
|
||||
public void configure(Binder binder) {
|
||||
super.configure(binder);
|
||||
super.config.setBaseApiUrl("http://localhost:" + mockServerPort);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,53 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import com.github.dreamhead.moco.HttpServer;
|
||||
import com.github.dreamhead.moco.Runner;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.ApiTestModule;
|
||||
import me.chanjar.weixin.cp.api.ApiTestModuleWithMockServer;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
|
||||
import org.testng.annotations.AfterTest;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
import static com.github.dreamhead.moco.Moco.file;
|
||||
import static com.github.dreamhead.moco.MocoJsonRunner.jsonHttpServer;
|
||||
import static me.chanjar.weixin.cp.api.ApiTestModuleWithMockServer.mockServerPort;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
|
||||
/***
|
||||
* 测试发送消息
|
||||
* @author Daniel Qian
|
||||
/**
|
||||
* 测试类.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-08-30
|
||||
*/
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpMessageAPITest {
|
||||
|
||||
@Guice(modules = ApiTestModuleWithMockServer.class)
|
||||
//@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpMessageServiceImplTest {
|
||||
@Inject
|
||||
protected WxCpService wxService;
|
||||
|
||||
private Runner mockRunner;
|
||||
private ApiTestModule.WxXmlCpInMemoryConfigStorage configStorage;
|
||||
|
||||
@BeforeTest
|
||||
public void setup() {
|
||||
configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage();
|
||||
HttpServer mockServer = jsonHttpServer(mockServerPort, file("src/test/resources/moco/message.json"));
|
||||
this.mockRunner = Runner.runner(mockServer);
|
||||
this.mockRunner.start();
|
||||
this.configStorage = (ApiTestModule.WxXmlCpInMemoryConfigStorage) this.wxService.getWxCpConfigStorage();
|
||||
}
|
||||
|
||||
@AfterTest
|
||||
public void stopMockServer() {
|
||||
this.mockRunner.stop();
|
||||
}
|
||||
|
||||
public void testSendMessage() throws WxErrorException {
|
||||
@@ -37,7 +57,7 @@ public class WxCpMessageAPITest {
|
||||
message.setToUser(configStorage.getUserId());
|
||||
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.getMessageService().messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
@@ -54,7 +74,7 @@ public class WxCpMessageAPITest {
|
||||
.content("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>")
|
||||
.build();
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.getMessageService().messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
@@ -82,7 +102,7 @@ public class WxCpMessageAPITest {
|
||||
" >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)")
|
||||
.build();
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.getMessageService().messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
@@ -96,12 +116,12 @@ public class WxCpMessageAPITest {
|
||||
.TEXTCARD()
|
||||
.toUser(configStorage.getUserId())
|
||||
.btnTxt("更多")
|
||||
.description( "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
|
||||
.description("<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
|
||||
.url("URL")
|
||||
.title("领奖通知")
|
||||
.build();
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.getMessageService().messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
@@ -110,7 +130,7 @@ public class WxCpMessageAPITest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendMessage_miniprogram_notice() throws WxErrorException {
|
||||
public void testSendMessage_miniProgram_notice() throws WxErrorException {
|
||||
WxCpMessage message = WxCpMessage
|
||||
.newMiniProgramNoticeBuilder()
|
||||
.toUser(configStorage.getUserId())
|
||||
@@ -119,12 +139,12 @@ public class WxCpMessageAPITest {
|
||||
.title("会议室预订成功通知")
|
||||
.description("4月27日 16:16")
|
||||
.emphasisFirstItem(true)
|
||||
.contentItems(ImmutableMap.of("会议室","402",
|
||||
"会议地点","广州TIT-402会议室",
|
||||
"会议时间","2018年8月1日 09:00-09:30"))
|
||||
.contentItems(ImmutableMap.of("会议室", "402",
|
||||
"会议地点", "广州TIT-402会议室",
|
||||
"会议时间", "2018年8月1日 09:00-09:30"))
|
||||
.build();
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.getMessageService().messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
@@ -49,7 +49,7 @@ public class WxCpTaskCardServiceImplTest {
|
||||
.buttons(Arrays.asList(btn1, btn2))
|
||||
.build();
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxCpService.messageSend(message);
|
||||
WxCpMessageSendResult messageSendResult = this.wxCpService.getMessageService().messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
|
||||
18
weixin-java-cp/src/test/resources/moco/message.json
Normal file
18
weixin-java-cp/src/test/resources/moco/message.json
Normal file
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"request": {
|
||||
"uri": "/cgi-bin/gettoken"
|
||||
},
|
||||
"response": {
|
||||
"text": "{\"errcode\":0,\"errmsg\":\"ok\",\"access_token\":\"oG1MrhLSzGBl4YxM1W2EHJlL_5vAotNwQ6KBp98sP2fO8XGPPRUlWS9w98CKjxSgPx4YnTy0DU_DvmNXAwt3mSDJ1Uhg_WCFrxX8GWbbCRlzrj2csK-1Y3tzI6dBCMa2YmblBo2sX7qkkzc9pnjP38GzO7Yuo_Bbpyi4doilNWZme0z9ovwiBCkAtV7DXYuh14EsnNrODG454kstOxsqWA\",\"expires_in\":7200}"
|
||||
}
|
||||
},
|
||||
{
|
||||
"request": {
|
||||
"uri": "/cgi-bin/message/send"
|
||||
},
|
||||
"response": {
|
||||
"text": "{\"errcode\":0,\"errmsg\":\"ok\",\"invaliduser\":\"\"}"
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user