|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Contributors to the Eclipse Foundation |
| 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 | + * Maximillian Arruda |
| 14 | + */ |
| 15 | +package org.eclipse.jnosql.databases.mongodb.integration; |
| 16 | + |
| 17 | +import com.mongodb.client.MongoClient; |
| 18 | +import com.mongodb.client.MongoCollection; |
| 19 | +import jakarta.data.Order; |
| 20 | +import jakarta.data.Sort; |
| 21 | +import jakarta.data.page.Page; |
| 22 | +import jakarta.data.page.PageRequest; |
| 23 | +import jakarta.inject.Inject; |
| 24 | +import org.bson.BsonDocument; |
| 25 | +import org.bson.Document; |
| 26 | +import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations; |
| 27 | +import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate; |
| 28 | +import org.eclipse.jnosql.mapping.Database; |
| 29 | +import org.eclipse.jnosql.mapping.DatabaseType; |
| 30 | +import org.eclipse.jnosql.mapping.core.Converters; |
| 31 | +import org.eclipse.jnosql.mapping.core.config.MappingConfigurations; |
| 32 | +import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension; |
| 33 | +import org.eclipse.jnosql.mapping.document.DocumentTemplate; |
| 34 | +import org.eclipse.jnosql.mapping.document.spi.DocumentExtension; |
| 35 | +import org.eclipse.jnosql.mapping.reflection.Reflections; |
| 36 | +import org.eclipse.jnosql.mapping.semistructured.EntityConverter; |
| 37 | +import org.jboss.weld.junit5.auto.AddExtensions; |
| 38 | +import org.jboss.weld.junit5.auto.AddPackages; |
| 39 | +import org.jboss.weld.junit5.auto.EnableAutoWeld; |
| 40 | +import org.junit.jupiter.api.BeforeEach; |
| 41 | +import org.junit.jupiter.api.Test; |
| 42 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 43 | + |
| 44 | +import java.time.Duration; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +import static java.util.UUID.randomUUID; |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | +import static org.assertj.core.api.SoftAssertions.assertSoftly; |
| 50 | +import static org.awaitility.Awaitility.await; |
| 51 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES; |
| 52 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED; |
| 53 | +import static org.eclipse.jnosql.databases.mongodb.communication.DocumentDatabase.INSTANCE; |
| 54 | + |
| 55 | +@EnableAutoWeld |
| 56 | +@AddPackages(value = {Database.class, EntityConverter.class, DocumentTemplate.class, MongoDBTemplate.class}) |
| 57 | +@AddPackages(Book.class) |
| 58 | +@AddPackages(Reflections.class) |
| 59 | +@AddPackages(Converters.class) |
| 60 | +@AddExtensions({EntityMetadataExtension.class, |
| 61 | + DocumentExtension.class}) |
| 62 | +@EnabledIfSystemProperty(named = NAMED, matches = MATCHES) |
| 63 | +class CustomRepositoryIntegrationTest { |
| 64 | + |
| 65 | + public static final String DATABASE_NAME = "library"; |
| 66 | + |
| 67 | + static { |
| 68 | + INSTANCE.get(DATABASE_NAME); |
| 69 | + System.setProperty(MongoDBDocumentConfigurations.HOST.get() + ".1", INSTANCE.host()); |
| 70 | + System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), DATABASE_NAME); |
| 71 | + } |
| 72 | + |
| 73 | + @Inject |
| 74 | + @Database(DatabaseType.DOCUMENT) |
| 75 | + BookCustomRepository bookCustomRepository; |
| 76 | + |
| 77 | + |
| 78 | + @BeforeEach |
| 79 | + void cleanUp() { |
| 80 | + try (MongoClient mongoClient = INSTANCE.mongoClient()) { |
| 81 | + MongoCollection<Document> collection = mongoClient.getDatabase(DATABASE_NAME) |
| 82 | + .getCollection(Book.class.getSimpleName()); |
| 83 | + collection.deleteMany(new BsonDocument()); |
| 84 | + await().atMost(Duration.ofSeconds(2)) |
| 85 | + .until(() -> collection.find().limit(1).first() == null); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void shouldSave() { |
| 91 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1); |
| 92 | + assertThat(bookCustomRepository.save(book)) |
| 93 | + .isNotNull() |
| 94 | + .isEqualTo(book); |
| 95 | + |
| 96 | + assertThat(bookCustomRepository.getById(book.id())) |
| 97 | + .as("should return the persisted book") |
| 98 | + .hasValue(book); |
| 99 | + |
| 100 | + Book updated = new Book(book.id(), book.title() + " updated", 2); |
| 101 | + |
| 102 | + assertThat(bookCustomRepository.save(updated)) |
| 103 | + .isNotNull() |
| 104 | + .isNotEqualTo(book); |
| 105 | + |
| 106 | + assertThat(bookCustomRepository.getById(book.id())) |
| 107 | + .as("should return the updated book") |
| 108 | + .hasValue(updated); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void shouldSaveAllAndFindByIdIn() { |
| 113 | + |
| 114 | + List<Book> books = List.of( |
| 115 | + new Book(randomUUID().toString(), "Java Persistence Layer", 1) |
| 116 | + , new Book(randomUUID().toString(), "Effective Java", 3) |
| 117 | + , new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1) |
| 118 | + , new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1) |
| 119 | + ); |
| 120 | + |
| 121 | + assertThat(bookCustomRepository.saveAll(books)) |
| 122 | + .isNotNull() |
| 123 | + .containsAll(books); |
| 124 | + |
| 125 | + assertThat(bookCustomRepository.findByIdIn(books.stream().map(Book::id).toList())) |
| 126 | + .as("should return the persisted books") |
| 127 | + .containsAll(books); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void shouldSaveAllAndFindBy() { |
| 133 | + |
| 134 | + Book javaPersistenceLayer = new Book(randomUUID().toString(), "Java Persistence Layer", 1); |
| 135 | + Book effectiveJava = new Book(randomUUID().toString(), "Effective Java", 3); |
| 136 | + Book jakartaEeCookbook = new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1); |
| 137 | + Book masteringTheJavaVirtualMachine = new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1); |
| 138 | + |
| 139 | + List<Book> books = List.of( |
| 140 | + javaPersistenceLayer |
| 141 | + , effectiveJava |
| 142 | + , jakartaEeCookbook |
| 143 | + , masteringTheJavaVirtualMachine |
| 144 | + ); |
| 145 | + |
| 146 | + assertThat(bookCustomRepository.saveAll(books)) |
| 147 | + .isNotNull() |
| 148 | + .containsAll(books); |
| 149 | + |
| 150 | + PageRequest pageRequest = PageRequest.ofSize(2); |
| 151 | + Order<Book> orderByTitleAsc = Order.by(Sort.asc("title")); |
| 152 | + |
| 153 | + Page<Book> page1 = bookCustomRepository.listAll(pageRequest, orderByTitleAsc); |
| 154 | + Page<Book> page2 = bookCustomRepository.listAll(page1.nextPageRequest(), orderByTitleAsc); |
| 155 | + Page<Book> page3 = bookCustomRepository.listAll(page2.nextPageRequest(), orderByTitleAsc); |
| 156 | + |
| 157 | + assertSoftly(softly -> { |
| 158 | + |
| 159 | + softly.assertThat(page1) |
| 160 | + .as("should return the first page") |
| 161 | + .hasSize(2) |
| 162 | + .containsSequence(effectiveJava, jakartaEeCookbook); |
| 163 | + |
| 164 | + softly.assertThat(page2) |
| 165 | + .as("should return the first page") |
| 166 | + .hasSize(2) |
| 167 | + .containsSequence(javaPersistenceLayer, masteringTheJavaVirtualMachine); |
| 168 | + |
| 169 | + softly.assertThat(page3) |
| 170 | + .as("should return the third and last page with no items") |
| 171 | + .hasSize(0); |
| 172 | + }); |
| 173 | + |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + @Test |
| 179 | + void shouldDelete() { |
| 180 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1); |
| 181 | + assertThat(bookCustomRepository.save(book)) |
| 182 | + .isNotNull() |
| 183 | + .isEqualTo(book); |
| 184 | + |
| 185 | + assertThat(bookCustomRepository.getById(book.id())) |
| 186 | + .isNotNull() |
| 187 | + .hasValue(book); |
| 188 | + |
| 189 | + bookCustomRepository.delete(book); |
| 190 | + |
| 191 | + assertThat(bookCustomRepository.getById(book.id())) |
| 192 | + .isNotNull() |
| 193 | + .isEmpty(); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void shouldDeleteAll() { |
| 198 | + |
| 199 | + List<Book> books = List.of( |
| 200 | + new Book(randomUUID().toString(), "Java Persistence Layer", 1) |
| 201 | + , new Book(randomUUID().toString(), "Effective Java", 3) |
| 202 | + , new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1) |
| 203 | + , new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1) |
| 204 | + ); |
| 205 | + |
| 206 | + assertThat(bookCustomRepository.saveAll(books)) |
| 207 | + .isNotNull() |
| 208 | + .containsAll(books); |
| 209 | + |
| 210 | + await().atMost(Duration.ofSeconds(2)) |
| 211 | + .until(() -> bookCustomRepository.listAll().toList().size() >= books.size()); |
| 212 | + |
| 213 | + assertThat(bookCustomRepository.listAll()) |
| 214 | + .isNotNull() |
| 215 | + .containsAll(books); |
| 216 | + |
| 217 | + bookCustomRepository.deleteAll(); |
| 218 | + |
| 219 | + await().atMost(Duration.ofSeconds(2)) |
| 220 | + .until(() -> bookCustomRepository.listAll().toList().isEmpty()); |
| 221 | + |
| 222 | + |
| 223 | + assertThat(bookCustomRepository.listAll()) |
| 224 | + .isNotNull() |
| 225 | + .isEmpty(); |
| 226 | + |
| 227 | + } |
| 228 | + |
| 229 | + @Test |
| 230 | + void shouldRemoveAll() { |
| 231 | + |
| 232 | + List<Book> books = List.of( |
| 233 | + new Book(randomUUID().toString(), "Java Persistence Layer", 1) |
| 234 | + , new Book(randomUUID().toString(), "Effective Java", 3) |
| 235 | + , new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1) |
| 236 | + , new Book(randomUUID().toString(), "Mastering The Java Virtual Machine", 1) |
| 237 | + ); |
| 238 | + |
| 239 | + assertThat(bookCustomRepository.saveAll(books)) |
| 240 | + .isNotNull() |
| 241 | + .containsAll(books); |
| 242 | + |
| 243 | + await().atMost(Duration.ofSeconds(2)) |
| 244 | + .until(() -> bookCustomRepository.listAll().toList().size() >= books.size()); |
| 245 | + |
| 246 | + assertThat(bookCustomRepository.listAll()) |
| 247 | + .isNotNull() |
| 248 | + .containsAll(books); |
| 249 | + |
| 250 | + bookCustomRepository.removeAll(books); |
| 251 | + |
| 252 | + await().atMost(Duration.ofSeconds(2)) |
| 253 | + .until(() -> bookCustomRepository.listAll() |
| 254 | + .filter(books::contains) |
| 255 | + .toList().isEmpty()); |
| 256 | + |
| 257 | + assertThat(bookCustomRepository.findByIdIn(books.stream().map(Book::id).toList())) |
| 258 | + .isNotNull() |
| 259 | + .isEmpty(); |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments