|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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.couchdb.communication.integration; |
| 16 | + |
| 17 | + |
| 18 | +import jakarta.inject.Inject; |
| 19 | +import org.assertj.core.api.SoftAssertions; |
| 20 | +import org.eclipse.jnosql.databases.couchdb.communication.CouchDBConfigurations; |
| 21 | +import org.eclipse.jnosql.databases.couchdb.communication.configuration.DocumentDatabase; |
| 22 | +import org.eclipse.jnosql.mapping.Database; |
| 23 | +import org.eclipse.jnosql.mapping.core.Converters; |
| 24 | +import org.eclipse.jnosql.mapping.core.config.MappingConfigurations; |
| 25 | +import org.eclipse.jnosql.mapping.document.DocumentTemplate; |
| 26 | +import org.eclipse.jnosql.mapping.document.spi.DocumentExtension; |
| 27 | +import org.eclipse.jnosql.mapping.reflection.Reflections; |
| 28 | +import org.eclipse.jnosql.mapping.reflection.spi.ReflectionEntityMetadataExtension; |
| 29 | +import org.eclipse.jnosql.mapping.semistructured.EntityConverter; |
| 30 | +import org.jboss.weld.junit5.auto.AddExtensions; |
| 31 | +import org.jboss.weld.junit5.auto.AddPackages; |
| 32 | +import org.jboss.weld.junit5.auto.EnableAutoWeld; |
| 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 | + |
| 39 | +import static java.util.UUID.randomUUID; |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES; |
| 42 | +import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED; |
| 43 | + |
| 44 | +@EnableAutoWeld |
| 45 | +@AddPackages(value = {Database.class, EntityConverter.class, DocumentTemplate.class}) |
| 46 | +@AddPackages(Magazine.class) |
| 47 | +@AddPackages(Reflections.class) |
| 48 | +@AddPackages(Converters.class) |
| 49 | +@AddExtensions({ReflectionEntityMetadataExtension.class, |
| 50 | + DocumentExtension.class}) |
| 51 | +@EnabledIfSystemProperty(named = NAMED, matches = MATCHES) |
| 52 | +class TemplateIntegrationTest { |
| 53 | + |
| 54 | + static { |
| 55 | + DocumentDatabase database = DocumentDatabase.INSTANCE; |
| 56 | + System.setProperty(CouchDBConfigurations.PORT.get(), database.getPort().toString()); |
| 57 | + System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library"); |
| 58 | + System.setProperty(CouchDBConfigurations.USER.get(), database.getUser()); |
| 59 | + System.setProperty(CouchDBConfigurations.PASSWORD.get(), database.getPassword()); |
| 60 | + } |
| 61 | + |
| 62 | + @Inject |
| 63 | + private DocumentTemplate template; |
| 64 | + |
| 65 | + @Test |
| 66 | + void shouldFindById() { |
| 67 | + Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1); |
| 68 | + assertThat(template.insert(magazine)) |
| 69 | + .isNotNull() |
| 70 | + .isEqualTo(magazine); |
| 71 | + |
| 72 | + assertThat(template.find(Magazine.class, magazine.id())) |
| 73 | + .isNotNull().get().isEqualTo(magazine); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void shouldInsert() { |
| 78 | + Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1); |
| 79 | + template.insert(magazine); |
| 80 | + Optional<Magazine> optional = template.find(Magazine.class, magazine.id()); |
| 81 | + assertThat(optional).isNotNull().isNotEmpty() |
| 82 | + .get().isEqualTo(magazine); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void shouldUpdate() { |
| 87 | + Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1); |
| 88 | + assertThat(template.insert(magazine)) |
| 89 | + .isNotNull() |
| 90 | + .isEqualTo(magazine); |
| 91 | + |
| 92 | + Magazine updated = new Magazine(magazine.id(), magazine.title() + " updated", 2); |
| 93 | + |
| 94 | + assertThat(template.update(updated)) |
| 95 | + .isNotNull() |
| 96 | + .isNotEqualTo(magazine); |
| 97 | + |
| 98 | + assertThat(template.find(Magazine.class, magazine.id())) |
| 99 | + .isNotNull().get().isEqualTo(updated); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void shouldDeleteById() { |
| 105 | + Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", 1); |
| 106 | + assertThat(template.insert(magazine)) |
| 107 | + .isNotNull() |
| 108 | + .isEqualTo(magazine); |
| 109 | + |
| 110 | + template.delete(Magazine.class, magazine.id()); |
| 111 | + assertThat(template.find(Magazine.class, magazine.id())) |
| 112 | + .isNotNull().isEmpty(); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void shouldDeleteAll(){ |
| 117 | + for (int index = 0; index < 20; index++) { |
| 118 | + Magazine magazine = new Magazine(randomUUID().toString(), "Effective Java", index); |
| 119 | + assertThat(template.insert(magazine)) |
| 120 | + .isNotNull() |
| 121 | + .isEqualTo(magazine); |
| 122 | + } |
| 123 | + |
| 124 | + template.delete(Magazine.class).execute(); |
| 125 | + var magazines = template.select(Magazine.class).result(); |
| 126 | + assertThat(magazines).isNotNull().isEmpty(); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void shouldInsertByteArray() { |
| 131 | + var failure = new Failure("test", new byte[]{'a','b','c','d'}); |
| 132 | + template.insert(failure); |
| 133 | + Optional<Failure> entity = template.find(Failure.class, "test"); |
| 134 | + SoftAssertions.assertSoftly(softly -> { |
| 135 | + softly.assertThat(entity).isNotNull().isNotEmpty(); |
| 136 | + softly.assertThat(entity).map(Failure::id).get().isEqualTo("test"); |
| 137 | + softly.assertThat(entity).map(Failure::data).get().isEqualTo(failure.data()); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | +} |
| 142 | + |
0 commit comments