Skip to content

Commit 5cdb6df

Browse files
authored
Merge pull request #222 from eclipse/integration-test-mongodb
Create integration tests on the repositories databases[MongoDB]
2 parents ed78fcf + 7079e80 commit 5cdb6df

File tree

6 files changed

+255
-15
lines changed

6 files changed

+255
-15
lines changed

CHANGELOG.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
1919

2020
=== Fixed
2121

22-
- Fix the ArangoDBDocumentManager implementation to shut down the ArangoDB instance
22+
- Fix the ArangoDBDocumentManager implementation to shut down the ArangoDB instance.
23+
- Fix integration on MongoDBTemplate
2324

2425
== [1.0.0-b6] - 2023-03-11
2526

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/mapping/DefaultMongoDBTemplate.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,18 @@
4040
@Typed(MongoDBTemplate.class)
4141
class DefaultMongoDBTemplate extends AbstractDocumentTemplate implements MongoDBTemplate {
4242

43-
private Instance<MongoDBDocumentManager> manager;
43+
private final Instance<MongoDBDocumentManager> manager;
4444

45-
private DocumentEntityConverter converter;
45+
private final DocumentEntityConverter converter;
4646

47-
private DocumentWorkflow workflow;
47+
private final DocumentWorkflow workflow;
4848

49-
private EntitiesMetadata entities;
49+
private final EntitiesMetadata entities;
5050

51-
private Converters converters;
51+
private final Converters converters;
5252

53-
private DocumentEventPersistManager persistManager;
53+
private final DocumentEventPersistManager persistManager;
5454

55-
/**
56-
* To CDI only
57-
*/
58-
@Deprecated
59-
DefaultMongoDBTemplate() {
60-
}
6155

6256
@Inject
6357
DefaultMongoDBTemplate(Instance<MongoDBDocumentManager> manager,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ public MongoDBDocumentManager get(String database) {
4747

4848
private Settings getSettings() {
4949
Map<String,Object> settings = new HashMap<>();
50-
String host = mongodb.getHost() + ":" + mongodb.getFirstMappedPort();
51-
settings.put(MongoDBDocumentConfigurations.HOST.get()+".1", host);
50+
settings.put(MongoDBDocumentConfigurations.HOST.get()+".1", host());
5251
return Settings.of(settings);
5352
}
5453

54+
public String host() {
55+
return mongodb.getHost() + ":" + mongodb.getFirstMappedPort();
56+
}
57+
5558
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Entity;
19+
import jakarta.nosql.Id;
20+
21+
@Entity
22+
public record Book(@Id String id, @Column("title") String title, @Column("edition") int edition) {
23+
24+
25+
}
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+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 jakarta.nosql.document.DocumentTemplate;
20+
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
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+
@AddExtensions({EntityMetadataExtension.class,
44+
DocumentExtension.class})
45+
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
46+
class TemplateIntegrationTest {
47+
48+
@Inject
49+
private DocumentTemplate template;
50+
51+
static {
52+
INSTANCE.get("library");
53+
System.setProperty(MongoDBDocumentConfigurations.HOST.get() + ".1", INSTANCE.host());
54+
System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library");
55+
}
56+
57+
@Test
58+
public void shouldInsert() {
59+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
60+
template.insert(book);
61+
Optional<Book> optional = template.find(Book.class, book.id());
62+
assertThat(optional).isNotNull().isNotEmpty()
63+
.get().isEqualTo(book);
64+
}
65+
66+
@Test
67+
public void shouldUpdate() {
68+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
69+
assertThat(template.insert(book))
70+
.isNotNull()
71+
.isEqualTo(book);
72+
73+
Book updated = new Book(book.id(), book.title() + " updated", 2);
74+
75+
assertThat(template.update(updated))
76+
.isNotNull()
77+
.isNotEqualTo(book);
78+
79+
assertThat(template.find(Book.class, book.id()))
80+
.isNotNull().get().isEqualTo(updated);
81+
82+
}
83+
84+
@Test
85+
public void shouldFindById() {
86+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
87+
assertThat(template.insert(book))
88+
.isNotNull()
89+
.isEqualTo(book);
90+
91+
assertThat(template.find(Book.class, book.id()))
92+
.isNotNull().get().isEqualTo(book);
93+
}
94+
95+
@Test
96+
public void shouldDelete() {
97+
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
98+
assertThat(template.insert(book))
99+
.isNotNull()
100+
.isEqualTo(book);
101+
102+
template.delete(Book.class, book.id());
103+
assertThat(template.find(Book.class, book.id()))
104+
.isNotNull().isEmpty();
105+
}
106+
107+
108+
}

0 commit comments

Comments
 (0)