You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/06/12 07:03:27 UTC

[camel] branch jira created (now 1bb2f87dbd6)

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

davsclaus pushed a change to branch jira
in repository https://gitbox.apache.org/repos/asf/camel.git


      at 1bb2f87dbd6 CAMEL-19423: camel-jira - Do not have special type converter. Attach should work with body as-is

This branch includes the following new commits:

     new 1bb2f87dbd6 CAMEL-19423: camel-jira - Do not have special type converter. Attach should work with body as-is

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[camel] 01/01: CAMEL-19423: camel-jira - Do not have special type converter. Attach should work with body as-is

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch jira
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 1bb2f87dbd67f446ec1f12ca43ee6bab9eba261e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 12 09:03:04 2023 +0200

    CAMEL-19423: camel-jira - Do not have special type converter. Attach should work with body as-is
---
 .../camel/component/jira/FileConverterLoader.java  | 52 ----------------------
 .../services/org/apache/camel/TypeConverterLoader  |  2 -
 .../apache/camel/component/jira/FileConverter.java | 50 ---------------------
 .../jira/producer/AttachFileProducer.java          | 30 +++++++++++--
 ...va => AttachFileProducerFromFileRouteTest.java} | 26 +++++------
 ...java => AttachFileProducerInputStreamTest.java} | 47 +++++++++----------
 .../jira/producer/AttachFileProducerTest.java      |  7 ++-
 7 files changed, 64 insertions(+), 150 deletions(-)

diff --git a/components/camel-jira/src/generated/java/org/apache/camel/component/jira/FileConverterLoader.java b/components/camel-jira/src/generated/java/org/apache/camel/component/jira/FileConverterLoader.java
deleted file mode 100644
index 8c55edbdd91..00000000000
--- a/components/camel-jira/src/generated/java/org/apache/camel/component/jira/FileConverterLoader.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/* Generated by camel build tools - do NOT edit this file! */
-package org.apache.camel.component.jira;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.CamelContextAware;
-import org.apache.camel.DeferredContextBinding;
-import org.apache.camel.Exchange;
-import org.apache.camel.TypeConversionException;
-import org.apache.camel.TypeConverterLoaderException;
-import org.apache.camel.spi.TypeConverterLoader;
-import org.apache.camel.spi.TypeConverterRegistry;
-import org.apache.camel.support.SimpleTypeConverter;
-import org.apache.camel.support.TypeConverterSupport;
-import org.apache.camel.util.DoubleMap;
-
-/**
- * Generated by camel build tools - do NOT edit this file!
- */
-@SuppressWarnings("unchecked")
-@DeferredContextBinding
-public final class FileConverterLoader implements TypeConverterLoader, CamelContextAware {
-
-    private CamelContext camelContext;
-
-    public FileConverterLoader() {
-    }
-
-    @Override
-    public void setCamelContext(CamelContext camelContext) {
-        this.camelContext = camelContext;
-    }
-
-    @Override
-    public CamelContext getCamelContext() {
-        return camelContext;
-    }
-
-    @Override
-    public void load(TypeConverterRegistry registry) throws TypeConverterLoaderException {
-        registerConverters(registry);
-    }
-
-    private void registerConverters(TypeConverterRegistry registry) {
-        addTypeConverter(registry, java.io.File.class, org.apache.camel.component.file.GenericFile.class, false,
-            (type, exchange, value) -> org.apache.camel.component.jira.FileConverter.genericToFile((org.apache.camel.component.file.GenericFile) value, exchange));
-    }
-
-    private static void addTypeConverter(TypeConverterRegistry registry, Class<?> toType, Class<?> fromType, boolean allowNull, SimpleTypeConverter.ConversionMethod method) { 
-        registry.addTypeConverter(toType, fromType, new SimpleTypeConverter(allowNull, method));
-    }
-
-}
diff --git a/components/camel-jira/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader b/components/camel-jira/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader
deleted file mode 100644
index 49423652c19..00000000000
--- a/components/camel-jira/src/generated/resources/META-INF/services/org/apache/camel/TypeConverterLoader
+++ /dev/null
@@ -1,2 +0,0 @@
-# Generated by camel build tools - do NOT edit this file!
-org.apache.camel.component.jira.FileConverterLoader
diff --git a/components/camel-jira/src/main/java/org/apache/camel/component/jira/FileConverter.java b/components/camel-jira/src/main/java/org/apache/camel/component/jira/FileConverter.java
deleted file mode 100644
index f3d838aedea..00000000000
--- a/components/camel-jira/src/main/java/org/apache/camel/component/jira/FileConverter.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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.jira;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
-
-import org.apache.camel.Converter;
-import org.apache.camel.Exchange;
-import org.apache.camel.component.file.GenericFile;
-
-@Converter(generateLoader = true)
-public final class FileConverter {
-
-    private FileConverter() {
-    }
-
-    @Converter
-    public static File genericToFile(GenericFile<File> genericFile, Exchange exchange) throws IOException {
-        Object body = genericFile.getBody();
-        File file = null;
-        Path path;
-        if (body instanceof byte[]) {
-            byte[] bos = (byte[]) body;
-            path = Files.createTempFile(genericFile.getFileName(), null);
-            Files.write(path, bos, StandardOpenOption.CREATE);
-            path.toFile().deleteOnExit();
-        } else {
-            file = (File) body;
-        }
-        return file;
-    }
-}
diff --git a/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java b/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java
index b9184f3d7d7..e130481f76e 100644
--- a/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java
+++ b/components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/AttachFileProducer.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.jira.producer;
 
 import java.io.File;
