|
15 | 15 |
|
16 | 16 | package org.eclipse.jnosql.communication.mongodb.document; |
17 | 17 |
|
| 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.Test; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.stream.Stream; |
| 33 | + |
| 34 | +import static jakarta.nosql.document.DocumentDeleteQuery.delete; |
| 35 | +import static jakarta.nosql.document.DocumentQuery.select; |
| 36 | + |
18 | 37 | public class MongoDBQueryTest { |
19 | 38 |
|
| 39 | + public static final String COLLECTION_NAME = "person"; |
| 40 | + private static DocumentCollectionManager entityManager; |
| 41 | + |
| 42 | + @BeforeAll |
| 43 | + public static void setUp() throws IOException { |
| 44 | + entityManager = ManagerFactorySupplier.INSTANCE.get("database"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void shouldQuery() { |
| 49 | + DocumentEntity entity = getEntity(); |
| 50 | + entityManager.insert(entity); |
| 51 | + |
| 52 | + DocumentQuery query = select() |
| 53 | + .from(COLLECTION_NAME) |
| 54 | + .where("name").eq("Otavio") |
| 55 | + .or("name").eq("Poliana").build(); |
| 56 | + |
| 57 | + Stream<DocumentEntity> stream = entityManager.select(query); |
| 58 | + long count = stream.count(); |
| 59 | + Assertions.assertEquals(1L, count); |
| 60 | + entityManager.delete(delete().from(COLLECTION_NAME).build()); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + private DocumentEntity getEntity() { |
| 65 | + DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME); |
| 66 | + Map<String, Object> map = new HashMap<>(); |
| 67 | + map.put("name", "Poliana"); |
| 68 | + map.put("city", "Salvador"); |
| 69 | + List<Document> documents = Documents.of(map); |
| 70 | + documents.forEach(entity::add); |
| 71 | + return entity; |
| 72 | + } |
20 | 73 |
|
21 | 74 | } |
0 commit comments