This is an automated email from the ASF dual-hosted git repository.

mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new 635f038ad [ISSUE #4642] unit test for FileSinkConnectorTest.java 
(#5197)
635f038ad is described below

commit 635f038ad0279ef4a03b6a0c3cd171cda92b0213
Author: Wangzy <[email protected]>
AuthorDate: Mon Apr 28 14:12:57 2025 +0800

    [ISSUE #4642] unit test for FileSinkConnectorTest.java (#5197)
    
    * feat: unit test for FileSinkConnectorTest.java
    
    * feat: unit test for FileSinkConnectorTest.java
    
    * feat: add License
    
    * feat: fix codestyle
    
    * feat: fix codestyle
---
 .../file/sink/connector/FileSinkConnectorTest.java | 89 ++++++++++++++++++++++
 .../connector}/FileSourceConnectorTest.java        |  3 +-
 2 files changed, 90 insertions(+), 2 deletions(-)

diff --git 
a/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/sink/connector/FileSinkConnectorTest.java
 
b/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/sink/connector/FileSinkConnectorTest.java
new file mode 100644
index 000000000..281d31f04
--- /dev/null
+++ 
b/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/sink/connector/FileSinkConnectorTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.file.sink.connector;
+
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.eventmesh.common.config.connector.file.FileSinkConfig;
+import org.apache.eventmesh.common.config.connector.file.SinkConnectorConfig;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
+
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+
+
+public class FileSinkConnectorTest {
+
+    private FileSinkConnector fileSinkConnector;
+
+    @Mock
+    private FileSinkConfig fileSinkConfig;
+
+    @Test
+    void testFileSinkConnector() throws Exception {
+
+        fileSinkConfig = mock(FileSinkConfig.class);
+        SinkConnectorConfig connectorConfig = mock(SinkConnectorConfig.class);
+        when(fileSinkConfig.getConnectorConfig()).thenReturn(connectorConfig);
+        when(connectorConfig.getTopic()).thenReturn("test-topic");
+        when(fileSinkConfig.getFlushSize()).thenReturn(10);
+        when(fileSinkConfig.isHourlyFlushEnabled()).thenReturn(false);
+
+        fileSinkConnector = new FileSinkConnector();
+        fileSinkConnector.init(fileSinkConfig);
+        fileSinkConnector.start();
+
+        String content = "line1\nline2\nline3";
+        ConnectRecord record = new ConnectRecord(null, null, 
System.currentTimeMillis(), content.getBytes(StandardCharsets.UTF_8));
+        List<ConnectRecord> connectRecords = Collections.singletonList(record);
+        fileSinkConnector.put(connectRecords);
+        fileSinkConnector.stop();
+
+        Calendar calendar = Calendar.getInstance(Locale.CHINA);
+        int year = calendar.get(Calendar.YEAR);
+        int month = calendar.get(Calendar.MONTH) + 1;
+        int day = calendar.get(Calendar.DATE);
+        Path topicPath = Paths.get("test-topic",
+            String.valueOf(year),
+            String.valueOf(month),
+            String.valueOf(day));
+        Assertions.assertTrue(Files.exists(topicPath), "Directory for topic 
should exist");
+
+        Path outputPath = Files.list(topicPath)
+            .filter(path -> path.toString().contains("test-topic"))
+            .findFirst()
+            .orElseThrow(() -> new RuntimeException("No output file found with 
'test-topic' in the name"));
+
+        List<String> lines = Files.readAllLines(outputPath, 
StandardCharsets.UTF_8);
+        String actualContent = String.join("\n", lines);
+        Assertions.assertEquals(content, actualContent);
+
+    }
+}
\ No newline at end of file
diff --git 
a/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/FileSourceConnectorTest.java
 
b/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/source/connector/FileSourceConnectorTest.java
similarity index 95%
rename from 
eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/FileSourceConnectorTest.java
rename to 
eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/source/connector/FileSourceConnectorTest.java
index 9cfea3cc5..5c36a95d5 100644
--- 
a/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/FileSourceConnectorTest.java
+++ 
b/eventmesh-connectors/eventmesh-connector-file/src/test/java/org/apache/eventmesh/connector/file/source/connector/FileSourceConnectorTest.java
@@ -15,14 +15,13 @@
  * limitations under the License.
  */
 
-package org.apache.eventmesh.connector.file;
+package org.apache.eventmesh.connector.file.source.connector;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import org.apache.eventmesh.common.config.connector.file.FileSourceConfig;
 import org.apache.eventmesh.common.config.connector.file.SourceConnectorConfig;
-import 
org.apache.eventmesh.connector.file.source.connector.FileSourceConnector;
 import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
 
 import java.nio.charset.StandardCharsets;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to