Skip to content

Commit 9305fda

Browse files
committed
tests: improve setUp logic for Couchbase container configuration
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 474bb36 commit 9305fda

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

jnosql-couchbase/src/main/java/org/eclipse/jnosql/databases/couchbase/communication/CouchbaseSettings.java

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Contributors:
1212
*
1313
* Otavio Santana
14+
* Maximillian Arruda
1415
*/
1516
package org.eclipse.jnosql.databases.couchbase.communication;
1617

@@ -22,15 +23,14 @@
2223
import com.couchbase.client.java.manager.collection.CollectionManager;
2324
import com.couchbase.client.java.manager.collection.CollectionSpec;
2425
import com.couchbase.client.java.manager.collection.ScopeSpec;
25-
import com.couchbase.client.java.manager.query.QueryIndex;
2626
import com.couchbase.client.java.manager.query.QueryIndexManager;
2727

28-
import java.util.Collections;
29-
import java.util.List;
30-
import java.util.Objects;
31-
import java.util.Optional;
28+
import java.time.Duration;
29+
import java.time.temporal.ChronoUnit;
30+
import java.util.*;
3231
import java.util.logging.Level;
3332
import java.util.logging.Logger;
33+
import java.util.stream.Collectors;
3434

3535
import static com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions;
3636
import static com.couchbase.client.java.manager.query.GetAllQueryIndexesOptions.getAllQueryIndexesOptions;
@@ -154,61 +154,78 @@ public Cluster getCluster() {
154154
* @throws NullPointerException when parameter is null
155155
*/
156156
public void setUp(String database) {
157+
157158
Objects.requireNonNull(database, "database is required");
158159

160+
CouchbaseSettings settings=this;
161+
162+
var collections = settings.getCollections().stream().map(String::trim)
163+
.filter(index -> !index.isBlank()).toList();
164+
165+
var collectionsToIndex = Arrays
166+
.stream(Optional.ofNullable(settings.getIndex()).orElse("").split(","))
167+
.map(String::trim)
168+
.filter(index -> !index.isBlank()).collect(Collectors.toSet());
169+
170+
var scope = settings.getScope();
171+
159172
long start = System.currentTimeMillis();
160-
LOGGER.log(Level.FINEST, "starting the setup with database: " + database);
173+
LOGGER.log(Level.FINEST,"starting the setup with database: " + database);
161174

162-
try (Cluster cluster = getCluster()) {
175+
try (Cluster cluster = settings.getCluster()) {
163176

164177
BucketManager buckets = cluster.buckets();
165178
try {
166179
buckets.getBucket(database);
167180
} catch (BucketNotFoundException exp) {
168-
LOGGER.log(Level.FINEST, "The database/bucket does not exist, creating it: " + database);
181+
LOGGER.log(Level.FINEST,"The database/bucket does not exist, creating it: " + database);
169182
buckets.createBucket(BucketSettings.create(database));
170183
}
184+
171185
Bucket bucket = cluster.bucket(database);
172186

187+
waitUntilReady(bucket);
188+
173189
CollectionManager manager = bucket.collections();
190+
174191
List<ScopeSpec> scopes = manager.getAllScopes();
175-
String finalScope = getScope().orElseGet(() -> bucket.defaultScope().name());
192+
String finalScope = scope.orElseGet(() -> bucket.defaultScope().name());
176193
ScopeSpec spec = scopes.stream().filter(s -> finalScope.equals(s.name()))
177194
.findFirst().get();
178-
for (String collection : collections) {
179-
if (spec.collections().stream().noneMatch(c -> collection.equals(c.name()))) {
195+
196+
collectionsToIndex.forEach(collection -> {
197+
if (spec.collections().stream().noneMatch(c -> collectionsToIndex.contains(c.name()))) {
180198
manager.createCollection(CollectionSpec.create(collection, finalScope));
181199
}
182-
}
183-
if (index != null) {
184-
QueryIndexManager queryIndexManager = cluster.queryIndexes();
185-
List<QueryIndex> indexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions()
186-
.scopeName(finalScope).collectionName(index));
187-
if (indexes.isEmpty()) {
188-
LOGGER.log(Level.FINEST, "Index does not exist, creating primary key with scope "
189-
+ scope + " collection " + index + " at database " + database);
190-
queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions()
191-
.scopeName(finalScope).collectionName(index));
192-
}
200+
});
193201

194-
for (String collection : collections) {
195-
queryIndexManager = cluster.queryIndexes();
196-
indexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions()
197-
.scopeName(finalScope).collectionName(collection));
198-
if (indexes.isEmpty()) {
199-
LOGGER.log(Level.FINEST, "Index for " + collection + " collection does not exist, creating primary key with scope "
200-
+ scope + " collection " + collection + " at database " + database);
201-
queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions()
202-
.scopeName(finalScope).collectionName(collection));
203-
}
204-
}
202+
waitUntilReady(bucket);
205203

204+
if (!collectionsToIndex.isEmpty()) {
205+
QueryIndexManager queryIndexManager = cluster.queryIndexes();
206+
collections.stream()
207+
.filter(collectionsToIndex::contains)
208+
.forEach(collection -> {
209+
var allIndexes = queryIndexManager.getAllIndexes(database, getAllQueryIndexesOptions()
210+
.scopeName(finalScope).collectionName(collection));
211+
if (allIndexes.isEmpty()) {
212+
LOGGER.log(Level.FINEST,"Index for " + collection + " collection does not exist, creating primary key with scope "
213+
+ finalScope + " collection " + collection + " at database " + database);
214+
queryIndexManager.createPrimaryIndex(database, createPrimaryQueryIndexOptions()
215+
.scopeName(finalScope).collectionName(collection));
216+
}
217+
});
206218
}
207219

208220
long end = System.currentTimeMillis() - start;
209-
LOGGER.log(Level.FINEST, "Finished the setup with database: " + database + " end with millis "
221+
LOGGER.log(Level.FINEST,"Finished the setup with database: " + database + " end with millis "
210222
+ end);
211223
}
224+
225+
}
226+
227+
private void waitUntilReady(Bucket bucket) {
228+
bucket.waitUntilReady(Duration.of(4, ChronoUnit.SECONDS));
212229
}
213230

214231
@Override

0 commit comments

Comments
 (0)