Skip to content

Commit

Permalink
Bring OpenSearch background instance for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-ronge committed Sep 6, 2024
1 parent d811c12 commit a48688e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Kitodo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</dependency>
<dependency>
<groupId>org.opensearch</groupId>
<artifactId>opensearch</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opensearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
Expand Down
18 changes: 7 additions & 11 deletions Kitodo/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@
*
-->
<Configuration monitorInterval="60">
<Properties>
<Property name="filename">/usr/local/kitodo/logs</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="LOGFILE" fileName="${filename}/kitodo.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="de.unigoettingen.sub.search.opac" level="info" additivity="false">
<AppenderRef ref="LOGFILE"/>
<Logger name="de.unigoettingen.sub.search.opac" level="debug" additivity="false">
<AppenderRef ref="STDOUT"/>
</Logger>
<Logger name="org.kitodo" level="info" additivity="false">
<AppenderRef ref="LOGFILE"/>
<Logger name="org.goobi" level="debug" additivity="false">
<AppenderRef ref="STDOUT"/>
</Logger>
<Logger name="org.kitodo" level="debug" additivity="false">
<AppenderRef ref="STDOUT"/>
</Logger>
<Root level="error">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="LOGFILE"/>
</Root>
</Loggers>
</Configuration>
23 changes: 23 additions & 0 deletions Kitodo/src/test/java/org/kitodo/ExtendedNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* 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<Class<? extends Plugin>> classpathPlugins) {
super(initialEnvironment, classpathPlugins, true);
}
}
43 changes: 39 additions & 4 deletions Kitodo/src/test/java/org/kitodo/MockDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.xebialabs.restito.semantics.Action;
import com.xebialabs.restito.server.StubServer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -39,6 +40,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -51,6 +53,7 @@
import org.kitodo.api.externaldatamanagement.SearchInterfaceType;
import org.kitodo.api.schemaconverter.FileFormat;
import org.kitodo.api.schemaconverter.MetadataFormat;
import org.kitodo.config.ConfigMain;
import org.kitodo.data.database.beans.Authority;
import org.kitodo.data.database.beans.Batch;
import org.kitodo.data.database.beans.Client;
Expand Down Expand Up @@ -91,14 +94,22 @@
import org.kitodo.production.services.workflow.WorkflowControllerService;
import org.kitodo.production.workflow.model.Converter;
import org.kitodo.test.utils.ProcessTestUtils;
import org.opensearch.common.settings.Settings;
import org.opensearch.env.Environment;
import org.opensearch.node.Node;
import org.opensearch.transport.Netty4Plugin;
import java.nio.file.Path;

/**
* Insert data to test database.
*/
public class MockDatabase {

private static Node node;
private static final String GLOBAL_ASSIGNABLE = "_globalAssignable";
private static final String CLIENT_ASSIGNABLE = "_clientAssignable";
private static final String HTTP_TRANSPORT_PORT = "9305";
private static final String TARGET = "target";
private static final String CHOICE = "choice";
private static final String TEST = "test";
private static final String FIRST_VALUE = "first value";
Expand All @@ -125,13 +136,17 @@ public static void stopDatabaseServer() {
}

public static void startNode() throws Exception {
startNodeWithoutMapping();
}

public static void startNodeWithoutMapping() 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;
}

public static void setUpAwaitility() {
Expand Down Expand Up @@ -207,6 +222,26 @@ public static void insertForDataEditorTesting() throws Exception {
insertDataEditorSettings();
}

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);
}

public static void insertAuthorities() throws DAOException {
List<Authority> authorities = new ArrayList<>();

Expand Down
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<saxon.version>9.9.1-8</saxon.version>
<log4j.version>2.19.0</log4j.version>
<junit.version>5.9.2</junit.version>
<opensearch.version>1.3.19</opensearch.version>
<maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version>

<!-- maven-resources-plugin versions greater 3.2.0 introduce changes that
Expand Down Expand Up @@ -531,6 +532,18 @@ from system library in Java 11+ -->
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opensearch</groupId>
<artifactId>opensearch</artifactId>
<version>${opensearch.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.opensearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${opensearch.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
Expand Down

0 comments on commit a48688e

Please sign in to comment.