Skip to content

Commit

Permalink
Closes #197 - added DatabaseService enum for integration
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Feb 21, 2024
1 parent d2f01fb commit 3712cbb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
12 changes: 2 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ buildscript {
// https://github.com/FasterXML/jackson/wiki/Jackson-Releases
jacksonVersion = '2.16.1' // https://github.com/FasterXML/jackson-databind/tags
// retrofit is why we need allow-opens. context: https://stackoverflow.com/questions/60915381/retrofit2-maven-project-illegal-reflective-access-warning // https://github.com/square/retrofit/issues/3341
// Note that after Java 14, this isn't a problem, so I'm not inclined to keep having add-opens because of retrofit
retrofitVersion = "2.9.0" // https://github.com/square/retrofit/tags
shadowVersion = '8.1.1' // https://github.com/johnrengelman/shadow/releases
ioLibVersion = '2.3.0' // https://github.com/retrodaredevil/io-lib/releases
Expand Down Expand Up @@ -105,11 +106,6 @@ subprojects {
}
test {
useJUnitPlatform()
if(JavaVersion.current() >= JavaVersion.VERSION_1_9) {
// If we don't have this, we get warnings for something about retrofit2.Platform
// - likely solcast action node
jvmArgs "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED"
}
}
tasks.withType(JavaCompile).configureEach {
options.errorprone {
Expand Down Expand Up @@ -235,15 +231,11 @@ project(":client"){
excludeTags 'integration'
}
}
task integration(type: Test) {
tasks.register('integration', Test) {
// In the future, we can use the Test Suite feature rather than doing this manually: https://docs.gradle.org/7.3.1/userguide/jvm_test_suite_plugin.html
useJUnitPlatform {
includeTags 'integration'
}
if(JavaVersion.current() >= JavaVersion.VERSION_1_9) {
// If we don't have this, we get warnings for something about retrofit2.Platform
jvmArgs "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED"
}
}
dockerCompose {
useComposeFiles = ['../testing/couchdb-compose.yml']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import me.retrodaredevil.solarthing.packets.instance.InstanceFragmentIndicatorPackets;
import me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Instant;
import java.time.ZoneId;
Expand All @@ -41,13 +42,15 @@ private static PacketCollection createSimplePacketCollection() {
);
}

@Test
void test() throws CouchDbException, SolarThingDatabaseException {
IntegrationSetup.setup(IntegrationUtil.createCouchDbInstance(IntegrationUtil.DEFAULT_ADMIN_AUTH));

@ParameterizedTest
@MethodSource("me.retrodaredevil.solarthing.integration.DatabaseService#all")
void test(DatabaseService databaseService) throws CouchDbException, SolarThingDatabaseException {
IntegrationSetup.setup(IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.DEFAULT_ADMIN_AUTH));

{
// No auth
CouchDbInstance noAuthInstance = IntegrationUtil.createCouchDbInstance(CouchDbAuth.createNoAuth());
CouchDbInstance noAuthInstance = IntegrationUtil.createCouchDbInstance(databaseService, CouchDbAuth.createNoAuth());
SolarThingDatabase database = CouchDbSolarThingDatabase.create(noAuthInstance);
database.getStatusDatabase().query(new MillisQueryBuilder().startKey(System.currentTimeMillis()).build()); // anyone can query status
database.getEventDatabase().query(new MillisQueryBuilder().startKey(System.currentTimeMillis()).build()); // anyone can query events
Expand All @@ -69,7 +72,7 @@ void test() throws CouchDbException, SolarThingDatabaseException {
}
{
// Uploader user
CouchDbInstance uploaderInstance = IntegrationUtil.createCouchDbInstance(IntegrationUtil.getAuthFor(SolarThingDatabaseType.UserType.UPLOADER));
CouchDbInstance uploaderInstance = IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.getAuthFor(SolarThingDatabaseType.UserType.UPLOADER));
SolarThingDatabase database = CouchDbSolarThingDatabase.create(uploaderInstance);

PacketCollection packetCollection = createSimplePacketCollection();
Expand All @@ -78,7 +81,7 @@ void test() throws CouchDbException, SolarThingDatabaseException {
}
{
// Manager user
CouchDbInstance managerInstance = IntegrationUtil.createCouchDbInstance(IntegrationUtil.getAuthFor(SolarThingDatabaseType.UserType.MANAGER));
CouchDbInstance managerInstance = IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.getAuthFor(SolarThingDatabaseType.UserType.MANAGER));
SolarThingDatabase database = CouchDbSolarThingDatabase.create(managerInstance);

PacketCollection packetCollection = createSimplePacketCollection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.retrodaredevil.solarthing.integration;

public enum DatabaseService {
COUCHDB("couchdb"),
;

private final String serviceName;
DatabaseService(String serviceName) {
this.serviceName = serviceName;
}

public String getHost() {
String key = serviceName + ".host";
String host = System.getProperty(key);
if (host == null) {
throw new IllegalStateException("System property does not exist for key: " + key);
}
return host;
}
public int getPort() {
// The gradle compose plugin sets system properties for the services defined in the docker compose file:
// https://github.com/avast/gradle-docker-compose-plugin
String key = serviceName + ".tcp.5984";
String portString = System.getProperty(key);
if (portString == null) {
throw new IllegalStateException("System property does not exist for key: " + key);
}
return Integer.parseInt(portString);
}
public static DatabaseService[] couchOnly() {
return new DatabaseService[]{COUCHDB};
}
public static DatabaseService[] all() {
return values();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public final class IntegrationUtil {

public static final CouchDbAuth DEFAULT_ADMIN_AUTH = CouchDbAuth.create("admin", "password");

public static CouchDbInstance createCouchDbInstance(CouchDbAuth auth) {
return createCouchDbInstance(auth, false);
public static CouchDbInstance createCouchDbInstance(DatabaseService databaseService, CouchDbAuth auth) {
return createCouchDbInstance(databaseService, auth, false);
}
public static CouchDbInstance createDebugCouchDbInstance(CouchDbAuth auth) {
return createCouchDbInstance(auth, true);
public static CouchDbInstance createDebugCouchDbInstance(DatabaseService databaseService, CouchDbAuth auth) {
return createCouchDbInstance(databaseService, auth, true);
}

private static CouchDbInstance createCouchDbInstance(CouchDbAuth auth, boolean debug) {
private static CouchDbInstance createCouchDbInstance(DatabaseService databaseService, CouchDbAuth auth, boolean debug) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (debug){
builder.addInterceptor(new HttpLoggingInterceptor(System.out::println).setLevel(HttpLoggingInterceptor.Level.BODY));
Expand All @@ -32,8 +32,8 @@ private static CouchDbInstance createCouchDbInstance(CouchDbAuth auth, boolean d
builder.build(),
new HttpUrl.Builder()
.scheme("http")
.host("localhost")
.port(5984)
.host(databaseService.getHost())
.port(databaseService.getPort())
.build(),
new BasicAuthHandler(auth)
);
Expand Down
2 changes: 1 addition & 1 deletion testing/couchdb-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ services:
- 'COUCHDB_USER=admin'
- 'COUCHDB_PASSWORD=password'
ports:
- '5984:5984'
- '5984'

0 comments on commit 3712cbb

Please sign in to comment.