Skip to content

Commit 9f0c75e

Browse files
authored
Merge pull request #306 from eclipse-jnosql/ISSUE-570
BUG] New version of jnosq-arango is not backward compatible
2 parents f42bcf5 + 9f7e8e0 commit 9f0c75e

File tree

4 files changed

+359
-4
lines changed

4 files changed

+359
-4
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/DefaultArangoDBDocumentManager.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.time.Duration;
2929
import java.util.Map;
30+
import java.util.Optional;
3031
import java.util.logging.Level;
3132
import java.util.logging.Logger;
3233
import java.util.stream.Collectors;
@@ -76,12 +77,23 @@ public CommunicationEntity update(CommunicationEntity entity) {
7677
requireNonNull(entity, "entity is required");
7778
String collectionName = entity.name();
7879
checkCollection(collectionName);
79-
entity.find(KEY, String.class)
80-
.orElseThrow(() -> new IllegalArgumentException("The document does not provide" +
81-
" the _key column"));
80+
Optional<String> keyElement = entity.find(KEY, String.class);
81+
Optional<String> idElement = entity.find(ID, String.class);
82+
if (keyElement.isEmpty() && idElement.isEmpty()) {
83+
throw new IllegalArgumentException("To update an entity is necessary to have either " + KEY + " or " + ID);
84+
}
85+
var key = keyElement.orElseGet(() -> {
86+
String id = idElement.orElseThrow();
87+
var elements = id.split("/");
88+
if (elements.length == 2) {
89+
return elements[1];
90+
} else {
91+
return elements[0];
92+
}
93+
});
8294
JsonObject jsonObject = ArangoDBUtil.toJsonObject(entity);
8395
DocumentUpdateEntity<Void> arangoDocument = arangoDB.db(database)
84-
.collection(collectionName).updateDocument(jsonObject.getString(KEY), jsonObject);
96+
.collection(collectionName).updateDocument(key, jsonObject);
8597
updateEntity(entity, arangoDocument.getKey(), arangoDocument.getId(), arangoDocument.getRev());
8698
return entity;
8799
}

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ void shouldRetrieveListSubdocumentList() {
178178
assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
179179
}
180180

181+
@Test
182+
void shouldConvertFromListSubdocumentListNotUsingKey() {
183+
CommunicationEntity entity = createDocumentListNotHavingId();
184+
entityManager.insert(entity);
185+
186+
}
187+
188+
@Test
189+
void shouldRetrieveListSubdocumentListNotUsingKey() {
190+
CommunicationEntity entity = entityManager.insert(createDocumentListNotHavingId());
191+
Element key = entity.find(KEY_NAME).get();
192+
SelectQuery query = select().from("AppointmentBook").where(key.name()).eq(key.get()).build();
193+
194+
CommunicationEntity documentEntity = entityManager.singleResult(query).get();
195+
assertNotNull(documentEntity);
196+
197+
List<List<Element>> contacts = (List<List<Element>>) documentEntity.find("contacts").get().get();
198+
199+
assertEquals(3, contacts.size());
200+
assertTrue(contacts.stream().allMatch(d -> d.size() == 3));
201+
}
202+
181203
@Test
182204
void shouldRunAQL() {
183205
CommunicationEntity entity = getEntity();
@@ -412,6 +434,25 @@ private CommunicationEntity createDocumentList() {
412434
return entity;
413435
}
414436

437+
private CommunicationEntity createDocumentListNotHavingId() {
438+
String id = UUID.randomUUID().toString();
439+
CommunicationEntity entity = CommunicationEntity.of("AppointmentBook");
440+
entity.add(Element.of("_id", "ids"));
441+
List<List<Element>> documents = new ArrayList<>();
442+
443+
documents.add(asList(Element.of("name", "Ada"), Element.of("type", ContactType.EMAIL),
444+
Element.of("information", "ada@lovelace.com")));
445+
446+
documents.add(asList(Element.of("name", "Ada"), Element.of("type", ContactType.MOBILE),
447+
Element.of("information", "11 1231231 123")));
448+
449+
documents.add(asList(Element.of("name", "Ada"), Element.of("type", ContactType.PHONE),
450+
Element.of("information", "phone")));
451+
452+
entity.add(Element.of("contacts", documents));
453+
return entity;
454+
}
455+
415456
private List<CommunicationEntity> getEntitiesWithValues() {
416457
var lucas = CommunicationEntity.of(COLLECTION_NAME);
417458
lucas.add(Element.of("name", "Lucas"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Entity;
19+
import jakarta.nosql.Id;
20+
21+
import java.util.Objects;
22+
23+
@Entity
24+
public class Article {
25+
26+
@Id
27+
private String id;
28+
29+
@Column("title")
30+
private String title;
31+
32+
@Column("edition")
33+
private int edition;
34+
35+
public Article(String id, String title, int edition) {
36+
this.id = id;
37+
this.title = title;
38+
this.edition = edition;
39+
}
40+
41+
Article() {
42+
}
43+
44+
public String id() {
45+
return id;
46+
}
47+
48+
public String title() {
49+
return title;
50+
}
51+
52+
public int edition() {
53+
return edition;
54+
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) return true;
59+
if (o == null || getClass() != o.getClass()) return false;
60+
Article magazine = (Article) o;
61+
return edition == magazine.edition
62+
&& Objects.equals(id, magazine.id)
63+
&& Objects.equals(title, magazine.title);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(id, title, edition);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "Book{" +
74+
"id='" + id + '\'' +
75+
", title='" + title + '\'' +
76+
", edition=" + edition +
77+
'}';
78+
}
79+
}

0 commit comments

Comments
 (0)