-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAPI.java
More file actions
265 lines (232 loc) · 9.64 KB
/
API.java
File metadata and controls
265 lines (232 loc) · 9.64 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* Fusion Studio API - API for Fusion Studio
* Copyright © 2017 Evolved Binary (tech@evolvedbinary.com)
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.fusiondb.studio.api;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import com.evolvedbinary.j8fu.tuple.Tuple2;
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
import static io.restassured.RestAssured.given;
import static org.apache.http.HttpStatus.SC_OK;
public class API {
/**
* Default Host for FusionDB Server
*/
public static String DEFAULT_HOST = "localhost";
/**
* Default Port for FusionDB Server
*/
public static int DEFAULT_PORT = 4059;
/**
* Default admin username
*/
public static String DEFAULT_ADMIN_USERNAME = "admin";
/**
* Default admin password
*/
public static String DEFAULT_ADMIN_PASSWORD = "";
/**
* Default API Endpoint for Fusion Studio API
*/
public static String DEFAULT_ENDPOINT = "fusiondb";
/**
* System Property for setting the API Host
*/
public static String SYS_PROP_NAME_API_HOST = "api.host";
/**
* Environment variable name for setting the API Host
*/
public static String ENV_VAR_API_HOST = "API_HOST";
/**
* System Property for setting the API Port
*/
public static String SYS_PROP_NAME_API_PORT = "api.port";
/**
* Environment variable name for setting the API Port
*/
public static String ENV_VAR_API_PORT = "API_PORT";
/**
* System Property for setting the Docker DB Image
*/
public static String SYS_PROP_NAME_DOCKER_DB_IMAGE = "docker.db.image";
/**
* Environment Variable for setting the Docker DB Image
*/
private static String ENV_VAR_DOCKER_DB_IMAGE = "DOCKER_DB_IMAGE";
/**
* Get the Base URI for the Fusion Studio API.
*
* The URI can be overridden by System Properties firstly,
* or Environment Variables secondly.
* See {@link #SYS_PROP_NAME_API_HOST} {@link #SYS_PROP_NAME_API_PORT}
* {@link #ENV_VAR_API_HOST} and {@link #ENV_VAR_API_PORT}.
*
* @return the base URI
*/
public static String getApiBaseUri() {
final String host = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_HOST, ENV_VAR_API_HOST, DEFAULT_HOST, envVarValue -> envVarValue);
final int port = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_PORT, ENV_VAR_API_PORT, DEFAULT_PORT, envVarValue -> {
try {
return Integer.parseInt(envVarValue);
} catch (final NumberFormatException e) {
// invalid number
System.err.println("Port " + envVarValue + " is not a valid TCP port number. Using default: " + DEFAULT_PORT);
e.printStackTrace();
return DEFAULT_PORT;
}
});
return "http://" + host + ":" + port + "/exist/restxq/" + DEFAULT_ENDPOINT;
}
/**
* Get the Base URI for the eXist-db REST API.
*
* The URI can be overridden by System Properties firstly,
* or Environment Variables secondly.
* See {@link #SYS_PROP_NAME_API_HOST} {@link #SYS_PROP_NAME_API_PORT}
* {@link #ENV_VAR_API_HOST} and {@link #ENV_VAR_API_PORT}.
*
* @return the REST base URI
*/
public static String getRestApiBaseUri() {
final String host = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_HOST, ENV_VAR_API_HOST, DEFAULT_HOST, envVarValue -> envVarValue);
final int port = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_PORT, ENV_VAR_API_PORT, DEFAULT_PORT, envVarValue -> {
try {
return Integer.parseInt(envVarValue);
} catch (final NumberFormatException e) {
// invalid number
System.err.println("Port " + envVarValue + " is not a valid TCP port number. Using default: " + DEFAULT_PORT);
e.printStackTrace();
return DEFAULT_PORT;
}
});
return "http://" + host + ":" + port + "/exist/rest";
}
/**
* Gets a value from an Environment variable or uses the default
* if there is no such variable.
*
* @param envVarName the name of the environment variable.
* @param defaultValue the default value to use if there is no environment variable
* @param typeConverter a function for converting the value of the environment
* variable (if present) to the desired type
*
* @return the value from the environment variable, or the default value
*/
private static <T> T envVarOrDefault(final String envVarName, final T defaultValue, final Function<String, T> typeConverter) {
final String envVarValue = System.getenv(envVarName);
if (envVarValue != null && !envVarValue.isEmpty()) {
return typeConverter.apply(envVarValue);
} else {
return defaultValue;
}
}
/**
* Gets a value from a System Property or uses the default
* if there is no such variable.
*
* @param sysPropName the name of the system property.
* @param defaultValue the default value to use if there is no system property
* @param typeConverter a function for converting the value of the system
* property (if present) to the desired type
*
* @return the value from the system property, or the default value
*/
private static <T> T sysPropOrDefault(final String sysPropName, final T defaultValue, final Function<String, T> typeConverter) {
final String sysPropValue = System.getProperty(sysPropName);
if (sysPropValue != null && !sysPropValue.isEmpty()) {
return typeConverter.apply(sysPropValue);
} else {
return defaultValue;
}
}
/**
* Gets a value from a System Property, or an Environment Variable if there
* is not such system property. If there is no environment variable, then
* the default value is returned.
*
* @param sysPropName the name of the system property.
* @param envVarName the name of the environment variable if there is no system property.
* @param defaultValue the default value to use if there is no system property or environment variable.
* @param typeConverter a function for converting the value of the system
* property or environment variable (if present) to the desired type
*
* @return the value from the system property, environment variable, or the default value
*/
private static <T> T sysPropOrEnvVarOrDefault(final String sysPropName, final String envVarName,
final T defaultValue, final Function<String, T> typeConverter) {
final T sysPropValue = sysPropOrDefault(sysPropName, null, typeConverter);
if (sysPropValue != null) {
return sysPropValue;
}
return envVarOrDefault(envVarName, defaultValue, typeConverter);
}
static <K, V> Map<K, V> mapOf(final Tuple2<K, V>... entries) {
if (entries == null) {
return Collections.emptyMap();
}
final Map<K, V> map = new HashMap<>(entries.length);
for (final Tuple2<K, V> entry : entries) {
map.put(entry._1, entry._2);
}
return map;
}
static <K, V> Map<K, V>[] arrayOf(final Map<K, V>... entries) {
if (entries == null) {
return new Map[0];
}
final Map<K, V>[] arrayOfMaps = new Map[entries.length];
for (int i = 0; i < entries.length; i++) {
arrayOfMaps[i] = entries[i];
}
return arrayOfMaps;
}
static Tuple2<String, String> getServerVersion() {
final String productName =
given().
accept("text/text").
when().
get(getRestApiBaseUri() + "/db?_query=system:get-product-name()&_wrap=no").
then().
statusCode(SC_OK).
extract().body().asString();
final String version =
given().
accept("text/text").
when().
get(getRestApiBaseUri() + "/db?_query=system:get-version()&_wrap=no").
then().
statusCode(SC_OK).
extract().body().asString();
return Tuple(productName, version);
}
static boolean testServerHasBadJsonSerialization() {
final Tuple2<String, String> serverVersion = getServerVersion();
return serverVersion.equals(Tuple("FusionDB", "1.0.0-ALPHA2"))
|| serverVersion.equals(Tuple("eXist-db", "5.0.0"))
|| serverVersion.equals(Tuple("eXist-db", "5.2.0"));
}
static boolean testServerHasBadXmldbSetMimeType() {
final Tuple2<String, String> serverVersion = getServerVersion();
return serverVersion.equals(Tuple("FusionDB", "1.0.0-ALPHA2"));
}
static boolean testServerHasBadCopyMoveCollectionOperations() {
final Tuple2<String, String> serverVersion = getServerVersion();
return serverVersion.equals(Tuple("FusionDB", "1.0.0-ALPHA3"));
}
}