|
14 | 14 | */ |
15 | 15 | package org.eclipse.jnosql.communication.mongodb.document; |
16 | 16 |
|
| 17 | +import com.mongodb.client.model.Filters; |
| 18 | +import jakarta.nosql.document.Document; |
| 19 | +import jakarta.nosql.document.DocumentCollectionManager; |
| 20 | +import jakarta.nosql.document.DocumentDeleteQuery; |
| 21 | +import jakarta.nosql.document.DocumentEntity; |
| 22 | +import jakarta.nosql.document.DocumentQuery; |
| 23 | +import org.eclipse.jnosql.communication.document.Documents; |
| 24 | +import org.junit.jupiter.api.Assertions; |
| 25 | +import org.junit.jupiter.api.BeforeAll; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.Optional; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +import static com.mongodb.client.model.Filters.eq; |
| 37 | +import static jakarta.nosql.document.DocumentQuery.select; |
| 38 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | +import static org.hamcrest.Matchers.contains; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 41 | + |
17 | 42 | public class MongoDBSpecificFeaturesTest { |
| 43 | + |
| 44 | + public static final String COLLECTION_NAME = "person"; |
| 45 | + private static MongoDBDocumentCollectionManager entityManager; |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + public static void setUp() throws IOException { |
| 49 | + entityManager = ManagerFactorySupplier.INSTANCE.get("database"); |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void beforeEach() { |
| 54 | + DocumentDeleteQuery.delete().from(COLLECTION_NAME).delete(entityManager); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void shouldReturnErrorWhenThereIsNullParameter(){ |
| 59 | + Assertions.assertThrows(NullPointerException.class, |
| 60 | + () -> entityManager.select(null, null)); |
| 61 | + Assertions.assertThrows(NullPointerException.class, |
| 62 | + () -> entityManager.select(COLLECTION_NAME, null)); |
| 63 | + |
| 64 | + Assertions.assertThrows(NullPointerException.class, |
| 65 | + () -> entityManager.select(null, eq("name", "Poliana"))); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldFindDocument() { |
| 70 | + DocumentEntity entity = entityManager.insert(getEntity()); |
| 71 | + Optional<Document> id = entity.find("_id"); |
| 72 | + |
| 73 | + |
| 74 | + List<DocumentEntity> entities = entityManager.select(COLLECTION_NAME, |
| 75 | + eq("name", "Poliana")).collect(Collectors.toList()); |
| 76 | + assertFalse(entities.isEmpty()); |
| 77 | + assertThat(entities, contains(entity)); |
| 78 | + } |
| 79 | + |
| 80 | + private DocumentEntity getEntity() { |
| 81 | + DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME); |
| 82 | + Map<String, Object> map = new HashMap<>(); |
| 83 | + map.put("name", "Poliana"); |
| 84 | + map.put("city", "Salvador"); |
| 85 | + List<Document> documents = Documents.of(map); |
| 86 | + documents.forEach(entity::add); |
| 87 | + return entity; |
| 88 | + } |
| 89 | + |
18 | 90 | } |
0 commit comments