gnodet-bot commented on code in PR #27692:
URL: https://github.com/apache/camel/pull/27692#discussion_r4243837630


##########
components/camel-jira/src/main/java/org/apache/camel/component/jira/producer/WatcherProducer.java:
##########
@@ -63,7 +64,7 @@ public void process(Exchange exchange) {
                 for (String s : watArr) {
                     String watcher = s.trim();
                     if (!watcher.isEmpty()) {
-                        
issueClient.removeWatcher(issue.getWatchers().getSelf(), watcher);
+                        
issueClient.removeWatcher(issue.getWatchers().getSelf(), watcher).claim();

Review Comment:
   💡 **Consistency:** The `addWatcher` block above has a `// wait for the 
result...` comment before its `.claim()` call explaining why. This 
`removeWatcher` call has the same intent but no comment. Add the same comment 
here for consistency:
   
   ```suggestion
                           // wait for the result, so that a request that Jira 
rejects fails the exchange
                           
issueClient.removeWatcher(issue.getWatchers().getSelf(), watcher).claim();
   ```



##########
components/camel-jira/src/test/java/org/apache/camel/component/jira/producer/JiraProducerRejectedRequestTest.java:
##########
@@ -0,0 +1,218 @@
+/*
+ * 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.producer;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.atlassian.jira.rest.client.api.IssueRestClient;
+import com.atlassian.jira.rest.client.api.JiraRestClient;
+import com.atlassian.jira.rest.client.api.JiraRestClientFactory;
+import com.atlassian.jira.rest.client.api.RestClientException;
+import com.atlassian.jira.rest.client.api.domain.BasicIssue;
+import com.atlassian.jira.rest.client.api.domain.BasicWatchers;
+import com.atlassian.jira.rest.client.api.domain.Comment;
+import com.atlassian.jira.rest.client.api.domain.Issue;
+import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
+import com.atlassian.jira.rest.client.api.domain.input.LinkIssuesInput;
+import com.atlassian.jira.rest.client.api.domain.input.TransitionInput;
+import com.atlassian.jira.rest.client.api.domain.input.WorklogInput;
+import io.atlassian.util.concurrent.Promise;
+import io.atlassian.util.concurrent.Promises;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jira.JiraComponent;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.apache.camel.component.jira.JiraConstants.CHILD_ISSUE_KEY;
+import static org.apache.camel.component.jira.JiraConstants.ISSUE_KEY;
+import static org.apache.camel.component.jira.JiraConstants.ISSUE_PROJECT_KEY;
+import static org.apache.camel.component.jira.JiraConstants.ISSUE_SUMMARY;
+import static 
org.apache.camel.component.jira.JiraConstants.ISSUE_TRANSITION_ID;
+import static org.apache.camel.component.jira.JiraConstants.ISSUE_TYPE_ID;
+import static org.apache.camel.component.jira.JiraConstants.ISSUE_WATCHERS_ADD;
+import static 
org.apache.camel.component.jira.JiraConstants.ISSUE_WATCHERS_REMOVE;
+import static org.apache.camel.component.jira.JiraConstants.JIRA;
+import static 
org.apache.camel.component.jira.JiraConstants.JIRA_REST_CLIENT_FACTORY;
+import static org.apache.camel.component.jira.JiraConstants.LINK_TYPE;
+import static org.apache.camel.component.jira.JiraConstants.MINUTES_SPENT;
+import static org.apache.camel.component.jira.JiraConstants.PARENT_ISSUE_KEY;
+import static 
org.apache.camel.component.jira.JiraTestConstants.JIRA_CREDENTIALS;
+import static org.apache.camel.component.jira.JiraTestConstants.KEY;
+import static org.apache.camel.component.jira.JiraTestConstants.TEST_JIRA_URL;
+import static org.apache.camel.component.jira.Utils.createIssue;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.lenient;
+
+/**
+ * The Jira client is asynchronous: a request that Jira rejects (no 
permission, an invalid transition, a missing field)
+ * fails its promise. The producers must wait for the promise, so that the 
exchange fails too.
+ */
+@ExtendWith(MockitoExtension.class)
+class JiraProducerRejectedRequestTest extends CamelTestSupport {
+
+    private static final String ISSUE = KEY + "-11";
+
+    private final RestClientException rejection
+            = new RestClientException("Jira rejected the request", new 
IllegalStateException("HTTP 400"));
+
+    private Issue issue;
+
+    @Mock
+    private JiraRestClient jiraClient;
+    @Mock
+    private JiraRestClientFactory jiraRestClientFactory;
+    @Mock
+    private IssueRestClient issueRestClient;
+
+    @Override
+    protected void bindToRegistry(Registry registry) {
+        registry.bind(JIRA_REST_CLIENT_FACTORY, jiraRestClientFactory);
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        URI watchersUri = URI.create(TEST_JIRA_URL + "/rest/api/2/issue/" + 
ISSUE + "/watchers");
+        issue = createIssue(11L, "Test issue", ISSUE, null, null, null, null, 
null,
+                new BasicWatchers(watchersUri, true, 0));
+        Promise<Void> rejected = Promises.rejected(rejection);
+
+        
lenient().when(jiraRestClientFactory.createWithBasicHttpAuthentication(any(), 
any(), any())).thenReturn(jiraClient);
+        
lenient().when(jiraClient.getIssueClient()).thenReturn(issueRestClient);
+        // reading and creating an issue work
+        
lenient().when(issueRestClient.getIssue(anyString())).thenReturn(Promises.promise(issue));
+        lenient().when(issueRestClient.createIssue(any(IssueInput.class)))
+                .thenReturn(Promises.promise(new BasicIssue(issue.getSelf(), 
ISSUE, 11L)));
+        // every request that changes the issue is rejected
+        lenient().when(issueRestClient.addComment(any(), 
any(Comment.class))).thenReturn(rejected);
+        
lenient().when(issueRestClient.linkIssue(any(LinkIssuesInput.class))).thenReturn(rejected);
+        lenient().when(issueRestClient.addWorklog(any(), 
any(WorklogInput.class))).thenReturn(rejected);
+        lenient().when(issueRestClient.addAttachments(any(), 
any(File.class))).thenReturn(rejected);
+        lenient().when(issueRestClient.addAttachment(any(), 
any(InputStream.class), anyString())).thenReturn(rejected);
+        lenient().when(issueRestClient.transition(any(Issue.class), 
any(TransitionInput.class))).thenReturn(rejected);
+        lenient().when(issueRestClient.addWatcher(any(), 
anyString())).thenReturn(rejected);
+        lenient().when(issueRestClient.removeWatcher(any(), 
anyString())).thenReturn(rejected);
+
+        CamelContext camelContext = super.createCamelContext();
+        camelContext.disableJMX();
+        camelContext.addComponent(JIRA, new JiraComponent(camelContext));
+        return camelContext;
+    }
+
+    @Test
+    void addCommentFails() {
+        assertRejected("addComment", "A comment", Map.of(ISSUE_KEY, ISSUE));
+    }
+
+    @Test
+    void addIssueLinkFails() {
+        assertRejected("addIssueLink", null,
+                Map.of(PARENT_ISSUE_KEY, ISSUE, CHILD_ISSUE_KEY, KEY + "-12", 
LINK_TYPE, "Relates"));
+    }
+
+    @Test
+    void addWorkLogFails() {
+        assertRejected("addWorkLog", "A comment", Map.of(ISSUE_KEY, ISSUE, 
MINUTES_SPENT, 10));
+    }
+
+    @Test
+    void attachAFileFails(@TempDir Path dir) throws Exception {
+        File file = Files.writeString(dir.resolve("attachment.txt"), 
"content").toFile();
+
+        assertRejected("attach", file, Map.of(ISSUE_KEY, ISSUE));
+    }
+
+    @Test
+    void attachAStreamFails() {
+        InputStream body = new 
ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8));
+
+        assertRejected("attach", body, Map.of(ISSUE_KEY, ISSUE, 
Exchange.FILE_NAME, "attachment.txt"));
+    }
+
+    @Test
+    void transitionIssueFails() {
+        assertRejected("transitionIssue", null, Map.of(ISSUE_KEY, ISSUE, 
ISSUE_TRANSITION_ID, 3));

Review Comment:
   💡 **Missing test case:** `TransitionIssueProducer` makes two sequential 
`.claim()` calls — one for the transition and one for the optional comment 
(when `ISSUE_COMMENT` header is set). `assertRejected()` mocks all operations 
as `rejected`, so the first `.claim()` throws immediately and the second is 
never reached. Consider adding a test for the partial-failure path:
   
   ```java
   @Test
   void transitionIssueSucceedsButCommentFails() {
       // mock transition to succeed, comment to be rejected
       assertRejected("transitionIssue", null,
               Map.of(ISSUE_KEY, ISSUE, ISSUE_TRANSITION_ID, 3, ISSUE_COMMENT, 
"A comment"));
       // (needs a more targeted mock setup where transition resolves but 
addComment rejects)
   }
   ```
   This path is reachable in production (e.g. permission to transition but not 
to comment) and is worth testing given the multi-step nature of the producer.



-- 
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