|  | 
|  | 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.arangodb.integration; | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +import jakarta.data.page.CursoredPage; | 
|  | 19 | +import jakarta.data.page.PageRequest; | 
|  | 20 | +import jakarta.inject.Inject; | 
|  | 21 | +import org.assertj.core.api.SoftAssertions; | 
|  | 22 | +import org.eclipse.jnosql.communication.semistructured.SelectQuery; | 
|  | 23 | +import org.eclipse.jnosql.databases.arangodb.communication.ArangoDBConfigurations; | 
|  | 24 | +import org.eclipse.jnosql.databases.arangodb.mapping.ArangoDBTemplate; | 
|  | 25 | +import org.eclipse.jnosql.mapping.Database; | 
|  | 26 | +import org.eclipse.jnosql.mapping.core.Converters; | 
|  | 27 | +import org.eclipse.jnosql.mapping.core.config.MappingConfigurations; | 
|  | 28 | +import org.eclipse.jnosql.mapping.core.spi.EntityMetadataExtension; | 
|  | 29 | +import org.eclipse.jnosql.mapping.document.DocumentTemplate; | 
|  | 30 | +import org.eclipse.jnosql.mapping.document.spi.DocumentExtension; | 
|  | 31 | +import org.eclipse.jnosql.mapping.reflection.Reflections; | 
|  | 32 | +import org.eclipse.jnosql.mapping.semistructured.EntityConverter; | 
|  | 33 | +import org.jboss.weld.junit5.auto.AddExtensions; | 
|  | 34 | +import org.jboss.weld.junit5.auto.AddPackages; | 
|  | 35 | +import org.jboss.weld.junit5.auto.EnableAutoWeld; | 
|  | 36 | +import org.junit.jupiter.api.BeforeEach; | 
|  | 37 | +import org.junit.jupiter.api.Test; | 
|  | 38 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; | 
|  | 39 | + | 
|  | 40 | +import java.util.List; | 
|  | 41 | +import java.util.Optional; | 
|  | 42 | + | 
|  | 43 | +import static java.util.UUID.randomUUID; | 
|  | 44 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 45 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES; | 
|  | 46 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED; | 
|  | 47 | +import static org.eclipse.jnosql.databases.arangodb.communication.DocumentDatabase.INSTANCE; | 
|  | 48 | + | 
|  | 49 | +@EnableAutoWeld | 
|  | 50 | +@AddPackages(value = {Database.class, EntityConverter.class, DocumentTemplate.class}) | 
|  | 51 | +@AddPackages(Article.class) | 
|  | 52 | +@AddPackages(ArangoDBTemplate.class) | 
|  | 53 | +@AddExtensions({EntityMetadataExtension.class, | 
|  | 54 | +        DocumentExtension.class}) | 
|  | 55 | +@AddPackages(Reflections.class) | 
|  | 56 | +@AddPackages(Converters.class) | 
|  | 57 | +@EnabledIfSystemProperty(named = NAMED, matches = MATCHES) | 
|  | 58 | +class ArangoDBTemplateIntegrationUsingIdAnnotationTest { | 
|  | 59 | + | 
|  | 60 | +    @Inject | 
|  | 61 | +    private ArangoDBTemplate template; | 
|  | 62 | + | 
|  | 63 | +    static { | 
|  | 64 | +        INSTANCE.get("library"); | 
|  | 65 | +        System.setProperty(ArangoDBConfigurations.HOST.get() + ".1", INSTANCE.host()); | 
|  | 66 | +        System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library"); | 
|  | 67 | +    } | 
|  | 68 | + | 
|  | 69 | +    @BeforeEach | 
|  | 70 | +    void setUp() { | 
|  | 71 | +        this.template.delete(Article.class).execute(); | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    @Test | 
|  | 75 | +    void shouldInsert() { | 
|  | 76 | +        var article = new Article(randomUUID().toString(), "Effective Java", 1); | 
|  | 77 | +        template.insert(article); | 
|  | 78 | +        Optional<Article> optional = template.find(Article.class, article.id()); | 
|  | 79 | +        assertThat(optional).isNotNull().isNotEmpty() | 
|  | 80 | +                .get().isEqualTo(article); | 
|  | 81 | +    } | 
|  | 82 | + | 
|  | 83 | +    @Test | 
|  | 84 | +    void shouldUpdate() { | 
|  | 85 | +        var article = new Article(randomUUID().toString(), "Effective Java", 1); | 
|  | 86 | +        assertThat(template.insert(article)) | 
|  | 87 | +                .isNotNull() | 
|  | 88 | +                .isEqualTo(article); | 
|  | 89 | + | 
|  | 90 | +         Article updated = new Article(article.id(), article.title() + " updated", 2); | 
|  | 91 | + | 
|  | 92 | +        assertThat(template.update(updated)) | 
|  | 93 | +                .isNotNull() | 
|  | 94 | +                .isNotEqualTo(article); | 
|  | 95 | + | 
|  | 96 | +        assertThat(template.find(Article.class, article.id())) | 
|  | 97 | +                .isNotNull().get().isEqualTo(updated); | 
|  | 98 | + | 
|  | 99 | +    } | 
|  | 100 | + | 
|  | 101 | +    @Test | 
|  | 102 | +    void shouldFindById() { | 
|  | 103 | +        Article article = new Article(randomUUID().toString(), "Effective Java", 1); | 
|  | 104 | +        assertThat(template.insert(article)) | 
|  | 105 | +                .isNotNull() | 
|  | 106 | +                .isEqualTo(article); | 
|  | 107 | + | 
|  | 108 | +        assertThat(template.find(Article.class, article.id())) | 
|  | 109 | +                .isNotNull().get().isEqualTo(article); | 
|  | 110 | +    } | 
|  | 111 | + | 
|  | 112 | +    @Test | 
|  | 113 | +    void shouldDelete() { | 
|  | 114 | +        var article = new Article(randomUUID().toString(), "Effective Java", 1); | 
|  | 115 | +        assertThat(template.insert(article)) | 
|  | 116 | +                .isNotNull() | 
|  | 117 | +                .isEqualTo(article); | 
|  | 118 | + | 
|  | 119 | +        template.delete(Article.class, article.id()); | 
|  | 120 | +        assertThat(template.find(Article.class, article.id())) | 
|  | 121 | +                .isNotNull().isEmpty(); | 
|  | 122 | +    } | 
|  | 123 | + | 
|  | 124 | +    @Test | 
|  | 125 | +    void shouldDeleteAll() { | 
|  | 126 | +        for (int index = 0; index < 20; index++) { | 
|  | 127 | +            var article = new Article(randomUUID().toString(), "Effective Java", 1); | 
|  | 128 | +            assertThat(template.insert(article)) | 
|  | 129 | +                    .isNotNull() | 
|  | 130 | +                    .isEqualTo(article); | 
|  | 131 | +        } | 
|  | 132 | + | 
|  | 133 | +        template.delete(Article.class).execute(); | 
|  | 134 | +        assertThat(template.select(Article.class).result()).isEmpty(); | 
|  | 135 | +    } | 
|  | 136 | + | 
|  | 137 | + | 
|  | 138 | +    @Test | 
|  | 139 | +    void shouldUpdateNullValues() { | 
|  | 140 | +        var article = new Article(randomUUID().toString(), "Effective Java", 1); | 
|  | 141 | +        template.insert(article); | 
|  | 142 | +        template.update(new Article(article.id(), null, 2)); | 
|  | 143 | +        Optional<Article> optional = template.select(Article.class).where("id") | 
|  | 144 | +                .eq(article.id()).singleResult(); | 
|  | 145 | +        SoftAssertions.assertSoftly(softly -> { | 
|  | 146 | +            softly.assertThat(optional).isPresent(); | 
|  | 147 | +            softly.assertThat(optional).get().extracting(Article::title).isNull(); | 
|  | 148 | +            softly.assertThat(optional).get().extracting(Article::edition).isEqualTo(2); | 
|  | 149 | +        }); | 
|  | 150 | +    } | 
|  | 151 | + | 
|  | 152 | +    @Test | 
|  | 153 | +    void shouldExecuteLimit() { | 
|  | 154 | + | 
|  | 155 | +        for (int index = 1; index < 10; index++) { | 
|  | 156 | +            var article = new Article(randomUUID().toString(), "Effective Java", index); | 
|  | 157 | +            template.insert(article); | 
|  | 158 | +        } | 
|  | 159 | + | 
|  | 160 | +        List<Article> articles = template.select(Article.class).orderBy("edition") | 
|  | 161 | +                .asc().limit(4).result(); | 
|  | 162 | + | 
|  | 163 | +        SoftAssertions.assertSoftly(soft -> { | 
|  | 164 | +            soft.assertThat(articles).hasSize(4); | 
|  | 165 | +            var editions = articles.stream().map(Article::edition).toList(); | 
|  | 166 | +            soft.assertThat(editions).hasSize(4).contains(1, 2, 3, 4); | 
|  | 167 | +        }); | 
|  | 168 | + | 
|  | 169 | +    } | 
|  | 170 | + | 
|  | 171 | +    @Test | 
|  | 172 | +    void shouldExecuteSkip() { | 
|  | 173 | +        for (int index = 1; index < 10; index++) { | 
|  | 174 | +            var book = new Article(randomUUID().toString(), "Effective Java", index); | 
|  | 175 | +            template.insert(book); | 
|  | 176 | +        } | 
|  | 177 | + | 
|  | 178 | +        List<Article> articles = template.select(Article.class).orderBy("edition") | 
|  | 179 | +                .asc().skip(4).result(); | 
|  | 180 | + | 
|  | 181 | +        SoftAssertions.assertSoftly(soft -> { | 
|  | 182 | +            soft.assertThat(articles).hasSize(5); | 
|  | 183 | +            var editions = articles.stream().map(Article::edition).toList(); | 
|  | 184 | +            soft.assertThat(editions).hasSize(5).contains(5, 6, 7, 8, 9); | 
|  | 185 | +        }); | 
|  | 186 | +    } | 
|  | 187 | + | 
|  | 188 | +    @Test | 
|  | 189 | +    void shouldExecuteLimitStart() { | 
|  | 190 | +        for (int index = 1; index < 10; index++) { | 
|  | 191 | +            var article = new Article(randomUUID().toString(), "Effective Java", index); | 
|  | 192 | +            template.insert(article); | 
|  | 193 | +        } | 
|  | 194 | + | 
|  | 195 | +        List<Article> articles = template.select(Article.class).orderBy("edition") | 
|  | 196 | +                .asc().skip(4).limit(3).result(); | 
|  | 197 | + | 
|  | 198 | +        SoftAssertions.assertSoftly(soft -> { | 
|  | 199 | +            soft.assertThat(articles).hasSize(3); | 
|  | 200 | +            var editions = articles.stream().map(Article::edition).toList(); | 
|  | 201 | +            soft.assertThat(editions).hasSize(3).contains(5, 6, 7); | 
|  | 202 | +        }); | 
|  | 203 | +    } | 
|  | 204 | + | 
|  | 205 | +    @Test | 
|  | 206 | +    void shouldSelectCursorSize() { | 
|  | 207 | +        for (int index = 1; index < 10; index++) { | 
|  | 208 | +            var article = new Article(randomUUID().toString(), "Effective Java", index); | 
|  | 209 | +            template.insert(article); | 
|  | 210 | +        } | 
|  | 211 | +        var select = SelectQuery.select().from("Article").orderBy("edition").asc() | 
|  | 212 | +                .skip(4).limit(3).build(); | 
|  | 213 | +        var pageRequest = PageRequest.ofSize(3); | 
|  | 214 | +        CursoredPage<Article> entities = template.selectCursor(select, pageRequest); | 
|  | 215 | + | 
|  | 216 | +        SoftAssertions.assertSoftly(soft -> { | 
|  | 217 | +            var content = entities.content(); | 
|  | 218 | +            soft.assertThat(content).hasSize(3); | 
|  | 219 | +            var editions = content.stream().map(Article::edition).toList(); | 
|  | 220 | +            soft.assertThat(editions).hasSize(3).contains(1, 2, 3); | 
|  | 221 | +        }); | 
|  | 222 | +    } | 
|  | 223 | +} | 
0 commit comments