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#4510] Add test case for SpringSinkConnector. #4511

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/eventmesh-connector-spring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ configurations {

dependencies {
api project(":eventmesh-openconnect:eventmesh-openconnect-java")
implementation project(":eventmesh-common")
implementation project(":eventmesh-sdks:eventmesh-sdk-java")
implementation "org.springframework.boot:spring-boot-starter:$spring_boot_version"
implementation "org.springframework.boot:spring-boot-starter-validation:$spring_boot_version"
Expand All @@ -32,4 +33,5 @@ dependencies {

testImplementation "org.mockito:mockito-core"
testImplementation "org.mockito:mockito-junit-jupiter"
testImplementation libs.hamcrest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.eventmesh.connector.spring.sink.connector;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.apache.eventmesh.connector.spring.sink.config.SpringSinkConfig;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordOffset;
import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordPartition;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class SpringSinkConnectorTest {

@Spy
private SpringSinkConnector connector;

@BeforeEach
public void setUp() throws Exception {
SpringSinkConfig sinkConfig = new SpringSinkConfig();
connector.init(sinkConfig);
connector.start();
}

@Test
public void springSinkConnectorTest() throws Exception {
final int count = 5;
final String message = "testMessage";
writeMockedRecords(count, message);
BlockingQueue<ConnectRecord> queue = connector.getQueue();
assertThat(count, is(queue.size()));
for (int i = 0; i < count; i++) {
ConnectRecord poll = queue.poll();
assertNotNull(poll);
String expectedMessage = message + i;
assertThat(poll.getData(), is(expectedMessage));
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it more convenient to use JUnit 5's Assertions directly? Why did you deliberately introduce 'hamcrest' to make assertions?


直接使用JUnit 5的Assertions不是更方便吗?为何特意引入'hamcrest'来断言?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hamcrest's assertion is more powerful than junit5, and I think can be combined.


hamcrest的断言比junit5更强大, 我认为两者可以组合使用。

Copy link
Member

Choose a reason for hiding this comment

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

But your test case does not require more powerful features in 'hamcrest' than 'JUnit'. Shouldn't it be used on demand?


但是你的测试用例并不需要'hamcrest'比'JUnit'更强大的特性啊。不应该按需使用吗?

Copy link
Member

Choose a reason for hiding this comment

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

Hamcrest is an additional dependency, and if we're using it solely for simple assertions, I believe that standard JUnit assertions should suffice. This can help us keep our project dependencies minimal and reduce complexity in our tests. This is just my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also think it's right to keep minimal dependencies, it has been fixed in new commit.

}
}

private void writeMockedRecords(int count, String message) throws Exception {
List<ConnectRecord> records = new ArrayList<>();
for (int i = 0; i < count; i++) {
RecordPartition partition = new RecordPartition();
RecordOffset offset = new RecordOffset();
records.add(new ConnectRecord(partition, offset, System.currentTimeMillis(), message + i));
}
connector.put(records);
}
}
22 changes: 22 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

[versions]
hamcrest = "2.2"

[libraries]
hamcrest = { module = "org.hamcrest:hamcrest", version.ref = "hamcrest" }
Loading