|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Otávio Santana and others |
| 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.communication.mongodb.document; |
| 16 | + |
| 17 | +import com.mongodb.client.model.Accumulators; |
| 18 | +import com.mongodb.client.model.Aggregates; |
| 19 | +import com.mongodb.client.model.Filters; |
| 20 | +import jakarta.nosql.document.Document; |
| 21 | +import jakarta.nosql.document.DocumentCollectionManager; |
| 22 | +import jakarta.nosql.document.DocumentDeleteQuery; |
| 23 | +import jakarta.nosql.document.DocumentEntity; |
| 24 | +import jakarta.nosql.document.DocumentQuery; |
| 25 | +import org.bson.BsonValue; |
| 26 | +import org.bson.conversions.Bson; |
| 27 | +import org.eclipse.jnosql.communication.document.Documents; |
| 28 | +import org.junit.jupiter.api.Assertions; |
| 29 | +import org.junit.jupiter.api.BeforeAll; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | +import java.util.Optional; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import java.util.stream.Stream; |
| 42 | + |
| 43 | +import static com.mongodb.client.model.Filters.eq; |
| 44 | +import static jakarta.nosql.document.DocumentQuery.select; |
| 45 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 46 | +import static org.hamcrest.Matchers.contains; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 48 | + |
| 49 | +public class MongoDBSpecificFeaturesTest { |
| 50 | + |
| 51 | + public static final String COLLECTION_NAME = "person"; |
| 52 | + private static MongoDBDocumentCollectionManager entityManager; |
| 53 | + |
| 54 | + @BeforeAll |
| 55 | + public static void setUp() throws IOException { |
| 56 | + entityManager = ManagerFactorySupplier.INSTANCE.get("database"); |
| 57 | + } |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + public void beforeEach() { |
| 61 | + DocumentDeleteQuery.delete().from(COLLECTION_NAME).delete(entityManager); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void shouldReturnErrorOnSelectWhenThereIsNullParameter(){ |
| 66 | + Assertions.assertThrows(NullPointerException.class, |
| 67 | + () -> entityManager.select(null, null)); |
| 68 | + Assertions.assertThrows(NullPointerException.class, |
| 69 | + () -> entityManager.select(COLLECTION_NAME, null)); |
| 70 | + |
| 71 | + Assertions.assertThrows(NullPointerException.class, |
| 72 | + () -> entityManager.select(null, eq("name", "Poliana"))); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldFindDocument() { |
| 77 | + DocumentEntity entity = entityManager.insert(getEntity()); |
| 78 | + |
| 79 | + List<DocumentEntity> entities = entityManager.select(COLLECTION_NAME, |
| 80 | + eq("name", "Poliana")).collect(Collectors.toList()); |
| 81 | + assertFalse(entities.isEmpty()); |
| 82 | + assertThat(entities, contains(entity)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void shouldReturnErrorOnDeleteWhenThereIsNullParameter(){ |
| 87 | + Assertions.assertThrows(NullPointerException.class, |
| 88 | + () -> entityManager.delete(null, null)); |
| 89 | + Assertions.assertThrows(NullPointerException.class, |
| 90 | + () -> entityManager.delete(COLLECTION_NAME, null)); |
| 91 | + |
| 92 | + Assertions.assertThrows(NullPointerException.class, |
| 93 | + () -> entityManager.delete(null, eq("name", "Poliana"))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void shouldDelete() { |
| 98 | + entityManager.insert(getEntity()); |
| 99 | + |
| 100 | + long result = entityManager.delete(COLLECTION_NAME, |
| 101 | + eq("name", "Poliana")); |
| 102 | + |
| 103 | + Assertions.assertEquals(1L, result); |
| 104 | + List<DocumentEntity> entities = entityManager.select(COLLECTION_NAME, |
| 105 | + eq("name", "Poliana")).collect(Collectors.toList()); |
| 106 | + Assertions.assertTrue(entities.isEmpty()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void shouldReturnErrorOnAggregateWhenThereIsNullParameter(){ |
| 111 | + Assertions.assertThrows(NullPointerException.class, |
| 112 | + () -> entityManager.aggregate(null, null)); |
| 113 | + Assertions.assertThrows(NullPointerException.class, |
| 114 | + () -> entityManager.aggregate(COLLECTION_NAME, null)); |
| 115 | + |
| 116 | + Assertions.assertThrows(NullPointerException.class, |
| 117 | + () -> entityManager.aggregate(null, |
| 118 | + Collections.singletonList(eq("name", "Poliana")))); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void shouldAggregate() { |
| 123 | + List<Bson> predicates = Arrays.asList( |
| 124 | + Aggregates.match(eq("name", "Poliana")), |
| 125 | + Aggregates.group("$stars", Accumulators.sum("count", 1)) |
| 126 | + ); |
| 127 | + entityManager.insert(getEntity()); |
| 128 | + Stream<Map<String, BsonValue>> aggregate = entityManager.aggregate(COLLECTION_NAME, predicates); |
| 129 | + Assertions.assertNotNull(aggregate); |
| 130 | + Map<String, BsonValue> result = aggregate.findFirst() |
| 131 | + .orElseThrow(() -> new IllegalStateException("There is an issue with the aggregate test result")); |
| 132 | + |
| 133 | + Assertions.assertNotNull(result); |
| 134 | + Assertions.assertFalse(result.isEmpty()); |
| 135 | + BsonValue count = result.get("count"); |
| 136 | + Assertions.assertEquals(1L, count.asNumber().longValue()); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + private DocumentEntity getEntity() { |
| 142 | + DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME); |
| 143 | + Map<String, Object> map = new HashMap<>(); |
| 144 | + map.put("name", "Poliana"); |
| 145 | + map.put("city", "Salvador"); |
| 146 | + List<Document> documents = Documents.of(map); |
| 147 | + documents.forEach(entity::add); |
| 148 | + return entity; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments