Skip to content

Commit 3f8b3f9

Browse files
committed
HHH-19232 Add test for issue
1 parent a5331ad commit 3f8b3f9

File tree

1 file changed

+257
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)