+import java.io.InputStream;
 import java.net.URI;
 
 import com.atlassian.jira.rest.client.api.IssueRestClient;
@@ -24,6 +25,7 @@ import com.atlassian.jira.rest.client.api.JiraRestClient;
 import com.atlassian.jira.rest.client.api.domain.Issue;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.WrappedFile;
 import org.apache.camel.component.jira.JiraEndpoint;
 import org.apache.camel.support.DefaultProducer;
 
@@ -39,13 +41,35 @@ public class AttachFileProducer extends DefaultProducer {
         String issueKey = exchange.getIn().getHeader(ISSUE_KEY, String.class);
         if (issueKey == null) {
             throw new IllegalArgumentException(
-                    "Missing exchange input header named \'IssueKey\', it should specify the issue key to attach a file.");
+                    "Missing exchange input header named 'IssueKey', it should specify the issue key to attach a file.");
         }
-        File file = exchange.getIn().getMandatoryBody(File.class);
+
+        // check for java.io.File first before using input stream for file content
+        InputStream is = null;
+        String name = null;
+        File file = null;
+        Object body = exchange.getIn().getBody();
+        if (body instanceof File) {
+            file = (File) body;
+        } else {
+            WrappedFile wf = exchange.getIn().getBody(WrappedFile.class);
+            if (wf != null && wf.getFile() instanceof File) {
+                file = (File) wf.getFile();
+            }
+        }
+        if (file == null) {
+            is = exchange.getIn().getMandatoryBody(InputStream.class);
+            name = exchange.getIn().getHeader(Exchange.FILE_NAME, exchange.getMessage().getMessageId(), String.class);
+        }
+
         JiraRestClient client = ((JiraEndpoint) getEndpoint()).getClient();
         IssueRestClient issueClient = client.getIssueClient();
         Issue issue = issueClient.getIssue(issueKey).claim();
         URI attachmentsUri = issue.getAttachmentsUri();
-        issueClient.addAttachments(attachmentsUri, file);
+        if (file != null) {
+            issueClient.addAttachments(attachmentsUri, file);
+        } else {
+            issueClient.addAttachment(attachmentsUri, is, name);
+        }
     }
 }
diff --git a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerFromFileRouteTest.java
similarity index 92%
copy from components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
copy to components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerFromFileRouteTest.java
index 754795fc222..fb25a6fc381 100644
--- a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
+++ b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerFromFileRouteTest.java
@@ -21,7 +21,6 @@ import java.io.IOException;
 import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
