Skip to content

Commit 101a894

Browse files
GH-2328 - Create tests for named queries in a non-default location.
This closes #2328.
1 parent b3b0505 commit 101a894

File tree

6 files changed

+235
-1
lines changed

6 files changed

+235
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected static void setupData() {
4343
}
4444
}
4545

46-
protected static void assertLabels(List<String> ids) {
46+
protected final void assertLabels(List<String> ids) {
4747
try (Session session = neo4jConnectionSupport.getDriver().session()) {
4848
for (String id : ids) {
4949
List<String> labels = session.readTransaction(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.Neo4jIntegrationTest;
31+
import org.springframework.transaction.annotation.EnableTransactionManagement;
32+
33+
/**
34+
* @author Michael J. Simons
35+
* @soundtrack Motörhead - Better Motörhead Than Dead - Live At Hammersmith
36+
*/
37+
@Neo4jIntegrationTest
38+
class GH2328IT extends TestBase {
39+
40+
@Test
41+
void queriesFromCustomLocationsShouldBeFound(@Autowired SomeRepository someRepository) {
42+
43+
assertThat(someRepository.getSomeEntityViaNamedQuery())
44+
.satisfies(this::requirements);
45+
}
46+
47+
public interface SomeRepository extends Neo4jRepository<SomeEntity, UUID> {
48+
49+
// Without a custom query, repository creation would fail with
50+
// Could not create query for
51+
// public abstract org.springframework.data.neo4j.integration.issues.gh2328.SomeEntity org.springframework.data.neo4j.integration.issues.gh2328.GH2328IT$SomeRepository.getSomeEntityViaNamedQuery()!
52+
// Reason: No property getSomeEntityViaNamedQuery found for type SomeEntity!;
53+
SomeEntity getSomeEntityViaNamedQuery();
54+
}
55+
56+
@Configuration
57+
@EnableTransactionManagement
58+
@EnableNeo4jRepositories(considerNestedRepositories = true, namedQueriesLocation = "more-custom-queries.properties")
59+
static class Config extends AbstractNeo4jConfig {
60+
61+
@Bean
62+
public Driver driver() {
63+
64+
return neo4jConnectionSupport.getDriver();
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.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 ReactiveGH2328IT extends TestBase {
40+
41+
@Test
42+
void queriesFromCustomLocationsShouldBeFound(@Autowired SomeRepository someRepository) {
43+
44+
someRepository.getSomeEntityViaNamedQuery()
45+
.as(StepVerifier::create)
46+
.expectNextMatches(this::requirements)
47+
.verifyComplete();
48+
}
49+
50+
public interface SomeRepository extends ReactiveNeo4jRepository<SomeEntity, UUID> {
51+
52+
// Without a custom query, repository creation would fail with
53+
// Could not create query for
54+
// public abstract org.springframework.data.neo4j.integration.issues.gh2328.SomeEntity org.springframework.data.neo4j.integration.issues.gh2328.GH2328IT$SomeRepository.getSomeEntityViaNamedQuery()!
55+
// Reason: No property getSomeEntityViaNamedQuery found for type SomeEntity!;
56+
Mono<SomeEntity> getSomeEntityViaNamedQuery();
57+
}
58+
59+
@Configuration
60+
@EnableTransactionManagement
61+
@EnableReactiveNeo4jRepositories(considerNestedRepositories = true, namedQueriesLocation = "more-custom-queries.properties")
62+
static class Config extends AbstractReactiveNeo4jConfig {
63+
64+
@Bean
65+
public Driver driver() {
66+
67+
return neo4jConnectionSupport.getDriver();
68+
}
69+
}
70+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.data.neo4j.test.Neo4jExtension;
26+
27+
/**
28+
* @author Michael J. Simons
29+
* @soundtrack Motörhead - Better Motörhead Than Dead - Live At Hammersmith
30+
*/
31+
abstract class TestBase {
32+
33+
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
34+
35+
protected static UUID id;
36+
37+
@BeforeAll
38+
protected static void setupData() {
39+
try (Session session = neo4jConnectionSupport.getDriver().session();
40+
Transaction transaction = session.beginTransaction();
41+
) {
42+
transaction.run("MATCH (n) detach delete n");
43+
id = UUID.fromString(
44+
transaction.run("CREATE (f:SomeEntity {name: 'A name', id: randomUUID()}) RETURN f.id").single()
45+
.get(0).asString());
46+
transaction.commit();
47+
}
48+
}
49+
50+
protected final boolean requirements(SomeEntity someEntity) {
51+
assertThat(someEntity).isNotNull();
52+
assertThat(someEntity).extracting(SomeEntity::getId).isEqualTo(id);
53+
assertThat(someEntity).extracting(SomeEntity::getName).isEqualTo("A name");
54+
return true;
55+
}
56+
}
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)