Skip to content

Commit b3f302b

Browse files
committed
fix: fixed the persistence of multi-level entities at jnosql-mongodb
Signed-off-by: Maximillian Arruda <[email protected]>
1 parent 557faed commit b3f302b

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private static Object getMap(Object val) {
106106
Map<Object, Object> map = new HashMap<>();
107107
for (Object item : iterable) {
108108
var document = cast(item);
109-
map.put(document.name(), document.get());
109+
map.put(document.name(), convert(document.value()));
110110
}
111111
return map;
112112
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.eclipse.jnosql.databases.mongodb.integration;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Entity;
5+
import jakarta.nosql.Id;
6+
7+
import java.util.List;
8+
9+
@Entity
10+
public record BookOrder(@Id String id, @Column List<BookOrderItem> items){
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.eclipse.jnosql.databases.mongodb.integration;
2+
3+
import jakarta.nosql.Column;
4+
import jakarta.nosql.Entity;
5+
6+
@Entity
7+
public record BookOrderItem(@Column Book book, @Column Integer quantity) {
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.eclipse.jnosql.databases.mongodb.integration;
2+
3+
import jakarta.data.repository.Repository;
4+
import org.eclipse.jnosql.mapping.NoSQLRepository;
5+
6+
@Repository
7+
public interface BookStore extends NoSQLRepository<BookOrder, String> {
8+
}

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/RepositoryIntegrationTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.jnosql.databases.mongodb.integration;
1616

1717
import jakarta.inject.Inject;
18+
import org.assertj.core.api.SoftAssertions;
1819
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
1920
import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate;
2021
import jakarta.nosql.Convert;
@@ -33,8 +34,12 @@
3334
import org.junit.jupiter.api.Test;
3435
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3536

37+
import java.util.List;
38+
3639
import static java.util.UUID.randomUUID;
40+
import static org.assertj.core.api.Assertions.as;
3741
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
3843
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
3944
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
4045
import static org.eclipse.jnosql.databases.mongodb.communication.DocumentDatabase.INSTANCE;
@@ -59,6 +64,11 @@ class RepositoryIntegrationTest {
5964
@Database(DatabaseType.DOCUMENT)
6065
BookRepository repository;
6166

67+
68+
@Inject
69+
@Database(DatabaseType.DOCUMENT)
70+
BookStore bookStore;
71+
6272
@Test
6373
void shouldSave() {
6474
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
@@ -75,6 +85,36 @@ void shouldSave() {
7585
assertThat(repository.findById(book.id()))
7686
.isNotNull().get().isEqualTo(updated);
7787

88+
assertSoftly(softly -> {
89+
90+
BookOrder order = new BookOrder(
91+
randomUUID().toString(),
92+
List.of(
93+
new BookOrderItem(new Book(randomUUID().toString(), "Effective Java", 3), 1)
94+
,new BookOrderItem(new Book(randomUUID().toString(), "Java Persistence Layer", 1), 10)
95+
,new BookOrderItem(new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1), 5)
96+
)
97+
);
98+
99+
bookStore.save(order);
100+
101+
softly.assertThat(bookStore.findById(order.id()))
102+
.as("cannot find the order persisted previously")
103+
.isPresent()
104+
.get()
105+
.as("the loaded the persisted BookOrder doesn't matches with the BookOrder origin")
106+
.satisfies(persistedOrder ->{
107+
softly.assertThat(persistedOrder.id())
108+
.as("the loaded the persisted BookOrder id is not equals to the BookOrder origin id")
109+
.isEqualTo(order.id());
110+
softly.assertThat(persistedOrder.items())
111+
.as("the loaded the persisted BookOrder items is not equals to the BookOrder origin items")
112+
.containsAll(order.items());
113+
});
114+
115+
116+
});
117+
78118
}
79119

80120
@Test
@@ -111,7 +151,9 @@ void shouldDeleteAll() {
111151
}
112152

113153
repository.deleteAll();
114-
assertThat(repository.findAll()).isEmpty();
154+
bookStore.deleteAll();
155+
assertThat(repository.findAll()).as("the repository is not empty").isEmpty();
156+
assertThat(bookStore.findAll()).as("the bookStore is not empty").isEmpty();
115157
}
116158

117159
}

0 commit comments

Comments
 (0)