-import java.nio.file.StandardOpenOption;
 import java.util.ArrayList;
 import java.util.Collection;
 
@@ -33,6 +32,7 @@ import com.atlassian.jira.rest.client.api.domain.Issue;
 import io.atlassian.util.concurrent.Promises;
 import org.apache.camel.CamelContext;
 import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
@@ -40,6 +40,7 @@ import org.apache.camel.component.jira.JiraComponent;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.FileUtil;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
@@ -57,7 +58,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(MockitoExtension.class)
-public class AttachFileProducerTest extends CamelTestSupport {
+public class AttachFileProducerFromFileRouteTest extends CamelTestSupport {
 
     @Mock
     private JiraRestClient jiraClient;
@@ -109,16 +110,9 @@ public class AttachFileProducerTest extends CamelTestSupport {
         });
     }
 
-    private File generateSampleFile() throws IOException {
-        File sampleRandomFile = File.createTempFile("attach-test", null);
-        sampleRandomFile.deleteOnExit();
-        String text = "A random text to use on the AttachFileProducerTest.java of camel-jira component.";
-        Files.write(sampleRandomFile.toPath(), text.getBytes(), StandardOpenOption.CREATE);
-        return sampleRandomFile;
-    }
-
     @Override
     protected CamelContext createCamelContext() throws Exception {
+        FileUtil.removeDir(new File("target/attach"));
         setMocks();
         CamelContext camelContext = super.createCamelContext();
         camelContext.disableJMX();
@@ -132,7 +126,7 @@ public class AttachFileProducerTest extends CamelTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() {
-                from("direct:start")
+                from("file:target/attach")
                         .setHeader(ISSUE_KEY, () -> KEY + "-1")
                         .to("jira://attach?jiraUrl=" + JIRA_CREDENTIALS)
                         .to(mockResult);
@@ -142,14 +136,18 @@ public class AttachFileProducerTest extends CamelTestSupport {
 
     @Test
     public void verifyAttachment() throws InterruptedException, IOException {
-        template.sendBody(generateSampleFile());
+        mockResult.expectedMessageCount(1);
+
+        String text = "A random text to use on the AttachFileProducerTest.java of camel-jira component.";
+        template.sendBodyAndHeader("file:target/attach", text, Exchange.FILE_NAME, "hello.txt");
+
+        mockResult.assertIsSatisfied();
+
         Issue retrievedIssue = issueRestClient.getIssue(issue.getKey()).claim();
         assertEquals(issue, retrievedIssue);
         // there is only one attachment
         Attachment attachFile = retrievedIssue.getAttachments().iterator().next();
         assertEquals(attachFile.getFilename(), attachedFile.getName());
         assertEquals(attachFile.getSize(), attachedFile.length());
-        mockResult.expectedMessageCount(1);
-        mockResult.assertIsSatisfied();
     }
 }
diff --git a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerInputStreamTest.java
similarity index 79%
copy from components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
copy to components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerInputStreamTest.java
index 754795fc222..745907880ae 100644
--- a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
+++ b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerInputStreamTest.java
@@ -16,12 +16,9 @@
  */
 package org.apache.camel.component.jira.producer;
 
-import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
-import java.nio.file.Files;
-import java.nio.file.StandardCopyOption;
-import java.nio.file.StandardOpenOption;
 import java.util.ArrayList;
 import java.util.Collection;
 
@@ -57,7 +54,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(MockitoExtension.class)
-public class AttachFileProducerTest extends CamelTestSupport {
+public class AttachFileProducerInputStreamTest extends CamelTestSupport {
 
     @Mock
     private JiraRestClient jiraClient;
@@ -75,7 +72,8 @@ public class AttachFileProducerTest extends CamelTestSupport {
     private MockEndpoint mockResult;
 
     private Issue issue;
-    private File attachedFile;
+    private String attachedName;
+    private int attachedSize;
 
     @Override
     protected void bindToRegistry(Registry registry) {
@@ -92,16 +90,15 @@ public class AttachFileProducerTest extends CamelTestSupport {
             }
             return Promises.promise(issue);
         });
-        when(issueRestClient.addAttachments(any(URI.class), any(File.class))).then(inv -> {
-            File attachedFileTmp = inv.getArgument(1);
-            // create a temp destiny file as the attached file is marked for removal on AttachFileProducer
-            attachedFile = File.createTempFile("camel-jira-test-", null);
-            Files.copy(attachedFileTmp.toPath(), attachedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
-            attachedFile.deleteOnExit();
+        when(issueRestClient.addAttachment(any(URI.class), any(InputStream.class), any(String.class))).then(inv -> {
+            InputStream is = inv.getArgument(1);
+            attachedName = inv.getArgument(2);
+            attachedSize = is.readAllBytes().length;
+
             Collection<Attachment> attachments = new ArrayList<>();
             attachments.add(new Attachment(
-                    issue.getAttachmentsUri(), attachedFile.getName(), null, null,
-                    Long.valueOf(attachedFile.length()).intValue(), null, null, null));
+                    issue.getAttachmentsUri(), attachedName, null, null,
+                    attachedSize, null, null, null));
             // re-create the issue with the attachment sent by the route
             issue = createIssueWithAttachment(issue.getId(), issue.getSummary(), issue.getKey(), issue.getIssueType(),
                     issue.getDescription(), issue.getPriority(), issue.getAssignee(), attachments);
@@ -109,14 +106,6 @@ public class AttachFileProducerTest extends CamelTestSupport {
         });
     }
 
-    private File generateSampleFile() throws IOException {
-        File sampleRandomFile = File.createTempFile("attach-test", null);
-        sampleRandomFile.deleteOnExit();
-        String text = "A random text to use on the AttachFileProducerTest.java of camel-jira component.";
-        Files.write(sampleRandomFile.toPath(), text.getBytes(), StandardOpenOption.CREATE);
-        return sampleRandomFile;
-    }
-
     @Override
     protected CamelContext createCamelContext() throws Exception {
         setMocks();
@@ -142,14 +131,18 @@ public class AttachFileProducerTest extends CamelTestSupport {
 
     @Test
     public void verifyAttachment() throws InterruptedException, IOException {
-        template.sendBody(generateSampleFile());
+        mockResult.expectedMessageCount(1);
+
+        String text = "A random text to use on the AttachFileProducerTest.java of camel-jira component.";
+        template.sendBody("direct:start", text);
+
+        mockResult.assertIsSatisfied();
+
         Issue retrievedIssue = issueRestClient.getIssue(issue.getKey()).claim();
         assertEquals(issue, retrievedIssue);
         // there is only one attachment
         Attachment attachFile = retrievedIssue.getAttachments().iterator().next();
-        assertEquals(attachFile.getFilename(), attachedFile.getName());
-        assertEquals(attachFile.getSize(), attachedFile.length());
-        mockResult.expectedMessageCount(1);
-        mockResult.assertIsSatisfied();
+        assertEquals(attachFile.getFilename(), attachedName);
+        assertEquals(attachFile.getSize(), attachedSize);
     }
 }
diff --git a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
index 754795fc222..3b15d3f4741 100644
--- a/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
+++ b/components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/AttachFileProducerTest.java
@@ -142,14 +142,17 @@ public class AttachFileProducerTest extends CamelTestSupport {
 
     @Test
     public void verifyAttachment() throws InterruptedException, IOException {
+        mockResult.expectedMessageCount(1);
+
         template.sendBody(generateSampleFile());
+
+        mockResult.assertIsSatisfied();
+
         Issue retrievedIssue = issueRestClient.getIssue(issue.getKey()).claim();
         assertEquals(issue, retrievedIssue);
         // there is only one attachment
         Attachment attachFile = retrievedIssue.getAttachments().iterator().next();
         assertEquals(attachFile.getFilename(), attachedFile.getName());
         assertEquals(attachFile.getSize(), attachedFile.length());
-        mockResult.expectedMessageCount(1);
-        mockResult.assertIsSatisfied();
     }
 }