diff --git a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/initialReindex/ElasticsearchInitialReindexingTests.java b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/initialReindex/ElasticsearchInitialReindexingTests.java index a94760d7f5..f7ea0fb49d 100644 --- a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/initialReindex/ElasticsearchInitialReindexingTests.java +++ b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/initialReindex/ElasticsearchInitialReindexingTests.java @@ -1,9 +1,9 @@ package org.alfresco.elasticsearch.initialReindex; import static org.alfresco.elasticsearch.SearchQueryService.req; -import static org.alfresco.tas.AlfrescoStackInitializer.getImagesConfig; +import static org.alfresco.tas.AlfrescoStackInitializer.reindex; +import static org.alfresco.utility.model.FileType.TEXT_PLAIN; -import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -13,19 +13,15 @@ import org.alfresco.utility.data.DataContent; import org.alfresco.utility.data.DataSite; import org.alfresco.utility.data.DataUser; +import org.alfresco.utility.model.FileModel; +import org.alfresco.utility.model.SiteModel; import org.alfresco.utility.model.TestGroup; +import org.alfresco.utility.model.UserModel; import org.alfresco.utility.network.ServerHealth; import org.alfresco.utility.report.log.Step; -import org.apache.http.HttpHost; -import org.opensearch.client.RestClient; -import org.opensearch.client.RestHighLevelClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.startupcheck.IndefiniteWaitOneShotStartupCheckStrategy; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -37,10 +33,6 @@ public class ElasticsearchInitialReindexingTests extends AbstractTestNGSpringContextTests { - private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchInitialReindexingTests.class); - - public static final String CUSTOM_ALFRESCO_INDEX = "custom-alfresco-index"; - @Autowired private ServerHealth serverHealth; @Autowired @@ -52,11 +44,8 @@ public class ElasticsearchInitialReindexingTests extends AbstractTestNGSpringCon @Autowired protected SearchQueryService searchQueryService; - private org.alfresco.utility.model.UserModel testUser; - - private org.alfresco.utility.model.SiteModel testSite; - - private RestHighLevelClient elasticClient; + private UserModel testUser; + private SiteModel testSite; /** * Create a user and a private site and wait for these to be indexed. @@ -72,13 +61,6 @@ public void dataPreparation() testUser = dataUser.createRandomTestUser(); testSite = dataSite.usingUser(testUser).createPrivateRandomSite(); createDocument(); - - Step.STEP("create ES client"); - elasticClient = new RestHighLevelClient( - RestClient.builder(new HttpHost(AlfrescoStackInitializer.searchEngineContainer.getContainerIpAddress(), - AlfrescoStackInitializer.searchEngineContainer.getFirstMappedPort(), - "http"))); - } /** @@ -96,36 +78,14 @@ public void testReindexerIndexesSystemDocuments() // WHEN // Run reindexer against the initial documents. reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByIds", - "ELASTICSEARCH_INDEX_NAME", CUSTOM_ALFRESCO_INDEX, - "ALFRESCO_REINDEX_FROM_ID", "0", - "ALFRESCO_REINDEX_TO_ID", "1000")); + "ALFRESCO_REINDEX_FROM_ID", "0", + "ALFRESCO_REINDEX_TO_ID", "1000")); // THEN // Check system document is indexed. searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), "budget.xls"); } - /** - * Run the alfresco-elasticsearch-reindexing container. - * - * @param envParam Any environment variables to override from the defaults. - */ - private void reindex(Map envParam) - { - // Run the reindexing container. - Map env = AlfrescoStackInitializer.getReindexEnvBasic(); - env.putAll(envParam); - - try (GenericContainer reindexingComponent = new GenericContainer(getImagesConfig().getReIndexingImage()) - .withEnv(env) - .withNetwork(AlfrescoStackInitializer.network) - .withStartupCheckStrategy( - new IndefiniteWaitOneShotStartupCheckStrategy())) - { - reindexingComponent.start(); - } - } - /** * Create a document using in the test site using the test user. * @@ -136,7 +96,7 @@ private String createDocument() String documentName = "TestFile" + UUID.randomUUID() + ".txt"; dataContent.usingUser(testUser) .usingSite(testSite) - .createContent(new org.alfresco.utility.model.FileModel(documentName, org.alfresco.utility.model.FileType.TEXT_PLAIN, "content")); + .createContent(new FileModel(documentName, TEXT_PLAIN, "content")); return documentName; } } diff --git a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/reindexing/ElasticsearchReindexingTests.java b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/reindexing/ElasticsearchReindexingTests.java index 75017d3984..4b32d969b4 100644 --- a/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/reindexing/ElasticsearchReindexingTests.java +++ b/tests/tas-elasticsearch/src/test/java/org/alfresco/elasticsearch/reindexing/ElasticsearchReindexingTests.java @@ -1,7 +1,11 @@ package org.alfresco.elasticsearch.reindexing; import static org.alfresco.elasticsearch.SearchQueryService.req; -import static org.alfresco.tas.AlfrescoStackInitializer.getImagesConfig; +import static org.alfresco.tas.AlfrescoStackInitializer.CUSTOM_ALFRESCO_INDEX; +import static org.alfresco.tas.AlfrescoStackInitializer.liveIndexer; +import static org.alfresco.tas.AlfrescoStackInitializer.reindex; +import static org.alfresco.tas.AlfrescoStackInitializer.searchEngineContainer; +import static org.alfresco.utility.model.FileType.TEXT_PLAIN; import static org.alfresco.utility.report.log.Step.STEP; import static org.junit.Assert.fail; @@ -18,7 +22,10 @@ import org.alfresco.utility.data.DataContent; import org.alfresco.utility.data.DataSite; import org.alfresco.utility.data.DataUser; +import org.alfresco.utility.model.FileModel; +import org.alfresco.utility.model.SiteModel; import org.alfresco.utility.model.TestGroup; +import org.alfresco.utility.model.UserModel; import org.alfresco.utility.network.ServerHealth; import org.apache.http.HttpHost; import org.opensearch.client.RequestOptions; @@ -30,8 +37,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.startupcheck.IndefiniteWaitOneShotStartupCheckStrategy; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -40,11 +45,9 @@ */ @ContextConfiguration(locations = "classpath:alfresco-elasticsearch-context.xml", initializers = AlfrescoStackInitializer.class) -@SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") // these are testng tests +@SuppressWarnings({"PMD.JUnit4TestShouldUseTestAnnotation", "PMD.JUnitTestsShouldIncludeAssert"}) // these are testng tests public class ElasticsearchReindexingTests extends AbstractTestNGSpringContextTests { - public static final String CUSTOM_ALFRESCO_INDEX = "custom-alfresco-index"; - @Autowired private ServerHealth serverHealth; @Autowired @@ -56,15 +59,10 @@ public class ElasticsearchReindexingTests extends AbstractTestNGSpringContextTes @Autowired protected SearchQueryService searchQueryService; - private org.alfresco.utility.model.UserModel testUser; - - private org.alfresco.utility.model.SiteModel testSite; - + private UserModel testUser; + private SiteModel testSite; private RestHighLevelClient elasticClient; - /** - * Create a user and a private site and wait for these to be indexed. - */ @BeforeClass(alwaysRun = true) public void dataPreparation() { @@ -75,284 +73,286 @@ public void dataPreparation() testUser = dataUser.createRandomTestUser(); testSite = dataSite.usingUser(testUser).createPrivateRandomSite(); - createDocument(); + createDocumentWithRandomName(); STEP("create ES client"); elasticClient = new RestHighLevelClient( - RestClient.builder(new HttpHost(AlfrescoStackInitializer.searchEngineContainer.getContainerIpAddress(), - AlfrescoStackInitializer.searchEngineContainer.getFirstMappedPort(), + RestClient.builder(new HttpHost(searchEngineContainer.getContainerIpAddress(), + searchEngineContainer.getFirstMappedPort(), "http"))); - } @Test(groups = TestGroup.SEARCH) public void testReindexerFixesBrokenIndex() { // GIVEN - - // Delete all documents inside Elasticsearch. cleanUpIndex(); - //stop live indexing - AlfrescoStackInitializer.liveIndexer.stop(); - // Create document. - - String testStart = DateTimeFormatter.ofPattern("yyyyMMddHHmm").format(ZonedDateTime.now(Clock.systemUTC())); - String documentName = createDocument(); + liveIndexer.stop(); + String reindexerStartTime = getReindexerStartTimeNow(); + String documentName = createDocumentWithRandomName(); // Check document not indexed. // Nb. The cm:name:* term ensures that the query hits the index rather than the db. - SearchRequest query = req("cm:name:" + documentName + " AND cm:name:*"); searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser()); // WHEN // Run reindexer (leaving ALFRESCO_REINDEX_TO_TIME as default). - try(GenericContainer reindexingComponent = createReindexContainer(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", - "ELASTICSEARCH_INDEX_NAME", CUSTOM_ALFRESCO_INDEX, - "ALFRESCO_REINDEX_FROM_TIME", testStart))) { - //Reindex - reindexingComponent.start(); - - // THEN - // Check document indexed. - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - } + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime)); + + // THEN + // Check document indexed. + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); + // TIDY - // Restart ElasticsearchConnector. cleanUpIndex(); - AlfrescoStackInitializer.liveIndexer.start(); - + liveIndexer.start(); } @Test(groups = TestGroup.SEARCH) public void testRecreateIndex() { // GIVEN - // Create document. - String documentName = createDocument(); - // Stop ElasticsearchConnector. - AlfrescoStackInitializer.liveIndexer.stop(); - // Delete index documents. + String documentName = createDocumentWithRandomName(); + liveIndexer.stop(); cleanUpIndex(); // WHEN // Run reindexer (with default dates to reindex everything). - try(GenericContainer reindexingComponent = createReindexContainer(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", - "ELASTICSEARCH_INDEX_NAME", CUSTOM_ALFRESCO_INDEX))) { - //Reindex - reindexingComponent.start(); - - // THEN - // Check document indexed. - // Nb. The cm:name:* term ensures that the query hits the index rather than the db. - SearchRequest query = req("cm:name:" + documentName + " AND cm:name:*"); - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - } + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate")); + + // THEN + // Check document indexed. + // Nb. The cm:name:* term ensures that the query hits the index rather than the db. + SearchRequest query = req("cm:name:" + documentName + " AND cm:name:*"); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); // TIDY // Restart ElasticsearchConnector. cleanUpIndex(); - AlfrescoStackInitializer.liveIndexer.start(); - + liveIndexer.start(); } - /** - * Common testing method for reindexing enabled and disabled features tests. - * @param metadataIndexingEnabled Reindexing metadata is enabled when true, disabled when false - * @param contentIndexingEnabled Reindexing content is enabled when true, disabled when false - * @param pathIndexingEnabled Reindexing path is enabled when true, disabled when false - * @param queryString Verification query string. It may include a mark that is replaced by the actual document name created. - * @param expectingDocNameAsResult Result from verification query string is the name of the document created when true, empty result when false. - */ - private void internalTestEnabledFeatures( - Boolean metadataIndexingEnabled, - Boolean contentIndexingEnabled, - Boolean pathIndexingEnabled, - String queryString, - Boolean expectingDocNameAsResult - ) + @Test(groups = TestGroup.SEARCH) + public void testRecreateIndexWithMetadataAndContent() { - // Initial timestamp for reindexing by date: this will save reindexing time for these tests - ZonedDateTime now = ZonedDateTime.now(Clock.systemUTC()); - // ACS-5044 Increased time to 20 minutes as 10 minutes proved insufficient to prevent intermittent failures - String testStart = DateTimeFormatter.ofPattern("yyyyMMddHHmm").format(now.minusMinutes(20)); - // GIVEN - // Stop ElasticsearchConnector - AlfrescoStackInitializer.liveIndexer.stop(); - // Create document - String documentName = createDocument(); - // Delete index documents + String reindexerStartTime = getReindexerStartTimeTwentyMinutesAgo(); + liveIndexer.stop(); + String documentName = createDocumentWithRandomName(); cleanUpIndex(); - // Restart ElasticsearchConnector to Index Content - AlfrescoStackInitializer.liveIndexer.start(); + // Reindexer requires lifeIndexer to index content. + liveIndexer.start(); // WHEN // Run reindexer leaving ALFRESCO_REINDEX_TO_TIME as default - try(GenericContainer reindexingComponent = createReindexContainer(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", - "ELASTICSEARCH_INDEX_NAME", CUSTOM_ALFRESCO_INDEX, - "ALFRESCO_REINDEX_FROM_TIME", testStart, - "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", metadataIndexingEnabled.toString(), - "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", contentIndexingEnabled.toString(), - "ALFRESCO_REINDEX_PATHINDEXINGENABLED", pathIndexingEnabled.toString()))) { - //Reindex - reindexingComponent.start(); - - // THEN - SearchRequest query = req(queryString.replace("", documentName)); - - if (expectingDocNameAsResult) { - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - } else { - searchQueryService.expectNoResultsFromQuery(query, dataUser.getAdminUser()); - } - } - - } - - @Test(groups = TestGroup.SEARCH) - public void testRecreateIndexWithMetadataAndContent() - { - internalTestEnabledFeatures(true, true, false, - "cm:name:'' AND TEXT:'content'", true); + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime, + "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_PATHINDEXINGENABLED", "false")); + + // THEN + // Document is still indexed after reindexing. + SearchRequest query = req("cm:name:'" + documentName + "' AND TEXT:'content'"); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); } @Test(groups = TestGroup.SEARCH) public void testRecreateIndexWithMetadataAndNoContent() { - internalTestEnabledFeatures(true, false, false, - "cm:name:'' AND TEXT:'content'", false); + // GIVEN + String reindexerStartTime = getReindexerStartTimeTwentyMinutesAgo(); + liveIndexer.stop(); + String documentName = createDocumentWithRandomName(); + cleanUpIndex(); + + // WHEN + // Run reindexer leaving ALFRESCO_REINDEX_TO_TIME as default + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime, + "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", "false", + "ALFRESCO_REINDEX_PATHINDEXINGENABLED", "false")); + + // THEN + SearchRequest query = req("cm:name:'" + documentName + "' AND TEXT:'content'"); + searchQueryService.expectNoResultsFromQuery(query, dataUser.getAdminUser()); } @Test(groups = TestGroup.SEARCH) public void testRecreateIndexWithNoMetadataAndContent() { + // GIVEN + String reindexerStartTime = getReindexerStartTimeTwentyMinutesAgo(); + liveIndexer.stop(); + String documentName = createDocumentWithRandomName(); + cleanUpIndex(); + // Reindexer requires lifeIndexer to index content. + liveIndexer.start(); + + // WHEN + // Run reindexer leaving ALFRESCO_REINDEX_TO_TIME as default + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime, + "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", "false", + "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_PATHINDEXINGENABLED", "false")); + + // THEN // When not using metadata, document shouldn't be present in Elasticsearch index, // since metadata reindexing process is indexing also permissions - internalTestEnabledFeatures(false, true, false, - "cm:name:'' AND cm:name:*", false); + SearchRequest query = req("cm:name:'" + documentName + "' AND cm:name:*"); + searchQueryService.expectNoResultsFromQuery(query, dataUser.getAdminUser()); } @Test(groups = TestGroup.SEARCH) public void testRecreateIndexWithMetadataAndNoContentAndPath() { - internalTestEnabledFeatures(true, false, true, - "cm:name:'' AND PATH:'/app:company_home/st:sites/cm:" + testSite + "/cm:documentLibrary/cm:'", true); + // GIVEN + String reindexerStartTime = getReindexerStartTimeTwentyMinutesAgo(); + liveIndexer.stop(); + String documentName = createDocumentWithRandomName(); + cleanUpIndex(); + + // WHEN + // Run reindexer leaving ALFRESCO_REINDEX_TO_TIME as default + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime, + "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", "false", + "ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true")); + + // THEN + SearchRequest query = req("cm:name:'%s' AND PATH:'/app:company_home/st:sites/cm:%s/cm:documentLibrary/cm:%s'".formatted(documentName, testSite, documentName)); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); } @Test(groups = TestGroup.SEARCH) public void testRecreateIndexWithMetadataAndContentAndPath() { - internalTestEnabledFeatures(true, true, true, - "cm:name:'' AND TEXT:'content' " + - "AND PATH:'/app:company_home/st:sites/cm:" + testSite + "/cm:documentLibrary/cm:'", true); + // GIVEN + String reindexerStartTime = getReindexerStartTimeTwentyMinutesAgo(); + liveIndexer.stop(); + String documentName = createDocumentWithRandomName(); + cleanUpIndex(); + // Reindexer requires lifeIndexer to index content. + liveIndexer.start(); + + // WHEN + // Run reindexer leaving ALFRESCO_REINDEX_TO_TIME as default + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime, + "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true")); + + // THEN + SearchRequest query = req("cm:name:'%s' AND TEXT:'content' AND PATH:'/app:company_home/st:sites/cm:%s/cm:documentLibrary/cm:%s'".formatted(documentName, testSite, documentName)); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); } @Test(groups = TestGroup.SEARCH) public void testRecreateIndexWithNoMetadataAndPath() { + // GIVEN + String reindexerStartTime = getReindexerStartTimeTwentyMinutesAgo(); + liveIndexer.stop(); + String documentName = createDocumentWithRandomName(); + cleanUpIndex(); + + // WHEN + // Run reindexer leaving ALFRESCO_REINDEX_TO_TIME as default + reindex(Map.of("ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", + "ALFRESCO_REINDEX_FROM_TIME", reindexerStartTime, + "ALFRESCO_REINDEX_METADATAINDEXINGENABLED", "false", + "ALFRESCO_REINDEX_CONTENTINDEXINGENABLED", "false", + "ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true")); + + // THEN // When not using metadata, document shouldn't be present in Elasticsearch index, // since metadata reindexing process is indexing also permissions - internalTestEnabledFeatures(false, false, true, - "cm:name:'' AND cm:name:*", false); + SearchRequest query = req("cm:name:'" + documentName + "' AND cm:name:*"); + searchQueryService.expectNoResultsFromQuery(query, dataUser.getAdminUser()); } @Test (groups = TestGroup.SEARCH) public void testPathReindex() { // GIVEN - // Create document. - String documentName = createDocument(); - // Stop ElasticsearchConnector. - AlfrescoStackInitializer.liveIndexer.stop(); - // Delete index documents. + String documentName = createDocumentWithRandomName(); + liveIndexer.stop(); cleanUpIndex(); // WHEN // Run reindexer with path indexing enabled (and with default dates to reindex everything). - try(GenericContainer reindexingComponent = createReindexContainer(Map.of("ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true", - "ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", - "ELASTICSEARCH_INDEX_NAME", CUSTOM_ALFRESCO_INDEX))) { - //Reindex - reindexingComponent.start(); - - // THEN - // Check path indexed. - // Nb. The cm:name:* term ensures that the query hits the index rather than the db. - SearchRequest query = req("PATH:\"//" + documentName + "\" AND cm:name:*"); - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - // Also check that the document can be obtained by a path query against the site. - query = req("PATH:\"//" + testSite.getTitle() + "/documentLibrary/*\" AND cm:name:" + documentName + " AND cm:name:*"); - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - } + reindex(Map.of("ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_JOB_NAME", "reindexByDate")); + + // THEN + // Check path indexed. + // Nb. The cm:name:* term ensures that the query hits the index rather than the db. + SearchRequest query = req("PATH:\"//" + documentName + "\" AND cm:name:*"); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); + // Also check that the document can be obtained by a path query against the site. + query = req("PATH:\"//" + testSite.getTitle() + "/documentLibrary/*\" AND cm:name:" + documentName + " AND cm:name:*"); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); // TIDY - // Restart ElasticsearchConnector. - AlfrescoStackInitializer.liveIndexer.start(); + liveIndexer.start(); } @Test (groups = TestGroup.SEARCH) public void testPathReindexQueryWithNamespaces() { // GIVEN - // Create document. - String documentName = createDocument(); - // Stop ElasticsearchConnector. - AlfrescoStackInitializer.liveIndexer.stop(); - // Delete index documents. + String documentName = createDocumentWithRandomName(); + liveIndexer.stop(); cleanUpIndex(); // WHEN // Run reindexer with path indexing enabled (and with default dates to reindex everything). - try(GenericContainer reindexingComponent = createReindexContainer(Map.of("ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true", - "ALFRESCO_REINDEX_JOB_NAME", "reindexByDate", - "ELASTICSEARCH_INDEX_NAME", CUSTOM_ALFRESCO_INDEX))) { - //Reindex - reindexingComponent.start(); - - // THEN - // Check path indexed. - // Nb. The cm:name:* term ensures that the query hits the index rather than the db. - SearchRequest query = req("PATH:\"//cm:" + documentName + "\" AND cm:name:*"); - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - // Also check that the document can be obtained by a path query against the site. - query = req("PATH:\"//cm:" + testSite.getTitle() + "/cm:documentLibrary/*\" AND cm:name:" + documentName + " AND cm:name:*"); - searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); - } + reindex(Map.of("ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true", + "ALFRESCO_REINDEX_JOB_NAME", "reindexByDate")); + + // THEN + // Check path indexed. + // Nb. The cm:name:* term ensures that the query hits the index rather than the db. + SearchRequest query = req("PATH:\"//cm:" + documentName + "\" AND cm:name:*"); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); + // Also check that the document can be obtained by a path query against the site. + query = req("PATH:\"//cm:" + testSite.getTitle() + "/cm:documentLibrary/*\" AND cm:name:" + documentName + " AND cm:name:*"); + searchQueryService.expectResultsFromQuery(query, dataUser.getAdminUser(), documentName); // TIDY - // Restart ElasticsearchConnector. - AlfrescoStackInitializer.liveIndexer.start(); + liveIndexer.start(); } - /** - * Run the alfresco-elasticsearch-reindexing container. - * - * @param envParam Any environment variables to override from the defaults. - * @return reindex container - */ - private GenericContainer createReindexContainer(Map envParam) + private String getReindexerStartTimeNow() { - // Run the reindexing container. - Map env = AlfrescoStackInitializer.getReindexEnvBasic(); - env.putAll(envParam); - - return new GenericContainer(getImagesConfig().getReIndexingImage()) - .withEnv(env) - .withNetwork(AlfrescoStackInitializer.network) - .withStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy()); + ZonedDateTime now = ZonedDateTime.now(Clock.systemUTC()); + return formatTimeForReindexer(now); } - /** - * Create a document using in the test site using the test user. - * - * @return The randomly generated name of the new document. - */ - private String createDocument() + private String getReindexerStartTimeTwentyMinutesAgo() + { + // Initial timestamp for reindexing by date: this will save reindexing time for these tests + ZonedDateTime now = ZonedDateTime.now(Clock.systemUTC()); + // ACS-5044 Increased time to 20 minutes as 10 minutes proved insufficient to prevent intermittent failures + return formatTimeForReindexer(now.minusMinutes(20)); + } + + private String formatTimeForReindexer(ZonedDateTime zonedDateTime) + { + return DateTimeFormatter.ofPattern("yyyyMMddHHmm").format(zonedDateTime); + } + + private String createDocumentWithRandomName() { String documentName = "TestFile" + UUID.randomUUID() + ".txt"; dataContent.usingUser(testUser) .usingSite(testSite) - .createContent(new org.alfresco.utility.model.FileModel(documentName, org.alfresco.utility.model.FileType.TEXT_PLAIN, "content")); + .createContent(new FileModel(documentName, TEXT_PLAIN, "content")); return documentName; } @@ -370,5 +370,4 @@ private void cleanUpIndex() fail("Failed to tidy index. " + e); } } - } diff --git a/tests/testcontainers-env/src/main/java/org/alfresco/tas/AlfrescoStackInitializer.java b/tests/testcontainers-env/src/main/java/org/alfresco/tas/AlfrescoStackInitializer.java index 0e00d01dcd..f77887535d 100644 --- a/tests/testcontainers-env/src/main/java/org/alfresco/tas/AlfrescoStackInitializer.java +++ b/tests/testcontainers-env/src/main/java/org/alfresco/tas/AlfrescoStackInitializer.java @@ -148,11 +148,21 @@ public void configureSecuritySettings(GenericContainer searchEngineContainer) * Run the alfresco-elasticsearch-reindexing container with path reindexing enabled. */ public static void reindexEverything() + { + reindex(Map.of("ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true", // Ensure path reindexing is enabled. + "ALFRESCO_REINDEX_JOB_NAME", "reindexByDate")); + } + + /** + * Run the alfresco-elasticsearch-reindexing container. + * + * @param envParam Any environment variables to override from the defaults. + */ + public static void reindex(Map envParam) { // Run the reindexing container. Map env = AlfrescoStackInitializer.getReindexEnvBasic(); - env.putAll(Map.of("ALFRESCO_REINDEX_PATHINDEXINGENABLED", "true", // Ensure path reindexing is enabled. - "ALFRESCO_REINDEX_JOB_NAME", "reindexByDate")); + env.putAll(envParam); try (GenericContainer reindexingComponent = new GenericContainer(getImagesConfig().getReIndexingImage()) .withEnv(env)