Skip to content

Commit

Permalink
Adjust tests to new ES version
Browse files Browse the repository at this point in the history
  • Loading branch information
solth committed Aug 31, 2021
1 parent 6689e28 commit 88632fc
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,8 +26,9 @@
* client in in-memory ElasticSearch server.
*/
public class ExtendedNode extends Node {
public ExtendedNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins) {
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null, null, null),
classpathPlugins, false);
public ExtendedNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins,
Supplier<String> nodeNameSupplier) {
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(),
Paths.get("target"), nodeNameSupplier), classpathPlugins, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> nodeNameSupplier = () -> NODE_NAME;
return new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class), nodeNameSupplier);
}

private static void removeOldDataDirectories(String dataDirectory) throws Exception {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -58,7 +58,7 @@ public static void prepareIndex() throws Exception {

@AfterClass
public static void cleanIndex() throws Exception {
searchRestClient.deleteAllIndexes();
searchRestClient.deleteIndex(testTypeName);
node.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand All @@ -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();
}

Expand Down
12 changes: 8 additions & 4 deletions Kitodo/src/test/java/org/kitodo/MockDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -139,7 +141,8 @@ public static void startNodeWithoutMapping() throws Exception {
if (node != null) {
stopNode();
}
node = new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class));
Supplier<String> nodeNameSupplier = () -> nodeName;
node = new ExtendedNode(settings, Collections.singleton(Netty4Plugin.class), nodeNameSupplier);
node.start();
}

Expand Down Expand Up @@ -222,8 +225,10 @@ public static void insertForDataEditorTesting() throws Exception {
}

private static class ExtendedNode extends Node {
ExtendedNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins) {
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, null, null, null), classpathPlugins, false);
ExtendedNode(Settings preparedSettings, Collection<Class<? extends Plugin>> classpathPlugins,
Supplier<String> nodeNameSupplier) {
super(InternalSettingsPreparer.prepareEnvironment(preparedSettings, Collections.emptyMap(),
Paths.get("target"), nodeNameSupplier), classpathPlugins, false);
}
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,7 +35,7 @@ public static void prepareDatabase() throws Exception {

/**
* Cleanup the database and stop elasticsearch.
*
*
* @throws Exception
* if elasticsearch could not been stopped.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -515,10 +517,10 @@ public void shouldBuildQueryForEmptyConditions() throws Exception {
List<TaskDTO> 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<ProcessDTO> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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)));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,6 @@ private void switchToTabByIndex(int index) throws Exception {

public String getWorkflowStatusForWorkflow() {
List<String> tableDataByColumn = getTableDataByColumn(workflowsTable, 1);
return tableDataByColumn.get(0);
return tableDataByColumn.get(1);
}
}

0 comments on commit 88632fc

Please sign in to comment.