Skip to content

Commit 2a9a068

Browse files
GH-2328 - Create tests for named queries in a non-default location.
This closes #2328.
1 parent db9c3b7 commit 2a9a068

File tree

6 files changed

+250
-1
lines changed

6 files changed

+250
-1
lines changed

src/test/java/org/springframework/data/neo4j/integration/issues/gh2326/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) {
4646
}
4747
}
4848

49-
protected static void assertLabels(BookmarkCapture bookmarkCapture, List<String> ids) {
49+
protected final void assertLabels(BookmarkCapture bookmarkCapture, List<String> ids) {
5050
try (Session session = neo4jConnectionSupport.getDriver().session(bookmarkCapture.createSessionConfig())) {
5151
for (String id : ids) {
5252
List<String> labels = session.readTransaction(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2328;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.UUID;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.neo4j.driver.Driver;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
28+
import org.springframework.data.neo4j.repository.Neo4jRepository;
29+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
30+
import org.springframework.data.neo4j.test.BookmarkCapture;
31+
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
32+
import org.springframework.transaction.annotation.EnableTransactionManagement;
33+
34+
/**
35+
* @author Michael J. Simons
36+
* @soundtrack Motörhead - Better Motörhead Than Dead - Live At Hammersmith
37+
*/
38+
@Neo4jIntegrationTest
39+
class GH2328IT extends TestBase {
40+
41+
@Test
42+
void queriesFromCustomLocationsShouldBeFound(@Autowired SomeRepository someRepository) {
43+
44+
assertThat(someRepository.getSomeEntityViaNamedQuery())
45+
.satisfies(this::requirements);
46+
}
47+
48+
public interface SomeRepository extends Neo4jRepository<SomeEntity, UUID> {
49+
50+
// Without a custom query, repository creation would fail with
51+
// Could not create query for
52+
// public abstract org.springframework.data.neo4j.integration.issues.gh2328.SomeEntity org.springframework.data.neo4j.integration.issues.gh2328.GH2328IT$SomeRepository.getSomeEntityViaNamedQuery()!
53+
// Reason: No property getSomeEntityViaNamedQuery found for type SomeEntity!;
54+
SomeEntity getSomeEntityViaNamedQuery();
55+
}
56+
57+
@Configuration
58+
@EnableTransactionManagement
59+
@EnableNeo4jRepositories(considerNestedRepositories = true, namedQueriesLocation = "more-custom-queries.properties")
60+
static class Config extends AbstractNeo4jConfig {
61+
62+
@Bean
63+
public BookmarkCapture bookmarkCapture() {
64+
return new BookmarkCapture();
65+
}
66+
67+
@Bean
68+
public Driver driver() {
69+
70+
return neo4jConnectionSupport.getDriver();
71+
}
72+
}
73+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2328;
17+
18+
import reactor.core.publisher.Mono;
19+
import reactor.test.StepVerifier;
20+
21+
import java.util.UUID;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.neo4j.driver.Driver;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig;
29+
import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository;
30+
import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories;
31+
import org.springframework.data.neo4j.test.BookmarkCapture;
32+
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
33+
import org.springframework.transaction.annotation.EnableTransactionManagement;
34+
35+
/**
36+
* @author Michael J. Simons
37+
* @soundtrack Motörhead - Better Motörhead Than Dead - Live At Hammersmith
38+
*/
39+
@Neo4jIntegrationTest
40+
class ReactiveGH2328IT extends TestBase {
41+
42+
@Test
43+
void queriesFromCustomLocationsShouldBeFound(@Autowired SomeRepository someRepository) {
44+
45+
someRepository.getSomeEntityViaNamedQuery()
46+
.as(StepVerifier::create)
47+
.expectNextMatches(this::requirements)
48+
.verifyComplete();
49+
}
50+
51+
public interface SomeRepository extends ReactiveNeo4jRepository<SomeEntity, UUID> {
52+
53+
// Without a custom query, repository creation would fail with
54+
// Could not create query for
55+
// public abstract org.springframework.data.neo4j.integration.issues.gh2328.SomeEntity org.springframework.data.neo4j.integration.issues.gh2328.GH2328IT$SomeRepository.getSomeEntityViaNamedQuery()!
56+
// Reason: No property getSomeEntityViaNamedQuery found for type SomeEntity!;
57+
Mono<SomeEntity> getSomeEntityViaNamedQuery();
58+
}
59+
60+
@Configuration
61+
@EnableTransactionManagement
62+
@EnableReactiveNeo4jRepositories(considerNestedRepositories = true, namedQueriesLocation = "more-custom-queries.properties")
63+
static class Config extends AbstractReactiveNeo4jConfig {
64+
65+
@Bean
66+
public BookmarkCapture bookmarkCapture() {
67+
return new BookmarkCapture();
68+
}
69+
70+
@Bean
71+
public Driver driver() {
72+
73+
return neo4jConnectionSupport.getDriver();
74+
}
75+
}
76+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2328;
17+
18+
import lombok.Getter;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.UUID;
22+
23+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
24+
import org.springframework.data.neo4j.core.schema.Id;
25+
import org.springframework.data.neo4j.core.schema.Node;
26+
27+
/**
28+
* @author Michael J. Simons
29+
* @soundtrack Motörhead - Better Motörhead Than Dead - Live At Hammersmith
30+
*/
31+
@Node
32+
@Getter
33+
@RequiredArgsConstructor
34+
public class SomeEntity {
35+
36+
@Id @GeneratedValue
37+
private UUID id;
38+
39+
private final String name;
40+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2011-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.neo4j.integration.issues.gh2328;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.UUID;
21+
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.neo4j.driver.Session;
24+
import org.neo4j.driver.Transaction;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.data.neo4j.test.BookmarkCapture;
27+
import org.springframework.data.neo4j.test.Neo4jExtension;
28+
29+
/**
30+
* @author Michael J. Simons
31+
* @soundtrack Motörhead - Better Motörhead Than Dead - Live At Hammersmith
32+
*/
33+
abstract class TestBase {
34+
35+
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
36+
37+
protected static UUID id;
38+
39+
@BeforeAll
40+
protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) {
41+
try (Session session = neo4jConnectionSupport.getDriver().session(bookmarkCapture.createSessionConfig());
42+
Transaction transaction = session.beginTransaction();
43+
) {
44+
transaction.run("MATCH (n) detach delete n");
45+
id = UUID.fromString(
46+
transaction.run("CREATE (f:SomeEntity {name: 'A name', id: randomUUID()}) RETURN f.id").single()
47+
.get(0).asString());
48+
transaction.commit();
49+
bookmarkCapture.seedWith(session.lastBookmark());
50+
}
51+
}
52+
53+
protected final boolean requirements(SomeEntity someEntity) {
54+
assertThat(someEntity).isNotNull();
55+
assertThat(someEntity).extracting(SomeEntity::getId).isEqualTo(id);
56+
assertThat(someEntity).extracting(SomeEntity::getName).isEqualTo("A name");
57+
return true;
58+
}
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SomeEntity.getSomeEntityViaNamedQuery=MATCH (n:SomeEntity {name: 'A name'}) RETURN n

0 commit comments

Comments
 (0)