Skip to content

Commit a6cba99

Browse files
authored
Merge pull request #253 from eclipse/null-values
Create test scenario and integration tests for null values
2 parents 9645488 + e3c94bb commit a6cba99

File tree

26 files changed

+519
-174
lines changed

26 files changed

+519
-174
lines changed

CHANGELOG.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
88

99
== [Unreleased]
1010

11+
=== Added
12+
13+
- Add support to null values
14+
1115
=== Changed
1216

1317
- Modify aggregate method to return DocumentEntity at MongoDB.

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

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.eclipse.jnosql.databases.arangodb.communication;
1717

1818
import com.arangodb.ArangoDB;
19+
import org.assertj.core.api.SoftAssertions;
1920
import org.eclipse.jnosql.communication.TypeReference;
2021
import org.eclipse.jnosql.communication.document.Document;
2122
import org.eclipse.jnosql.communication.document.DocumentDeleteQuery;
@@ -32,6 +33,7 @@
3233
import java.util.HashMap;
3334
import java.util.List;
3435
import java.util.Map;
36+
import java.util.Optional;
3537
import java.util.Random;
3638
import java.util.stream.Collectors;
3739

@@ -57,28 +59,28 @@ public class ArangoDBDocumentManagerTest {
5759
private final String KEY_NAME = "_key";
5860

5961
@BeforeEach
60-
public void setUp() {
62+
void setUp() {
6163
random = new Random();
6264
entityManager = DocumentDatabase.INSTANCE.get().apply(DATABASE);
6365
entityManager.delete(DocumentDeleteQuery.delete().from(COLLECTION_NAME).build());
6466

6567
}
6668

6769
@AfterEach
68-
public void after() {
70+
void after() {
6971
entityManager.delete(DocumentDeleteQuery.delete().from(COLLECTION_NAME).build());
7072
}
7173

7274
@Test
73-
public void shouldSave() {
75+
void shouldSave() {
7476
DocumentEntity entity = getEntity();
7577

7678
DocumentEntity documentEntity = entityManager.insert(entity);
7779
assertTrue(documentEntity.documents().stream().map(Document::name).anyMatch(s -> s.equals(KEY_NAME)));
7880
}
7981

8082
@Test
81-
public void shouldUpdateSave() {
83+
void shouldUpdateSave() {
8284
DocumentEntity entity = getEntity();
8385
entityManager.insert(entity);
8486
Document newField = Documents.of("newField", "10");
@@ -88,7 +90,7 @@ public void shouldUpdateSave() {
8890
}
8991

9092
@Test
91-
public void shouldRemoveEntity() {
93+
void shouldRemoveEntity() {
9294
DocumentEntity documentEntity = entityManager.insert(getEntity());
9395
Document id = documentEntity.find("_key").get();
9496
DocumentQuery select = select().from(COLLECTION_NAME).where(id.name()).eq(id.get()).build();
@@ -98,7 +100,7 @@ public void shouldRemoveEntity() {
98100
}
99101

100102
@Test
101-
public void shouldRemoveEntity2() {
103+
void shouldRemoveEntity2() {
102104
DocumentEntity documentEntity = entityManager.insert(getEntity());
103105
Document id = documentEntity.find("name").get();
104106
DocumentQuery select = select().from(COLLECTION_NAME).where(id.name()).eq(id.get()).build();
@@ -109,7 +111,7 @@ public void shouldRemoveEntity2() {
109111

110112

111113
@Test
112-
public void shouldFindDocument() {
114+
void shouldFindDocument() {
113115
DocumentEntity entity = entityManager.insert(getEntity());
114116
Document id = entity.find(KEY_NAME).get();
115117
DocumentQuery query = select().from(COLLECTION_NAME).where(id.name()).eq(id.get()).build();
@@ -124,7 +126,7 @@ public void shouldFindDocument() {
124126

125127

126128
@Test
127-
public void shouldSaveSubDocument() {
129+
void shouldSaveSubDocument() {
128130
DocumentEntity entity = getEntity();
129131
entity.add(Document.of("phones", Document.of("mobile", "1231231")));
130132
DocumentEntity entitySaved = entityManager.insert(entity);
@@ -138,7 +140,7 @@ public void shouldSaveSubDocument() {
138140
}
139141

140142
@Test
141-
public void shouldSaveSubDocument2() {
143+
void shouldSaveSubDocument2() {
142144
DocumentEntity entity = getEntity();
143145
entity.add(Document.of("phones", Arrays.asList(Document.of("mobile", "1231231"), Document.of("mobile2", "1231231"))));
144146
DocumentEntity entitySaved = entityManager.insert(entity);
@@ -154,15 +156,15 @@ public void shouldSaveSubDocument2() {
154156

155157

156158
@Test
157-
public void shouldConvertFromListSubdocumentList() {
158-
DocumentEntity entity = createSubdocumentList();
159+
void shouldConvertFromListSubdocumentList() {
160+
DocumentEntity entity = createDocumentList();
159161
entityManager.insert(entity);
160162

161163
}
162164

163165
@Test
164-
public void shouldRetrieveListSubdocumentList() {
165-
DocumentEntity entity = entityManager.insert(createSubdocumentList());
166+
void shouldRetrieveListSubdocumentList() {
167+
DocumentEntity entity = entityManager.insert(createDocumentList());
166168
Document key = entity.find(KEY_NAME).get();
167169
DocumentQuery query = select().from("AppointmentBook").where(key.name()).eq(key.get()).build();
168170

@@ -176,7 +178,7 @@ public void shouldRetrieveListSubdocumentList() {
176178
}
177179

178180
@Test
179-
public void shouldRunAQL() {
181+
void shouldRunAQL() {
180182
DocumentEntity entity = getEntity();
181183
DocumentEntity entitySaved = entityManager.insert(entity);
182184

@@ -188,15 +190,15 @@ public void shouldRunAQL() {
188190

189191

190192
@Test
191-
public void shouldCount() {
193+
void shouldCount() {
192194
DocumentEntity entity = getEntity();
193195
entityManager.insert(entity);
194196

195197
assertTrue(entityManager.count(COLLECTION_NAME) > 0);
196198
}
197199

198200
@Test
199-
public void shouldReadFromDifferentBaseDocumentUsingInstance() {
201+
void shouldReadFromDifferentBaseDocumentUsingInstance() {
200202
entityManager.insert(getEntity());
201203
ArangoDB arangoDB = DefaultArangoDBDocumentManager.class.cast(entityManager).getArangoDB();
202204
arangoDB.db(DATABASE).collection(COLLECTION_NAME).insertDocument(new Person());
@@ -206,7 +208,7 @@ public void shouldReadFromDifferentBaseDocumentUsingInstance() {
206208
}
207209

208210
@Test
209-
public void shouldReadFromDifferentBaseDocumentUsingMap() {
211+
void shouldReadFromDifferentBaseDocumentUsingMap() {
210212
entityManager.insert(getEntity());
211213
ArangoDB arangoDB = DefaultArangoDBDocumentManager.class.cast(entityManager).getArangoDB();
212214
Map<String, Object> map = new HashMap<>();
@@ -219,7 +221,7 @@ public void shouldReadFromDifferentBaseDocumentUsingMap() {
219221
}
220222

221223
@Test
222-
public void shouldExecuteAQLWithTypeParams() {
224+
void shouldExecuteAQLWithTypeParams() {
223225
entityManager.insert(getEntity());
224226
String aql = "FOR a IN person FILTER a.name == @name RETURN a.name";
225227
List<String> entities = entityManager.aql(aql,
@@ -229,15 +231,43 @@ public void shouldExecuteAQLWithTypeParams() {
229231
}
230232

231233
@Test
232-
public void shouldExecuteAQLWithType() {
234+
void shouldExecuteAQLWithType() {
233235
entityManager.insert(getEntity());
234236
String aql = "FOR a IN person RETURN a.name";
235237
List<String> entities = entityManager.aql(aql, String.class).collect(Collectors.toList());
236238
assertFalse(entities.isEmpty());
237239
}
240+
241+
@Test
242+
void shouldInsertNull() {
243+
DocumentEntity entity = DocumentEntity.of(COLLECTION_NAME);
244+
entity.add(Document.of(KEY_NAME, String.valueOf(random.nextLong())));
245+
entity.add(Document.of("name", null));
246+
DocumentEntity documentEntity = entityManager.insert(entity);
247+
Optional<Document> name = documentEntity.find("name");
248+
SoftAssertions.assertSoftly(soft -> {
249+
soft.assertThat(name).isPresent();
250+
soft.assertThat(name).get().extracting(Document::name).isEqualTo("name");
251+
soft.assertThat(name).get().extracting(Document::get).isNull();
252+
});
253+
}
254+
255+
@Test
256+
void shouldUpdateNull(){
257+
var entity = entityManager.insert(getEntity());
258+
entity.add(Document.of("name", null));
259+
var documentEntity = entityManager.update(entity);
260+
Optional<Document> name = documentEntity.find("name");
261+
SoftAssertions.assertSoftly(soft -> {
262+
soft.assertThat(name).isPresent();
263+
soft.assertThat(name).get().extracting(Document::name).isEqualTo("name");
264+
soft.assertThat(name).get().extracting(Document::get).isNull();
265+
});
266+
}
267+
238268
@Test
239-
public void shouldDeleteAll(){
240-
for (int index = 0; index < 20; index++) {
269+
void shouldDeleteAll() {
270+
for (int index = 0; index < 20; index++) {
241271
DocumentEntity entity = getEntity();
242272
entityManager.insert(entity);
243273
}
@@ -259,7 +289,7 @@ private DocumentEntity getEntity() {
259289
return entity;
260290
}
261291

262-
private DocumentEntity createSubdocumentList() {
292+
private DocumentEntity createDocumentList() {
263293
DocumentEntity entity = DocumentEntity.of("AppointmentBook");
264294
entity.add(Document.of("_id", "ids"));
265295
List<List<Document>> documents = new ArrayList<>();

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/integration/ArangoDBTemplateIntegrationTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
import jakarta.inject.Inject;
19+
import org.assertj.core.api.SoftAssertions;
1920
import org.eclipse.jnosql.databases.arangodb.communication.ArangoDBConfigurations;
2021
import org.eclipse.jnosql.databases.arangodb.mapping.ArangoDBTemplate;
2122
import org.eclipse.jnosql.mapping.Convert;
@@ -60,7 +61,7 @@ class ArangoDBTemplateIntegrationTest {
6061
}
6162

6263
@Test
63-
public void shouldInsert() {
64+
void shouldInsert() {
6465
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
6566
template.insert(book);
6667
Optional<Book> optional = template.find(Book.class, book.id());
@@ -69,7 +70,7 @@ public void shouldInsert() {
6970
}
7071

7172
@Test
72-
public void shouldUpdate() {
73+
void shouldUpdate() {
7374
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
7475
assertThat(template.insert(book))
7576
.isNotNull()
@@ -87,7 +88,7 @@ public void shouldUpdate() {
8788
}
8889

8990
@Test
90-
public void shouldFindById() {
91+
void shouldFindById() {
9192
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
9293
assertThat(template.insert(book))
9394
.isNotNull()
@@ -98,7 +99,7 @@ public void shouldFindById() {
9899
}
99100

100101
@Test
101-
public void shouldDelete() {
102+
void shouldDelete() {
102103
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
103104
assertThat(template.insert(book))
104105
.isNotNull()
@@ -110,7 +111,7 @@ public void shouldDelete() {
110111
}
111112

112113
@Test
113-
public void shouldDeleteAll(){
114+
void shouldDeleteAll(){
114115
for (int index = 0; index < 20; index++) {
115116
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
116117
assertThat(template.insert(book))
@@ -123,4 +124,20 @@ public void shouldDeleteAll(){
123124
}
124125

125126

127+
@Test
128+
void shouldUpdateNullValues(){
129+
var book = new Book(randomUUID().toString(), "Effective Java", 1);
130+
template.insert(book);
131+
template.update(new Book(book.id(), null, 2));
132+
Optional<Book> optional = template.select(Book.class).where("id")
133+
.eq(book.id()).singleResult();
134+
SoftAssertions.assertSoftly(softly -> {
135+
softly.assertThat(optional).isPresent();
136+
softly.assertThat(optional).get().extracting(Book::title).isNull();
137+
softly.assertThat(optional).get().extracting(Book::edition).isEqualTo(2);
138+
});
139+
}
140+
141+
142+
126143
}

jnosql-cassandra/src/main/java/org/eclipse/jnosql/databases/cassandra/communication/QueryUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ private static Collection<Object> getCollectionUdt(DataType type) {
172172

173173
private static void insertSingleField(Column column, Map<String, Term> values) {
174174
Object value = column.get();
175+
if(value == null) {
176+
values.put(getName(column), QueryBuilder.literal(null));
177+
return;
178+
}
175179
try {
176180
CodecRegistry.DEFAULT.codecFor(value);
177181
values.put(getName(column), QueryBuilder.literal(value));

jnosql-cassandra/src/main/java/org/eclipse/jnosql/databases/cassandra/mapping/CassandraUDTType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public boolean isNotEmpty() {
6060

6161
@Override
6262
public List<Column> toColumn(ColumnEntityConverter converter, Converters converters) {
63-
if (Iterable.class.isInstance(value)) {
63+
if (value == null) {
64+
return singletonList(Column.of(field.name(), null));
65+
} else if (Iterable.class.isInstance(value)) {
6466
List<Iterable<Column>> columns = new ArrayList<>();
6567
stream(Iterable.class.cast(value).spliterator(), false)
6668
.forEach(c -> columns.add(converter.toColumn(c).columns()));

0 commit comments

Comments
 (0)