diff --git a/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java b/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java index 84566dee499..b01507b9fc1 100644 --- a/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java +++ b/Kitodo-DataManagement/src/main/java/org/kitodo/data/database/beans/User.java @@ -360,6 +360,11 @@ public void setConfigProductionDateShow(boolean configProductionDateShow) { this.configProductionDateShow = configProductionDateShow; } + /** + * Get Metadata language. + * + * @return metadata language as String + */ public String getMetadataLanguage() { if (Objects.isNull(this.metadataLanguage)) { return ""; diff --git a/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/KitodoRestClient.java b/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/KitodoRestClient.java index bb7f34b3b40..2d5a9d5cf13 100644 --- a/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/KitodoRestClient.java +++ b/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/KitodoRestClient.java @@ -207,6 +207,14 @@ public boolean typeIndexesExist() throws IOException, CustomResponseException { return true; } + /** + * Delete index of given mappingType. Currently, only used in test cases. + * @param mappingType mapping type + */ + public void deleteIndex(String mappingType) throws IOException { + client.performRequest(new Request(HttpMethod.DELETE, "/" + indexBase + "_" + mappingType)); + } + /** * Delete all indexes. Used for cleaning after tests! */ diff --git a/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/index/IndexRestClient.java b/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/index/IndexRestClient.java index f70d8e1373a..c6e0b7f2460 100644 --- a/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/index/IndexRestClient.java +++ b/Kitodo-DataManagement/src/main/java/org/kitodo/data/elasticsearch/index/IndexRestClient.java @@ -175,14 +175,16 @@ void deleteDocument(String type, Integer id, boolean forceRefresh) throws Custom * * @param field * as String + * @param mappingType + * as String */ - public void enableSortingByTextField(String field) throws IOException, CustomResponseException { + public void enableSortingByTextField(String field, String mappingType) throws IOException, CustomResponseException { String query = "{\n \"properties\": {\n\"" + field + "\": {\n" + " \"type\": \"text\",\n" + " \"fielddata\": true,\n" + " \"fields\": {\n" + " \"raw\": {\n" + " \"type\": \"text\",\n" + " \"index\": false}\n" + " }\n" + " }}}"; HttpEntity entity = new NStringEntity(query, ContentType.APPLICATION_JSON); Request request = new Request(HttpMethod.PUT, - "/" + this.getIndexBase() + "/_mappings"); + "/" + this.getIndexBase() + "_" + mappingType + "/_mappings"); request.setEntity(entity); Response indexResponse = client.performRequest(request); processStatusCode(indexResponse.getStatusLine()); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/ExtendedNode.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/ExtendedNode.java index 721298217ee..7cdfebe5e4b 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/ExtendedNode.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/ExtendedNode.java @@ -11,7 +11,10 @@ package org.kitodo.data.elasticsearch; +import java.nio.file.Paths; import java.util.Collection; +import java.util.Collections; +import java.util.function.Supplier; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.node.InternalSettingsPreparer; @@ -23,8 +26,9 @@ * client in in-memory ElasticSearch server. */ public class ExtendedNode extends Node { - public ExtendedNode(Settings preparedSettings, Collection> classpathPlugins) { - super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null, null, null), - classpathPlugins, false); + public ExtendedNode(Settings preparedSettings, Collection> classpathPlugins, + Supplier nodeNameSupplier) { + super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(), + Paths.get("target"), nodeNameSupplier), classpathPlugins, false); } } diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/MockEntity.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/MockEntity.java index 32e4a07a299..7326cbdb2a5 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/MockEntity.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/MockEntity.java @@ -18,6 +18,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import org.awaitility.Awaitility; @@ -46,7 +47,8 @@ public class MockEntity { public static Node prepareNode() throws Exception { Settings settings = MockEntity.prepareNodeSettings(); removeOldDataDirectories("target/" + NODE_NAME); - return new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class)); + Supplier nodeNameSupplier = () -> NODE_NAME; + return new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class), nodeNameSupplier); } private static void removeOldDataDirectories(String dataDirectory) throws Exception { @@ -59,7 +61,6 @@ private static void removeOldDataDirectories(String dataDirectory) throws Except private static Settings prepareNodeSettings() { String port = ConfigMain.getParameter("elasticsearch.port", "9205"); return Settings.builder().put("node.name", NODE_NAME) - .put("path.conf", TARGET) .put("path.data", TARGET) .put("path.logs", TARGET) .put("path.home", TARGET) diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/index/IndexRestClientIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/index/IndexRestClientIT.java index 35b244ea0e4..b95423b45ff 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/index/IndexRestClientIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/index/IndexRestClientIT.java @@ -40,7 +40,7 @@ public class IndexRestClientIT { @BeforeClass public static void startElasticSearch() throws Exception { - testIndexName = ConfigMain.getParameter("elasticsearch.index", "testindex") + "_" + testTypeName; + testIndexName = ConfigMain.getParameter("elasticsearch.index", "testindex"); restClient = initializeRestClient(); node = MockEntity.prepareNode(); @@ -54,12 +54,12 @@ public static void stopElasticSearch() throws Exception { @Before public void createIndex() throws Exception { - restClient.createIndexes(); + restClient.createIndex(null, testTypeName); } @After public void deleteIndex() throws Exception { - restClient.deleteAllIndexes(); + restClient.deleteIndex(testTypeName); } @Test diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearchRestClientIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearchRestClientIT.java index 2caab666eb1..70358a8976d 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearchRestClientIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearchRestClientIT.java @@ -47,7 +47,7 @@ public static void prepareIndex() throws Exception { node = MockEntity.prepareNode(); node.start(); - searchRestClient.createIndexes(); + searchRestClient.createIndex(null, testTypeName); IndexRestClient indexRestClient = initializeIndexRestClient(); indexRestClient.addDocument(testTypeName, MockEntity.createEntities().get(1), 1, false); @@ -58,7 +58,7 @@ public static void prepareIndex() throws Exception { @AfterClass public static void cleanIndex() throws Exception { - searchRestClient.deleteAllIndexes(); + searchRestClient.deleteIndex(testTypeName); node.close(); } diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearcherIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearcherIT.java index 550af72eff5..ff93e51275e 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearcherIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/elasticsearch/search/SearcherIT.java @@ -42,8 +42,9 @@ public class SearcherIT { private static Node node; private static IndexRestClient indexRestClient; private static String testIndexName; - private static QueryBuilder query = QueryBuilders.matchAllQuery(); - private static Searcher searcher = new Searcher("testsearch"); + private static final String testSearch = "testsearch"; + private static final QueryBuilder query = QueryBuilders.matchAllQuery(); + private static final Searcher searcher = new Searcher(testSearch); private static final String TITLE = "title"; private static final String BATCH_ONE = "Batch1"; private static final String WRONG_AMOUNT = "Incorrect result - amount doesn't match to given number!"; @@ -61,17 +62,17 @@ public static void prepareIndex() throws Exception { node = MockEntity.prepareNode(); node.start(); - indexRestClient.createIndexes(); + indexRestClient.createIndex(null, testSearch); indexRestClient.addDocument(searcher.getType(), MockEntity.createEntities().get(1), 1, false); indexRestClient.addDocument(searcher.getType(), MockEntity.createEntities().get(2), 2, false); indexRestClient.addDocument(searcher.getType(), MockEntity.createEntities().get(3), 3, false); indexRestClient.addDocument(searcher.getType(), MockEntity.createEntities().get(4), 4, false); - indexRestClient.enableSortingByTextField(TITLE); + indexRestClient.enableSortingByTextField(TITLE, testSearch); } @AfterClass public static void cleanIndex() throws Exception { - indexRestClient.deleteAllIndexes(); + indexRestClient.deleteIndex(testSearch); node.close(); } diff --git a/Kitodo/src/test/java/org/kitodo/MockDatabase.java b/Kitodo/src/test/java/org/kitodo/MockDatabase.java index 7d2705aa294..3a3d563073c 100644 --- a/Kitodo/src/test/java/org/kitodo/MockDatabase.java +++ b/Kitodo/src/test/java/org/kitodo/MockDatabase.java @@ -16,6 +16,7 @@ import java.io.InputStream; import java.io.StringReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; import java.sql.Date; import java.sql.SQLException; import java.time.Duration; @@ -31,6 +32,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; import javax.json.Json; import javax.json.JsonObject; @@ -139,7 +141,8 @@ public static void startNodeWithoutMapping() throws Exception { if (node != null) { stopNode(); } - node = new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class)); + Supplier nodeNameSupplier = () -> nodeName; + node = new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class), nodeNameSupplier); node.start(); } @@ -222,8 +225,10 @@ public static void insertForDataEditorTesting() throws Exception { } private static class ExtendedNode extends Node { - ExtendedNode(Settings preparedSettings, Collection> classpathPlugins) { - super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null, null, null), classpathPlugins, false); + ExtendedNode(Settings preparedSettings, Collection> classpathPlugins, + Supplier nodeNameSupplier) { + super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(), + Paths.get("target"), nodeNameSupplier), classpathPlugins, false); } } @@ -261,7 +266,6 @@ private static void removeOldDataDirectories(String dataDirectory) throws Except private static Settings prepareNodeSettings(String httpPort, String nodeName) { return Settings.builder().put("node.name", nodeName) - .put("path.conf", TARGET) .put("path.data", TARGET) .put("path.logs", TARGET) .put("path.home", TARGET) diff --git a/Kitodo/src/test/java/org/kitodo/production/forms/SearchResultFormIT.java b/Kitodo/src/test/java/org/kitodo/production/forms/SearchResultFormIT.java index efb625fbd44..c41edf0abe6 100644 --- a/Kitodo/src/test/java/org/kitodo/production/forms/SearchResultFormIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/forms/SearchResultFormIT.java @@ -19,14 +19,12 @@ import org.junit.Test; import org.kitodo.MockDatabase; import org.kitodo.SecurityTestUtils; -import org.kitodo.data.database.beans.Process; -import org.kitodo.data.database.exceptions.DAOException; import org.kitodo.production.dto.ProcessDTO; import org.kitodo.production.services.ServiceManager; public class SearchResultFormIT { - private SearchResultForm searchResultForm = new SearchResultForm(); + private final SearchResultForm searchResultForm = new SearchResultForm(); @BeforeClass public static void prepareDatabase() throws Exception { @@ -37,7 +35,7 @@ public static void prepareDatabase() throws Exception { /** * Cleanup the database and stop elasticsearch. - * + * * @throws Exception * if elasticsearch could not been stopped. */ diff --git a/Kitodo/src/test/java/org/kitodo/production/services/data/FilterServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/data/FilterServiceIT.java index 565cf0bcf7d..e7f3f6c89ae 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/data/FilterServiceIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/data/FilterServiceIT.java @@ -19,17 +19,20 @@ import java.util.List; import java.util.Objects; +import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.search.sort.SortBuilders; +import org.elasticsearch.search.sort.SortOrder; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.kitodo.MockDatabase; import org.kitodo.SecurityTestUtils; import org.kitodo.data.database.beans.Filter; import org.kitodo.data.exceptions.DataException; -import org.kitodo.production.dto.ProcessDTO; import org.kitodo.production.dto.TaskDTO; import org.kitodo.production.enums.ObjectType; import org.kitodo.production.services.ServiceManager; @@ -80,8 +83,7 @@ public void shouldCountAllDatabaseRowsForFilters() throws Exception { public void shouldGetFilterById() throws Exception { Filter filter = filterService.getById(1); String actual = filter.getValue(); - String expected = filterValue; - assertEquals("Filter was not found in database!", expected, actual); + assertEquals("Filter was not found in database!", filterValue, actual); } @Test @@ -112,7 +114,7 @@ public void shouldBuildQueryAndFindByProcessServiceByProcessId() throws Exceptio processService.findByQuery(secondQuery, true).size()); assertEquals("Incorrect id for found process!", Integer.valueOf(2), - processService.findByQuery(secondQuery, true).get(0).getId()); + processService.findByQuery(secondQuery, SortBuilders.fieldSort("id").order(SortOrder.DESC), true).get(0).getId()); QueryBuilder thirdQuery = filterService.queryBuilder("\"id:2 3\"", ObjectType.PROCESS, false, false); assertEquals("Incorrect amount of processes with id equal 2 or 3!", 2, @@ -515,10 +517,10 @@ public void shouldBuildQueryForEmptyConditions() throws Exception { List taskDTOS = taskService.findByQuery(query, true); assertEquals("Incorrect amount of closed tasks with no ordering!", 0, taskDTOS.size()); - // empty condition is not allowed and returns no results + // empty condition is not allowed and throws Exception in ElasticSearch 7 query = filterService.queryBuilder("\"id:\"", ObjectType.PROCESS, false, false); - List processDTOS = processService.findByQuery(query, true); - assertEquals("Incorrect amount of process with no id!", 0, processDTOS.size()); - + QueryBuilder finalQuery = query; + Assertions.assertThrows(ElasticsearchStatusException.class, + () -> processService.findByQuery(finalQuery, true)); } } diff --git a/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java b/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java index 0df15a28ec5..f09d83b4cf1 100644 --- a/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/services/data/ProcessServiceIT.java @@ -16,6 +16,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.File; @@ -65,7 +66,7 @@ */ public class ProcessServiceIT { - private static FileService fileService = new FileService(); + private static final FileService fileService = new FileService(); private static final ProcessService processService = ServiceManager.getProcessService(); private static final String firstProcess = "First process"; @@ -591,7 +592,7 @@ public void shouldUpdateChildrenFromLogicalStructure() throws Exception { Arrays.asList("HierarchChildToKeep", "HierarchChildToAdd").contains(child.getTitle())); assertEquals("Child should have parent as parent", process, child.getParent()); } - assertEquals("Process to remove should have no parent", null, processService.getById(6).getParent()); + assertNull("Process to remove should have no parent", processService.getById(6).getParent()); } @Test @@ -602,12 +603,11 @@ public void testFindAllIDs() throws DataException { allIDs = ServiceManager.getProcessService().findAllIDs(0L, 5); Assert.assertEquals("Wrong amount of id's in index", 5, allIDs.size()); - Assert.assertTrue("id's contain wrong entries", allIDs.containsAll(Arrays.asList(5, 2, 4, 1, 6))); - + Assert.assertTrue("id's contain wrong entries", allIDs.containsAll(Arrays.asList(5, 3, 1, 2, 6))); allIDs = ServiceManager.getProcessService().findAllIDs(5L, 10); Assert.assertEquals("Wrong amount of id's in index", 2, allIDs.size()); - Assert.assertTrue("id's contain wrong entries", allIDs.containsAll(Arrays.asList(7, 3))); + Assert.assertTrue("id's contain wrong entries", allIDs.containsAll(Arrays.asList(7, 4))); } diff --git a/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/ProjectsPage.java b/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/ProjectsPage.java index 127866436f2..ef3a99911d1 100644 --- a/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/ProjectsPage.java +++ b/Kitodo/src/test/java/org/kitodo/selenium/testframework/pages/ProjectsPage.java @@ -443,6 +443,6 @@ private void switchToTabByIndex(int index) throws Exception { public String getWorkflowStatusForWorkflow() { List tableDataByColumn = getTableDataByColumn(workflowsTable, 1); - return tableDataByColumn.get(0); + return tableDataByColumn.get(1); } }