davsclaus commented on code in PR #28198: URL: https://github.com/apache/camel/pull/28198#discussion_r4333294676
########## components/camel-jira/src/test/java/org/apache/camel/component/jira/consumer/NewIssuesConsumerMoreThanMaxResultsTest.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.consumer; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import com.atlassian.jira.rest.client.api.JiraRestClient; +import com.atlassian.jira.rest.client.api.JiraRestClientFactory; +import com.atlassian.jira.rest.client.api.SearchRestClient; +import com.atlassian.jira.rest.client.api.UserRestClient; +import com.atlassian.jira.rest.client.api.domain.Issue; +import com.atlassian.jira.rest.client.api.domain.SearchResult; +import com.atlassian.jira.rest.client.api.domain.User; +import io.atlassian.util.concurrent.Promises; +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +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.junit6.CamelTestSupport; +import org.joda.time.DateTime; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +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.JiraTestConstants.JIRA_CREDENTIALS; +import static org.apache.camel.component.jira.JiraTestConstants.PROJECT; +import static org.apache.camel.component.jira.Utils.createIssueWithCreationDate; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +/** + * More new issues than maxResults between two polls (a burst, or the polls after Jira was unreachable for a while): + * every one of them must be consumed, not only the newest maxResults. + */ +@ExtendWith(MockitoExtension.class) +public class NewIssuesConsumerMoreThanMaxResultsTest extends CamelTestSupport { + + private static final int NEW_ISSUES = 60; + + private final DateTime start = DateTime.now().minusHours(2); + private final Issue latestAtStart = createIssueWithCreationDate(1000, start); + // newest first, as the JQL of the consumer orders them + private final List<Issue> newIssues = new ArrayList<>(); + private final AtomicBoolean served = new AtomicBoolean(); + + @Mock + private JiraRestClient jiraClient; + + @Mock + private JiraRestClientFactory jiraRestClientFactory; + + @Mock + private SearchRestClient searchRestClient; + + @Mock + private UserRestClient userRestClient; + + @EndpointInject("mock:result") + private MockEndpoint mockResult; + + @Override + protected void bindToRegistry(Registry registry) { + registry.bind(JIRA_REST_CLIENT_FACTORY, jiraRestClientFactory); + } + + @Override + protected CamelContext createCamelContext() throws Exception { + for (int i = NEW_ISSUES; i >= 1; i--) { + newIssues.add(createIssueWithCreationDate(1000 + i, start.plusMinutes(i))); + } + User user = new User( + null, "admin", null, null, true, null, + Map.of("48x48", URI.create("")), DateTime.now().getZone().getID()); + when(jiraClient.getSearchClient()).thenReturn(searchRestClient); + when(jiraClient.getUserClient()).thenReturn(userRestClient); + when(jiraRestClientFactory.createWithBasicHttpAuthentication(any(), any(), any())).thenReturn(jiraClient); + when(userRestClient.getUser(any(URI.class))).thenReturn(Promises.promise(user)); + when(searchRestClient.searchJql(any(), any(), any(), any())).then(invocation -> { Review Comment: The mock is a plain `SearchRestClient`, so the test only covers the `startAt` paging of Jira Server/Data Center. A second case with `mock(AsynchronousCloudSearchRestClient.class)` that throws for `startAt > 0` (like the real client) and answers `totalCount(...)` would catch the Cloud problem. ########## components/camel-jira/src/main/java/org/apache/camel/component/jira/consumer/NewIssuesConsumer.java: ########## @@ -129,16 +129,19 @@ protected int doPoll() throws Exception { * @return queue of new issues */ private Queue<Issue> getNewIssues() { - String jqlFilter; + Queue<Issue> issues; // If we have processed an issue before, use its timestamp to get only the newer ones if (latestIssue != null) { - jqlFilter = String.format(NEW_ISSUES_JQL_FORMAT, getServerTimestamp(), getEndpoint().getJql()); + String jqlFilter = String.format(NEW_ISSUES_JQL_FORMAT, getServerTimestamp(), getEndpoint().getJql()); + // all of them: the newest one becomes the latest issue, so a new issue left out by maxResults would never + // be returned again; maxResults is the page size here + int maxResults = getEndpoint().getMaxResults(); + int pageSize = maxResults > 0 ? Math.min(maxResults, SEARCH_MAX_PER_QUERY) : SEARCH_MAX_PER_QUERY; + issues = getIssues(jqlFilter, SEARCH_START_AT, pageSize, Integer.MAX_VALUE); Review Comment: On Jira Cloud this loop reaches `searchJql(jql, pageSize, 50, fields)` and `AsynchronousCloudSearchRestClient` throws `UnsupportedOperationException` for a `startAt` that is not 0. The poll then fails before `latestIssue` moves, so the next polls fail the same way and nothing is routed any more (see point 1). Paging with `enhancedSearchJql(...)` and `getNextPageToken()` on Cloud in `AbstractJiraConsumer.getIssues` would fix it. -- 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]
