Skip to content

Commit 6501b47

Browse files
authored
Merge pull request #39 from evolvedbinary/sys-props
Prefer system properties over environment variables for test config
2 parents f0ba6ac + c22109c commit 6501b47

File tree

4 files changed

+143
-34
lines changed

4 files changed

+143
-34
lines changed

.travis.yml

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,33 @@ jobs:
1414
- name: Linux / FusionDB Server Nightly
1515
os: linux
1616
env:
17-
- FS_API_PORT=4059
18-
- DOCKER_TEST_IMAGE=repo.evolvedbinary.com:9543/evolvedbinary/fusiondb-server:nightly
19-
script: mvn verify -Ddocker.username=$FDB_NIGHTLY_DOCKER_USER -Ddocker.password=$FDB_NIGHTLY_DOCKER_PASS -Ddocker.test.port=$FS_API_PORT -Ddocker.test.image=$DOCKER_TEST_IMAGE -B
17+
- DOCKER_DB_IMAGE=repo.evolvedbinary.com:9543/evolvedbinary/fusiondb-server:nightly
18+
script: mvn verify -Ddocker.username=$FDB_NIGHTLY_DOCKER_USER -Ddocker.password=$FDB_NIGHTLY_DOCKER_PASS -Dapi.port=4059 -Ddocker.db.image=$DOCKER_DB_IMAGE -B
2019

2120
- name: Linux / FusionDB Server 1.0.0-ALPHA3
2221
os: linux
2322
env:
24-
- FS_API_PORT=4059
25-
- DOCKER_TEST_IMAGE=repo.evolvedbinary.com:9443/evolvedbinary/fusiondb-server:1.0.0-ALPHA3
26-
script: mvn verify -Ddocker.username=$FDB_DOCKER_USER -Ddocker.password=$FDB_DOCKER_PASS -Ddocker.test.port=$FS_API_PORT -Ddocker.test.image=$DOCKER_TEST_IMAGE -B
23+
- DOCKER_DB_IMAGE=repo.evolvedbinary.com:9443/evolvedbinary/fusiondb-server:1.0.0-ALPHA3
24+
script: mvn verify -Ddocker.username=$FDB_DOCKER_USER -Ddocker.password=$FDB_DOCKER_PASS -Dapi.port=4059 -Ddocker.db.image=$DOCKER_DB_IMAGE -B
2725

2826
- name: Linux / eXist-db 5.0.0
2927
os: linux
3028
env:
31-
- FS_API_PORT=8080
32-
- DOCKER_TEST_IMAGE=existdb/existdb:5.0.0
29+
- DOCKER_DB_IMAGE=existdb/existdb:5.0.0
3330

3431
- name: Linux / eXist-db 5.2.0
3532
os: linux
3633
env:
37-
- FS_API_PORT=8080
38-
- DOCKER_TEST_IMAGE=existdb/existdb:5.2.0
34+
- DOCKER_DB_IMAGE=existdb/existdb:5.2.0
3935

4036
- name: Linux / eXist-db Latest
4137
os: linux
4238
env:
43-
- FS_API_PORT=8080
44-
- DOCKER_TEST_IMAGE=existdb/existdb:latest
39+
- DOCKER_DB_IMAGE=existdb/existdb:latest
4540

4641
install: mvn install -Ddocker.skip=true -Dexec.skip=true -DskipTests=true -Dmaven.javadoc.skip=true -B -V
4742

48-
script: mvn verify -Ddocker.test.port=$FS_API_PORT -Ddocker.test.image=$DOCKER_TEST_IMAGE -B
43+
script: mvn verify -Dapi.port=8080 -Ddocker.db.image=$DOCKER_DB_IMAGE -B
4944

