Skip to content

Commit 4ca140a

Browse files
committed
HHH-19232 Add test for issue
1 parent d1d121e commit 4ca140a

File tree

1 file changed

+230
-0
lines changed

1 file changed

+230
-0
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.annotations.beanvalidation;
6+
7+
import jakarta.persistence.CascadeType;
8+
import jakarta.persistence.ElementCollection;
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.Id;
11+
import jakarta.persistence.ManyToMany;
12+
import jakarta.persistence.ValidationMode;
13+
import jakarta.validation.ConstraintViolation;
14+
import jakarta.validation.ConstraintViolationException;
15+
import jakarta.validation.Path;
16+
import jakarta.validation.constraints.NotBlank;
17+
import jakarta.validation.constraints.NotEmpty;
18+
import jakarta.validation.constraints.Size;
19+
import org.hibernate.SessionFactory;
20+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
21+
import org.hibernate.testing.orm.junit.Jira;
22+
import org.hibernate.testing.orm.junit.Jpa;
23+
import org.junit.jupiter.api.AfterEach;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
26+
27+
import java.util.List;
28+
import java.util.Set;
29+
import java.util.stream.Collectors;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
34+
@Jpa(annotatedClasses = {
35+
CollectionActionsValidationTest.TestEntity.class,
36+
CollectionActionsValidationTest.ChildEntity.class,
37+
}, validationMode = ValidationMode.AUTO)
38+
@Jira( "https://hibernate.atlassian.net/browse/HHH-19232" )
39+
public class CollectionActionsValidationTest {
40+
@Test
41+
public void testPersistEmpty(EntityManagerFactoryScope scope) {
42+
scope.inTransaction( entityManager -> {
43+
final ConstraintViolationException e = assertThrows( ConstraintViolationException.class, () -> {
44+
final TestEntity entity = new TestEntity( 2L );
45+
assertThat( entity.getNotEmptySet() ).isNull();
46+
assertThat( entity.getMinSizeList() ).isNull();
47+
entityManager.persist( entity );
48+
entityManager.flush();
49+
} );
50+
assertThat( e.getConstraintViolations() ).hasSize( 1 );
51+
assertThat( getPropertyPaths( e ) ).containsOnly( "notEmptySet" );
52+
} );
53+
}
54+
55+
@Test
56+
public void testPersistInvalidChild(EntityManagerFactoryScope scope) {
57+
scope.inTransaction( entityManager -> {
58+
final ConstraintViolationException e = assertThrows( ConstraintViolationException.class, () -> {
59+
final TestEntity entity = new TestEntity( 2L );
60+
entity.setNotEmptySet( Set.of( new ChildEntity( 2L, "" ) ) );
61+
entity.setMinSizeList( List.of( "test" ) );
62+
entityManager.persist( entity );
63+
entityManager.flush();
64+
} );
65+
assertThat( e.getConstraintViolations() ).hasSize( 1 );
66+
assertThat( getPropertyPaths( e ) ).containsOnly( "name" );
67+
} );
68+
}
69+
70+
@Test
71+
public void testUpdateEmptyUsingGetter(EntityManagerFactoryScope scope) {
72+
scope.inTransaction( entityManager -> {
73+
final ConstraintViolationException e = assertThrows( ConstraintViolationException.class, () -> {
74+
final TestEntity entity = entityManager.find( TestEntity.class, 1L );
75+
entity.getNotEmptySet().clear();
76+
entityManager.flush();
77+
} );
78+
assertThat( e.getConstraintViolations() ).hasSize( 1 );
79+
assertThat( getPropertyPaths( e ) ).containsOnly( "notEmptySet" );
80+
81+
entityManager.clear();
82+
83+
final ConstraintViolationException e2 = assertThrows( ConstraintViolationException.class, () -> {
84+
final TestEntity entity = entityManager.find( TestEntity.class, 1L );
85+
entity.getMinSizeList().clear();
86+
entityManager.flush();
87+
} );
88+
assertThat( e2.getConstraintViolations() ).hasSize( 1 );
89+
assertThat( getPropertyPaths( e2 ) ).containsOnly( "minSizeList" );
90+
} );
91+
}
92+
93+
@Test
94+
public void testUpdateEmptyUsingSetter(EntityManagerFactoryScope scope) {
95+
scope.inTransaction( entityManager -> {
96+
final ConstraintViolationException e = assertThrows( ConstraintViolationException.class, () -> {
97+
final TestEntity entity = entityManager.find( TestEntity.class, 1L );
98+
entity.setNotEmptySet( Set.of() );
99+
entityManager.flush();
100+
} );
101+
assertThat( e.getConstraintViolations() ).hasSize( 1 );
102+
assertThat( getPropertyPaths( e ) ).containsOnly( "notEmptySet" );
103+
104+
entityManager.clear();
105+
106+
final ConstraintViolationException e2 = assertThrows( ConstraintViolationException.class, () -> {
107+
final TestEntity entity = entityManager.find( TestEntity.class, 1L );
108+
entity.setMinSizeList( List.of() );
109+
entityManager.flush();
110+
} );
111+
assertThat( e2.getConstraintViolations() ).hasSize( 1 );
112+
assertThat( getPropertyPaths( e2 ) ).containsOnly( "minSizeList" );
113+
} );
114+
}
115+
116+
@Test
117+
public void testUpdateNull(EntityManagerFactoryScope scope) {
118+
scope.inTransaction( entityManager -> {
119+
final TestEntity entity = new TestEntity( 3L );
120+
entity.setNotEmptySet( Set.of( new ChildEntity( 3L, "child_3" ) ) );
121+
entity.setMinSizeList( List.of( "three" ) );
122+
entityManager.persist( entity );
123+
} );
124+
scope.inTransaction( entityManager -> {
125+
final ConstraintViolationException e = assertThrows( ConstraintViolationException.class, () -> {
126+
final TestEntity entity = entityManager.find( TestEntity.class, 3L );
127+
entity.setNotEmptySet( null );
128+
entityManager.flush();
129+
} );
130+
assertThat( e.getConstraintViolations() ).hasSize( 1 );
131+
assertThat( getPropertyPaths( e ) ).containsOnly( "notEmptySet" );
132+
} );
133+
}
134+
135+
@Test
136+
public void testUpdateInvalidChild(EntityManagerFactoryScope scope) {
137+
scope.inTransaction( entityManager -> {
138+
final ConstraintViolationException e = assertThrows( ConstraintViolationException.class, () -> {
139+
final TestEntity entity = entityManager.find( TestEntity.class, 1L );
140+
final ChildEntity child = entity.getNotEmptySet().iterator().next();
141+
child.setName( "" );
142+
entityManager.flush();
143+
} );
144+
assertThat( e.getConstraintViolations() ).hasSize( 1 );
145+
assertThat( getPropertyPaths( e ) ).containsOnly( "name" );
146+
} );
147+
}
148+
149+
private static List<String> getPropertyPaths(ConstraintViolationException e) {
150+
return e.getConstraintViolations().stream().map( ConstraintViolation::getPropertyPath ).map( Path::toString )
151+
.collect( Collectors.toList() );
152+
}
153+
154+
@BeforeEach
155+
public void setUp(EntityManagerFactoryScope scope) {
156+
scope.inTransaction( entityManager -> {
157+
final TestEntity a = new TestEntity( 1L );
158+
a.setNotEmptySet( Set.of( new ChildEntity( 1L, "child_1" ) ) );
159+
a.setMinSizeList( List.of( "one" ) );
160+
entityManager.persist( a );
161+
} );
162+
}
163+
164+
@AfterEach
165+
public void tearDown(EntityManagerFactoryScope scope) {
166+
scope.getEntityManagerFactory().unwrap( SessionFactory.class ).getSchemaManager().truncateMappedObjects();
167+
}
168+
169+
@Entity(name = "TestEntity")
170+
static class TestEntity {
171+
@Id
172+
private Long id;
173+
174+
@ManyToMany(cascade = CascadeType.PERSIST)
175+
@NotEmpty
176+
private Set<ChildEntity> notEmptySet;
177+
178+
@ElementCollection
179+
@Size(min = 1)
180+
private List<String> minSizeList;
181+
182+
public TestEntity() {
183+
}
184+
185+
public TestEntity(Long id) {
186+
this.id = id;
187+
}
188+
189+
public Set<ChildEntity> getNotEmptySet() {
190+
return notEmptySet;
191+
}
192+
193+
public void setNotEmptySet(Set<ChildEntity> notEmptySet) {
194+
this.notEmptySet = notEmptySet;
195+
}
196+
197+
public List<String> getMinSizeList() {
198+
return minSizeList;
199+
}
200+
201+
public void setMinSizeList(List<String> minSizeList) {
202+
this.minSizeList = minSizeList;
203+
}
204+
}
205+
206+
@Entity(name = "ChildEntity")
207+
static class ChildEntity {
208+
@Id
209+
private Long id;
210+
211+
@NotBlank
212+
private String name;
213+
214+
public ChildEntity() {
215+
}
216+
217+
public ChildEntity(Long id, String name) {
218+
this.id = id;
219+
this.name = name;
220+
}
221+
222+
public String getName() {
223+
return name;
224+
}
225+
226+
public void setName(String name) {
227+
this.name = name;
228+
}
229+
}
230+
}

0 commit comments

Comments
 (0)