TestUtilities.kt 2.7 KB
Newer Older
1 2 3
package org.memobase

import io.mockk.mockk
4 5 6 7 8 9 10 11 12
import java.net.ConnectException
import java.net.SocketTimeoutException
import kotlin.system.exitProcess
import org.apache.http.HttpHost
import org.apache.logging.log4j.LogManager
import org.elasticsearch.ElasticsearchException
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest
import org.elasticsearch.client.RequestOptions
import org.elasticsearch.client.RestClient
13
import org.elasticsearch.client.RestHighLevelClient
14
import org.elasticsearch.client.indices.GetIndexRequest
15 16 17 18
import org.memobase.helpers.ElasticSearchWrapper
import org.memobase.helpers.TranslationMappers

object TestUtilities {
19
    private val log = LogManager.getLogger("TestUtils")
20 21 22
    private const val institutionTypePath = "src/test/resources/configs/institution-type-labels.csv"
    private const val accessTermPath = "src/test/resources/configs/access-term-labels.csv"
    private const val documentTypePath = "src/test/resources/configs/document-type-labels.csv"
23
    private const val reuseStatementPath = "src/test/resources/configs/reuse-statement-labels.csv"
24
    const val currentDocumentsIndex = "documents-v17"
25

Jonas Waeber's avatar
Jonas Waeber committed
26 27

    val dateRegex = Regex("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{2,3})")
28 29
    const val mediaUrl = "https://media.memobase.k8s.unibas.ch/memo/"

30 31
    val translationMappers =
        TranslationMappers(institutionTypePath, documentTypePath, accessTermPath, reuseStatementPath)
32 33 34

    val elasticSearchClient = mockk<RestHighLevelClient>()
    val elasticSearchWrapperMocked = mockk<ElasticSearchWrapper>()
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64


    fun connectToElasticSearch(host: String, port: Int, documentsIndex: String): RestHighLevelClient {
        return try {
            val c = RestHighLevelClient(
                RestClient.builder(
                    HttpHost(host, port)
                )
            )
            val indexExists = c.indices().exists(GetIndexRequest(documentsIndex), RequestOptions.DEFAULT)
            val aliasExists = c.indices().existsAlias(GetAliasesRequest(documentsIndex), RequestOptions.DEFAULT)

            if (!indexExists && !aliasExists) {
                log.error("Could not find the index or alias defined in the configuration: $documentsIndex.")
                exitProcess(1)
            } else {
                log.info("Successfully connected to index $documentsIndex. Ready to query.")
                c
            }
        } catch (ex: ElasticsearchException) {
            log.error(ex.detailedMessage)
            exitProcess(1)
        } catch (ex: SocketTimeoutException) {
            log.error(ex.localizedMessage)
            exitProcess(1)
        } catch (ex: ConnectException) {
            log.error(ex.localizedMessage)
            exitProcess(1)
        }
    }
65
}