5045
cache:
5146
directories:

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,47 @@ $ mvn clean package
2323
```
2424

2525
This will create an EXPath package in `target/fusion-studio-api-x.y.z.xar`, which can be deployed to FusionDB (or eXist-db).
26+
27+
## Test Instructions
28+
By default tests are executed against the latest FusionDB nightly build running as a Docker Container.
29+
For development purposes, it is also possible to execute the tests from an IDE such as IntelliJ against a locally installed FusionDB (or eXist-db) server.
30+
31+
Requirements:
32+
* Docker
33+
* Username/Password for accessing the FusionDB nightly build Docker repository.
34+
35+
Various settings of the build can be overridden using the following System Properties and Environment Variables:
36+
| Purpose | System Property | Environment Variable |
37+
|---------|-----------------|----------------------|
38+
| Docker Repo Username | `docker.username` | N/A |
39+
| Docker Repo Password | `docker.password` | N/A |
40+
| Docker Database Image | `docker.db.image` | N/A |
41+
| Host for Fusion Studio API | `api.host` | `API_HOST` |
42+
| Port for Fusion Studio API | `api.port` | `API_PORT` |
43+
44+
**NOTE:** If you wish to use the FusionDB Nightly Build Docker Container, you must configure your username and password for the
45+
repository in either your Maven Settings file (`~/.m2/settings.xml`), or provide the values via System Properties (see above).
46+
For the Maven Settings file, the following should be added to the `<servers>` section:
47+
```xml
48+
<server>
49+
<id>repo.evolvedbinary.com:9543</id>
50+
<username>your-username</username>
51+
<password>your-password</password>
52+
</server>
53+
```
54+
55+
You can also alternatively test the latest release version of FusionDB by setting the system properties for the Docker image: `repo.evolvedbinary.com:9443/evolvedbinary/fusiondb-server:latest`.
56+
57+
**NOTE:** If you wish to perform the tests against eXist-db as opposed to FusionDB, you need to set the Docker Image, and then the Host and Port
58+
for the API via System Properties of Environment Variables, as by default it is configured for FusionDB.
59+
60+
### Running tests with Docker
61+
Just execute the following:
62+
```
63+
$ mvn clean verify
64+
```
65+
66+
or, if you need to set system properties you can do so like (e.g. for testing against eXist-db 5.2.0):
67+
```
68+
$ mvn clean verify -Dapi.port=8080 -Ddocker.db.image=existdb/existdb:5.2.0
69+
```

pom.xml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@
5454
<exist.version>5.0.0</exist.version>
5555

5656
<!-- used for integration-test -->
57-
<docker.test.image>repo.evolvedbinary.com:9543/evolvedbinary/fusiondb-server:nightly</docker.test.image>
58-
<docker.test.port>4059</docker.test.port>
57+
<docker.db.image>repo.evolvedbinary.com:9543/evolvedbinary/fusiondb-server:nightly</docker.db.image>
58+
<api.host>localhost</api.host>
59+
<api.port>4059</api.port>
5960

6061
<!-- used in the EXPath Package Descriptor -->
6162
<package-name>https://fusiondb.com/studio/api</package-name>
@@ -186,9 +187,11 @@
186187
<images>
187188
<image>
188189
<alias>db-with-api</alias>
189-
<name>${docker.test.image}</name>
190+
<name>${docker.db.image}</name>
190191
<run>
191-
<ports>${docker.test.port}:${docker.test.port}</ports>
192+
<ports>
193+
<port>${api.port}:${api.port}</port>
194+
</ports>
192195
<wait>
193196
<log>Server has started, listening on</log>
194197
<time>120000</time>
@@ -238,7 +241,7 @@
238241
<argument>Content-Type: application/octet-stream</argument>
239242
<argument>--data-binary</argument>
240243
<argument>@${project.build.directory}/${package-final-name}.xar</argument>
241-
<argument>http://admin:@localhost:${docker.test.port}/exist/rest/db/${package-final-name}.xar</argument>
244+
<argument>http://admin:@${api.host}:${api.port}/exist/rest/db/${package-final-name}.xar</argument>
242245
</arguments>
243246
</configuration>
244247
</execution>
@@ -259,7 +262,7 @@
259262
<argument>Content-Type: application/xml</argument>
260263
<argument>--data-binary</argument>
261264
<argument>@${project.build.testOutputDirectory}/deploy-xar.xq.xml</argument>
262-
<argument>http://admin:@localhost:${docker.test.port}/exist/rest/db/</argument>
265+
<argument>http://admin:@${api.host}:${api.port}/exist/rest/db/</argument>
263266
</arguments>
264267
</configuration>
265268
</execution>
@@ -277,6 +280,9 @@
277280
<goals>
278281
<goal>integration-test</goal>
279282
</goals>
283+
<configuration>
284+
<argLine>-Dapi.port=@{api.port}</argLine>
285+
</configuration>
280286
</execution>
281287
<execution>
282288
<id>verify-integration-tests-result</id>

src/test/java/com/fusiondb/studio/api/API.java

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,54 @@ public class API {
5151
*/
5252
public static String DEFAULT_ENDPOINT = "fusiondb";
5353

54+
/**
55+
* System Property for setting the API Host
56+
*/
57+
public static String SYS_PROP_NAME_API_HOST = "api.host";
58+
5459
/**
5560
* Environment variable name for setting the API Host
5661
*/
57-
public static String ENV_VAR_FS_API_HOST = "FS_API_HOST";
62+
public static String ENV_VAR_API_HOST = "API_HOST";
63+
64+
/**
65+
* System Property for setting the API Port
66+
*/
67+
public static String SYS_PROP_NAME_API_PORT = "api.port";
5868

5969
/**
6070
* Environment variable name for setting the API Port
6171
*/
62-
public static String ENV_VAR_FS_API_PORT = "FS_API_PORT";
72+
public static String ENV_VAR_API_PORT = "API_PORT";
73+
74+
/**
75+
* System Property for setting the Docker DB Image
76+
*/
77+
public static String SYS_PROP_NAME_DOCKER_DB_IMAGE = "docker.db.image";
6378

64-
private static String ENV_VAR_DOCKER_TEST_IMAGE = "DOCKER_TEST_IMAGE";
79+
/**
80+
* Environment Variable for setting the Docker DB Image
81+
*/
82+
private static String ENV_VAR_DOCKER_DB_IMAGE = "DOCKER_DB_IMAGE";
6583

6684
/**
6785
* Get the Base URI for the Fusion Studio API.
6886
*
69-
* The URI can be overridden by environment variables
70-
* see {@link #ENV_VAR_FS_API_HOST} and {@link #ENV_VAR_FS_API_PORT}.
87+
* The URI can be overridden by System Properties firstly,
88+
* or Environment Variables secondly.
89+
* See {@link #SYS_PROP_NAME_API_HOST} {@link #SYS_PROP_NAME_API_PORT}
90+
* {@link #ENV_VAR_API_HOST} and {@link #ENV_VAR_API_PORT}.
7191
*
7292
* @return the base URI
7393
*/
7494
public static String getApiBaseUri() {
75-
final String host = envVarOrDefault(ENV_VAR_FS_API_HOST, DEFAULT_HOST, envVarValue -> envVarValue);
76-
final int port = envVarOrDefault(ENV_VAR_FS_API_PORT, DEFAULT_PORT, envVarValue -> {
95+
final String host = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_HOST, ENV_VAR_API_HOST, DEFAULT_HOST, envVarValue -> envVarValue);
96+
final int port = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_PORT, ENV_VAR_API_PORT, DEFAULT_PORT, envVarValue -> {
7797
try {
7898
return Integer.parseInt(envVarValue);
7999
} catch (final NumberFormatException e) {
80100
// invalid number
81-
System.err.println("ENV.FS_API_PORT=" + envVarValue + ", is not a valid TCP port number. Using default: " + DEFAULT_PORT);
101+
System.err.println("Port " + envVarValue + " is not a valid TCP port number. Using default: " + DEFAULT_PORT);
82102
e.printStackTrace();
83103
return DEFAULT_PORT;
84104
}
@@ -90,19 +110,21 @@ public static String getApiBaseUri() {
90110
/**
91111
* Get the Base URI for the eXist-db REST API.
92112
*
93-
* The URI can be overridden by environment variables
94-
* see {@link #ENV_VAR_FS_API_HOST} and {@link #ENV_VAR_FS_API_PORT}.
113+
* The URI can be overridden by System Properties firstly,
114+
* or Environment Variables secondly.
115+
* See {@link #SYS_PROP_NAME_API_HOST} {@link #SYS_PROP_NAME_API_PORT}
116+
* {@link #ENV_VAR_API_HOST} and {@link #ENV_VAR_API_PORT}.
95117
*
96118
* @return the REST base URI
97119
*/
98120
public static String getRestApiBaseUri() {
99-
final String host = envVarOrDefault(ENV_VAR_FS_API_HOST, DEFAULT_HOST, envVarValue -> envVarValue);
100-
final int port = envVarOrDefault(ENV_VAR_FS_API_PORT, DEFAULT_PORT, envVarValue -> {
121+
final String host = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_HOST, ENV_VAR_API_HOST, DEFAULT_HOST, envVarValue -> envVarValue);
122+
final int port = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_API_PORT, ENV_VAR_API_PORT, DEFAULT_PORT, envVarValue -> {
101123
try {
102124
return Integer.parseInt(envVarValue);
103125
} catch (final NumberFormatException e) {
104126
// invalid number
105-
System.err.println("ENV.FS_API_PORT=" + envVarValue + ", is not a valid TCP port number. Using default: " + DEFAULT_PORT);
127+
System.err.println("Port " + envVarValue + " is not a valid TCP port number. Using default: " + DEFAULT_PORT);
106128
e.printStackTrace();
107129
return DEFAULT_PORT;
108130
}
@@ -131,6 +153,48 @@ private static <T> T envVarOrDefault(final String envVarName, final T defaultVal
131153
}
132154
}
133155

156+
/**
157+
* Gets a value from a System Property or uses the default
158+
* if there is no such variable.
159+
*
160+
* @param sysPropName the name of the system property.
161+
* @param defaultValue the default value to use if there is no system property
162+
* @param typeConverter a function for converting the value of the system
163+
* property (if present) to the desired type
164+
*
165+
* @return the value from the system property, or the default value
166+
*/
167+
private static <T> T sysPropOrDefault(final String sysPropName, final T defaultValue, final Function<String, T> typeConverter) {
168+
final String sysPropValue = System.getProperty(sysPropName);
169+
if (sysPropValue != null && !sysPropValue.isEmpty()) {
170+
return typeConverter.apply(sysPropValue);
171+
} else {
172+
return defaultValue;
173+
}
174+
}
175+
176+
/**
177+
* Gets a value from a System Property, or an Environment Variable if there
178+
* is not such system property. If there is no environment variable, then
179+
* the default value is returned.
180+
*
181+
* @param sysPropName the name of the system property.
182+
* @param envVarName the name of the environment variable if there is no system property.
183+
* @param defaultValue the default value to use if there is no system property or environment variable.
184+
* @param typeConverter a function for converting the value of the system
185+
* property or environment variable (if present) to the desired type
186+
*
187+
* @return the value from the system property, environment variable, or the default value
188+
*/
189+
private static <T> T sysPropOrEnvVarOrDefault(final String sysPropName, final String envVarName,
190+
final T defaultValue, final Function<String, T> typeConverter) {
191+
final T sysPropValue = sysPropOrDefault(sysPropName, null, typeConverter);
192+
if (sysPropValue != null) {
193+
return sysPropValue;
194+
}
195+
return envVarOrDefault(envVarName, defaultValue, typeConverter);
196+
}
197+
134198
static <K, V> Map<K, V> mapOf(final Tuple2<K, V>... entries) {
135199
if (entries == null) {
136200
return Collections.emptyMap();
@@ -157,7 +221,7 @@ static <K, V> Map<K, V>[] arrayOf(final Map<K, V>... entries) {
157221
}
158222

159223
static boolean testServerHasBadJsonSerialization() {
160-
final String dockerTestImage = envVarOrDefault(ENV_VAR_DOCKER_TEST_IMAGE, null, envVarValue -> envVarValue);
224+
final String dockerTestImage = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_DOCKER_DB_IMAGE, ENV_VAR_DOCKER_DB_IMAGE, null, envVarValue -> envVarValue);
161225
if (dockerTestImage == null || dockerTestImage.isEmpty()) {
162226
return false;
163227
}
@@ -168,7 +232,7 @@ static boolean testServerHasBadJsonSerialization() {
168232
}
169233

170234
static boolean testServerHasBadXmldbSetMimeType() {
171-
final String dockerTestImage = envVarOrDefault(ENV_VAR_DOCKER_TEST_IMAGE, null, envVarValue -> envVarValue);
235+
final String dockerTestImage = sysPropOrEnvVarOrDefault(SYS_PROP_NAME_DOCKER_DB_IMAGE, ENV_VAR_DOCKER_DB_IMAGE, null, envVarValue -> envVarValue);
172236
if (dockerTestImage == null || dockerTestImage.isEmpty()) {
173237
return false;
174238
}

0 commit comments

Comments
 (0)