TestUtilities.kt 3.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * search-doc-service
 * Copyright (C) 2020 Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package ch.memobase
19

20
import ch.memobase.helpers.TranslationMappers
21
import io.mockk.mockk
22
23
24
25
26
27
28
29
30
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
31
import org.elasticsearch.client.RestHighLevelClient
32
import org.elasticsearch.client.indices.GetIndexRequest
33
34

object TestUtilities {
35
    private val log = LogManager.getLogger("TestUtils")
36
37
38
    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"
39
    private const val reuseStatementPath = "src/test/resources/configs/reuse-statement-labels.csv"
40
    const val currentDocumentsIndex = "documents-v17"
41

Jonas Waeber's avatar
Jonas Waeber committed
42
43

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

46
47
    val translationMappers =
        TranslationMappers(institutionTypePath, documentTypePath, accessTermPath, reuseStatementPath)
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    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)
        }
    }
77
}