Skip to content

Commit 7990006

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

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

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

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616

1717
import com.couchbase.client.core.error.DocumentNotFoundException;
1818
import com.couchbase.client.java.json.JsonObject;
19+
import org.eclipse.jnosql.communication.Settings;
1920
import org.eclipse.jnosql.communication.TypeReference;
2021
import org.eclipse.jnosql.communication.document.Document;
2122
import org.eclipse.jnosql.communication.document.DocumentDeleteQuery;
2223
import org.eclipse.jnosql.communication.document.DocumentEntity;
2324
import org.eclipse.jnosql.communication.document.DocumentQuery;
24-
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
2525
import org.eclipse.jnosql.communication.document.Documents;
26+
import org.eclipse.jnosql.communication.keyvalue.BucketManager;
2627
import org.junit.jupiter.api.AfterEach;
28+
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.Test;
2830
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
2931

@@ -36,14 +38,14 @@
3638
import java.util.Set;
3739
import java.util.stream.Collectors;
3840

39-
import static org.eclipse.jnosql.communication.document.DocumentDeleteQuery.delete;
40-
import static org.eclipse.jnosql.communication.document.DocumentQuery.select;
4141
import static java.util.Arrays.asList;
4242
import static org.assertj.core.api.Assertions.assertThat;
43-
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
43+
import static org.awaitility.Awaitility.await;
44+
import static org.eclipse.jnosql.communication.document.DocumentDeleteQuery.delete;
45+
import static org.eclipse.jnosql.communication.document.DocumentQuery.select;
4446
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
47+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
4548
import static org.junit.jupiter.api.Assertions.assertEquals;
46-
import static org.junit.jupiter.api.Assertions.assertFalse;
4749
import static org.junit.jupiter.api.Assertions.assertNotNull;
4850
import static org.junit.jupiter.api.Assertions.assertTrue;
4951

@@ -53,35 +55,39 @@ public class CouchbaseDocumentManagerTest {
5355

5456
public static final String COLLECTION_PERSON_NAME = "person";
5557
public static final String COLLECTION_APP_NAME = "AppointmentBook";
56-
private CouchbaseDocumentManager entityManager;
58+
private static Settings settings;
59+
private static CouchbaseDocumentManager entityManager;
60+
private static BucketManager keyValueEntityManagerForPerson;
61+
private static BucketManager keyValueEntityManagerForAppointmentBook;
5762

58-
{
63+
static {
64+
settings = Database.INSTANCE.getSettings();
5965
CouchbaseDocumentConfiguration configuration = Database.INSTANCE.getDocumentConfiguration();
60-
61-
CouchbaseDocumentManagerFactory managerFactory = configuration.apply(CouchbaseUtil.getSettings());
66+
CouchbaseDocumentManagerFactory managerFactory = configuration.apply(settings);
6267
entityManager = managerFactory.apply(CouchbaseUtil.BUCKET_NAME);
68+
69+
CouchbaseKeyValueConfiguration keyValueConfiguration = Database.INSTANCE.getKeyValueConfiguration();
70+
CouchbaseBucketManagerFactory keyValueEntityManagerFactory = keyValueConfiguration.apply(settings);
71+
keyValueEntityManagerForPerson = keyValueEntityManagerFactory
72+
.getBucketManager(CouchbaseUtil.BUCKET_NAME, COLLECTION_PERSON_NAME);
73+
keyValueEntityManagerForAppointmentBook = keyValueEntityManagerFactory
74+
.getBucketManager(CouchbaseUtil.BUCKET_NAME, COLLECTION_APP_NAME);
6375
}
6476

77+
@BeforeEach
6578
@AfterEach
66-
public void afterEach() {
67-
CouchbaseKeyValueConfiguration configuration = Database.INSTANCE.getKeyValueConfiguration();
68-
CouchbaseBucketManagerFactory keyValueEntityManagerFactory = configuration.apply(CouchbaseUtil.getSettings());
69-
79+
public void cleanUpData() throws InterruptedException {
7080
try {
71-
BucketManager keyValueEntityManager = keyValueEntityManagerFactory
72-
.getBucketManager(CouchbaseUtil.BUCKET_NAME, COLLECTION_PERSON_NAME);
73-
keyValueEntityManager.delete("id");
81+
keyValueEntityManagerForPerson.delete("id");
7482
} catch (DocumentNotFoundException exp) {
7583
//IGNORE
7684
}
77-
7885
try {
79-
BucketManager keyValueEntityManager = keyValueEntityManagerFactory
80-
.getBucketManager(CouchbaseUtil.BUCKET_NAME, COLLECTION_APP_NAME);
81-
keyValueEntityManager.delete("ids");
86+
keyValueEntityManagerForAppointmentBook.delete("ids");
8287
} catch (DocumentNotFoundException exp) {
8388
//IGNORE
8489
}
90+
Thread.sleep(1_000L);
8591
}
8692

8793
@Test
@@ -128,7 +134,6 @@ public void shouldSaveSubDocument() {
128134
entity.add(Document.of("phones", Document.of("mobile", "1231231")));
129135
DocumentEntity entitySaved = entityManager.insert(entity);
130136
Document id = entitySaved.find("_id").get();
131-
132137
DocumentQuery query = select().from(COLLECTION_PERSON_NAME).where(id.name()).eq(id.get()).build();
133138
DocumentEntity entityFound = entityManager.select(query).collect(Collectors.toList()).get(0);
134139
Document subDocument = entityFound.find("phones").get();
@@ -217,19 +222,25 @@ private DocumentEntity createSubdocumentList() {
217222
public void shouldRunN1Ql() {
218223
DocumentEntity entity = getEntity();
219224
entityManager.insert(entity);
220-
List<DocumentEntity> entities = entityManager.n1qlQuery("select * from jnosql._default.person").collect(Collectors.toList());
221-
assertFalse(entities.isEmpty());
225+
await().until(() ->
226+
!entityManager
227+
.n1qlQuery("select * from `jnosql`._default.person")
228+
.collect(Collectors.toList()).isEmpty()
229+
);
222230
}
223231

224232
@Test
225233
public void shouldRunN1QlParameters() {
226234
DocumentEntity entity = getEntity();
227235
entityManager.insert(entity);
228-
JsonObject params = JsonObject.create().put("name", "Poliana");
229-
List<DocumentEntity> entities = entityManager.n1qlQuery("select * from jnosql._default.person where name = $name",
230-
params).collect(Collectors.toList());
231-
assertNotNull(entities);
232-
assertFalse(entities.isEmpty());
236+
237+
JsonObject params = JsonObject.create().put("name", entity.find("name", String.class).orElse(null));
238+
239+
await().until(() ->
240+
!entityManager
241+
.n1qlQuery("select * from `jnosql`._default.person where name = $name", params)
242+
.collect(Collectors.toList()).isEmpty()
243+
);
233244
}
234245

235246

0 commit comments

Comments
 (0)