Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couchbase: Update to latest version, add flushEnabled flag #4041

Merged
merged 6 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions docs/modules/databases/couchbase.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ Running Couchbase as a stand-in in a test:
[Container definition](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:container_definition
<!--/codeinclude-->

3. create an environment & cluster:
3. create an cluster:
<!--codeinclude-->
[Cluster creation](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:cluster_creation
<!--/codeinclude-->

4. authenticate:
<!--codeinclude-->
[Authentication](../../../modules/couchbase/src/test/java/org/testcontainers/couchbase/CouchbaseContainerTest.java) inside_block:auth
<!--/codeinclude-->

## Adding this module to your project dependencies

Add the following dependency to your `pom.xml`/`build.gradle` file:
Expand Down
1 change: 1 addition & 0 deletions modules/couchbase/AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Tayeb Chlyah <tayebchlyah@gmail.com>
Tobias Happ <tobias.happ@gmx.de>
3 changes: 2 additions & 1 deletion modules/couchbase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ description = "Testcontainers :: Couchbase"
dependencies {
api project(':testcontainers')

testImplementation 'com.couchbase.client:java-client:2.7.15'
testImplementation 'com.couchbase.client:java-client:3.1.6'
testImplementation 'org.awaitility:awaitility:3.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@
public class BucketDefinition {

private final String name;
private boolean flushEnabled = false;
private boolean queryPrimaryIndex = true;
private int quota = 100;

public BucketDefinition(final String name) {
this.name = name;
}

/**
* Enables flush for this bucket (disabled by default).
*
* @param flushEnabled if true, the bucket can be flushed.
* @return this {@link BucketDefinition} for chaining purposes.
*/
public BucketDefinition withFlushEnabled(final boolean flushEnabled) {
this.flushEnabled = flushEnabled;
return this;
}

/**
* Sets a custom bucket quota (100MB by default).
*
Expand Down Expand Up @@ -58,6 +70,10 @@ public String getName() {
return name;
}

public boolean hasFlushEnabled() {
return flushEnabled;
}

public boolean hasPrimaryIndex() {
return queryPrimaryIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ private void createBuckets() {
@Cleanup Response response = doHttpRequest(MGMT_PORT, "/pools/default/buckets", "POST", new FormBody.Builder()
.add("name", bucket.getName())
.add("ramQuotaMB", Integer.toString(bucket.getQuota()))
.add("flushEnabled", bucket.hasFlushEnabled() ? "1" : "0")
.build(), true);

checkSuccessfulResponse(response, "Could not create bucket " + bucket.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.json.JsonObject;
import org.junit.Test;
import org.testcontainers.utility.DockerImageName;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.time.Duration;
import java.util.function.Consumer;

import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class CouchbaseContainerTest {

Expand All @@ -45,37 +46,60 @@ public void testBasicContainerUsage() {
.withBucket(bucketDefinition)
// }
) {
container.start();

// cluster_creation {
CouchbaseEnvironment environment = DefaultCouchbaseEnvironment
.builder()
.bootstrapCarrierDirectPort(container.getBootstrapCarrierDirectPort())
.bootstrapHttpDirectPort(container.getBootstrapHttpDirectPort())
.build();

Cluster cluster = CouchbaseCluster.create(
environment,
container.getHost()
);
// }
setUpClient(container, cluster -> {
Bucket bucket = cluster.bucket(bucketDefinition.getName());
bucket.waitUntilReady(Duration.ofSeconds(10L));

Collection collection = bucket.defaultCollection();

collection.upsert("foo", JsonObject.create().put("key", "value"));

JsonObject fooObject = collection.get("foo").contentAsObject();

assertEquals("value", fooObject.getString("key"));
});
}
}

@Test
public void testBucketIsFlushableIfEnabled() {
BucketDefinition bucketDefinition = new BucketDefinition("mybucket")
.withFlushEnabled(true);

try {
// auth {
cluster.authenticate(container.getUsername(), container.getPassword());
// }
try (
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE)
.withBucket(bucketDefinition)
) {
setUpClient(container, cluster -> {
Bucket bucket = cluster.bucket(bucketDefinition.getName());
bucket.waitUntilReady(Duration.ofSeconds(10L));

Collection collection = bucket.defaultCollection();

Bucket bucket = cluster.openBucket(bucketDefinition.getName());
collection.upsert("foo", JsonObject.create().put("key", "value"));

bucket.upsert(JsonDocument.create("foo", JsonObject.empty()));
cluster.buckets().flushBucket(bucketDefinition.getName());
Gerschtli marked this conversation as resolved.
Show resolved Hide resolved

assertTrue(bucket.exists("foo"));
assertNotNull(cluster.clusterManager().getBucket(bucketDefinition.getName()));
} finally {
cluster.disconnect();
environment.shutdown();
}
await().untilAsserted(() -> assertFalse(collection.exists("foo").exists()));
});
}
}

private void setUpClient(CouchbaseContainer container, Consumer<Cluster> consumer) {
container.start();

// cluster_creation {
Cluster cluster = Cluster.connect(
container.getConnectionString(),
container.getUsername(),
container.getPassword()
);
// }

try {
consumer.accept(cluster);
} finally {
cluster.disconnect();
}
}
}