Skip to content

Commit 14fbd3b

Browse files
committed
added test changes from #303
1 parent 9e76930 commit 14fbd3b

20 files changed

+479
-16
lines changed

src/test/java/com/arangodb/springframework/AbstractArangoTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package com.arangodb.springframework;
2222

23+
import com.arangodb.ArangoDatabase;
2324
import org.junit.jupiter.api.AfterAll;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +38,7 @@ public abstract class AbstractArangoTest {
3738

3839
@Autowired
3940
protected ArangoOperations template;
41+
protected ArangoDatabase db;
4042
protected final Class<?>[] collections;
4143

4244
protected AbstractArangoTest(final Class<?>... collections) {
@@ -50,6 +52,7 @@ public void before() {
5052
template.collection(collection).truncate();
5153
}
5254
AbstractArangoTest.staticTemplate = template;
55+
db = template.driver().db(ArangoTestConfiguration.DB);
5356
}
5457

5558
@AfterAll

src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
@EnableArangoRepositories(basePackages = {
5050
"com.arangodb.springframework.repository",
5151
"com.arangodb.springframework.example.polymorphic.repository",
52-
"com.arangodb.springframework.debug.repository"},
52+
"com.arangodb.springframework.debug.repository",
53+
"com.arangodb.springframework.testdata.chess.repo"},
5354
namedQueriesLocation = "classpath*:arango-named-queries-test.properties")
5455
@EnableArangoAuditing(auditorAwareRef = "auditorProvider")
5556
public class ArangoTestConfiguration implements ArangoConfiguration {

src/test/java/com/arangodb/springframework/core/mapping/CustomMappingTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void jsonNodeToCustom() {
117117

118118
@Test
119119
public void customToJsonNodeFromDriver() {
120-
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("customJsonNodeTestEntity");
120+
ArangoCollection col = db.collection("customJsonNodeTestEntity");
121121
final DocumentEntity meta = col.insertDocument(new CustomJsonNodeTestEntity("abc"));
122122
final BaseDocument doc = col.getDocument(meta.getKey(), BaseDocument.class);
123123
assertThat(doc.getAttribute(FIELD), is("abc"));
@@ -126,7 +126,7 @@ public void customToJsonNodeFromDriver() {
126126

127127
@Test
128128
public void jsonNodeToCustomFromDriver() {
129-
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("testEntity");
129+
ArangoCollection col = db.collection("testEntity");
130130
final DocumentEntity meta = col.insertDocument(new TestEntity("abc"));
131131
final CustomJsonNodeTestEntity doc = col.getDocument(meta.getKey(), CustomJsonNodeTestEntity.class);
132132
assertThat(doc.getValue(), is("abc"));
@@ -192,7 +192,7 @@ public void jsonNodeToDBEntity() {
192192

193193
@Test
194194
public void customToDBEntityFromDriver() {
195-
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("customDBEntityTestEntity");
195+
ArangoCollection col = db.collection("customDBEntityTestEntity");
196196
final DocumentEntity meta = col.insertDocument(new CustomDBEntityTestEntity("abc"));
197197
final BaseDocument doc = col.getDocument(meta.getKey(), BaseDocument.class);
198198
assertThat(doc.getAttribute(FIELD), is("abc"));
@@ -201,7 +201,7 @@ public void customToDBEntityFromDriver() {
201201

202202
@Test
203203
public void jsonNodeToDBEntityFromDriver() {
204-
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("testEntity");
204+
ArangoCollection col = db.collection("testEntity");
205205
final DocumentEntity meta = col.insertDocument(new TestEntity("abc"));
206206
final CustomDBEntityTestEntity doc = col.getDocument(meta.getKey(), CustomDBEntityTestEntity.class);
207207
assertThat(doc.getValue(), is("abc"));

src/test/java/com/arangodb/springframework/core/mapping/GeneralMappingTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void fieldNameAnnotation() {
9797
entity.test = "1234";
9898
final DocumentEntity res = template.insert(entity);
9999
String colName = res.getId().split("/")[0];
100-
final ObjectNode slice = template.driver().db(ArangoTestConfiguration.DB)
100+
final ObjectNode slice = db
101101
.collection(colName)
102102
.getDocument(res.getKey(), ObjectNode.class);
103103
assertThat(slice, is(notNullValue()));
@@ -632,7 +632,7 @@ public void auditingTest() throws InterruptedException {
632632
assertThat(find.modifiedBy.getId(), is(createID));
633633
}
634634

635-
final ObjectNode doc = template.driver().db(ArangoTestConfiguration.DB).collection("auditingTestEntity")
635+
final ObjectNode doc = db.collection("auditingTestEntity")
636636
.getDocument(value.id, ObjectNode.class);
637637
assertThat(doc, is(notNullValue()));
638638
assertThat(doc.get("createdBy").isObject(), is(true));

src/test/java/com/arangodb/springframework/core/mapping/InheritanceMappingTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ static class SubClassWithOwnDocumentAnnotation extends BasicTestEntity {
331331
public void overrideDocumentAnnotation() {
332332
final SubClassWithOwnDocumentAnnotation doc = new SubClassWithOwnDocumentAnnotation();
333333
template.insert(doc);
334-
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideDocAn").exists(), is(true));
335-
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideDocAn").count().getCount(),
334+
assertThat(db.collection("overrideDocAn").exists(), is(true));
335+
assertThat(db.collection("overrideDocAn").count().getCount(),
336336
is(1L));
337337
}
338338

@@ -351,8 +351,8 @@ public void overrideEdgeAnnotation() {
351351
edge.from = from;
352352
edge.to = to;
353353
template.insert(edge);
354-
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideEdgeAn").exists(), is(true));
355-
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideEdgeAn").count().getCount(),
354+
assertThat(db.collection("overrideEdgeAn").exists(), is(true));
355+
assertThat(db.collection("overrideEdgeAn").count().getCount(),
356356
is(1L));
357357
}
358358

src/test/java/com/arangodb/springframework/core/mapping/MultiTenancyCollectionLevelMappingTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ public void collectionLevel() {
5050
{
5151
tenantProvider.setId("tenant00");
5252
template.insert(new MultiTenancyTestEntity());
53-
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").exists(),
53+
assertThat(db.collection("tenant00_collection").exists(),
5454
is(true));
5555
}
5656
{
5757
tenantProvider.setId("tenant01");
5858
template.insert(new MultiTenancyTestEntity());
59-
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").exists(),
59+
assertThat(db.collection("tenant01_collection").exists(),
6060
is(true));
6161
}
6262
assertThat(
63-
template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").count().getCount(),
63+
db.collection("tenant00_collection").count().getCount(),
6464
is(1L));
6565
assertThat(
66-
template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").count().getCount(),
66+
db.collection("tenant01_collection").count().getCount(),
6767
is(1L));
6868
}
6969

src/test/java/com/arangodb/springframework/example/polymorphic/template/PolymorphicTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
public class PolymorphicTemplate extends AbstractArangoTest {
5050

5151
private ArangoCollection col() {
52-
return template.driver().db(ArangoTestConfiguration.DB).collection("animals");
52+
return db.collection("animals");
5353
}
5454

5555
@BeforeEach
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.AbstractArangoTest;
4+
import com.arangodb.springframework.core.ArangoOperations;
5+
import com.arangodb.springframework.testdata.chess.entity.Player;
6+
import com.arangodb.springframework.testdata.chess.entity.Score;
7+
import com.arangodb.springframework.testdata.chess.entity.Tournament;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.geo.Point;
11+
12+
import java.time.LocalDate;
13+
import java.util.List;
14+
15+
abstract class AbstractRepositoryTest extends AbstractArangoTest {
16+
17+
@Autowired
18+
private ArangoOperations ops;
19+
20+
protected List<Player> players = List.of(
21+
new Player("Magnus Carlsen", 2830, "Norway"),
22+
new Player("Fabiano Caruana", 2803, "US"),
23+
new Player("Maxime Vachier-Lagrave", 2732, "France"),
24+
new Player("Hikaru Nakamura", 2789, "US"),
25+
new Player("Ding Liren", 2762, "China"),
26+
new Player("Wesley So", 2757, "US"),
27+
new Player("Alireza Firouzja", 2760, "France"),
28+
new Player("Anish Giri", 2745, "Netherlands"),
29+
new Player("Ian Nepomniachtchi", 2758, "Russia")
30+
);
31+
32+
protected List<Tournament> tournaments = List.of(
33+
new Tournament(
34+
"Tata Steel 2023",
35+
LocalDate.of(2023, 1, 13),
36+
"Wijk aan Zee",
37+
new Point(4.6, 52.5)
38+
),
39+
new Tournament(
40+
"World Chess Championship 2023",
41+
LocalDate.of(2023, 4, 9),
42+
"Astana",
43+
new Point(71.422222, 51.147222)
44+
),
45+
new Tournament(
46+
"Norway Chess 2023",
47+
LocalDate.of(2023, 5, 30),
48+
"Stavanger",
49+
new Point(5.731389, 58.97)
50+
)
51+
);
52+
53+
protected List<Score> scores = List.of(
54+
new Score(players.get(7), tournaments.get(0), 1),
55+
new Score(players.get(0), tournaments.get(0), 3),
56+
new Score(players.get(5), tournaments.get(0), 4),
57+
new Score(players.get(1), tournaments.get(0), 5),
58+
new Score(players.get(4), tournaments.get(1), 1),
59+
new Score(players.get(8), tournaments.get(1), 2),
60+
new Score(players.get(3), tournaments.get(1), 4),
61+
new Score(players.get(1), tournaments.get(1), 5),
62+
new Score(players.get(6), tournaments.get(1), 6),
63+
new Score(players.get(3), tournaments.get(2), 1),
64+
new Score(players.get(1), tournaments.get(2), 2),
65+
new Score(players.get(7), tournaments.get(2), 4),
66+
new Score(players.get(5), tournaments.get(2), 5),
67+
new Score(players.get(0), tournaments.get(2), 6)
68+
);
69+
70+
protected AbstractRepositoryTest() {
71+
super(Player.class, Score.class, Tournament.class);
72+
}
73+
74+
@BeforeEach
75+
void importData() {
76+
ops.insertAll(players, Player.class);
77+
ops.insertAll(tournaments, Tournament.class);
78+
ops.insertAll(scores, Score.class);
79+
}
80+
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.testdata.chess.entity.*;
4+
import com.arangodb.springframework.testdata.chess.repo.PlayerRepository;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.PageRequest;
9+
10+
import java.util.Comparator;
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
abstract class PlayerRepositoryAbstract extends AbstractRepositoryTest {
16+
17+
@Autowired
18+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
19+
PlayerRepository repo;
20+
21+
@Test
22+
void findAllByCountry() {
23+
List<Player> expected = players.stream()
24+
.filter(it -> "US".equals(it.getCountry()))
25+
.toList();
26+
Iterable<Player> found = repo.findAllByCountry("US");
27+
assertThat(found).containsExactlyInAnyOrderElementsOf(expected);
28+
found.forEach(this::checkRefs);
29+
}
30+
31+
@Test
32+
void findAllByRatingGreaterThan() {
33+
int rating = 2780;
34+
List<Player> expected = players.stream()
35+
.filter(it -> it.getRating() > rating)
36+
.sorted(Comparator.comparingInt(Player::getRating).reversed())
37+
.toList();
38+
39+
for (int i = 0; i < expected.size(); i++) {
40+
Page<Player> page = repo.findAllByRatingGreaterThanOrderByRatingDesc(PageRequest.of(i, 1), rating);
41+
assertThat(page.getTotalElements()).isEqualTo(expected.size());
42+
assertThat(page.getTotalPages()).isEqualTo(expected.size());
43+
Player current = page.iterator().next();
44+
assertThat(current).isEqualTo(expected.get(i));
45+
checkRefs(current);
46+
}
47+
}
48+
49+
private void checkRefs(Player p) {
50+
List<Score> expectedScores = scores.stream()
51+
.filter(it -> it.player().equals(p))
52+
.toList();
53+
assertThat(p.getScores()).containsExactlyInAnyOrderElementsOf(expectedScores);
54+
55+
List<Tournament> expectedTournaments = expectedScores.stream()
56+
.map(Score::tournament)
57+
.toList();
58+
assertThat(p.getTournaments()).containsExactlyInAnyOrderElementsOf(expectedTournaments);
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class PlayerRepositoryTest extends PlayerRepositoryAbstract {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.testdata.chess.repo.ScoreRepository;
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
abstract class ScoreRepositoryAbstract extends AbstractRepositoryTest {
11+
12+
@Autowired
13+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
14+
ScoreRepository repo;
15+
16+
@Test
17+
@Disabled("BTS-1859")
18+
void findAll() {
19+
assertThat(repo.findAll())
20+
.hasSize(scores.size())
21+
.containsExactlyInAnyOrderElementsOf(scores);
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class ScoreRepositoryTest extends ScoreRepositoryAbstract {
4+
}

0 commit comments

Comments
 (0)