dan-s1 commented on code in PR #7537: URL: https://github.com/apache/nifi/pull/7537#discussion_r1284651843
########## nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-restapi-processors/src/test/java/org/apache/nifi/processors/elasticsearch/SearchElasticsearchTest.java: ########## @@ -0,0 +1,243 @@ +/* + * 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.nifi.processors.elasticsearch; + +import org.apache.nifi.components.state.Scope; +import org.apache.nifi.processors.elasticsearch.api.PaginationType; +import org.apache.nifi.processors.elasticsearch.api.ResultOutputStrategy; +import org.apache.nifi.state.MockStateManager; +import org.apache.nifi.util.TestRunner; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.time.Instant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +public class SearchElasticsearchTest extends AbstractPaginatedJsonQueryElasticsearchTest { + private static final String MATCH_ALL_WITH_SORT_BY_MSG_WITH_SIZE_QUERY; + + static { + try { + MATCH_ALL_WITH_SORT_BY_MSG_WITH_SIZE_QUERY = Files.readString(Paths.get("src/test/resources/AbstractPaginatedJsonQueryElasticsearchTest/matchAllWithSortByMsgQueryWithSize.json")); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public AbstractPaginatedJsonQueryElasticsearch getProcessor() { + return new SearchElasticsearch(); + } + + public boolean isStateUsed() { + return true; + } + + public boolean isInput() { + return false; + } + + @Test + public void testScrollError() { + final TestRunner runner = createRunner(false); + final TestElasticsearchClientService service = AbstractJsonQueryElasticsearchTest.getService(runner); + service.setMaxPages(2); + service.setThrowErrorInSearch(false); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, PaginationType.SCROLL.getValue()); + runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, MATCH_ALL_WITH_SORT_BY_MSG_WITH_SIZE_QUERY); + + // initialize search + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.clearTransferState(); + + // scroll (error) + service.setThrowErrorInSearch(true); + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 0, 0, 0); + assertTrue(runner.getLogger().getErrorMessages().stream().anyMatch(logMessage -> + logMessage.getMsg().contains("Could not query documents") && logMessage.getThrowable().getMessage().contains("Simulated IOException - scroll"))); + } + + @Test + public void testScrollExpiration() throws Exception { + testPaginationExpiration(PaginationType.SCROLL); + } + + @Test + public void testPitExpiration() throws Exception { + testPaginationExpiration(PaginationType.POINT_IN_TIME); + } + + @Test + public void testSearchAfterExpiration() throws Exception { + testPaginationExpiration(PaginationType.SEARCH_AFTER); + } + + private void testPaginationExpiration(final PaginationType paginationType) throws Exception{ + // test flowfile per page + final TestRunner runner = createRunner(false); + final TestElasticsearchClientService service = AbstractJsonQueryElasticsearchTest.getService(runner); + service.setMaxPages(2); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue()); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_KEEP_ALIVE, "1 sec"); + runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, MATCH_ALL_WITH_SORT_BY_MSG_WITH_SIZE_QUERY); + + // first page + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1"); + assertState(runner.getStateManager(), paginationType, 10, 1); + + // wait for expiration + final Instant expiration = Instant.ofEpochMilli(Long.parseLong(runner.getStateManager().getState(Scope.LOCAL).get(SearchElasticsearch.STATE_PAGE_EXPIRATION_TIMESTAMP))); + while (expiration.isAfter(Instant.now())) { + Thread.sleep(10); + } + + if ("true".equalsIgnoreCase(System.getenv("CI"))) { + // allow extra time if running in CI Pipeline to prevent intermittent timing-issue failures + Thread.sleep(1000); + } + + service.resetPageCount(); + runner.clearTransferState(); + + // first page again (new query after first query expired) + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1"); + assertState(runner.getStateManager(), paginationType, 10, 1); + runner.clearTransferState(); + + // second page + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "2"); + assertState(runner.getStateManager(), paginationType, 20, 2); + runner.clearTransferState(); + } + + @Override + public void testPagination(final PaginationType paginationType) throws Exception{ + // test flowfile per page + final TestRunner runner = createRunner(false); + final TestElasticsearchClientService service = AbstractJsonQueryElasticsearchTest.getService(runner); + service.setMaxPages(2); + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.PAGINATION_TYPE, paginationType.getValue()); + runner.setProperty(AbstractJsonQueryElasticsearch.QUERY, MATCH_ALL_WITH_SORT_BY_MSG_WITH_SIZE_QUERY); + + // first page + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "1"); + assertState(runner.getStateManager(), paginationType, 10, 1); + runner.clearTransferState(); + + // second page + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 1, 0, 0); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("hit.count", "10"); + runner.getFlowFilesForRelationship(AbstractJsonQueryElasticsearch.REL_HITS).get(0).assertAttributeEquals("page.number", "2"); + assertState(runner.getStateManager(), paginationType, 20, 2); + runner.clearTransferState(); + + // third page - no hits + runOnce(runner); + AbstractJsonQueryElasticsearchTest.testCounts(runner, 0, 0, 0, 0); + assertTrue(runner.getStateManager().getState(Scope.LOCAL).toMap().isEmpty()); + reset(runner); + + + // test hits splitting + runner.setProperty(AbstractPaginatedJsonQueryElasticsearch.SEARCH_RESULTS_SPLIT, ResultOutputStrategy.PER_HIT.getValue()); Review Comment: `testPagination` is not an actual unit test. It is an overridden method. It is called in the parent's `AbstractPaginatedJsonQueryElasticsearchTest` unit tests 1. `testScroll` 2. `testPit` 3. `testSearchAfter` -- 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: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org