|
| 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 | + * Otavio Santana |
| 14 | + */ |
| 15 | +package org.eclipse.jnosql.databases.elasticsearch.integration; |
| 16 | + |
| 17 | + |
| 18 | +import jakarta.inject.Inject; |
| 19 | +import org.awaitility.Awaitility; |
| 20 | +import org.eclipse.jnosql.databases.elasticsearch.communication.DocumentDatabase; |
| 21 | +import org.eclipse.jnosql.databases.elasticsearch.communication.ElasticsearchConfigurations; |
| 22 | +import org.eclipse.jnosql.databases.elasticsearch.mapping.ElasticsearchTemplate; |
| 23 | +import org.eclipse.jnosql.mapping.Convert; |
| 24 | +import org.eclipse.jnosql.mapping.config.MappingConfigurations; |
| 25 | +import org.eclipse.jnosql.mapping.document.DocumentEntityConverter; |
| 26 | +import org.eclipse.jnosql.mapping.document.spi.DocumentExtension; |
| 27 | +import org.eclipse.jnosql.mapping.reflection.EntityMetadataExtension; |
| 28 | +import org.jboss.weld.junit5.auto.AddExtensions; |
| 29 | +import org.jboss.weld.junit5.auto.AddPackages; |
| 30 | +import org.jboss.weld.junit5.auto.EnableAutoWeld; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | +import java.util.Optional; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.concurrent.atomic.AtomicReference; |
| 40 | + |
| 41 | +import static java.util.UUID.randomUUID; |
| 42 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 43 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
| 45 | +import static org.assertj.core.api.SoftAssertions.assertSoftly; |
| 46 | +import static org.awaitility.Awaitility.await; |
| 47 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES; |
| 48 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED; |
| 49 | + |
| 50 | +@EnableAutoWeld |
| 51 | +@AddPackages(value = {Convert.class, DocumentEntityConverter.class}) |
| 52 | +@AddPackages(Book.class) |
| 53 | +@AddPackages(ElasticsearchTemplate.class) |
| 54 | +@AddExtensions({EntityMetadataExtension.class, |
| 55 | + DocumentExtension.class}) |
| 56 | +@EnabledIfSystemProperty(named = NAMED, matches = MATCHES) |
| 57 | +class RepositoryIntegrationTest { |
| 58 | + |
| 59 | + public static final String INDEX = "library"; |
| 60 | + |
| 61 | + static { |
| 62 | + DocumentDatabase instance = DocumentDatabase.INSTANCE; |
| 63 | + instance.get("library"); |
| 64 | + System.setProperty(ElasticsearchConfigurations.HOST.get() + ".1", instance.host()); |
| 65 | + System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), INDEX); |
| 66 | + Awaitility.setDefaultPollDelay(100, MILLISECONDS); |
| 67 | + Awaitility.setDefaultTimeout(60L, SECONDS); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Inject |
| 72 | + private Library library; |
| 73 | + |
| 74 | + @BeforeEach |
| 75 | + @AfterEach |
| 76 | + public void clearDatabase() { |
| 77 | + DocumentDatabase.clearDatabase(INDEX); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void shouldInsert() { |
| 82 | + Author joshuaBloch = new Author("Joshua Bloch"); |
| 83 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1, joshuaBloch); |
| 84 | + library.save(book); |
| 85 | + |
| 86 | + AtomicReference<Book> reference = new AtomicReference<>(); |
| 87 | + await().until(() -> { |
| 88 | + Optional<Book> optional = library.findById(book.id()); |
| 89 | + optional.ifPresent(reference::set); |
| 90 | + return optional.isPresent(); |
| 91 | + }); |
| 92 | + assertThat(reference.get()).isNotNull().isEqualTo(book); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void shouldUpdate() { |
| 97 | + Author joshuaBloch = new Author("Joshua Bloch"); |
| 98 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1, joshuaBloch); |
| 99 | + assertThat(library.save(book)) |
| 100 | + .isNotNull() |
| 101 | + .isEqualTo(book); |
| 102 | + |
| 103 | + Book updated = book.updateEdition(2); |
| 104 | + |
| 105 | + assertThat(library.save(updated)) |
| 106 | + .isNotNull() |
| 107 | + .isNotEqualTo(book); |
| 108 | + |
| 109 | + AtomicReference<Book> reference = new AtomicReference<>(); |
| 110 | + await().until(() -> { |
| 111 | + Optional<Book> optional = library.findById(book.id()); |
| 112 | + optional.ifPresent(reference::set); |
| 113 | + return optional.isPresent(); |
| 114 | + }); |
| 115 | + assertThat(reference.get()).isNotNull().isEqualTo(updated); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void shouldFindById() { |
| 121 | + Author joshuaBloch = new Author("Joshua Bloch"); |
| 122 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1, joshuaBloch); |
| 123 | + |
| 124 | + assertThat(library.save(book)) |
| 125 | + .isNotNull() |
| 126 | + .isEqualTo(book); |
| 127 | + |
| 128 | + AtomicReference<Book> reference = new AtomicReference<>(); |
| 129 | + await().until(() -> { |
| 130 | + Optional<Book> optional = library.findById(book.id()); |
| 131 | + optional.ifPresent(reference::set); |
| 132 | + return optional.isPresent(); |
| 133 | + }); |
| 134 | + |
| 135 | + assertThat(reference.get()).isNotNull().isEqualTo(book); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void shouldDelete() { |
| 140 | + Author joshuaBloch = new Author("Joshua Bloch"); |
| 141 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1, joshuaBloch); |
| 142 | + assertThat(library.save(book)) |
| 143 | + .isNotNull() |
| 144 | + .isEqualTo(book); |
| 145 | + |
| 146 | + |
| 147 | + library.deleteById(book.id()); |
| 148 | + |
| 149 | + assertThat(library.findById(book.id())) |
| 150 | + .isNotNull().isEmpty(); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + @Test |
| 155 | + public void shouldFindByAuthorName() throws InterruptedException { |
| 156 | + Author joshuaBloch = new Author("Joshua Bloch"); |
| 157 | + Book book = new Book(randomUUID().toString(), "Effective Java", 1, joshuaBloch); |
| 158 | + |
| 159 | + Set<Book> expectedBooks = Set.of(book, book.newEdition(), book.newEdition()); |
| 160 | + library.saveAll(expectedBooks); |
| 161 | + |
| 162 | + await().until(() -> |
| 163 | + !library.findByAuthorName(book.author().name()).toList().isEmpty()); |
| 164 | + |
| 165 | + var books = library.findByAuthorName(book.author().name()).toList(); |
| 166 | + assertThat(books) |
| 167 | + .hasSize(3); |
| 168 | + |
| 169 | + assertThat(books) |
| 170 | + .containsAll(expectedBooks); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void shouldFindByTitleLike() throws InterruptedException { |
| 175 | + Author joshuaBloch = new Author("Joshua Bloch"); |
| 176 | + |
| 177 | + Book effectiveJava1stEdition = new Book(randomUUID().toString(), "Effective Java", 1, joshuaBloch); |
| 178 | + Book effectiveJava2ndEdition = effectiveJava1stEdition.newEdition(); |
| 179 | + Book effectiveJava3rdEdition = effectiveJava2ndEdition.newEdition(); |
| 180 | + |
| 181 | + Author elderMoraes = new Author("Elder Moraes"); |
| 182 | + Book jakartaEECookBook = new Book(randomUUID().toString(), "Jakarta EE CookBook", 1, elderMoraes); |
| 183 | + |
| 184 | + Set<Book> allBooks = Set.of(jakartaEECookBook, effectiveJava1stEdition, effectiveJava2ndEdition, effectiveJava3rdEdition); |
| 185 | + |
| 186 | + Set<Book> effectiveBooks = Set.of(effectiveJava1stEdition, effectiveJava2ndEdition, effectiveJava3rdEdition); |
| 187 | + |
| 188 | + library.saveAll(allBooks); |
| 189 | + |
| 190 | + AtomicReference<List<Book>> booksWithEffective = new AtomicReference<>(); |
| 191 | + await().until(() -> { |
| 192 | + var books = library.findByTitleLike("Effective").toList(); |
| 193 | + booksWithEffective.set(books); |
| 194 | + return !books.isEmpty(); |
| 195 | + }); |
| 196 | + |
| 197 | + AtomicReference<List<Book>> booksWithJa = new AtomicReference<>(); |
| 198 | + await().until(() -> { |
| 199 | + var books = library.findByTitleLike("Ja*").toList(); |
| 200 | + booksWithJa.set(books); |
| 201 | + return !books.isEmpty(); |
| 202 | + }); |
| 203 | + |
| 204 | + assertSoftly(softly -> { |
| 205 | + assertThat(booksWithEffective.get()) |
| 206 | + .as("returned book list with 'Effective' is not equals to the expected items ") |
| 207 | + .containsAll(effectiveBooks); |
| 208 | + }); |
| 209 | + |
| 210 | + |
| 211 | + assertSoftly(softly -> { |
| 212 | + assertThat(booksWithJa.get()) |
| 213 | + .as("returned book list with 'Ja*' is not equals to the expected items ") |
| 214 | + .containsAll(allBooks); |
| 215 | + }); |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | +} |
0 commit comments