Skip to content

Commit

Permalink
fix: fixed the persistence of multi-level entities at jnosql-mongodb
Browse files Browse the repository at this point in the history
Signed-off-by: Maximillian Arruda <dearrudam@gmail.com>
  • Loading branch information
dearrudam committed Mar 15, 2024
1 parent 557faed commit b3f302b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static Object getMap(Object val) {
Map<Object, Object> map = new HashMap<>();
for (Object item : iterable) {
var document = cast(item);
map.put(document.name(), document.get());
map.put(document.name(), convert(document.value()));
}
return map;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.eclipse.jnosql.databases.mongodb.integration;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;
import jakarta.nosql.Id;

import java.util.List;

@Entity
public record BookOrder(@Id String id, @Column List<BookOrderItem> items){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.eclipse.jnosql.databases.mongodb.integration;

import jakarta.nosql.Column;
import jakarta.nosql.Entity;

@Entity
public record BookOrderItem(@Column Book book, @Column Integer quantity) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.eclipse.jnosql.databases.mongodb.integration;

import jakarta.data.repository.Repository;
import org.eclipse.jnosql.mapping.NoSQLRepository;

@Repository
public interface BookStore extends NoSQLRepository<BookOrder, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.jnosql.databases.mongodb.integration;

import jakarta.inject.Inject;
import org.assertj.core.api.SoftAssertions;
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate;
import jakarta.nosql.Convert;
Expand All @@ -33,8 +34,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

import java.util.List;

import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
import static org.eclipse.jnosql.databases.mongodb.communication.DocumentDatabase.INSTANCE;
Expand All @@ -59,6 +64,11 @@ class RepositoryIntegrationTest {
@Database(DatabaseType.DOCUMENT)
BookRepository repository;


@Inject
@Database(DatabaseType.DOCUMENT)
BookStore bookStore;

@Test
void shouldSave() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
Expand All @@ -75,6 +85,36 @@ void shouldSave() {
assertThat(repository.findById(book.id()))
.isNotNull().get().isEqualTo(updated);

assertSoftly(softly -> {

BookOrder order = new BookOrder(
randomUUID().toString(),
List.of(
new BookOrderItem(new Book(randomUUID().toString(), "Effective Java", 3), 1)
,new BookOrderItem(new Book(randomUUID().toString(), "Java Persistence Layer", 1), 10)
,new BookOrderItem(new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1), 5)
)
);

bookStore.save(order);

softly.assertThat(bookStore.findById(order.id()))
.as("cannot find the order persisted previously")
.isPresent()
.get()
.as("the loaded the persisted BookOrder doesn't matches with the BookOrder origin")
.satisfies(persistedOrder ->{
softly.assertThat(persistedOrder.id())
.as("the loaded the persisted BookOrder id is not equals to the BookOrder origin id")
.isEqualTo(order.id());
softly.assertThat(persistedOrder.items())
.as("the loaded the persisted BookOrder items is not equals to the BookOrder origin items")
.containsAll(order.items());
});


});

}

@Test
Expand Down Expand Up @@ -111,7 +151,9 @@ void shouldDeleteAll() {
}

repository.deleteAll();
assertThat(repository.findAll()).isEmpty();
bookStore.deleteAll();
assertThat(repository.findAll()).as("the repository is not empty").isEmpty();
assertThat(bookStore.findAll()).as("the bookStore is not empty").isEmpty();
}

}

0 comments on commit b3f302b

Please sign in to comment.