Skip to content

Commit 65ff52f

Browse files
authored
test: refactor assertions to use assertThatThrownBy for exception handling (#2552)
1 parent 4b749a9 commit 65ff52f

15 files changed

Lines changed: 341 additions & 414 deletions

engine/src/test/java/com/arcadedb/query/sql/executor/ScriptExecutionTest.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import com.arcadedb.exception.ConcurrentModificationException;
3030
import com.arcadedb.query.sql.SQLQueryEngine;
3131
import com.arcadedb.query.sql.function.SQLFunctionAbstract;
32-
import org.assertj.core.api.Assertions;
33-
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.Test;
3433

3534
import java.io.IOException;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3838

3939
/**
4040
* @author Luigi Dell'Aquila (l.dellaquila-(at)-orientdatabase.com)
@@ -352,7 +352,7 @@ public void testCommitRetryWithFailure() {
352352
SELECT throwCME(#-1:-1, 1, 1, 1);
353353
COMMIT RETRY 10;
354354
""".formatted(typeName);
355-
;
355+
356356
try {
357357
database.command("sqlscript", script);
358358
} catch (ConcurrentModificationException x) {
@@ -440,11 +440,8 @@ public void testCommitRetryWithFailureScriptAndFail() {
440440
} AND FAIL;
441441
""".formatted(typeName, typeName);
442442

443-
try {
444-
database.command("sqlscript", script);
445-
Assertions.fail();
446-
} catch (ConcurrentModificationException e) {
447-
}
443+
assertThatThrownBy(() -> database.command("sqlscript", script))
444+
.isInstanceOf(ConcurrentModificationException.class);
448445
});
449446

450447
ResultSet result = database.query("sql", "select from " + typeName);
@@ -472,12 +469,8 @@ public void testCommitRetryWithFailureScriptAndFail2() {
472469
}
473470
""".formatted(typeName, typeName);
474471

475-
try {
476-
database.command("sqlscript", script);
477-
Assertions.fail();
478-
} catch (ConcurrentModificationException e) {
479-
480-
}
472+
assertThatThrownBy(() -> database.command("sqlscript", script))
473+
.isInstanceOf(ConcurrentModificationException.class);
481474

482475
ResultSet result = database.query("sql", "select from " + typeName);
483476
Result item = result.next();
@@ -489,14 +482,10 @@ public void testCommitRetryWithFailureScriptAndFail2() {
489482
@Test
490483
public void testFunctionAsStatement() {
491484
database.transaction(() -> {
492-
String script = "";
493-
script += "sqrt(64);";
485+
String script = "sqrt(64);";
494486

495-
try {
496-
database.command("sql", script);
497-
Assertions.fail();
498-
} catch (CommandSQLParsingException e) {
499-
}
487+
assertThatThrownBy(() -> database.command("sql", script))
488+
.isInstanceOf(CommandSQLParsingException.class);
500489

501490
ResultSet rs = database.command("sqlscript", script);
502491
Result item = rs.next();

engine/src/test/java/com/arcadedb/query/sql/parser/SQLGrammarErrorsTest.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
package com.arcadedb.query.sql.parser;
22

33
import com.arcadedb.exception.CommandSQLParsingException;
4-
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
57

68
/**
79
* Simple test to verify improved SQL grammar error messages
810
*/
9-
public class SQLGrammarErrorsTest {
10-
public static void main(String[] args) {
11+
class SQLGrammarErrorsTest {
12+
13+
@Test
14+
void testSQLGrammarErrors() {
1115
StatementCache cache = new StatementCache(null, 100);
1216

1317
// Test case 1: Duplicate AND operators
14-
try {
15-
cache.get("select from Chat where #22:1 IN $brain AND and contextVariables['local:insuranceGrid'] = true");
16-
Assertions.fail();
17-
} catch (CommandSQLParsingException e) {
18-
Assertions.assertTrue(e.getMessage().contains("AND operator") && e.getMessage().contains("must be followed by a condition"),
19-
"Expected improved error message about AND operator, got: " + e.getMessage());
20-
}
18+
assertThatThrownBy(
19+
() -> cache.get("select from Chat where #22:1 IN $brain AND and contextVariables['local:insuranceGrid'] = true"))
20+
.isInstanceOf(CommandSQLParsingException.class)
21+
.hasMessageContaining("AND operator")
22+
.hasMessageContaining("must be followed by a condition");
2123

2224
// Test case 2: OR followed by AND
23-
try {
24-
cache.get("select from Chat where col = 1 OR AND col2 = 2");
25-
Assertions.fail();
26-
} catch (CommandSQLParsingException e) {
27-
Assertions.assertTrue(e.getMessage().contains("OR operator") && e.getMessage().contains("must be followed by a condition"),
28-
"Expected improved error message about OR operator, got: " + e.getMessage());
29-
}
25+
assertThatThrownBy(() -> cache.get("select from Chat where col = 1 OR AND col2 = 2"))
26+
.isInstanceOf(CommandSQLParsingException.class)
27+
.hasMessageContaining("OR operator")
28+
.hasMessageContaining("must be followed by a condition");
3029

3130
// Test case 3: Missing condition after AND
32-
try {
33-
cache.get("select from Chat where col = 1 AND");
34-
Assertions.fail();
35-
} catch (CommandSQLParsingException e) {
36-
Assertions.assertTrue(e.getMessage().contains("AND operator") && e.getMessage().contains("must be followed by a condition"),
37-
"Expected improved error message about AND operator, got: " + e.getMessage());
38-
}
31+
assertThatThrownBy(() -> cache.get("select from Chat where col = 1 AND"))
32+
.isInstanceOf(CommandSQLParsingException.class)
33+
.hasMessageContaining("AND operator")
34+
.hasMessageContaining("must be followed by a condition");
3935

4036
// Test case 4: Valid query should work
4137
cache.get("select from Chat where col = 1 AND col2 = 2");

engine/src/test/java/com/arcadedb/schema/DictionaryTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@
2525
import com.arcadedb.graph.MutableVertex;
2626
import com.arcadedb.graph.Vertex;
2727
import com.arcadedb.query.sql.executor.ResultSet;
28-
29-
import org.assertj.core.api.Assertions;
3028
import org.junit.jupiter.api.Test;
3129

3230
import java.util.Iterator;
3331

3432
import static org.assertj.core.api.Assertions.assertThat;
35-
import static org.assertj.core.api.Assertions.fail;
33+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3634

3735
public class DictionaryTest extends TestHelper {
3836
@Test
@@ -99,14 +97,10 @@ public void updateName() {
9997
assertThat(i).isEqualTo(11);
10098
});
10199

102-
try {
103-
database.transaction(() -> {
104-
assertThat(database.getSchema().existsType("V")).isTrue();
105-
database.getSchema().getDictionary().updateName("V", "V2");
106-
});
107-
fail("");
108-
} catch (final Exception e) {
109-
}
100+
assertThatThrownBy(() -> database.transaction(() -> {
101+
assertThat(database.getSchema().existsType("V")).isTrue();
102+
database.getSchema().getDictionary().updateName("V", "V2");
103+
})).isInstanceOf(Exception.class);
110104
}
111105

112106
@Test

engine/src/test/java/com/arcadedb/schema/DocumentValidationTest.java

Lines changed: 38 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929
import com.arcadedb.graph.MutableVertex;
3030
import com.arcadedb.query.sql.executor.Result;
3131
import com.arcadedb.query.sql.executor.ResultSet;
32-
import org.assertj.core.api.Assertions;
3332
import org.junit.jupiter.api.Test;
3433

35-
import java.text.*;
36-
import java.util.*;
34+
import java.text.SimpleDateFormat;
35+
import java.util.ArrayList;
36+
import java.util.Arrays;
37+
import java.util.Calendar;
38+
import java.util.Date;
39+
import java.util.HashMap;
40+
import java.util.Map;
3741

3842
import static org.assertj.core.api.Assertions.assertThat;
39-
import static org.assertj.core.api.Assertions.fail;
43+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4044

4145
public class DocumentValidationTest extends TestHelper {
4246

@@ -279,12 +283,8 @@ public void testRequiredValidationEdge() {
279283
database.transaction(() -> {
280284
final MutableVertex v1 = database.newVertex("V").save();
281285
final MutableVertex v2 = database.newVertex("V").save();
282-
try {
283-
v1.newEdge("E", v2);
284-
Assertions.fail();
285-
} catch (ValidationException e) {
286-
// EXPECTED
287-
}
286+
assertThatThrownBy(() -> v1.newEdge("E", v2))
287+
.isInstanceOf(ValidationException.class);
288288

289289
final MutableEdge e = v1.newEdge("E", v2, "id", "12345");
290290
assertThat(e.getString("id")).isEqualTo("12345");
@@ -303,26 +303,14 @@ public void testRequiredValidationEdgeSQL() {
303303
database.transaction(() -> {
304304
final MutableVertex v1 = database.newVertex("V").save();
305305
final MutableVertex v2 = database.newVertex("V").save();
306-
try {
307-
database.command("sql", "create edge E from ? to ?", v1, v2);
308-
Assertions.fail();
309-
} catch (ValidationException e) {
310-
// EXPECTED
311-
}
312-
313-
try {
314-
database.command("sql", "create edge E from ? to ? set a = '12345'", v1, v2);
315-
Assertions.fail();
316-
} catch (ValidationException e) {
317-
// EXPECTED
318-
}
319-
320-
try {
321-
database.command("sql", "create edge E from ? to ? set a = '12345', b = '4444'", v1, v2);
322-
Assertions.fail();
323-
} catch (ValidationException e) {
324-
// EXPECTED
325-
}
306+
assertThatThrownBy(() -> database.command("sql", "create edge E from ? to ?", v1, v2))
307+
.isInstanceOf(ValidationException.class);
308+
309+
assertThatThrownBy(() -> database.command("sql", "create edge E from ? to ? set a = '12345'", v1, v2))
310+
.isInstanceOf(ValidationException.class);
311+
312+
assertThatThrownBy(() -> database.command("sql", "create edge E from ? to ? set a = '12345', b = '4444'", v1, v2))
313+
.isInstanceOf(ValidationException.class);
326314

327315
final Edge e = database.command("sql", "create edge E from ? to ? set a = '12345', b = '4444', c = '2222'", v1, v2)
328316
.nextIfAvailable().getEdge().get();
@@ -348,12 +336,9 @@ public void testValidationNotValidEmbedded() {
348336

349337
final MutableDocument embedded = d.newEmbeddedDocument("EmbeddedValidation", "embedded");
350338
embedded.set("test", "test");
351-
try {
352-
d.validate();
353-
fail("Validation doesn't throw exception");
354-
} catch (final ValidationException e) {
355-
assertThat(e.toString().contains("int")).isTrue();
356-
}
339+
assertThatThrownBy(() -> d.validate())
340+
.isInstanceOf(ValidationException.class)
341+
.hasMessageContaining("int");
357342
}
358343

359344
@Test
@@ -381,12 +366,9 @@ public void testValidationNotValidEmbeddedList() {
381366
final MutableDocument embeddedInList2 = d.newEmbeddedDocument("EmbeddedValidation", "embeddedList");
382367
embeddedInList2.set("int", 30);
383368

384-
try {
385-
d.validate();
386-
fail("Validation doesn't throw exception");
387-
} catch (final ValidationException e) {
388-
assertThat(e.toString().contains("long")).isTrue();
389-
}
369+
assertThatThrownBy(() -> d.validate())
370+
.isInstanceOf(ValidationException.class)
371+
.hasMessageContaining("long");
390372
}
391373

392374
@Test
@@ -415,12 +397,9 @@ public void testValidationNotValidEmbeddedMap() {
415397
embeddedInMap2.set("int", 30);
416398
embeddedMap.put("2", embeddedInMap2);
417399

418-
try {
419-
d.validate();
420-
fail("Validation doesn't throw exception");
421-
} catch (final ValidationException e) {
422-
assertThat(e.toString().contains("long")).isTrue();
423-
}
400+
assertThatThrownBy(() -> d.validate())
401+
.isInstanceOf(ValidationException.class)
402+
.hasMessageContaining("long");
424403
}
425404

426405
@Test
@@ -736,19 +715,11 @@ public void testPropertyMetadataAreSavedAndReloadded() {
736715
@Test
737716
public void testMinMaxNotApplicable() {
738717
final DocumentType clazz = database.getSchema().getOrCreateDocumentType("Validation");
739-
try {
740-
clazz.createProperty("invString", Type.STRING).setMin("-1");
741-
fail("");
742-
} catch (IllegalArgumentException e) {
743-
// EXPECTED
744-
}
745-
746-
try {
747-
clazz.createProperty("invBinary", Type.LIST).setMax("-1");
748-
fail("");
749-
} catch (IllegalArgumentException e) {
750-
// EXPECTED
751-
}
718+
assertThatThrownBy(() -> clazz.createProperty("invString", Type.STRING).setMin("-1"))
719+
.isInstanceOf(IllegalArgumentException.class);
720+
721+
assertThatThrownBy(() -> clazz.createProperty("invBinary", Type.LIST).setMax("-1"))
722+
.isInstanceOf(IllegalArgumentException.class);
752723
}
753724

754725
@Test
@@ -762,31 +733,25 @@ public void testEmbeddedDocumentConversion() {
762733
}
763734

764735
private void checkFieldValue(final Document toCheck, final String field, final Object newValue) {
765-
try {
736+
assertThatThrownBy(() -> {
766737
final MutableDocument newD = database.newDocument(toCheck.getTypeName()).fromMap(toCheck.toMap());
767738
newD.set(field, newValue);
768739
newD.validate();
769-
fail("");
770-
} catch (final ValidationException v) {
771-
}
740+
}).isInstanceOf(ValidationException.class);
772741
}
773742

774743
private void checkRequireField(final MutableDocument toCheck, final String fieldName) {
775-
try {
744+
assertThatThrownBy(() -> {
776745
final MutableDocument newD = database.newDocument(toCheck.getTypeName()).fromMap(toCheck.toMap());
777746
newD.remove(fieldName);
778747
newD.validate();
779-
fail("");
780-
} catch (final ValidationException v) {
781-
}
748+
}).isInstanceOf(ValidationException.class);
782749
}
783750

784751
private void checkReadOnlyField(final MutableDocument toCheck, final String fieldName) {
785-
try {
752+
assertThatThrownBy(() -> {
786753
toCheck.remove(fieldName);
787754
toCheck.validate();
788-
fail("");
789-
} catch (final ValidationException v) {
790-
}
755+
}).isInstanceOf(ValidationException.class);
791756
}
792757
}

engine/src/test/java/com/arcadedb/schema/DropTypeTest.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.arcadedb.engine.Bucket;
2424
import com.arcadedb.exception.SchemaException;
2525

26-
import org.assertj.core.api.Assertions;
2726
import org.junit.jupiter.api.Test;
2827

2928
import java.util.*;
@@ -72,23 +71,14 @@ public void testDropAndRecreateType() {
7271

7372
// CHECK ALL THE BUCKETS ARE REMOVED
7473
for (final Bucket b : buckets) {
75-
try {
76-
database.getSchema().getBucketById(b.getFileId());
77-
fail();
78-
} catch (final SchemaException e) {
79-
}
80-
81-
try {
82-
database.getSchema().getBucketByName(b.getName());
83-
fail();
84-
} catch (final SchemaException e) {
85-
}
86-
87-
try {
88-
database.getSchema().getFileById(b.getFileId());
89-
fail();
90-
} catch (final SchemaException e) {
91-
}
74+
assertThatThrownBy(() -> database.getSchema().getBucketById(b.getFileId()))
75+
.isInstanceOf(SchemaException.class);
76+
77+
assertThatThrownBy(() -> database.getSchema().getBucketByName(b.getName()))
78+
.isInstanceOf(SchemaException.class);
79+
80+
assertThatThrownBy(() -> database.getSchema().getFileById(b.getFileId()))
81+
.isInstanceOf(SchemaException.class);
9282
}
9383

9484
// CHECK TYPE HAS BEEN REMOVED FROM INHERITANCE

0 commit comments

Comments
 (0)