-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from MartinWitt/passing-falcon
feat: Add dev service with testcontainer
- Loading branch information
Showing
7 changed files
with
180 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
deployment/src/main/java/com/couchbase/quarkus/extension/deployment/CouchbaseDevService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.couchbase.quarkus.extension.deployment; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.testcontainers.couchbase.CouchbaseContainer; | ||
import org.testcontainers.couchbase.CouchbaseService; | ||
import io.quarkus.deployment.IsNormal; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.annotations.BuildSteps; | ||
import io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem; | ||
import io.quarkus.deployment.builditem.DevServicesResultBuildItem; | ||
import io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService; | ||
import io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig; | ||
|
||
/** | ||
* This class provides a Quarkus DevService for Couchbase. The dev service provides all non enterprise features of Couchbase. | ||
*/ | ||
@BuildSteps(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class) | ||
|
||
public class CouchbaseDevService { | ||
|
||
|
||
static volatile List<RunningDevService> devServices = new ArrayList<>(); | ||
|
||
@BuildStep | ||
List<DevServicesResultBuildItem> startCouchBase( | ||
CuratedApplicationShutdownBuildItem closeBuildItem) { | ||
QuarkusCouchbaseContainer couchbase = startContainer(); | ||
devServices.add(new RunningDevService(CouchbaseQuarkusExtensionProcessor.FEATURE, | ||
couchbase.getContainerId(), couchbase::close, Map.of())); | ||
closeBuildItem.addCloseTask(couchbase::close, true); | ||
return devServices.stream().map(RunningDevService::toBuildItem).collect(Collectors.toList()); | ||
|
||
} | ||
|
||
private QuarkusCouchbaseContainer startContainer() { | ||
String userName = ConfigProvider.getConfig() | ||
.getOptionalValue("quarkus.couchbase.username", String.class).orElse("Administrator");; | ||
String password = ConfigProvider.getConfig() | ||
.getOptionalValue("quarkus.couchbase.password", String.class).orElse("password"); | ||
String version = ConfigProvider.getConfig().getValue("quarkus.couchbase.version", String.class); | ||
QuarkusCouchbaseContainer couchbase = | ||
new QuarkusCouchbaseContainer(version, userName, password); | ||
couchbase.start(); | ||
return couchbase; | ||
} | ||
|
||
/** | ||
* A {@link CouchbaseContainer} that exposes all ports to fixed values. | ||
* <p> | ||
* This is needed because the default {@link CouchbaseContainer} exposes ports to random values. | ||
*/ | ||
private static class QuarkusCouchbaseContainer extends CouchbaseContainer { | ||
|
||
// copied from org.testcontainers.couchbase.CouchbaseContainer | ||
private static final int MGMT_PORT = 8091; | ||
private static final int MGMT_SSL_PORT = 18091; | ||
private static final int VIEW_PORT = 8092; | ||
private static final int VIEW_SSL_PORT = 18092; | ||
private static final int QUERY_PORT = 8093; | ||
private static final int QUERY_SSL_PORT = 18093; | ||
private static final int SEARCH_PORT = 8094; | ||
private static final int SEARCH_SSL_PORT = 18094; | ||
private static final int ANALYTICS_PORT = 8095; | ||
private static final int ANALYTICS_SSL_PORT = 18095; | ||
private static final int EVENTING_PORT = 8096; | ||
private static final int EVENTING_SSL_PORT = 18096; | ||
private static final int KV_PORT = 11210; | ||
private static final int KV_SSL_PORT = 11207; | ||
|
||
|
||
public QuarkusCouchbaseContainer(String version, String userName, String password) { | ||
super("couchbase/server:" + version); | ||
withCredentials(userName, password); | ||
// we enable all non-enterprise services because we don't know which ones are needed | ||
withEnabledServices(CouchbaseService.EVENTING, CouchbaseService.INDEX, CouchbaseService.KV, | ||
CouchbaseService.QUERY, CouchbaseService.SEARCH); | ||
addFixedExposedPort(MGMT_PORT, MGMT_PORT); | ||
addFixedExposedPort(MGMT_SSL_PORT, MGMT_SSL_PORT); | ||
addFixedExposedPort(ANALYTICS_PORT, ANALYTICS_PORT); | ||
addFixedExposedPort(VIEW_PORT, VIEW_PORT); | ||
addFixedExposedPort(VIEW_SSL_PORT, VIEW_SSL_PORT); | ||
addFixedExposedPort(ANALYTICS_PORT, ANALYTICS_PORT); | ||
addFixedExposedPort(ANALYTICS_SSL_PORT, ANALYTICS_SSL_PORT); | ||
addFixedExposedPort(QUERY_PORT, QUERY_PORT); | ||
addFixedExposedPort(QUERY_SSL_PORT, QUERY_SSL_PORT); | ||
addFixedExposedPort(SEARCH_PORT, SEARCH_PORT); | ||
addFixedExposedPort(SEARCH_SSL_PORT, SEARCH_SSL_PORT); | ||
addFixedExposedPort(EVENTING_PORT, EVENTING_PORT); | ||
addFixedExposedPort(EVENTING_SSL_PORT, EVENTING_SSL_PORT); | ||
addFixedExposedPort(KV_PORT, KV_PORT); | ||
addFixedExposedPort(KV_SSL_PORT, KV_SSL_PORT); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
quarkus.couchbase.connection-string=localhost | ||
quarkus.couchbase.username=admin | ||
quarkus.couchbase.password=admin | ||
quarkus.couchbase.username=Administrator | ||
# needs to be at least 8 characters | ||
quarkus.couchbase.password=password |
64 changes: 64 additions & 0 deletions
64
integration-tests/src/test/java/com/couchbase/quarkus/extension/it/DevServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.couchbase.quarkus.extension.it; | ||
|
||
import java.time.Duration; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import com.couchbase.client.java.Bucket; | ||
import com.couchbase.client.java.Cluster; | ||
import com.couchbase.client.java.Collection; | ||
import com.couchbase.client.java.Scope; | ||
import com.couchbase.client.java.json.JsonObject; | ||
import com.couchbase.client.java.kv.GetResult; | ||
import com.couchbase.client.java.kv.MutationResult; | ||
import com.couchbase.client.java.manager.bucket.BucketSettings; | ||
import com.couchbase.client.java.manager.collection.CollectionSpec; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class DevServiceTest { | ||
|
||
|
||
Cluster cluster; | ||
|
||
public DevServiceTest(Cluster cluster) { | ||
this.cluster = cluster; | ||
} | ||
|
||
@Test | ||
void createBucketTest() { | ||
// tests creating a bucket and waiting for it to be ready | ||
cluster.buckets().createBucket(BucketSettings.create("testBucket")); | ||
cluster.bucket("testBucket").waitUntilReady(Duration.ofMinutes(2)); | ||
Assertions.assertNotNull(cluster.buckets().getAllBuckets().get("testBucket")); | ||
cluster.buckets().dropBucket("testBucket"); | ||
} | ||
|
||
@Test | ||
void insertDocument() { | ||
// this is an adapted example from the couchbase doc and licensed under their terms. | ||
//TODO: what license is this under? | ||
String bucketName = "travel-sample"; | ||
cluster.buckets().createBucket(BucketSettings.create(bucketName)); | ||
// get a bucket reference | ||
Bucket bucket = cluster.bucket(bucketName); | ||
bucket.waitUntilReady(Duration.ofSeconds(120)); | ||
bucket.collections().createScope("tenant_agent_00"); | ||
bucket.collections().createCollection(CollectionSpec.create("users", "tenant_agent_00")); | ||
// get a user-defined collection reference | ||
Scope scope = bucket.scope("tenant_agent_00"); | ||
Collection collection = scope.collection("users"); | ||
|
||
// Upsert Document | ||
MutationResult upsertResult = | ||
collection.upsert("my-document", JsonObject.create().put("name", "mike")); | ||
Assertions.assertNotNull(upsertResult, | ||
"Upsert result is null and therefore the operation failed."); | ||
// Get Document | ||
GetResult getResult = collection.get("my-document"); | ||
String name = getResult.contentAsObject().getString("name"); | ||
Assertions.assertEquals("mike", name); | ||
// cleanup afterwards | ||
// TODO: maybe we need a @AfterEach method to clean up? | ||
cluster.buckets().dropBucket(bucketName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters