Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #4602] when wechat send message api response errcode is not zero, the wechat sink connector does not throw IllegalAccessException #4603

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eventmesh-connectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ Add a new connector by implementing the source/sink interface using :
| [Spring](eventmesh-connector-spring) | Sink | ✅ |
| WeCom | Source | ⬜ |
| [WeCom](eventmesh-connector-wecom) | Sink | ✅ |
| WeChat | Source | ⬜ |
| [WeChat](eventmesh-connector-wechat) | Sink | ✅ |
| More connectors will be added... | Source/Sink | N/A |
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Data
public class TemplateMessageResponse {

private int errocode;
private int errcode;

private String errmsg;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ private void sendMessage(ConnectRecord record) {
throw new IOException("message response is null.");
}

if (messageResponse.getErrocode() != 0) {
throw new IllegalAccessException(String.format("Send message to weCom error! errorCode=%s, errorMessage=%s",
messageResponse.getErrocode(), messageResponse.getErrmsg()));
if (messageResponse.getErrcode() != 0) {
throw new IllegalAccessException(String.format("Send message to WeChat error! errorCode=%s, errorMessage=%s",
messageResponse.getErrcode(), messageResponse.getErrmsg()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -79,6 +82,20 @@ public void setUp() throws Exception {
Mockito.doReturn(tokenCall).when(okHttpClient).newCall(Mockito.argThat(tokenMatcher));
Mockito.doReturn(tokenResponse).when(tokenCall).execute();

weChatSinkConnector = new WeChatSinkConnector();
WeChatSinkConfig weChatSinkConfig = (WeChatSinkConfig) ConfigUtil.parse(weChatSinkConnector.configClass());
weChatSinkConnector.init(weChatSinkConfig);
Field clientField = ReflectionSupport.findFields(weChatSinkConnector.getClass(),
(f) -> f.getName().equals("okHttpClient"),
HierarchyTraversalMode.BOTTOM_UP).get(0);
clientField.setAccessible(true);
clientField.set(weChatSinkConnector, okHttpClient);
weChatSinkConnector.start();
}

@Test
public void testSendMessageToWeChat() throws Exception {

Request sendMessageRequest = new Request.Builder().url("https://api.weixin.qq.com/cgi-bin/message/template/send").build();
String sendMessageResponseJson = "{\"errcode\":0,\"errmsg\":\"ok\",\"msgid\":200228332}";
ResponseBody sendMessageBody = ResponseBody.create(MediaType.parse("application/json; charset=utf-8"), sendMessageResponseJson);
Expand All @@ -95,31 +112,44 @@ public void setUp() throws Exception {
Mockito.doReturn(sendMessageRequestCall).when(okHttpClient).newCall(Mockito.argThat(sendMessageMatcher));
Mockito.doReturn(sendMessageResponse).when(sendMessageRequestCall).execute();

weChatSinkConnector = new WeChatSinkConnector();
WeChatSinkConfig weChatSinkConfig = (WeChatSinkConfig) ConfigUtil.parse(weChatSinkConnector.configClass());
weChatSinkConnector.init(weChatSinkConfig);
Field clientField = ReflectionSupport.findFields(weChatSinkConnector.getClass(),
(f) -> f.getName().equals("okHttpClient"),
HierarchyTraversalMode.BOTTOM_UP).get(0);
clientField.setAccessible(true);
clientField.set(weChatSinkConnector, okHttpClient);
weChatSinkConnector.start();
List<ConnectRecord> records = new ArrayList<>();
RecordPartition partition = new RecordPartition();
RecordOffset offset = new RecordOffset();
ConnectRecord connectRecord = new ConnectRecord(partition, offset,
System.currentTimeMillis(), "Hello, EventMesh!".getBytes(StandardCharsets.UTF_8));
records.add(connectRecord);

weChatSinkConnector.put(records);
verify(okHttpClient, times(2)).newCall(any(Request.class));

WeChatSinkConnector.ACCESS_TOKEN_CACHE.invalidate(WeChatSinkConnector.ACCESS_TOKEN_CACHE_KEY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking about the necessity of this line of code. It seems that you did not actually interact with WeChat in your test, so you should not be using this token.

我在思考这行代码的必要性。您的测试中好像并不会真的与wechat交互,应该用不到该token吧。

Copy link
Contributor Author

@wizardzhang wizardzhang Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first u should use app id and app secret to get a token, then use token to send message. so i mock a get token response and store the token in cache, in testSendMessageToWeChat() method, the mock token repsone is used, and then in testSendMessageToWeChatAbnormally() method, the mock token repsone will not be used, because token has been stored in cache and then will throw a org.mockito.exceptions.misusing.UnnecessaryStubbingException. the test result like this
image
testSendMessageToWeChat() passed, testSendMessageToWeChatAbnormally() failed

}

@Test
public void testSendMessageToWeChat() throws Exception {
final int times = 1;
List<ConnectRecord> records = new ArrayList<>();
for (int i = 0; i < times; i++) {
RecordPartition partition = new RecordPartition();
RecordOffset offset = new RecordOffset();
ConnectRecord connectRecord = new ConnectRecord(partition, offset,
System.currentTimeMillis(), "Hello, EventMesh!".getBytes(StandardCharsets.UTF_8));
records.add(connectRecord);
}
public void testSendMessageToWeChatAbnormally() throws Exception {
Request sendMessageRequest = new Request.Builder().url("https://api.weixin.qq.com/cgi-bin/message/template/send").build();
String sendMessageResponseJson = "{\"errcode\":42001,\"errmsg\":\"access_token expired rid: 656e8793-061949b5-738cb8f4\"}";
ResponseBody sendMessageBody = ResponseBody.create(MediaType.parse("application/json; charset=utf-8"), sendMessageResponseJson);
Response sendMessageResponse = new Response.Builder()
.code(200)
.protocol(Protocol.HTTP_1_0)
.request(sendMessageRequest)
.body(sendMessageBody)
.message("ok")
.build();
ArgumentMatcher<Request> sendMessageMatcher = (anyRequest) ->
sendMessageRequest.url().encodedPath().startsWith(anyRequest.url().encodedPath());
Call sendMessageRequestCall = Mockito.mock(Call.class);
Mockito.doReturn(sendMessageRequestCall).when(okHttpClient).newCall(Mockito.argThat(sendMessageMatcher));
Mockito.doReturn(sendMessageResponse).when(sendMessageRequestCall).execute();

weChatSinkConnector.put(records);
verify(okHttpClient, times(times + 1)).newCall(any(Request.class));
RecordPartition partition = new RecordPartition();
RecordOffset offset = new RecordOffset();
ConnectRecord connectRecord = new ConnectRecord(partition, offset,
System.currentTimeMillis(), "Hello, EventMesh!".getBytes(StandardCharsets.UTF_8));
Method sendMessageMethod = WeChatSinkConnector.class.getDeclaredMethod("sendMessage", ConnectRecord.class);
sendMessageMethod.setAccessible(true);
Assertions.assertThrows(InvocationTargetException.class, () -> sendMessageMethod.invoke(weChatSinkConnector, connectRecord));
}

@AfterEach
Expand Down