Skip to content

Commit 765edcb

Browse files
committed
test: create integration test to MongodbTemplate
Signed-off-by: Otavio Santana <[email protected]>
1 parent dfb012b commit 765edcb

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.integration;
16+
17+
18+
import jakarta.inject.Inject;
19+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
20+
import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate;
21+
import org.eclipse.jnosql.mapping.Convert;
22+
import org.eclipse.jnosql.mapping.config.MappingConfigurations;
23+
import org.eclipse.jnosql.mapping.document.DocumentEntityConverter;
24+
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
25+
import org.eclipse.jnosql.mapping.reflection.EntityMetadataExtension;
26+
import org.jboss.weld.junit5.auto.AddExtensions;
27+
import org.jboss.weld.junit5.auto.AddPackages;
28+
import org.jboss.weld.junit5.auto.EnableAutoWeld;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
31+
32+
import java.util.Optional;
33+
34+
import static java.util.UUID.randomUUID;
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
37+
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
38+
import static org.eclipse.jnosql.databases.mongodb.communication.DocumentDatabase.INSTANCE;
39+
40+
@EnableAutoWeld
41+
@AddPackages(value = {Convert.class, DocumentEntityConverter.class})
42+
@AddPackages(Book.class)
43+
@AddPackages(MongoDBTemplate.class)
44+
@AddExtensions({EntityMetadataExtension.class,
45+
DocumentExtension.class})
46+
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
47+
class MongoDBTemplateIntegrationTest {
48+
49+
@Inject
50+
private MongoDBTemplate template;
51+
52+
static {
53+
INSTANCE.get("library");
54+
System.setProperty(MongoDBDocumentConfigurations.HOST.get() + ".1", INSTANCE.host());
55+
System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library");
56+
}
57+
58+
@Test
59+
public void shouldInsert() {
60+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
61+
template.insert(book);
62+
Optional<Book> optional = template.find(Book.class, book.id());
63+
assertThat(optional).isNotNull().isNotEmpty()
64+
.get().isEqualTo(book);
65+
}
66+
67+
@Test
68+
public void shouldUpdate() {
69+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
70+
assertThat(template.insert(book))
71+
.isNotNull()
72+
.isEqualTo(book);
73+
74+
Book updated = new Book(book.id(), book.title() + " updated", 2);
75+
76+
assertThat(template.update(updated))
77+
.isNotNull()
78+
.isNotEqualTo(book);
79+
80+
assertThat(template.find(Book.class, book.id()))
81+
.isNotNull().get().isEqualTo(updated);
82+
83+
}
84+
85+
@Test
86+
public void shouldFindById() {
87+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
88+
assertThat(template.insert(book))
89+
.isNotNull()
90+
.isEqualTo(book);
91+
92+
assertThat(template.find(Book.class, book.id()))
93+
.isNotNull().get().isEqualTo(book);
94+
}
95+
96+
@Test
97+
public void shouldDelete() {
98+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
99+
assertThat(template.insert(book))
100+
.isNotNull()
101+
.isEqualTo(book);
102+
103+
template.delete(Book.class, book.id());
104+
assertThat(template.find(Book.class, book.id()))
105+
.isNotNull().isEmpty();
106+
}
107+
108+
109+
}

0 commit comments

Comments
 (0)