Skip to content

Commit 4d5a280

Browse files
spring-projectsGH-2542 - Ensure correct exception is thrown.
1 parent d06fc12 commit 4d5a280

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2011-2022 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.gh2542;
17+
18+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
19+
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
import org.neo4j.driver.Driver;
23+
import org.neo4j.driver.Session;
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.dao.DataIntegrityViolationException;
28+
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
29+
import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager;
30+
import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager;
31+
import org.springframework.data.neo4j.repository.Neo4jRepository;
32+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
33+
import org.springframework.data.neo4j.test.BookmarkCapture;
34+
import org.springframework.data.neo4j.test.Neo4jExtension;
35+
import org.springframework.data.neo4j.test.Neo4jImperativeTestConfiguration;
36+
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
37+
import org.springframework.transaction.PlatformTransactionManager;
38+
import org.springframework.transaction.annotation.EnableTransactionManagement;
39+
40+
/**
41+
* @author Michael J. Simons
42+
*/
43+
@Neo4jIntegrationTest
44+
class GH2542IT {
45+
protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport;
46+
47+
interface TestNodeRepository extends Neo4jRepository<TestNode, Long> {
48+
}
49+
50+
@BeforeAll
51+
static void createConstraint() {
52+
53+
try (Session session = neo4jConnectionSupport.getDriver().session()) {
54+
session.run("MATCH (n) DETACH DELETE n");
55+
session.run("CREATE CONSTRAINT TNC IF NOT EXISTS ON (tn:TestNode) ASSERT tn.name IS UNIQUE").consume();
56+
}
57+
}
58+
59+
@Test // GH2542
60+
void shouldThrowDataIntegrityViolationException(@Autowired TestNodeRepository repository) {
61+
62+
repository.save(new TestNode("Bob"));
63+
var secondNode = new TestNode("Bob");
64+
assertThatExceptionOfType(DataIntegrityViolationException.class)
65+
.isThrownBy(() -> repository.save(secondNode));
66+
}
67+
68+
@Configuration
69+
@EnableTransactionManagement
70+
@EnableNeo4jRepositories(considerNestedRepositories = true)
71+
static class Config extends Neo4jImperativeTestConfiguration {
72+
73+
@Bean
74+
public BookmarkCapture bookmarkCapture() {
75+
return new BookmarkCapture();
76+
}
77+
78+
@Override
79+
public PlatformTransactionManager transactionManager(
80+
Driver driver, DatabaseSelectionProvider databaseNameProvider) {
81+
82+
BookmarkCapture bookmarkCapture = bookmarkCapture();
83+
return new Neo4jTransactionManager(driver, databaseNameProvider,
84+
Neo4jBookmarkManager.create(bookmarkCapture));
85+
}
86+
87+
@Bean
88+
public Driver driver() {
89+
return neo4jConnectionSupport.getDriver();
90+
}
91+
92+
@Override
93+
public boolean isCypher5Compatible() {
94+
return neo4jConnectionSupport.isCypher5SyntaxCompatible();
95+
}
96+
}
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2011-2022 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.gh2542;
17+
18+
import org.springframework.data.neo4j.core.schema.GeneratedValue;
19+
import org.springframework.data.neo4j.core.schema.Id;
20+
import org.springframework.data.neo4j.core.schema.Node;
21+
22+
/**
23+
* @author Michael J. Simons
24+
*/
25+
@Node
26+
public class TestNode {
27+
28+
@Id
29+
@GeneratedValue
30+
private Long id;
31+
32+
private final String name;
33+
34+
public TestNode(String name) {
35+
this.name = name;
36+
}
37+
38+
public Long getId() {
39+
return id;
40+
}
41+
42+
public String getName() {
43+
return name;
44+
}
45+
}

0 commit comments

Comments
 (0)