gnodet commented on code in PR #27675:
URL: https://github.com/apache/camel/pull/27675#discussion_r4239697032


##########
components/camel-mina-sftp/src/test/java/org/apache/camel/component/file/remote/mina/sftp/SftpProducerAllowNullBodyIT.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.camel.component.file.remote.mina.sftp;
+
+import java.io.File;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.component.file.GenericFileOperationFailedException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIf;
+
+import static org.apache.camel.test.junit6.TestSupport.assertIsInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@EnabledIf(value = 
"org.apache.camel.test.infra.ftp.services.embedded.SftpUtil#hasRequiredAlgorithms('src/test/resources/sftp/hostkey.pem')")
+class SftpProducerAllowNullBodyIT extends SftpServerTestSupport {
+
+    private String getSftpUri(String fileName) {
+        return 
"mina-sftp://localhost:{{ftp.server.port}}/{{ftp.root.dir}}/allownull?username=admin&password=admin";
+               + "&fileName=" + fileName + "&knownHostsFile=" + 
service.getKnownHostsFile();
+    }
+
+    @Test
+    void testAllowNullBodyTrue() {
+        template.sendBody(getSftpUri("allowNullBodyTrue.txt") + 
"&allowNullBody=true", null);
+
+        File file = ftpFile("allownull/allowNullBodyTrue.txt").toFile();
+        assertTrue(file.exists(), "allowNullBody set to true with null body 
should create an empty file");
+        assertEquals(0, file.length());
+    }
+
+    @Test
+    void testAllowNullBodyFalse() {
+        String uri = getSftpUri("allowNullBodyFalse.txt") + 
"&allowNullBody=false";
+        Exception ex = assertThrows(CamelExecutionException.class, () -> 
template.sendBody(uri, null));
+
+        GenericFileOperationFailedException cause
+                = 
assertIsInstanceOf(GenericFileOperationFailedException.class, ex.getCause());
+        assertTrue(cause.getMessage().endsWith("allowNullBodyFalse.txt"));

Review Comment:
   💡 **Observation:** `cause.getMessage().endsWith("allowNullBodyFalse.txt")` 
is a weak assertion — it would also pass against the old code path, where 
`getMandatoryBody(InputStream.class)` throws an `InvalidPayloadException` that 
gets re-wrapped into a `GenericFileOperationFailedException` whose message also 
contains the filename. Consider asserting on the full message prefix instead:
   ```suggestion
           assertTrue(cause.getMessage().startsWith("Cannot write null body to 
file:"));
   ```
   This pins the test to the new explicit error message and makes the intent 
clearer.



##########
components/camel-mina-sftp/src/main/java/org/apache/camel/component/file/remote/mina/MinaSftpOperations.java:
##########
@@ -1343,8 +1344,28 @@ private boolean doStoreFile(String name, Exchange 
exchange) throws GenericFileOp
             }
         }
 
+        InputStream is = null;
+        if (exchange.getIn().getBody() == null) {
+            // Do an explicit test for a null body and decide what to do
+            if (endpoint.isAllowNullBody()) {
+                LOG.trace("Writing empty file.");
+                is = new ByteArrayInputStream(new byte[] {});

Review Comment:
   💡 **Nit:** `new byte[] {}` is equivalent to `new byte[0]` but the latter is 
the more conventional idiom in the JDK and across the Camel codebase.
   ```suggestion
                   is = new ByteArrayInputStream(new byte[0]);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to