Skip to content

Commit 9200422

Browse files
committed
increase test coverage
1 parent 84a12f2 commit 9200422

29 files changed

+1197
-1412
lines changed

astra-db-java/src/main/java/com/datastax/astra/client/Collection.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
* #L%
2121
*/
2222

23-
import com.datastax.astra.client.exception.DataApiException;
2423
import com.datastax.astra.client.exception.DataAPIFaultyResponseException;
24+
import com.datastax.astra.client.exception.DataApiException;
25+
import com.datastax.astra.client.exception.DataApiResponseException;
2526
import com.datastax.astra.client.exception.TooManyDocumentsToCountException;
2627
import com.datastax.astra.client.model.BulkWriteOptions;
2728
import com.datastax.astra.client.model.BulkWriteResult;
28-
import com.datastax.astra.client.model.CollectionIdTypes;
2929
import com.datastax.astra.client.model.CollectionInfo;
3030
import com.datastax.astra.client.model.CollectionOptions;
3131
import com.datastax.astra.client.model.Command;
@@ -51,14 +51,13 @@
5151
import com.datastax.astra.client.model.UUIDv6;
5252
import com.datastax.astra.client.model.UUIDv7;
5353
import com.datastax.astra.client.model.Update;
54+
import com.datastax.astra.client.model.UpdateManyOptions;
5455
import com.datastax.astra.client.model.UpdateOneOptions;
5556
import com.datastax.astra.client.model.UpdateResult;
56-
import com.datastax.astra.internal.command.AbstractCommandRunner;
5757
import com.datastax.astra.internal.api.ApiResponse;
58+
import com.datastax.astra.internal.command.AbstractCommandRunner;
5859
import com.datastax.astra.internal.command.LoggingCommandObserver;
5960
import com.datastax.astra.internal.utils.Assert;
60-
import com.datastax.astra.internal.utils.CustomEJsonInstantDeserializer;
61-
import com.datastax.astra.internal.utils.CustomUuidv6Serializer;
6261
import com.datastax.astra.internal.utils.JsonUtils;
6362
import lombok.Getter;
6463
import lombok.extern.slf4j.Slf4j;
@@ -71,6 +70,7 @@
7170
import java.util.UUID;
7271
import java.util.concurrent.Callable;
7372
import java.util.concurrent.CompletableFuture;
73+
import java.util.concurrent.ExecutionException;
7474
import java.util.concurrent.ExecutorService;
7575
import java.util.concurrent.Executors;
7676
import java.util.concurrent.Future;
@@ -354,7 +354,7 @@ public String getName() {
354354
*/
355355
public final InsertOneResult insertOne(T document) {
356356
Assert.notNull(document, "document");
357-
return _insertOne(JsonUtils.convertValueForDataApi(document, Document.class));
357+
return _insertOne(JsonUtils.convertValue(document, Document.class));
358358
}
359359

360360
/**
@@ -428,7 +428,7 @@ public final CompletableFuture<InsertOneResult> insertOneAsync(T document) {
428428
public final InsertOneResult insertOne(T document, float[] embeddings) {
429429
Assert.notNull(document, "document");
430430
Assert.notNull(embeddings, "vectorize");
431-
return _insertOne(JsonUtils.convertValueForDataApi(document, Document.class).vector(embeddings));
431+
return _insertOne(JsonUtils.convertValue(document, Document.class).vector(embeddings));
432432
}
433433

434434
/**
@@ -510,7 +510,7 @@ public final CompletableFuture<InsertOneResult> insertOneAsync(T document, float
510510
public final InsertOneResult insertOne(T document, String vectorize) {
511511
Assert.notNull(document, "document");
512512
Assert.hasLength(vectorize, "vectorize");
513-
return _insertOne(JsonUtils.convertValueForDataApi(document, Document.class).vectorize(vectorize));
513+
return _insertOne(JsonUtils.convertValue(document, Document.class).vectorize(vectorize));
514514
}
515515

516516
/**
@@ -581,7 +581,7 @@ private InsertOneResult _insertOne(Document document) {
581581
* unmarshalled id
582582
*/
583583
@SuppressWarnings("unchecked")
584-
protected Object unmarshallDocumentId(Object id) {
584+
private Object unmarshallDocumentId(Object id) {
585585
if (id instanceof Map) {
586586
// only maps will required to be unmarshalled
587587
Map<String, Object> mapId = (Map<String, Object>) id;
@@ -700,13 +700,14 @@ public InsertManyResult insertMany(List<? extends T> documents, InsertManyOption
700700
} else {
701701
throw new TimeoutException("Request did not complete withing ");
702702
}
703-
} catch (InterruptedException e) {
703+
} catch (InterruptedException | ExecutionException e) {
704+
if (e.getCause() instanceof DataApiException) {
705+
throw (DataApiException) e.getCause();
706+
}
704707
Thread.currentThread().interrupt();
705708
throw new RuntimeException("Thread was interrupted while waiting", e);
706709
} catch (TimeoutException e) {
707710
throw new RuntimeException("Operation timed out", e);
708-
} catch (Exception e) {
709-
throw new RuntimeException("Error occurred during async operation", e.getCause());
710711
}
711712
return finalResult;
712713
}
@@ -1357,7 +1358,7 @@ public int countDocuments(Filter filter, int upperBound) throws TooManyDocuments
13571358
*
13581359
*/
13591360
public DeleteResult deleteOne(Filter filter) {
1360-
return deleteOne(filter, new DeleteOneOptions());
1361+
return deleteOne(filter, DeleteOneOptions.builder().build());
13611362
}
13621363

13631364
/**
@@ -1406,9 +1407,7 @@ public DeleteResult deleteMany(Filter filter) {
14061407
if (status.containsKey(DELETED_COUNT)) {
14071408
totalCount.addAndGet(status.getInteger(DELETED_COUNT));
14081409
}
1409-
if (status.containsKey(MORE_DATA)) {
1410-
moreData = status.getBoolean(MORE_DATA);
1411-
}
1410+
moreData = status.containsKey(MORE_DATA);
14121411
}
14131412
} while(moreData);
14141413
return new DeleteResult(totalCount.get());
@@ -1547,7 +1546,7 @@ public UpdateResult replaceOne(Filter filter, T replacement, ReplaceOneOptions r
15471546
result.setMatchedCount(res.getMatchedCount());
15481547
result.setModifiedCount(res.getModifiedCount());
15491548
if (res.getDocument() != null) {
1550-
Document doc = JsonUtils.convertValueForDataApi(res.getDocument(), Document.class);
1549+
Document doc = JsonUtils.convertValue(res.getDocument(), Document.class);
15511550
if (doc.getId(Object.class) != null) {
15521551
result.setUpsertedId(doc.getId(Object.class));
15531552
}
@@ -1601,7 +1600,7 @@ private FindOneAndReplaceResult<T> executeFindOneAndReplace(Command cmd) {
16011600
* returned
16021601
*/
16031602
public Optional<T> findOneAndUpdate(Filter filter, Update update) {
1604-
return findOneAndUpdate(filter, update, new FindOneAndUpdateOptions());
1603+
return findOneAndUpdate(filter, update, FindOneAndUpdateOptions.builder().build());
16051604
}
16061605

16071606
/**
@@ -1654,7 +1653,7 @@ public Optional<T> findOneAndUpdate(Filter filter, Update update, FindOneAndUpda
16541653
* the result of the update one operation
16551654
*/
16561655
public UpdateResult updateOne(Filter filter, Update update) {
1657-
return updateOne(filter, update, new UpdateOneOptions());
1656+
return updateOne(filter, update, UpdateOneOptions.builder().build());
16581657
}
16591658

16601659
/**
@@ -1711,7 +1710,7 @@ private static UpdateResult getUpdateResult(ApiResponse apiResponse) {
17111710
* the result of the update many operation
17121711
*/
17131712
public UpdateResult updateMany(Filter filter, Update update) {
1714-
return updateMany(filter, update, new UpdateOneOptions());
1713+
return updateMany(filter, update, UpdateManyOptions.builder().build());
17151714
}
17161715

17171716
/**
@@ -1726,7 +1725,7 @@ public UpdateResult updateMany(Filter filter, Update update) {
17261725
* @return
17271726
* the result of the update many operation
17281727
*/
1729-
public UpdateResult updateMany(Filter filter, Update update, UpdateOneOptions options) {
1728+
public UpdateResult updateMany(Filter filter, Update update, UpdateManyOptions options) {
17301729
notNull(update, "update");
17311730
notNull(options, "options");
17321731
boolean moreData = true;

astra-db-java/src/main/java/com/datastax/astra/client/Database.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ public Collection<Document> createCollection(String collectionName, int dimensio
305305
*/
306306
public <DOC> Collection<DOC> createCollection(String collectionName, int dimension, SimilarityMetric metric, Class<DOC> documentClass) {
307307
return createCollection(collectionName, CollectionOptions.builder()
308-
.withVectorDimension(dimension)
309-
.withVectorSimilarityMetric(metric)
308+
.vectorDimension(dimension)
309+
.vectorSimilarity(metric)
310310
.build(), documentClass);
311311
}
312312

@@ -357,7 +357,7 @@ public <DOC> Collection<DOC> createCollection(String collectionName, CollectionO
357357
Command createCollection = Command
358358
.create("createCollection")
359359
.append("name", collectionName)
360-
.withOptions(JsonUtils.convertValueForDataApi(collectionOptions, Document.class));
360+
.withOptions(JsonUtils.convertValue(collectionOptions, Document.class));
361361
runCommand(createCollection);
362362
log.info("Collection '" + green("{}") + "' has been created", collectionName);
363363
return getCollection(collectionName, documentClass);

astra-db-java/src/main/java/com/datastax/astra/client/model/CollectionInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ public CollectionInfo() {}
4747
/** {@inheritDoc} */
4848
@Override
4949
public String toString() {
50-
return JsonUtils.marshallForDataApi(this);
50+
return JsonUtils.marshall(this);
5151
}
5252
}

astra-db-java/src/main/java/com/datastax/astra/client/model/CollectionOptions.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public CollectionOptionsBuilder() {}
231231
* @return
232232
* self reference
233233
*/
234-
public CollectionOptionsBuilder withDefaultId(CollectionIdTypes idType) {
234+
public CollectionOptionsBuilder defaultId(CollectionIdTypes idType) {
235235
this.defaultId = idType;
236236
return this;
237237
}
@@ -242,7 +242,7 @@ public CollectionOptionsBuilder withDefaultId(CollectionIdTypes idType) {
242242
* @param size size
243243
* @return self reference
244244
*/
245-
public CollectionOptionsBuilder withVectorDimension(int size) {
245+
public CollectionOptionsBuilder vectorDimension(int size) {
246246
getVector().setDimension(size);
247247
return this;
248248
}
@@ -253,7 +253,7 @@ public CollectionOptionsBuilder withVectorDimension(int size) {
253253
* @param function function
254254
* @return self reference
255255
*/
256-
public CollectionOptionsBuilder withVectorSimilarityMetric(@NonNull SimilarityMetric function) {
256+
public CollectionOptionsBuilder vectorSimilarity(@NonNull SimilarityMetric function) {
257257
getVector().setMetric(function);
258258
return this;
259259
}
@@ -264,7 +264,7 @@ public CollectionOptionsBuilder withVectorSimilarityMetric(@NonNull SimilarityMe
264264
* @param properties size
265265
* @return self reference
266266
*/
267-
public CollectionOptionsBuilder withIndexingDeny(@NonNull String... properties) {
267+
public CollectionOptionsBuilder indexingDeny(@NonNull String... properties) {
268268
if (getIndexing().getAllow() != null) {
269269
throw new IllegalStateException("'indexing.deny' and 'indexing.allow' are mutually exclusive");
270270
}
@@ -278,7 +278,7 @@ public CollectionOptionsBuilder withIndexingDeny(@NonNull String... properties)
278278
* @param properties size
279279
* @return self reference
280280
*/
281-
public CollectionOptionsBuilder withIndexingAllow(String... properties) {
281+
public CollectionOptionsBuilder indexingAllow(String... properties) {
282282
if (getIndexing().getDeny() != null) {
283283
throw new IllegalStateException("'indexing.deny' and 'indexing.allow' are mutually exclusive");
284284
}
@@ -294,8 +294,8 @@ public CollectionOptionsBuilder withIndexingAllow(String... properties) {
294294
* @return self reference
295295
*/
296296
public CollectionOptionsBuilder vector(int dimension, @NonNull SimilarityMetric function) {
297-
withVectorSimilarityMetric(function);
298-
withVectorDimension(dimension);
297+
vectorSimilarity(function);
298+
vectorDimension(dimension);
299299
return this;
300300
}
301301

@@ -309,7 +309,7 @@ public CollectionOptionsBuilder vector(int dimension, @NonNull SimilarityMetric
309309
* @return
310310
* self reference
311311
*/
312-
public CollectionOptionsBuilder withVectorize(String provider, String modeName) {
312+
public CollectionOptionsBuilder vectorize(String provider, String modeName) {
313313
Service embeddingService = new Service();
314314
embeddingService.setProvider(provider);
315315
embeddingService.setModelName(modeName);

astra-db-java/src/main/java/com/datastax/astra/client/model/DeleteOneOptions.java

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,128 @@
2323
import com.datastax.astra.internal.utils.Assert;
2424
import lombok.Getter;
2525

26+
import java.util.List;
27+
2628
/**
2729
* Options to delete One document.
2830
*/
2931
@Getter
3032
public class DeleteOneOptions {
3133

34+
/**
35+
* Order by.
36+
*/
37+
private Document sort;
38+
3239
/**
3340
* Default constructor.
3441
*/
35-
public DeleteOneOptions() {
42+
public DeleteOneOptions(DeleteOneOptionsBuilder builder) {
43+
this.sort = builder.sort;
3644
}
3745

3846
/**
39-
* Order by.
47+
* Syntax sugar as delete option is only a sort
48+
*
49+
* @param sort
50+
* add a filter
51+
* @return
52+
* current command.
4053
*/
41-
private Document sort;
54+
public static DeleteOneOptions sort(Sort sort) {
55+
return new DeleteOneOptionsBuilder().sort(sort).build();
56+
}
57+
58+
/**
59+
* Create a builder for those options.
60+
*
61+
* @return
62+
* instance of the builder.
63+
*/
64+
public static DeleteOneOptionsBuilder builder() {
65+
return new DeleteOneOptionsBuilder();
66+
}
67+
68+
/**
69+
* Find is an operation with multiple options to filter, sort, project, skip, limit, and more.
70+
* This builder will help to chain options.
71+
*/
72+
public static class DeleteOneOptionsBuilder {
73+
74+
/**
75+
* Order by.
76+
*/
77+
private Document sort;
78+
79+
/**
80+
* Default Builder.
81+
*/
82+
public DeleteOneOptionsBuilder() {}
83+
84+
/**
85+
* Fluent api.
86+
*
87+
* @param pSort
88+
* list of sorts
89+
* @return
90+
* Self reference
91+
*/
92+
public DeleteOneOptionsBuilder sort(Sort pSort) {
93+
Assert.notNull(pSort, "sort");
94+
if (this.sort == null) {
95+
this.sort = new Document();
96+
}
97+
this.sort.put(pSort.getField(), pSort.getOrder().getCode());
98+
return this;
99+
}
100+
101+
/**
102+
* Fluent api.
103+
*
104+
* @param sorts
105+
* list of sorts
106+
* @return
107+
* Self reference
108+
*/
109+
public DeleteOneOptionsBuilder sort(List<Sort> sorts) {
110+
Assert.notNull(sorts, "sort");
111+
if (this.sort == null) {
112+
sort = new Document();
113+
}
114+
for (Sort s : sorts) {
115+
this.sort.put(s.getField(), s.getOrder().getCode());
116+
}
117+
return this;
118+
}
119+
120+
/**
121+
* Fluent api.
122+
*
123+
* @param pSort
124+
* add a filter
125+
* @return
126+
* current command.
127+
*/
128+
public DeleteOneOptionsBuilder sort(Document pSort) {
129+
Assert.notNull(pSort, "sort");
130+
if (this.sort == null) {
131+
sort = new Document();
132+
}
133+
this.sort.putAll(pSort);
134+
return this;
135+
}
136+
137+
/**
138+
* Builder for the find Options.
139+
*
140+
* @return
141+
* the find options object
142+
*/
143+
public DeleteOneOptions build() {
144+
return new DeleteOneOptions(this);
145+
}
146+
147+
}
42148

43149
/**
44150
* Fluent api.

astra-db-java/src/main/java/com/datastax/astra/client/model/DistinctIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public DistinctIterator(PageableIterable<DOC> findIterable, String fieldName, Cl
8484
* extraction of field from document
8585
*/
8686
private FIELD extractField(DOC doc) {
87-
return JsonUtils.convertValueForDataApi(doc, Document.class).get(fieldName, fieldClass);
87+
return JsonUtils.convertValue(doc, Document.class).get(fieldName, fieldClass);
8888
}
8989

9090
/** {@inheritDoc} */

0 commit comments

Comments
 (0)