Skip to content

Commit 8fa340c

Browse files
committed
test: fix to use couchbase with testcontainers
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 7990006 commit 8fa340c

File tree

1 file changed

+49
-72
lines changed

1 file changed

+49
-72
lines changed

jnosql-couchbase/src/test/java/org/eclipse/jnosql/databases/couchbase/communication/DocumentQueryTest.java

Lines changed: 49 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,60 @@
1515
package org.eclipse.jnosql.databases.couchbase.communication;
1616

1717

18+
import com.couchbase.client.core.env.TimeoutConfig;
1819
import com.couchbase.client.core.error.DocumentExistsException;
1920
import com.couchbase.client.core.error.DocumentNotFoundException;
20-
import org.eclipse.jnosql.communication.Settings;
2121
import org.eclipse.jnosql.communication.document.Document;
2222
import org.eclipse.jnosql.communication.document.DocumentEntity;
2323
import org.eclipse.jnosql.communication.document.DocumentQuery;
2424
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
2525
import org.eclipse.jnosql.communication.keyvalue.BucketManagerFactory;
26-
import org.junit.jupiter.api.AfterAll;
27-
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
2929
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3030

3131
import java.util.Arrays;
3232
import java.util.List;
33-
import java.util.Optional;
3433
import java.util.stream.Collectors;
3534

36-
import static org.eclipse.jnosql.communication.document.DocumentQuery.select;
3735
import static java.util.Arrays.asList;
3836
import static org.assertj.core.api.Assertions.assertThat;
39-
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
37+
import static org.awaitility.Awaitility.await;
38+
import static org.eclipse.jnosql.communication.document.DocumentQuery.select;
4039
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
40+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
4141
import static org.junit.jupiter.api.Assertions.assertEquals;
4242
import static org.junit.jupiter.api.Assertions.assertFalse;
43-
import static org.junit.jupiter.api.Assertions.assertTrue;
4443

4544
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
4645
public class DocumentQueryTest {
4746

4847
public static final String COLLECTION_NAME = "person";
49-
private CouchbaseDocumentManager entityManager;
50-
private static CouchbaseDocumentConfiguration configuration;
51-
52-
{
53-
Settings settings = CouchbaseUtil.getSettings();
48+
private static CouchbaseDocumentManager entityManager;
49+
private static BucketManager keyValueEntityManager;
5450

51+
static {
52+
var settings = Database.INSTANCE.getSettings();
53+
var configuration = Database.INSTANCE.getDocumentConfiguration();
5554
CouchbaseDocumentManagerFactory managerFactory = configuration.apply(settings);
5655
entityManager = managerFactory.apply(CouchbaseUtil.BUCKET_NAME);
56+
BucketManagerFactory keyValueEntityManagerFactory =
57+
Database.INSTANCE.getKeyValueConfiguration().apply(settings);
58+
keyValueEntityManager = keyValueEntityManagerFactory.apply(CouchbaseUtil.BUCKET_NAME);
5759
}
5860

59-
@AfterAll
60-
public static void afterEach() {
61-
CouchbaseKeyValueConfiguration configuration = Database.INSTANCE.getKeyValueConfiguration();
62-
BucketManagerFactory keyValueEntityManagerFactory = configuration.apply(CouchbaseUtil.getSettings());
63-
BucketManager keyValueEntityManager = keyValueEntityManagerFactory.apply(CouchbaseUtil.BUCKET_NAME);
64-
try {
65-
keyValueEntityManager.delete("id");
66-
keyValueEntityManager.delete("id2");
67-
keyValueEntityManager.delete("id3");
68-
keyValueEntityManager.delete("id4");
69-
} catch (DocumentNotFoundException exp) {
61+
@AfterEach
62+
public void afterEach() {
63+
List.of("id", "id2", "id3", "id4")
64+
.forEach(key -> ignoreException(() -> keyValueEntityManager.delete(key)));
7065

71-
}
66+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
7267
}
7368

74-
@BeforeAll
75-
public static void beforeEach() {
76-
configuration = Database.INSTANCE.getDocumentConfiguration();
77-
Settings settings = CouchbaseUtil.getSettings();
78-
CouchbaseDocumentManagerFactory managerFactory = configuration.apply(settings);
79-
CouchbaseDocumentManager entityManager = managerFactory.apply(CouchbaseUtil.BUCKET_NAME);
69+
70+
@BeforeEach
71+
public void beforeEach() {
8072

8173
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id")
8274
, Document.of("name", "name")));
@@ -87,25 +79,25 @@ public static void beforeEach() {
8779
DocumentEntity entity4 = DocumentEntity.of("person", asList(Document.of("_id", "id4")
8880
, Document.of("name", "name3")));
8981

90-
try {
91-
entityManager.insert(Arrays.asList(entity, entity2, entity3, entity4));
92-
} catch (DocumentExistsException exp) {
93-
94-
}
82+
entityManager.update(Arrays.asList(entity, entity2, entity3, entity4));
9583

84+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
9685
}
9786

87+
private void ignoreException(Runnable runnable) {
88+
try {
89+
runnable.run();
90+
} catch (DocumentNotFoundException
91+
| DocumentExistsException ex) {
92+
//IGNORED
93+
}
94+
}
9895

9996
@Test
10097
public void shouldShouldDefineLimit() {
10198

102-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id")
103-
, Document.of("name", "name")));
104-
105-
Document name = entity.find("name").get();
106-
10799
DocumentQuery query = select().from(COLLECTION_NAME)
108-
.where(name.name()).eq(name.get())
100+
.where("name").eq("name")
109101
.limit(2L)
110102
.build();
111103

@@ -116,60 +108,49 @@ public void shouldShouldDefineLimit() {
116108

117109
@Test
118110
public void shouldShouldDefineStart() {
119-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id")
120-
, Document.of("name", "name")));
121-
122-
Document name = entity.find("name").get();
123-
124111
DocumentQuery query = select().from(COLLECTION_NAME)
125-
.where(name.name()).eq(name.get())
112+
.where("name").eq("name")
126113
.skip(1L)
127114
.build();
128115
List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
129-
assertEquals(3, entities.size());
116+
assertEquals(2, entities.size());
130117

131118
}
132119

133120
@Test
134121
public void shouldShouldDefineLimitAndStart() {
135122

136-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id")
137-
, Document.of("name", "name")));
123+
List<DocumentEntity> entities = entityManager.select(select().from(COLLECTION_NAME).build()).collect(Collectors.toList());
124+
assertEquals(4, entities.size());
138125

