Skip to content

Commit 862e9c6

Browse files
committed
adds test to ES
1 parent 047d9ec commit 862e9c6

File tree

5 files changed

+99
-9
lines changed

5 files changed

+99
-9
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.jnosql.diana.elasticsearch.document;
2+
3+
import org.elasticsearch.action.ActionListener;
4+
import org.elasticsearch.action.search.SearchResponse;
5+
import org.jnosql.diana.api.JNoSQLException;
6+
7+
import java.util.function.Consumer;
8+
9+
final class CountActionListener implements ActionListener<SearchResponse> {
10+
11+
private final Consumer<Long> callback;
12+
private final String documentCollection;
13+
14+
CountActionListener(Consumer<Long> callback, String documentCollection) {
15+
this.callback = callback;
16+
this.documentCollection = documentCollection;
17+
}
18+
19+
@Override
20+
public void onResponse(SearchResponse response) {
21+
callback.accept(response.getHits().getTotalHits());
22+
}
23+
24+
@Override
25+
public void onFailure(Exception e) {
26+
throw new ElasticsearchException("An error when do search from QueryBuilder on elasticsearch", e);
27+
}
28+
}

elasticsearch-driver/src/main/java/org/jnosql/diana/elasticsearch/document/DefaultElasticsearchDocumentCollectionManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import org.elasticsearch.action.search.SearchResponse;
2323
import org.elasticsearch.client.RestHighLevelClient;
2424
import org.elasticsearch.index.query.QueryBuilder;
25+
import org.elasticsearch.index.query.QueryBuilders;
2526
import org.elasticsearch.search.builder.SearchSourceBuilder;
27+
import org.jnosql.diana.api.JNoSQLException;
2628
import org.jnosql.diana.api.document.Document;
2729
import org.jnosql.diana.api.document.DocumentDeleteQuery;
2830
import org.jnosql.diana.api.document.DocumentEntity;
@@ -116,6 +118,21 @@ public List<DocumentEntity> select(DocumentQuery query) throws NullPointerExcept
116118
return EntityConverter.query(query, client, index);
117119
}
118120

