Skip to content

Commit 3c1eb54

Browse files
committed
test: add CouchbaseDocumentRepositoryIntegrationTest
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent d4c4dc8 commit 3c1eb54

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
* Copyright (c) 2023 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Maximillian Arruda
14+
*/
15+
package org.eclipse.jnosql.databases.couchbase.integration;
16+
17+
18+
import com.couchbase.client.core.env.TimeoutConfig;
19+
import com.couchbase.client.java.json.JsonObject;
20+
import jakarta.inject.Inject;
21+
import org.eclipse.jnosql.databases.couchbase.communication.CouchbaseUtil;
22+
import org.eclipse.jnosql.databases.couchbase.mapping.CouchbaseExtension;
23+
import org.eclipse.jnosql.databases.couchbase.mapping.CouchbaseTemplate;
24+
import org.eclipse.jnosql.mapping.Convert;
25+
import org.eclipse.jnosql.mapping.document.DocumentEntityConverter;
26+
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
27+
import org.eclipse.jnosql.mapping.reflection.EntityMetadataExtension;
28+
import org.jboss.weld.junit5.auto.AddExtensions;
29+
import org.jboss.weld.junit5.auto.AddPackages;
30+
import org.jboss.weld.junit5.auto.EnableAutoWeld;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
35+
36+
import java.util.Map;
37+
import java.util.Optional;
38+
39+
import static java.util.UUID.randomUUID;
40+
import static org.assertj.core.api.Assertions.assertThat;
41+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
42+
import static org.awaitility.Awaitility.await;
43+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
44+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
45+
46+
@EnableAutoWeld
47+
@AddPackages(value = {Convert.class, DocumentEntityConverter.class})
48+
@AddPackages(Book.class)
49+
@AddPackages(CouchbaseTemplate.class)
50+
@AddExtensions({EntityMetadataExtension.class,
51+
DocumentExtension.class,
52+
CouchbaseExtension.class})
53+
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
54+
class CouchbaseDocumentRepositoryIntegrationTest {
55+
56+
@Inject
57+
private Library library;
58+
59+
static {
60+
CouchbaseUtil.systemPropertySetup();
61+
}
62+
63+
@BeforeEach
64+
@AfterEach
65+
public void cleanUp() {
66+
library.deleteAll();
67+
}
68+
69+
@Test
70+
public void shouldInsert() {
71+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
72+
library.save(book);
73+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
74+
Optional<Book> optional = library.findById(book.id());
75+
assertThat(optional).isNotNull().isNotEmpty()
76+
.get().isEqualTo(book);
77+
}
78+
79+
@Test
80+
public void shouldUpdate() throws InterruptedException {
81+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
82+
assertThat(library.save(book))
83+
.isNotNull()
84+
.isEqualTo(book);
85+
86+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
87+
88+
Book updated = new Book(book.id(), book.title() + " updated", 2);
89+
90+
assertThat(library.save(updated))
91+
.isNotNull()
92+
.isNotEqualTo(book);
93+
94+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
95+
96+
assertThat(library.findById(book.id()))
97+
.isNotNull().get().isEqualTo(updated);
98+
99+
}
100+
101+
@Test
102+
public void shouldFindById() {
103+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
104+
assertThat(library.save(book))
105+
.isNotNull()
106+
.isEqualTo(book);
107+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
108+
assertThat(library.findById(book.id()))
109+
.isNotNull().get().isEqualTo(book);
110+
}
111+
112+
@Test
113+
public void shouldFindByTitleLike() {
114+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
115+
assertThat(library.save(book))
116+
.isNotNull()
117+
.isEqualTo(book);
118+
119+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
120+
121+
var data = library.findByTitleLike(book.title()).toList();
122+
123+
assertSoftly(softly -> {
124+
softly.assertThat(data).as("query result is a non-null instance").isNotNull();
125+
softly.assertThat(data.size()).as("query result size is correct").isEqualTo(1);
126+
softly.assertThat(data.get(0)).as("returned data is correct").isEqualTo(book);
127+
});
128+
}
129+
130+
@Test
131+
public void shouldFindByEditionLessThan() {
132+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
133+
assertThat(library.save(book))
134+
.isNotNull()
135+
.isEqualTo(book);
136+
137+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
138+
139+
var booksFoundByEditionLessThan = library.findByEditionLessThan(Integer.MAX_VALUE).toList();
140+
141+
assertSoftly(softly -> {
142+
softly.assertThat(booksFoundByEditionLessThan).as("query result from findByEditionLessThan() is a non-null instance").isNotNull();
143+
softly.assertThat(booksFoundByEditionLessThan.size()).as("query result size from findByEditionLessThan() is correct").isEqualTo(1);
144+
softly.assertThat(booksFoundByEditionLessThan.get(0)).as("returned books from findByEditionLessThan() are correct").isEqualTo(book);
145+
});
146+
147+
var booksFoundByEditionLessThanZero = library.findByEditionLessThan(0).toList();
148+
149+
assertSoftly(softly -> {
150+
softly.assertThat(booksFoundByEditionLessThanZero).as("query result from findByEditionLessThan(0) is a non-null instance").isNotNull();
151+
softly.assertThat(booksFoundByEditionLessThanZero.size()).as("query result size from findByEditionLessThan(0) is correct").isEqualTo(0);
152+
});
153+
154+
var booksFoundByEditionGreaterThan = library.findByEditionGreaterThan(0).toList();
155+
156+
assertSoftly(softly -> {
157+
softly.assertThat(booksFoundByEditionGreaterThan).as("query result from findByEditionGreaterThan() is a non-null instance").isNotNull();
158+
softly.assertThat(booksFoundByEditionGreaterThan.size()).as("query result size from findByEditionGreaterThan() is correct").isEqualTo(1);
159+
softly.assertThat(booksFoundByEditionGreaterThan.get(0)).as("returned books from findByEditionGreaterThan() are correct").isEqualTo(book);
160+
});
161+
162+
163+
var booksFoundByEditionGreaterThanMaxValue = library.findByEditionGreaterThan(Integer.MAX_VALUE).toList();
164+
165+
assertSoftly(softly -> {
166+
softly.assertThat(booksFoundByEditionGreaterThanMaxValue).as("query result from findByEditionGreaterThan(Integer.MAX_VALUE) is a non-null instance").isNotNull();
167+
softly.assertThat(booksFoundByEditionGreaterThanMaxValue.size()).as("query result size from findByEditionGreaterThan(Integer.MAX_VALUE) is correct").isEqualTo(0);
168+
});
169+
}
170+
171+
@Test
172+
public void shouldFindByEditionGreaterThan() {
173+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
174+
assertThat(library.save(book))
175+
.isNotNull()
176+
.isEqualTo(book);
177+
178+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
179+
180+
var booksFoundByEditionGreaterThanZero = library.findByEditionGreaterThan(0).toList();
181+
182+
assertSoftly(softly -> {
183+
softly.assertThat(booksFoundByEditionGreaterThanZero).as("query result from findByEditionGreaterThan() is a non-null instance").isNotNull();
184+
softly.assertThat(booksFoundByEditionGreaterThanZero.size()).as("query result size from findByEditionGreaterThan() is correct").isEqualTo(1);
185+
softly.assertThat(booksFoundByEditionGreaterThanZero.get(0)).as("returned books from findByEditionGreaterThan() are correct").isEqualTo(book);
186+
});
187+
188+
189+
var booksFoundByEditionGreaterThanMaxValue = library.findByEditionGreaterThan(Integer.MAX_VALUE).toList();
190+
191+
assertSoftly(softly -> {
192+
softly.assertThat(booksFoundByEditionGreaterThanMaxValue).as("query result from findByEditionGreaterThan(Integer.MAX_VALUE) is a non-null instance").isNotNull();
193+
softly.assertThat(booksFoundByEditionGreaterThanMaxValue.size()).as("query result size from findByEditionGreaterThan(Integer.MAX_VALUE) is correct").isEqualTo(0);
194+
});
195+
}
196+
197+
@Test
198+
public void shouldDeleteById() {
199+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
200+
assertThat(library.save(book))
201+
.isNotNull()
202+
.isEqualTo(book);
203+
204+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
205+
206+
library.deleteById(book.id());
207+
208+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
209+
210+
assertThat(library.findById(book.id()))
211+
.isNotNull().isEmpty();
212+
}
213+
214+
}

0 commit comments

Comments
 (0)