Skip to content

Commit d4c4dc8

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

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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.CouchbaseTemplate;
23+
import org.eclipse.jnosql.mapping.Convert;
24+
import org.eclipse.jnosql.mapping.document.DocumentEntityConverter;
25+
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
26+
import org.eclipse.jnosql.mapping.reflection.EntityMetadataExtension;
27+
import org.jboss.weld.junit5.auto.AddExtensions;
28+
import org.jboss.weld.junit5.auto.AddPackages;
29+
import org.jboss.weld.junit5.auto.EnableAutoWeld;
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
34+
35+
import java.util.Map;
36+
import java.util.Optional;
37+
38+
import static java.util.UUID.randomUUID;
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
41+
import static org.awaitility.Awaitility.await;
42+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
43+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
44+
45+
@EnableAutoWeld
46+
@AddPackages(value = {Convert.class, DocumentEntityConverter.class})
47+
@AddPackages(Book.class)
48+
@AddPackages(CouchbaseTemplate.class)
49+
@AddExtensions({EntityMetadataExtension.class,
50+
DocumentExtension.class})
51+
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
52+
class CouchbaseTemplateIntegrationTest {
53+
54+
@Inject
55+
private CouchbaseTemplate template;
56+
57+
static {
58+
CouchbaseUtil.systemPropertySetup();
59+
}
60+
61+
@BeforeEach
62+
@AfterEach
63+
public void cleanUp(){
64+
template.deleteAll(Book.class);
65+
}
66+
67+
@Test
68+
public void shouldInsert() {
69+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
70+
template.insert(book);
71+
Optional<Book> optional = template.find(Book.class, book.id());
72+
assertThat(optional).isNotNull().isNotEmpty()
73+
.get().isEqualTo(book);
74+
}
75+
76+
@Test
77+
public void shouldUpdate() throws InterruptedException {
78+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
79+
assertThat(template.insert(book))
80+
.isNotNull()
81+
.isEqualTo(book);
82+
83+
Book updated = new Book(book.id(), book.title() + " updated", 2);
84+
85+
assertThat(template.update(updated))
86+
.isNotNull()
87+
.isNotEqualTo(book);
88+
89+
await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);
90+
91+
assertThat(template.find(Book.class, book.id()))
92+
.isNotNull().get().isEqualTo(updated);
93+
94+
}
95+
96+
@Test
97+
public void shouldFindById() {
98+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
99+
assertThat(template.insert(book))
100+
.isNotNull()
101+
.isEqualTo(book);
102+
103+
assertThat(template.find(Book.class, book.id()))
104+
.isNotNull().get().isEqualTo(book);
105+
}
106+
107+
@Test
108+
public void shouldFindByN1qlWithParams() {
109+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
110+
assertThat(template.insert(book))
111+
.isNotNull()
112+
.isEqualTo(book);
113+
114+
var data = template.n1qlQuery("select * from " + CouchbaseUtil.BUCKET_NAME + "._default.Book where title = $title",
115+
JsonObject.from(Map.of("title", book.title()))).toList();
116+
117+
assertSoftly(softly -> {
118+
softly.assertThat(data).as("query result is a non-null instance").isNotNull();
119+
softly.assertThat(data.size()).as("query result size is correct").isEqualTo(1);
120+
softly.assertThat(data.get(0)).as("returned data is correct").isEqualTo(book);
121+
});
122+
}
123+
124+
@Test
125+
public void shouldFindByN1qlWithoutParams() {
126+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
127+
assertThat(template.insert(book))
128+
.isNotNull()
129+
.isEqualTo(book);
130+
131+
var data = template.n1qlQuery("select * from " + CouchbaseUtil.BUCKET_NAME + "._default.Book").toList();
132+
assertSoftly(softly -> {
133+
softly.assertThat(data).as("query result is a non-null instance").isNotNull();
134+
softly.assertThat(data.size()).as("query result size is correct").isEqualTo(1);
135+
softly.assertThat(data.get(0)).as("returned data is correct").isEqualTo(book);
136+
});
137+
}
138+
139+
@Test
140+
public void shouldDelete() {
141+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
142+
assertThat(template.insert(book))
143+
.isNotNull()
144+
.isEqualTo(book);
145+
146+
template.delete(Book.class, book.id());
147+
assertThat(template.find(Book.class, book.id()))
148+
.isNotNull().isEmpty();
149+
}
150+
151+
152+
}

0 commit comments

Comments
 (0)