From ecbe5bb43cda45e704d2844c4de10dfa07932a23 Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Fri, 6 Sep 2024 13:55:14 +0200 Subject: [PATCH] Add MockDatabase index to Kitodo - DataManagement --- Kitodo-DataManagement/pom.xml | 10 +++ .../test/java/org/kitodo/ExtendedNode.java | 22 +++++++ .../test/java/org/kitodo/MockDatabase.java | 63 +++++++++++++++++++ .../database/persistence/AuthorityDaoIT.java | 13 ++++ .../database/persistence/ClientDaoIT.java | 13 ++++ .../persistence/DataEditorSettingDaoIT.java | 13 ++++ .../database/persistence/DocketDaoIT.java | 13 ++++ .../database/persistence/FilterDaoIT.java | 13 ++++ .../database/persistence/LdapGroupDaoIT.java | 13 ++++ .../database/persistence/LdapServerDaoIT.java | 13 ++++ .../database/persistence/PropertyDaoIT.java | 13 ++++ .../data/database/persistence/RoleDaoIT.java | 13 ++++ .../database/persistence/RulesetDaoIT.java | 13 ++++ .../data/database/persistence/UserDaoIT.java | 13 ++++ .../src/test/resources/log4j2.xml | 6 ++ .../test/java/org/kitodo/ExtendedNode.java | 1 - .../production/forms/IndexingFormIT.java | 2 +- 17 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 Kitodo-DataManagement/src/test/java/org/kitodo/ExtendedNode.java create mode 100644 Kitodo-DataManagement/src/test/java/org/kitodo/MockDatabase.java diff --git a/Kitodo-DataManagement/pom.xml b/Kitodo-DataManagement/pom.xml index e157202637f..5bc990a8619 100644 --- a/Kitodo-DataManagement/pom.xml +++ b/Kitodo-DataManagement/pom.xml @@ -83,6 +83,16 @@ org.hibernate hibernate-jcache + + org.opensearch + opensearch + test + + + org.opensearch.plugin + transport-netty4-client + test + com.github.spotbugs spotbugs-annotations diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/ExtendedNode.java b/Kitodo-DataManagement/src/test/java/org/kitodo/ExtendedNode.java new file mode 100644 index 00000000000..8b4cfa28a90 --- /dev/null +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/ExtendedNode.java @@ -0,0 +1,22 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * This file is part of the Kitodo project. + * + * It is licensed under GNU General Public License version 3 or later. + * + * For the full copyright and license information, please read the + * GPL3-License.txt file that was distributed with this source code. + */ +package org.kitodo; + +import java.util.Collection; +import org.opensearch.env.Environment; +import org.opensearch.node.Node; +import org.opensearch.plugins.Plugin; + +public class ExtendedNode extends Node { + ExtendedNode(Environment initialEnvironment, Collection> classpathPlugins) { + super(initialEnvironment, classpathPlugins, true); + } +} diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/MockDatabase.java b/Kitodo-DataManagement/src/test/java/org/kitodo/MockDatabase.java new file mode 100644 index 00000000000..61240e759ef --- /dev/null +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/MockDatabase.java @@ -0,0 +1,63 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * This file is part of the Kitodo project. + * + * It is licensed under GNU General Public License version 3 or later. + * + * For the full copyright and license information, please read the + * GPL3-License.txt file that was distributed with this source code. + */ +package org.kitodo; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import org.apache.commons.io.FileUtils; +import org.kitodo.config.ConfigMain; +import org.opensearch.common.settings.Settings; +import org.opensearch.env.Environment; +import org.opensearch.node.Node; +import org.opensearch.transport.Netty4Plugin; + +public class MockDatabase { + + private static Node node; + private static final String HTTP_TRANSPORT_PORT = "9305"; + private static final String TARGET = "target"; + + public static void startNode() throws Exception { + final String nodeName = "index"; + final String port = ConfigMain.getParameter("elasticsearch.port", "9205"); + Environment environment = prepareEnvironment(port, nodeName, Paths.get("target", "classes")); + removeOldDataDirectories("target/" + nodeName); + node = new ExtendedNode(environment, Collections.singleton(Netty4Plugin.class)); + node.start(); + } + + public static void stopNode() throws Exception { + node.close(); + node = null; + } + + private static void removeOldDataDirectories(String dataDirectory) throws Exception { + File dataDir = new File(dataDirectory); + if (dataDir.exists()) { + FileUtils.deleteDirectory(dataDir); + } + } + + private static Environment prepareEnvironment(String httpPort, String nodeName, Path configPath) { + Settings settings = Settings.builder().put("node.name", nodeName) + .put("path.data", TARGET) + .put("path.logs", TARGET) + .put("path.home", TARGET) + .put("http.type", "netty4") + .put("http.port", httpPort) + .put("transport.tcp.port", HTTP_TRANSPORT_PORT) + .put("transport.type", "netty4") + .put("action.auto_create_index", "false").build(); + return new Environment(settings, configPath); + } +} diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/AuthorityDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/AuthorityDaoIT.java index 380228e2abf..dc9cc4d9bb8 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/AuthorityDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/AuthorityDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Authority; import org.kitodo.data.database.exceptions.DAOException; public class AuthorityDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/ClientDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/ClientDaoIT.java index 68b74e9c523..fc14dd95847 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/ClientDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/ClientDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Client; import org.kitodo.data.database.exceptions.DAOException; public class ClientDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DataEditorSettingDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DataEditorSettingDaoIT.java index f625ccfeec4..67ed04ffed8 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DataEditorSettingDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DataEditorSettingDaoIT.java @@ -16,15 +16,28 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.DataEditorSetting; import org.kitodo.data.database.exceptions.DAOException; public class DataEditorSettingDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DocketDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DocketDaoIT.java index 3117864ef01..b0683a86d01 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DocketDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/DocketDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Docket; import org.kitodo.data.database.exceptions.DAOException; public class DocketDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/FilterDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/FilterDaoIT.java index 5f1e7ce0a75..c423b45b5cb 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/FilterDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/FilterDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Filter; import org.kitodo.data.database.exceptions.DAOException; public class FilterDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapGroupDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapGroupDaoIT.java index 8d2a29d88a0..417cbe63ca2 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapGroupDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapGroupDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.LdapGroup; import org.kitodo.data.database.exceptions.DAOException; public class LdapGroupDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapServerDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapServerDaoIT.java index dd643ea26d0..c53a4e408f9 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapServerDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/LdapServerDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.LdapServer; import org.kitodo.data.database.exceptions.DAOException; public class LdapServerDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/PropertyDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/PropertyDaoIT.java index f13a34b1f2f..a761d9d5317 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/PropertyDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/PropertyDaoIT.java @@ -16,15 +16,28 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Property; import org.kitodo.data.database.enums.PropertyType; import org.kitodo.data.database.exceptions.DAOException; public class PropertyDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RoleDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RoleDaoIT.java index da22aec849a..7ccfee08408 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RoleDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RoleDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Role; import org.kitodo.data.database.exceptions.DAOException; public class RoleDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RulesetDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RulesetDaoIT.java index 23d47a1a277..e1c27e1e32d 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RulesetDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/RulesetDaoIT.java @@ -16,14 +16,27 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.Ruleset; import org.kitodo.data.database.exceptions.DAOException; public class RulesetDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Rule public final ExpectedException exception = ExpectedException.none(); diff --git a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/UserDaoIT.java b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/UserDaoIT.java index 810a568dd7e..8d5400186f0 100644 --- a/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/UserDaoIT.java +++ b/Kitodo-DataManagement/src/test/java/org/kitodo/data/database/persistence/UserDaoIT.java @@ -17,12 +17,25 @@ import java.util.ArrayList; import java.util.List; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; +import org.kitodo.MockDatabase; import org.kitodo.data.database.beans.User; import org.kitodo.data.database.exceptions.DAOException; public class UserDaoIT { + @BeforeClass + public static void setUp() throws Exception { + MockDatabase.startNode(); + } + + @AfterClass + public static void tearDown() throws Exception { + MockDatabase.stopNode(); + } + @Test public void runPersistenceSuitTest() throws DAOException { List users = getAuthorities(); diff --git a/Kitodo-DataManagement/src/test/resources/log4j2.xml b/Kitodo-DataManagement/src/test/resources/log4j2.xml index 676f91f13fe..bcf7cf76fc2 100644 --- a/Kitodo-DataManagement/src/test/resources/log4j2.xml +++ b/Kitodo-DataManagement/src/test/resources/log4j2.xml @@ -18,6 +18,12 @@ + + + + + + diff --git a/Kitodo/src/test/java/org/kitodo/ExtendedNode.java b/Kitodo/src/test/java/org/kitodo/ExtendedNode.java index 24fcdc8aa28..8b4cfa28a90 100644 --- a/Kitodo/src/test/java/org/kitodo/ExtendedNode.java +++ b/Kitodo/src/test/java/org/kitodo/ExtendedNode.java @@ -8,7 +8,6 @@ * For the full copyright and license information, please read the * GPL3-License.txt file that was distributed with this source code. */ - package org.kitodo; import java.util.Collection; diff --git a/Kitodo/src/test/java/org/kitodo/production/forms/IndexingFormIT.java b/Kitodo/src/test/java/org/kitodo/production/forms/IndexingFormIT.java index fa2031d52d1..50f53ac9157 100644 --- a/Kitodo/src/test/java/org/kitodo/production/forms/IndexingFormIT.java +++ b/Kitodo/src/test/java/org/kitodo/production/forms/IndexingFormIT.java @@ -35,7 +35,7 @@ public class IndexingFormIT { @BeforeClass public static void setUp() throws Exception { - MockDatabase.startNodeWithoutMapping(); + MockDatabase.startNode(); Client client = new Client(); ServiceManager.getClientService().save(client); User user = new User();