Skip to content

Commit 17179d8

Browse files
committed
feat: add count method for SelectQuery
Implemented a count method in DefaultArangoDBDocumentManager to support counting documents based on a SelectQuery, including null query validation. Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 20fde43 commit 17179d8

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/DefaultArangoDBDocumentManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ public long count(String documentCollection) {
154154
.map(Number::longValue).orElse(0L);
155155
}
156156

157+
@Override
158+
public long count(SelectQuery query) {
159+
requireNonNull(query, "query is required");
160+
checkCollection(query.name());
161+
AQLQueryResult aqlQuery = QueryAQLConverter.count(query);
162+
LOGGER.finest("Executing AQL: " + aqlQuery.query());
163+
return aql(aqlQuery.query(), aqlQuery.values(), Long.class).findFirst().orElse(0L);
164+
}
157165

158166
@Override
159167
public Stream<CommunicationEntity> aql(String query, Map<String, Object> params) throws NullPointerException {

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static java.util.Arrays.asList;
4545
import static java.util.Collections.singletonMap;
4646
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
4748
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
4849
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
4950
import static org.eclipse.jnosql.communication.semistructured.DeleteQuery.delete;
@@ -223,6 +224,22 @@ void shouldCount() {
223224
assertTrue(entityManager.count(COLLECTION_NAME) > 0);
224225
}
225226

227+
@Test
228+
void shouldCountWithSelectQuery() {
229+
CommunicationEntity entity = entityManager.insert(createDocumentListNotHavingId());
230+
Element key = entity.find(KEY_NAME).get();
231+
SelectQuery query = select().from("AppointmentBook").where(key.name()).eq(key.get()).build();
232+
233+
assertSoftly(softly -> {
234+
softly.assertThat(entityManager.count(query))
235+
.as("should count documents matching the query")
236+
.isEqualTo(1L);
237+
softly.assertThatThrownBy(() -> entityManager.count((SelectQuery) null))
238+
.as("must not accept null query")
239+
.isInstanceOf(NullPointerException.class);
240+
});
241+
}
242+
226243
@Test
227244
void shouldReadFromDifferentBaseDocumentUsingInstance() {
228245
entityManager.insert(getEntity());
@@ -271,7 +288,7 @@ void shouldInsertNull() {
271288
entity.add(Element.of("name", null));
272289
CommunicationEntity documentEntity = entityManager.insert(entity);
273290
Optional<Element> name = documentEntity.find("name");
274-
SoftAssertions.assertSoftly(soft -> {
291+
assertSoftly(soft -> {
275292
soft.assertThat(name).isPresent();
276293
soft.assertThat(name).get().extracting(Element::name).isEqualTo("name");
277294
soft.assertThat(name).get().extracting(Element::get).isNull();
@@ -284,7 +301,7 @@ void shouldUpdateNull(){
284301
entity.add(Element.of("name", null));
285302
var documentEntity = entityManager.update(entity);
286303
Optional<Element> name = documentEntity.find("name");
287-
SoftAssertions.assertSoftly(soft -> {
304+
assertSoftly(soft -> {
288305
soft.assertThat(name).isPresent();
289306
soft.assertThat(name).get().extracting(Element::name).isEqualTo("name");
290307
soft.assertThat(name).get().extracting(Element::get).isNull();
@@ -359,7 +376,7 @@ void shouldInsertUUID() {
359376
entity.add("uuid", UUID.randomUUID());
360377
var documentEntity = entityManager.insert(entity);
361378
Optional<Element> uuid = documentEntity.find("uuid");
362-
SoftAssertions.assertSoftly(soft -> {
379+
assertSoftly(soft -> {
363380
soft.assertThat(uuid).isPresent();
364381
Element element = uuid.orElseThrow();
365382
soft.assertThat(element.name()).isEqualTo("uuid");
@@ -380,7 +397,7 @@ void shouldFindBetween() {
380397

381398
var result = entityManager.select(query).toList();
382399

383-
SoftAssertions.assertSoftly(softly -> {
400+
assertSoftly(softly -> {
384401
softly.assertThat(result).hasSize(2);
385402
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).contains(22, 23);
386403
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).doesNotContain(25);
@@ -400,7 +417,7 @@ void shouldFindBetween2() {
400417

401418
var result = entityManager.select(query).toList();
402419

403-
SoftAssertions.assertSoftly(softly -> {
420+
assertSoftly(softly -> {
404421
softly.assertThat(result).hasSize(2);
405422
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).contains(22, 23);
406423
softly.assertThat(result).map(e -> e.find("age").orElseThrow().get(Integer.class)).doesNotContain(25);
@@ -416,7 +433,7 @@ void shouldFindContains() {
416433
"lia")), COLLECTION_NAME, Collections.emptyList());
417434

418435
var result = entityManager.select(query).toList();
419-
SoftAssertions.assertSoftly(softly -> {
436+
assertSoftly(softly -> {
420437
softly.assertThat(result).hasSize(1);
421438
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
422439
});
@@ -431,7 +448,7 @@ void shouldStartsWith() {
431448
"Pol")), COLLECTION_NAME, Collections.emptyList());
432449

433450
var result = entityManager.select(query).toList();
434-
SoftAssertions.assertSoftly(softly -> {
451+
assertSoftly(softly -> {
435452
softly.assertThat(result).hasSize(1);
436453
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
437454
});
@@ -446,7 +463,7 @@ void shouldEndsWith() {
446463
"ana")), COLLECTION_NAME, Collections.emptyList());
447464

448465
var result = entityManager.select(query).toList();
449-
SoftAssertions.assertSoftly(softly -> {
466+
assertSoftly(softly -> {
450467
softly.assertThat(result).hasSize(1);
451468
softly.assertThat(result.get(0).find("name").orElseThrow().get(String.class)).isEqualTo("Poliana");
452469
});
@@ -483,7 +500,6 @@ private CommunicationEntity createDocumentList() {
483500
}
484501

485502
private CommunicationEntity createDocumentListNotHavingId() {
486-
String id = UUID.randomUUID().toString();
487503
CommunicationEntity entity = CommunicationEntity.of("AppointmentBook");
488504
entity.add(Element.of("_id", "ids"));
489505
List<List<Element>> documents = new ArrayList<>();

0 commit comments

Comments
 (0)