/
DatabaseSettingsTest.java
102 lines (88 loc) · 4.08 KB
/
DatabaseSettingsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright Siemens AG, 2021. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.datahandler.common;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.sw360.datahandler.thrift.ThriftUtils;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import com.cloudant.client.api.ClientBuilder;
import com.cloudant.client.api.CloudantClient;
import com.google.gson.GsonBuilder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.function.Supplier;
/**
* Constants for the database address
*/
public class DatabaseSettingsTest {
private static final Logger log = LogManager.getLogger(DatabaseSettingsTest.class);
public static final String PROPERTIES_FILE_PATH = "/couchdb-test.properties";
public static final String COUCH_DB_URL;
public static final String COUCH_DB_LUCENE_URL;
public static final String COUCH_DB_DATABASE;
public static final String COUCH_DB_ATTACHMENTS;
public static final String COUCH_DB_CONFIG;
public static final String COUCH_DB_USERS;
public static final String COUCH_DB_VM;
public static final String COUCH_CHANGELOGS;
private static final String COUCH_DB_USERNAME;
private static final String COUCH_DB_PASSWORD;
static {
Properties props = CommonUtils.loadProperties(DatabaseSettingsTest.class, PROPERTIES_FILE_PATH);
COUCH_DB_URL = props.getProperty("couchdb.url", "http://localhost:5984");
COUCH_DB_LUCENE_URL = props.getProperty("couchdb.lucene.url", "http://localhost:8080/couchdb-lucene");
COUCH_DB_DATABASE = props.getProperty("couchdb.database", "sw360_test_db");
COUCH_DB_USERNAME = props.getProperty("couchdb.user", "");
COUCH_DB_PASSWORD = props.getProperty("couchdb.password", "");
COUCH_DB_ATTACHMENTS = props.getProperty("couchdb.attachments", "sw360_test_attachments");
COUCH_DB_CONFIG = props.getProperty("couchdb.config", "sw360_test_config");
COUCH_DB_USERS = props.getProperty("couchdb.usersdb", "sw360_test_users");
COUCH_DB_VM = props.getProperty("couchdb.vulnerability_management", "sw360_test_vm");
COUCH_CHANGELOGS = props.getProperty("couchdb.change_logs", "sw360_test_changelogs");
}
public static Supplier<HttpClient> getConfiguredHttpClient() throws MalformedURLException {
StdHttpClient.Builder httpClientBuilder = new StdHttpClient.Builder().url(COUCH_DB_URL);
if(! "".equals(COUCH_DB_USERNAME)) {
httpClientBuilder.username(COUCH_DB_USERNAME);
}
if (! "".equals(COUCH_DB_PASSWORD)) {
httpClientBuilder.password(COUCH_DB_PASSWORD);
}
return httpClientBuilder::build;
}
public static Supplier<CloudantClient> getConfiguredClient() {
ClientBuilder clientBuilder = null;
GsonBuilder gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping();
for (Class<?> c : ThriftUtils.THRIFT_CLASSES) {
gson.registerTypeAdapter(c, new CustomThriftDeserializer());
gson.registerTypeAdapter(c, new CustomThriftSerializer());
}
for (Class<?> c : ThriftUtils.THRIFT_NESTED_CLASSES) {
gson.registerTypeAdapter(c, new CustomThriftSerializer());
}
try {
clientBuilder = ClientBuilder.url(new URL(COUCH_DB_URL)).gsonBuilder(gson);
if (!"".equals(COUCH_DB_USERNAME)) {
clientBuilder.username(COUCH_DB_USERNAME);
}
if (!"".equals(COUCH_DB_PASSWORD)) {
clientBuilder.password(COUCH_DB_PASSWORD);
}
} catch (MalformedURLException e) {
log.error("Error creating client", e);
}
return clientBuilder::build;
}
private DatabaseSettingsTest() {
// Utility class with only static functions
}
}