121+
@Override
122+
public long count(String documentCollection) {
123+
Objects.requireNonNull(documentCollection, "query is required");
124+
SearchRequest searchRequest = new SearchRequest(index);
125+
searchRequest.types(documentCollection);
126+
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
127+
searchSourceBuilder.size(0);
128+
try {
129+
SearchResponse search = client.search(searchRequest);
130+
return search.getHits().getTotalHits();
131+
} catch (IOException e) {
132+
throw new JNoSQLException("Error on ES when try to execute count to document collection:" + documentCollection, e);
133+
}
134+
}
135+
119136
@Override
120137
public List<DocumentEntity> search(QueryBuilder query, String... types) throws NullPointerException {
121138
Objects.requireNonNull(query, "query is required");

elasticsearch-driver/src/main/java/org/jnosql/diana/elasticsearch/document/DefaultElasticsearchDocumentCollectionManagerAsync.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import org.elasticsearch.action.delete.DeleteRequest;
2222
import org.elasticsearch.action.index.IndexRequest;
2323
import org.elasticsearch.action.search.SearchRequest;
24+
import org.elasticsearch.action.search.SearchResponse;
2425
import org.elasticsearch.client.RestHighLevelClient;
2526
import org.elasticsearch.index.query.QueryBuilder;
2627
import org.elasticsearch.search.builder.SearchSourceBuilder;
2728
import org.jnosql.diana.api.ExecuteAsyncQueryException;
29+
import org.jnosql.diana.api.JNoSQLException;
2830
import org.jnosql.diana.api.document.Document;
2931
import org.jnosql.diana.api.document.DocumentDeleteQuery;
3032
import org.jnosql.diana.api.document.DocumentEntity;
@@ -111,7 +113,7 @@ public void delete(DocumentDeleteQuery query, Consumer<Void> callBack) {
111113
DocumentQuery select = new ElasticsearchDocumentQuery(query);
112114

113115
List<DocumentEntity> entities = EntityConverter.query(select, client, index);
114-
if(entities.isEmpty()) {
116+
if (entities.isEmpty()) {
115117
callBack.accept(null);
116118
return;
117119
}
@@ -139,10 +141,22 @@ public void onFailure(Exception e) {
139141
}
140142

141143
@Override
142-
public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callBack) {
144+
public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callback) {
143145
requireNonNull(query, "query is required");
144-
requireNonNull(callBack, "callBack is required");
145-
EntityConverter.queryAsync(query, client, index, callBack);
146+
requireNonNull(callback, "callback is required");
147+
EntityConverter.queryAsync(query, client, index, callback);
148+
}
149+
150+
@Override
151+
public void count(String documentCollection, Consumer<Long> callback) {
152+
requireNonNull(documentCollection, "documentCollection is required");
153+
requireNonNull(callback, "callback is required");
154+
SearchRequest searchRequest = new SearchRequest(index);
155+
searchRequest.types(documentCollection);
156+
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
157+
searchSourceBuilder.size(0);
158+
ActionListener<SearchResponse> listener = new CountActionListener(callback, documentCollection);
159+
client.searchAsync(searchRequest, listener);
146160
}
147161

148162

elasticsearch-driver/src/test/java/org/jnosql/diana/elasticsearch/document/ElasticsearchDocumentCollectionManagerAsyncTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
import java.time.Duration;
2929
import java.util.List;
3030
import java.util.concurrent.atomic.AtomicBoolean;
31+
import java.util.concurrent.atomic.AtomicLong;
3132
import java.util.concurrent.atomic.AtomicReference;
33+
import java.util.function.Consumer;
3234

3335
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
3436
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.delete;
3537
import static org.jnosql.diana.api.document.query.DocumentQueryBuilder.select;
3638
import static org.jnosql.diana.elasticsearch.document.DocumentEntityGerator.COLLECTION_NAME;
3739
import static org.jnosql.diana.elasticsearch.document.DocumentEntityGerator.INDEX;
3840
import static org.jnosql.diana.elasticsearch.document.DocumentEntityGerator.getEntity;
41+
import static org.junit.jupiter.api.Assertions.assertEquals;
3942
import static org.junit.jupiter.api.Assertions.assertFalse;
4043
import static org.junit.jupiter.api.Assertions.assertThrows;
4144
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -104,9 +107,10 @@ public void shouldRemoveEntityAsync() throws InterruptedException {
104107
}
105108

106109
@Test
107-
public void shouldUserSearchBuilder() {
110+
public void shouldUserSearchBuilder() throws InterruptedException {
108111
DocumentEntity entity = getEntity();
109112
entityManager.insert(entity);
113+
Thread.sleep(1_000L);
110114
TermQueryBuilder query = termQuery("name", "Poliana");
111115
AtomicReference<List<DocumentEntity>> result = new AtomicReference<>();
112116
AtomicBoolean atomicBoolean = new AtomicBoolean(false);
@@ -122,9 +126,10 @@ public void shouldUserSearchBuilder() {
122126
}
123127

124128
@Test
125-
public void shouldReturnAll() {
129+
public void shouldReturnAll() throws InterruptedException {
126130
DocumentEntity entity = getEntity();
127131
entityManagerAsync.insert(entity);
132+
Thread.sleep(1_000L);
128133
DocumentQuery query = select().from(COLLECTION_NAME).build();
129134
AtomicBoolean condition = new AtomicBoolean(false);
130135
AtomicReference<List<DocumentEntity>> result = new AtomicReference<>();
@@ -139,6 +144,23 @@ public void shouldReturnAll() {
139144

140145
}
141146

147+
@Test
148+
public void shouldCount() throws InterruptedException {
149+
AtomicBoolean condition = new AtomicBoolean(false);
150+
AtomicLong value = new AtomicLong(0L);
151+
DocumentEntity entity = getEntity();
152+
entityManagerAsync.insert(entity);
153+
154+
Thread.sleep(1_000L);
155+
Consumer<Long> callback = l -> {
156+
condition.set(true);
157+
value.set(l);
158+
};
159+
entityManagerAsync.count(DocumentEntityGerator.COLLECTION_NAME, callback);
160+
Awaitility.await().untilTrue(condition);
161+
assertTrue(value.get() > 0);
162+
163+
}
142164

143165
@Test
144166
public void shouldInsertTTL() {

elasticsearch-driver/src/test/java/org/jnosql/diana/elasticsearch/document/ElasticsearchDocumentCollectionManagerTest.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
public class ElasticsearchDocumentCollectionManagerTest {
5050

5151

52-
5352
private ElasticsearchDocumentCollectionManager entityManager;
5453

5554
@BeforeEach
@@ -74,7 +73,7 @@ public void shouldInsert() {
7473

7574
@Test
7675
public void shouldInsertTTL() {
77-
assertThrows(UnsupportedOperationException.class, () ->{
76+
assertThrows(UnsupportedOperationException.class, () -> {
7877
entityManager.insert(getEntity(), Duration.ofSeconds(1L));
7978
});
8079
}
@@ -170,7 +169,6 @@ public void shouldFindAll() {
170169
}
171170

172171

173-
174172
@Test
175173
public void shouldSaveSubDocument() {
176174
DocumentEntity entity = getEntity();
@@ -224,6 +222,17 @@ public void shouldRetrieveListSubdocumentList() {
224222
assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
225223
}
226224

225+
@Test
226+
public void shouldCount() {
227+
DocumentEntity entity = getEntity();
228+
DocumentEntity entity2 = getEntity();
229+
entity2.add(Document.of("_id", "test"));
230+
entityManager.insert(entity);
231+
entityManager.insert(entity2);
232+
assertTrue(entityManager.count(COLLECTION_NAME) > 0);
233+
}
234+
235+
227236
private DocumentEntity createSubdocumentList() {
228237
DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
229238
entity.add(Document.of("_id", "ids"));

0 commit comments

Comments
 (0)