139-
Document name = entity.find("name").get();
140126
DocumentQuery query = select().from(COLLECTION_NAME)
141-
.where(name.name()).eq(name.get())
142-
.skip(2L)
127+
.where("name").eq("name")
128+
.skip(1L)
143129
.limit(2L)
144130
.build();
145131

146-
List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
132+
entities = entityManager.select(query).collect(Collectors.toList());
147133
assertEquals(2, entities.size());
148134

149135
}
150136

151137

152138
@Test
153139
public void shouldSelectAll() {
154-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id")
155-
, Document.of("name", "name")));
156-
157-
158140
DocumentQuery query = select().from(COLLECTION_NAME).build();
159-
Optional<Document> name = entity.find("name");
160141
List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
161142
assertFalse(entities.isEmpty());
162-
assertTrue(entities.size() >= 4);
143+
assertThat(entities).hasSize(4);
163144
}
164145

165146

166147
@Test
167148
public void shouldFindDocumentByName() {
168-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id4"),
169-
Document.of("name", "name"), Document.of("_key", "person:id4")));
170149

171-
Document name = entity.find("name").get();
172-
DocumentQuery query = select().from(COLLECTION_NAME).where(name.name()).eq(name.get()).build();
150+
DocumentQuery query = select().from(COLLECTION_NAME)
151+
.where("name")
152+
.eq("name")
153+
.build();
173154
List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
174155
assertFalse(entities.isEmpty());
175156
}
@@ -194,15 +175,13 @@ public void shouldFindDocumentByNameSortAsc() {
194175

195176
@Test
196177
public void shouldFindDocumentByNameSortDesc() {
197-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id4")
198-
, Document.of("name", "name3"), Document.of("_key", "person:id4")));
199-
200-
Optional<Document> name = entity.find("name");
201178

202179
DocumentQuery query = select().from(COLLECTION_NAME)
203180
.orderBy("name").desc()
204181
.build();
182+
205183
List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
184+
206185
List<String> result = entities.stream().flatMap(e -> e.documents().stream())
207186
.filter(d -> "name".equals(d.name()))
208187
.map(d -> d.get(String.class))
@@ -214,16 +193,14 @@ public void shouldFindDocumentByNameSortDesc() {
214193

215194
@Test
216195
public void shouldFindDocumentById() {
217-
DocumentEntity entity = DocumentEntity.of("person", asList(Document.of("_id", "id")
218-
, Document.of("name", "name"), Document.of("_key", "person:id")));
219-
Document id = entity.find("_id").get();
220196

221197
DocumentQuery query = select().from(COLLECTION_NAME)
222-
.where(id.name()).eq(id.get())
198+
.where("_id").eq("id")
223199
.build();
224200

225201
List<DocumentEntity> entities = entityManager.select(query).collect(Collectors.toList());
226202
assertFalse(entities.isEmpty());
203+
227204
}
228205

229206
}

0 commit comments

Comments
 (0)