Skip to content

Commit d28b6f0

Browse files
authored
Merge pull request #278 from dearrudam/add-custom-repository-mongodb
Added tests to verify the CustomRepository implementation required by Jakarta Data TCK
2 parents a5a9019 + f6c1313 commit d28b6f0

File tree

3 files changed

+324
-0
lines changed

3 files changed

+324
-0
lines changed

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/DocumentDatabase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package org.eclipse.jnosql.databases.mongodb.communication;
1717

1818

19+
import com.mongodb.ConnectionString;
20+
import com.mongodb.client.MongoClient;
21+
import com.mongodb.client.MongoClients;
1922
import org.eclipse.jnosql.communication.Settings;
2023
import org.testcontainers.containers.GenericContainer;
2124
import org.testcontainers.containers.wait.strategy.Wait;
@@ -37,6 +40,10 @@ public enum DocumentDatabase {
3740
mongodb.start();
3841
}
3942

43+
public MongoClient mongoClient() {
44+
return MongoClients.create(new ConnectionString("mongodb://" + host()));
45+
}
46+
4047
public MongoDBDocumentManager get(String database) {
4148
Settings settings = getSettings();
4249
MongoDBDocumentConfiguration configuration = new MongoDBDocumentConfiguration();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2022 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.mongodb.integration;
16+
17+
import jakarta.data.Order;
18+
import jakarta.data.page.Page;
19+
import jakarta.data.page.PageRequest;
20+
import jakarta.data.repository.*;
21+
22+
import java.util.Optional;
23+
import java.util.stream.Stream;
24+
25+
@Repository
26+
public interface BookCustomRepository {
27+
28+
@Save
29+
Book save(Book book);
30+
31+
@Save
32+
Iterable<Book> saveAll(Iterable<Book> books);
33+
34+
@Delete
35+
void delete(Book book);
36+
37+
@Delete
38+
void removeAll(Iterable<Book> books);
39+
40+
@Find
41+
Optional<Book> getById(@By("id") String id);
42+
43+
@Find
44+
Stream<Book> findByIdIn(Iterable<String> ids);
45+
46+
@Find
47+
Stream<Book> listAll();
48+
49+
@Find
50+
Page<Book> listAll(PageRequest pageRequest, Order<Book> sortBy);
51+
52+
@Query("delete from Book")
53+
void deleteAll();
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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.mongodb.integration;
16+
17+
import com.mongodb.client.MongoClient;
18+
import com.mongodb.client.MongoCollection;
19+
import jakarta.data.Order;
20+
import jakarta.data.Sort;
21+
import jakarta.data.page.Page;
22+
import jakarta.data.page.PageRequest;
23+
import jakarta.inject.Inject;
24+
import org.bson.BsonDocument;
25+
import org.bson.Document;
26+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
27+
import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate;
28+
import org.eclipse.jnosql.mapping.Database;
29+
import org.eclipse.jnosql.mapping.DatabaseType;
30+
import org.eclipse.jnosql.mapping.core.Converters;
31+
import org.eclipse.jnosql.mapping.core.config.MappingConfigurations;
32+
import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension;
33+
import org.eclipse.jnosql.mapping.document.DocumentTemplate;
34+
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
35+
import org.eclipse.jnosql.mapping.reflection.Reflections;
36+
import org.eclipse.jnosql.mapping.semistructured.EntityConverter;
37+
import org.jboss.weld.junit5.auto.AddExtensions;
38+
import org.jboss.weld.junit5.auto.AddPackages;
39+
import org.jboss.weld.junit5.auto.EnableAutoWeld;
40+
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
43+
44+
import java.time.Duration;
45+
import java.util.List;
46+
47+
import static java.util.UUID.randomUUID;
48+
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
50+
import static org.awaitility.Awaitility.await;
51+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
52+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
53+
import static org.eclipse.jnosql.databases.mongodb.communication.DocumentDatabase.INSTANCE;
54+
55+
@EnableAutoWeld
56+
@AddPackages(value = {Database.class, EntityConverter.class, DocumentTemplate.class, MongoDBTemplate.class})
57+
@AddPackages(Book.class)
58+
@AddPackages(Reflections.class)
59+
@AddPackages(Converters.class)
60+
@AddExtensions({EntityMetadataExtension.class,
61+
DocumentExtension.class})
62+
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
63+
class CustomRepositoryIntegrationTest {
64+
65+
public static final String DATABASE_NAME = "library";
66+
67+
static {
68+
INSTANCE.get(DATABASE_NAME);
69+
System.setProperty(MongoDBDocumentConfigurations.HOST.get() + ".1", INSTANCE.host());
70+
System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), DATABASE_NAME);
71+
}
72+
73+
@Inject
74+
@Database(DatabaseType.DOCUMENT)
75+
BookCustomRepository bookCustomRepository;
76+
77+
78+
@BeforeEach
79+
void cleanUp() {
80+
try (MongoClient mongoClient = INSTANCE.mongoClient()) {
81+
MongoCollection<Document> collection = mongoClient.getDatabase(DATABASE_NAME)
82+
.getCollection(Book.class.getSimpleName());
83+
collection.deleteMany(new BsonDocument());
84+
await().atMost(Duration.ofSeconds(2))
85+
.until(() -> collection.find().limit(1).first() == null);
86+
}
87+
}
88+
89+
@Test
90+
void shouldSave() {
91+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
92+
assertThat(bookCustomRepository.save(book))
93+
.isNotNull()
94+
.isEqualTo(book);
95+
96+
assertThat(bookCustomRepository.getById(book.id()))
97+
.as("should return the persisted book")
98+
.hasValue(book);
99+
100+
Book updated = new Book(book.id(), book.title() + " updated", 2);
101+
102+
assertThat(bookCustomRepository.save(updated))
103+
.isNotNull()
104+
.isNotEqualTo(book);
105+
106+
assertThat(bookCustomRepository.getById(book.id()))
107+
.as("should return the updated book")
108+
.hasValue(updated);
109+
}
110+
111+
@Test
112+
void shouldSaveAllAndFindByIdIn() {
113+
114+
List<Book> books = List.of(
115+
new Book(randomUUID().toString(), "Java Persistence Layer", 1)
116+
, new Book(randomUUID().toString(), "Effective Java", 3)
117+
, new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1)
118+
, new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1)
119+
);
120+
121+
assertThat(bookCustomRepository.saveAll(books))
122+
.isNotNull()
123+
.containsAll(books);
124+
125+
assertThat(bookCustomRepository.findByIdIn(books.stream().map(Book::id).toList()))
126+
.as("should return the persisted books")
127+
.containsAll(books);
128+
129+
}
130+
131+
@Test
132+
void shouldSaveAllAndFindBy() {
133+
134+
Book javaPersistenceLayer = new Book(randomUUID().toString(), "Java Persistence Layer", 1);
135+
Book effectiveJava = new Book(randomUUID().toString(), "Effective Java", 3);
136+
Book jakartaEeCookbook = new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1);
137+
Book masteringTheJavaVirtualMachine = new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1);
138+
139+
List<Book> books = List.of(
140+
javaPersistenceLayer
141+
, effectiveJava
142+
, jakartaEeCookbook
143+
, masteringTheJavaVirtualMachine
144+
);
145+
146+
assertThat(bookCustomRepository.saveAll(books))
147+
.isNotNull()
148+
.containsAll(books);
149+
150+
PageRequest pageRequest = PageRequest.ofSize(2);
151+
Order<Book> orderByTitleAsc = Order.by(Sort.asc("title"));
152+
153+
Page<Book> page1 = bookCustomRepository.listAll(pageRequest, orderByTitleAsc);
154+
Page<Book> page2 = bookCustomRepository.listAll(page1.nextPageRequest(), orderByTitleAsc);
155+
Page<Book> page3 = bookCustomRepository.listAll(page2.nextPageRequest(), orderByTitleAsc);
156+
157+
assertSoftly(softly -> {
158+
159+
softly.assertThat(page1)
160+
.as("should return the first page")
161+
.hasSize(2)
162+
.containsSequence(effectiveJava, jakartaEeCookbook);
163+
164+
softly.assertThat(page2)
165+
.as("should return the first page")
166+
.hasSize(2)
167+
.containsSequence(javaPersistenceLayer, masteringTheJavaVirtualMachine);
168+
169+
softly.assertThat(page3)
170+
.as("should return the third and last page with no items")
171+
.hasSize(0);
172+
});
173+
174+
175+
}
176+
177+
178+
@Test
179+
void shouldDelete() {
180+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
181+
assertThat(bookCustomRepository.save(book))
182+
.isNotNull()
183+
.isEqualTo(book);
184+
185+
assertThat(bookCustomRepository.getById(book.id()))
186+
.isNotNull()
187+
.hasValue(book);
188+
189+
bookCustomRepository.delete(book);
190+
191+
assertThat(bookCustomRepository.getById(book.id()))
192+
.isNotNull()
193+
.isEmpty();
194+
}
195+
196+
@Test
197+
void shouldDeleteAll() {
198+
199+
List<Book> books = List.of(
200+
new Book(randomUUID().toString(), "Java Persistence Layer", 1)
201+
, new Book(randomUUID().toString(), "Effective Java", 3)
202+
, new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1)
203+
, new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1)
204+
);
205+
206+
assertThat(bookCustomRepository.saveAll(books))
207+
.isNotNull()
208+
.containsAll(books);
209+
210+
await().atMost(Duration.ofSeconds(2))
211+
.until(() -> bookCustomRepository.listAll().toList().size() >= books.size());
212+
213+
assertThat(bookCustomRepository.listAll())
214+
.isNotNull()
215+
.containsAll(books);
216+
217+
bookCustomRepository.deleteAll();
218+
219+
await().atMost(Duration.ofSeconds(2))
220+
.until(() -> bookCustomRepository.listAll().toList().isEmpty());
221+
222+
223+
assertThat(bookCustomRepository.listAll())
224+
.isNotNull()
225+
.isEmpty();
226+
227+
}
228+
229+
@Test
230+
void shouldRemoveAll() {
231+
232+
List<Book> books = List.of(
233+
new Book(randomUUID().toString(), "Java Persistence Layer", 1)
234+
, new Book(randomUUID().toString(), "Effective Java", 3)
235+
, new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1)
236+
, new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1)
237+
);
238+
239+
assertThat(bookCustomRepository.saveAll(books))
240+
.isNotNull()
241+
.containsAll(books);
242+
243+
await().atMost(Duration.ofSeconds(2))
244+
.until(() -> bookCustomRepository.listAll().toList().size() >= books.size());
245+
246+
assertThat(bookCustomRepository.listAll())
247+
.isNotNull()
248+
.containsAll(books);
249+
250+
bookCustomRepository.removeAll(books);
251+
252+
await().atMost(Duration.ofSeconds(2))
253+
.until(() -> bookCustomRepository.listAll()
254+
.filter(books::contains)
255+
.toList().isEmpty());
256+
257+
assertThat(bookCustomRepository.findByIdIn(books.stream().map(Book::id).toList()))
258+
.isNotNull()
259+
.isEmpty();
260+
}
261+
262+
}

0 commit comments

Comments
 (0)