This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 469f4229e2b Zipsplitter null body (#15638)
469f4229e2b is described below
commit 469f4229e2ba2920d14af5304b64965bc5a9ee20
Author: thomas-gantenbein-tga
<[email protected]>
AuthorDate: Sun Sep 22 08:30:03 2024 +0200
Zipsplitter null body (#15638)
* Return oldExchange when incoming exchange body is null
* Remove deprecated `doPresetup` method
* Reduce cognitive complexity
---------
Co-authored-by: Thomas Gantenbein <[email protected]>
---
.../aggregate/zipfile/ZipAggregationStrategy.java | 70 +++++++++-------
.../zipfile/ZipFileSplitAndDeleteTest.java | 5 +-
.../zipfile/ZipFileSplitOneFileTest.java | 5 +-
.../zipfile/ZipSplitterRouteIssueTest.java | 5 +-
.../AggregationStrategyWithFilenameHeaderTest.java | 5 +-
.../AggregationStrategyWithPreservationTest.java | 5 +-
.../ZipAggregationStrategyEmptyFileTest.java | 5 +-
...ava => ZipAggregationStrategyNullBodyTest.java} | 95 +++++++++++++---------
.../zipfile/ZipAggregationStrategySplitTest.java | 5 +-
.../zipfile/ZipAggregationStrategyTest.java | 5 +-
10 files changed, 124 insertions(+), 81 deletions(-)
diff --git
a/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
b/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
index 156061709e9..650099e6084 100644
---
a/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
+++
b/components/camel-zipfile/src/main/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategy.java
@@ -221,46 +221,40 @@ public class ZipAggregationStrategy implements
AggregationStrategy {
File zipFile;
Exchange answer = oldExchange;
+ boolean isFirstTimeInAggregation = oldExchange == null;
// Guard against empty new exchanges
- if (newExchange == null) {
+ if (newExchange.getIn().getBody() == null &&
!isFirstTimeInAggregation) {
return oldExchange;
}
- // First time for this aggregation
- if (oldExchange == null) {
- try {
- zipFile = FileUtil.createTempFile(this.filePrefix,
this.fileSuffix, this.parentDir);
- newZipFile(zipFile);
- } catch (IOException | URISyntaxException e) {
- throw new GenericFileOperationFailedException(e.getMessage(),
e);
- }
+ if (isFirstTimeInAggregation) {
+ zipFile = createZipFile();
answer = newExchange;
answer.getExchangeExtension().addOnCompletion(new
DeleteZipFileOnCompletion(zipFile));
} else {
zipFile = oldExchange.getIn().getBody(File.class);
}
Object body = newExchange.getIn().getBody();
- if (body instanceof WrappedFile) {
- body = ((WrappedFile) body).getFile();
+ if (body instanceof WrappedFile wrappedFile) {
+ body = wrappedFile.getFile();
}
String charset = ExchangeHelper.getCharsetName(newExchange, true);
- if (body instanceof File) {
- try {
- File appendFile = (File) body;
- // try to append empty data only when explicit set
- if (this.allowEmptyFiles || appendFile.length() > 0) {
- String entryName = preserveFolderStructure
- ?
newExchange.getIn().getHeader(Exchange.FILE_NAME, String.class)
- : newExchange.getIn().getMessageId();
- addFileToZip(zipFile, appendFile,
this.preserveFolderStructure ? entryName : null);
- }
- } catch (Exception e) {
- throw new GenericFileOperationFailedException(e.getMessage(),
e);
- }
+ if (body instanceof File appendFile) {
+ appendFileToZip(newExchange, appendFile, zipFile);
} else {
- // Handle all other messages
+ appendIncomingBodyAsBytesToZip(newExchange, zipFile, charset);
+ }
+
+ GenericFile<File> genericFile =
FileConsumer.asGenericFile(zipFile.getParent(), zipFile, charset, false);
+ genericFile.bindToExchange(answer);
+
+ return answer;
+ }
+
+ private void appendIncomingBodyAsBytesToZip(Exchange newExchange, File
zipFile, String charset) {
+ if (newExchange.getIn().getBody() != null) {
try {
byte[] buffer =
newExchange.getIn().getMandatoryBody(byte[].class);
// try to append empty data only when explicit set
@@ -274,11 +268,31 @@ public class ZipAggregationStrategy implements
AggregationStrategy {
throw new GenericFileOperationFailedException(e.getMessage(),
e);
}
}
+ }
- GenericFile<File> genericFile =
FileConsumer.asGenericFile(zipFile.getParent(), zipFile, charset, false);
- genericFile.bindToExchange(answer);
+ private void appendFileToZip(Exchange newExchange, File appendFile, File
zipFile) {
+ try {
+ // try to append empty data only when explicit set
+ if (this.allowEmptyFiles || appendFile.length() > 0) {
+ String entryName = preserveFolderStructure
+ ? newExchange.getIn().getHeader(Exchange.FILE_NAME,
String.class)
+ : newExchange.getIn().getMessageId();
+ addFileToZip(zipFile, appendFile, this.preserveFolderStructure
? entryName : null);
+ }
+ } catch (Exception e) {
+ throw new GenericFileOperationFailedException(e.getMessage(), e);
+ }
+ }
- return answer;
+ private File createZipFile() {
+ File zipFile;
+ try {
+ zipFile = FileUtil.createTempFile(this.filePrefix,
this.fileSuffix, this.parentDir);
+ newZipFile(zipFile);
+ } catch (IOException | URISyntaxException e) {
+ throw new GenericFileOperationFailedException(e.getMessage(), e);
+ }
+ return zipFile;
}
@Override
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitAndDeleteTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitAndDeleteTest.java
index 22a9f22bdd2..2e8c12ae18d 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitAndDeleteTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitAndDeleteTest.java
@@ -31,6 +31,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.awaitility.Awaitility;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -38,8 +39,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
public class ZipFileSplitAndDeleteTest extends CamelTestSupport {
- @Override
- public void doPreSetup() {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory("target/testDeleteZipFileWhenUnmarshalWithDataFormat");
deleteDirectory("target/testDeleteZipFileWhenUnmarshalWithSplitter");
}
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitOneFileTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitOneFileTest.java
index afc041ac5e1..40b79e7bc2b 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitOneFileTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipFileSplitOneFileTest.java
@@ -30,14 +30,15 @@ import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
public class ZipFileSplitOneFileTest extends CamelTestSupport {
- @Override
- public void doPreSetup() {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory("target/zip-unmarshal");
}
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipSplitterRouteIssueTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipSplitterRouteIssueTest.java
index f377e19549c..03ad2150949 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipSplitterRouteIssueTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/dataformat/zipfile/ZipSplitterRouteIssueTest.java
@@ -21,14 +21,15 @@ import java.io.File;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
public class ZipSplitterRouteIssueTest extends CamelTestSupport {
- @Override
- public void doPreSetup() throws Exception {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory("target/zip");
}
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
index 0728c389b2f..7c1cde6d094 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithFilenameHeaderTest.java
@@ -28,6 +28,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -40,8 +41,8 @@ public class AggregationStrategyWithFilenameHeaderTest
extends CamelTestSupport
private static final List<String> FILE_NAMES = Arrays.asList("foo", "bar");
private static final String TEST_DIR =
"target/out_AggregationStrategyWithFilenameHeaderTest";
- @Override
- public void doPreSetup() {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory(TEST_DIR);
}
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
index b4873241374..5bf46e858bf 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/AggregationStrategyWithPreservationTest.java
@@ -28,6 +28,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -40,8 +41,8 @@ public class AggregationStrategyWithPreservationTest extends
CamelTestSupport {
private static final int EXPECTED_NO_FILES = 5;
private static final String TEST_DIR =
"target/out_AggregationStrategyWithPreservationTest";
- @Override
- public void doPreSetup() {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory(TEST_DIR);
}
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
index c8f0b3aed74..1b2845ba120 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
@@ -26,6 +26,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -39,8 +40,8 @@ public class ZipAggregationStrategyEmptyFileTest extends
CamelTestSupport {
private static final int EXPECTED_WITH_EMPTY_FILE = 4;
private static final String TEST_DIR =
"target/out_ZipAggregationStrategyEmptyFileTest";
- @Override
- public void doPreSetup() {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory("target/foo");
deleteDirectory("target/bar");
deleteDirectory(TEST_DIR);
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyNullBodyTest.java
similarity index 55%
copy from
components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
copy to
components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyNullBodyTest.java
index c8f0b3aed74..1970caebbca 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyEmptyFileTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyNullBodyTest.java
@@ -26,6 +26,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -33,47 +34,77 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
-public class ZipAggregationStrategyEmptyFileTest extends CamelTestSupport {
+public class ZipAggregationStrategyNullBodyTest extends CamelTestSupport {
- private static final int EXPECTED_NO_EMPTY_FILES = 3;
- private static final int EXPECTED_WITH_EMPTY_FILE = 4;
- private static final String TEST_DIR =
"target/out_ZipAggregationStrategyEmptyFileTest";
+ private static final String TEST_DIR =
"target/out_ZipAggregationStrategyNullBodyTest";
+ public static final String MOCK_AGGREGATE_TO_ZIP_ENTRY =
"mock:aggregateToZipEntry";
- @Override
- public void doPreSetup() {
- deleteDirectory("target/foo");
- deleteDirectory("target/bar");
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory(TEST_DIR);
}
@Test
- public void testNoEmptyFile() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
+ public void testNullBodyLast() throws Exception {
+ MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello");
+ template.sendBody("direct:start", "Hello again");
+ template.sendBody("direct:start", null);
+
+ assertZipContainsFiles(2);
+ }
+
+ @Test
+ public void testNullBodyFirst() throws Exception {
+ MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
mock.expectedMessageCount(1);
- template.sendBody("file:target/foo", "Hello");
- // empty file which is not aggregated
- template.sendBody("file:target/foo", "");
- template.sendBody("file:target/foo", "Bye");
- template.sendBody("file:target/foo", "Howdy");
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", "Hello");
+ template.sendBody("direct:start", "Hello again");
-
checkResult(ZipAggregationStrategyEmptyFileTest.EXPECTED_NO_EMPTY_FILES);
+ assertZipContainsFiles(2);
}
@Test
- public void testAddEmptyFile() throws Exception {
- MockEndpoint mock = getMockEndpoint("mock:aggregateToZipEntry");
+ public void testNullBodyMiddle() throws Exception {
+ MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
mock.expectedMessageCount(1);
- template.sendBody("file:target/bar", "Hello");
- template.sendBody("file:target/bar", "");
- template.sendBody("file:target/bar", "Bye");
- template.sendBody("file:target/bar", "Howdy");
+ template.sendBody("direct:start", "Hello");
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", "Hello again");
-
checkResult(ZipAggregationStrategyEmptyFileTest.EXPECTED_WITH_EMPTY_FILE);
+ assertZipContainsFiles(2);
}
- private void checkResult(int expectedCount) throws InterruptedException,
IOException {
+ @Test
+ public void testNullBodiesOnly() throws Exception {
+ MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", null);
+
+ assertZipContainsFiles(0);
+ }
+
+ @Test
+ public void testTwoNullBodies() throws Exception {
+ MockEndpoint mock = getMockEndpoint(MOCK_AGGREGATE_TO_ZIP_ENTRY);
+ mock.expectedMessageCount(1);
+
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", null);
+ template.sendBody("direct:start", "Hello");
+
+ assertZipContainsFiles(1);
+ }
+
+ private void assertZipContainsFiles(int expectedCount) throws
InterruptedException, IOException {
MockEndpoint.assertIsSatisfied(context);
File[] files = new File(TEST_DIR).listFiles();
@@ -100,25 +131,15 @@ public class ZipAggregationStrategyEmptyFileTest extends
CamelTestSupport {
return new RouteBuilder() {
@Override
public void configure() {
- from("file:target/foo")
+ from("direct:start")
.aggregate(new ZipAggregationStrategy())
.constant(true)
- .completionSize(4)
+ .completionSize(3)
.eagerCheckCompletion()
.to("file:" + TEST_DIR)
- .to("mock:aggregateToZipEntry")
+ .to(MOCK_AGGREGATE_TO_ZIP_ENTRY)
.log("Done processing zip file:
${header.CamelFileName}");
- ZipAggregationStrategy allowEmptyFilesZipAggregationStrategy =
new ZipAggregationStrategy();
- allowEmptyFilesZipAggregationStrategy.setAllowEmptyFiles(true);
- from("file:target/bar")
- .aggregate(allowEmptyFilesZipAggregationStrategy)
- .constant(true)
- .completionSize(4)
- .eagerCheckCompletion()
- .to("file:" + TEST_DIR)
- .to("mock:aggregateToZipEntry")
- .log("Done processing zip file:
${header.CamelFileName}");
}
};
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitTest.java
index 62be8019e37..09c38d81200 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategySplitTest.java
@@ -28,6 +28,7 @@ import
org.apache.camel.processor.aggregate.GroupedMessageAggregationStrategy;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.camel.util.IOHelper;
import org.awaitility.Awaitility;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -38,8 +39,8 @@ public class ZipAggregationStrategySplitTest extends
CamelTestSupport {
private static final int EXPECTED_NO_FILES = 3;
private static final String TEST_DIR =
"target/out_ZipAggregationStrategyTest";
- @Override
- public void doPreSetup() {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory(TEST_DIR);
}
diff --git
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyTest.java
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyTest.java
index a1b77a02c76..ed67bdbaf82 100644
---
a/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyTest.java
+++
b/components/camel-zipfile/src/test/java/org/apache/camel/processor/aggregate/zipfile/ZipAggregationStrategyTest.java
@@ -25,6 +25,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.apache.camel.util.IOHelper;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.apache.camel.test.junit5.TestSupport.deleteDirectory;
@@ -37,8 +38,8 @@ public class ZipAggregationStrategyTest extends
CamelTestSupport {
private static final int EXPECTED_NO_FILES = 3;
private static final String TEST_DIR =
"target/out_ZipAggregationStrategyTest";
- @Override
- public void doPreSetup() throws Exception {
+ @BeforeEach
+ public void deleteTestDirs() {
deleteDirectory(TEST_DIR);
}