26
26
import static org .junit .jupiter .api .Assertions .assertThrows ;
27
27
import static org .junit .jupiter .api .Assertions .assertTrue ;
28
28
29
+ import junit .framework .AssertionFailedError ;
30
+
29
31
import java .lang .reflect .Method ;
30
32
import java .time .Duration ;
31
33
import java .util .ArrayList ;
40
42
import java .util .concurrent .Future ;
41
43
import java .util .stream .Collectors ;
42
44
45
+ import javax .validation .ConstraintViolationException ;
46
+
43
47
import org .junit .jupiter .api .Test ;
44
48
import org .springframework .beans .factory .annotation .Autowired ;
45
49
import org .springframework .context .annotation .Bean ;
52
56
import org .springframework .data .couchbase .core .CouchbaseQueryExecutionException ;
53
57
import org .springframework .data .couchbase .core .CouchbaseTemplate ;
54
58
import org .springframework .data .couchbase .core .RemoveResult ;
59
+ import org .springframework .data .couchbase .core .mapping .event .ValidatingCouchbaseEventListener ;
55
60
import org .springframework .data .couchbase .core .query .N1QLExpression ;
56
61
import org .springframework .data .couchbase .core .query .QueryCriteria ;
57
62
import org .springframework .data .couchbase .domain .Address ;
82
87
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
83
88
import org .springframework .data .repository .core .support .DefaultRepositoryMetadata ;
84
89
import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
90
+ import org .springframework .validation .beanvalidation .LocalValidatorFactoryBean ;
85
91
86
92
import com .couchbase .client .core .error .AmbiguousTimeoutException ;
87
93
import com .couchbase .client .core .error .CouchbaseException ;
@@ -123,6 +129,7 @@ void shouldSaveAndFindAll() {
123
129
Airport vie = null ;
124
130
try {
125
131
vie = new Airport ("airports::vie" , "vie" , "low4" );
132
+ vie .setSize (2 );
126
133
airportRepository .save (vie );
127
134
List <Airport > all = new ArrayList <>();
128
135
airportRepository .findAll ().forEach (all ::add );
@@ -133,6 +140,18 @@ void shouldSaveAndFindAll() {
133
140
}
134
141
}
135
142
143
+ @ Test
144
+ void shouldNotSave () {
145
+ Airport vie = new Airport ("airports::vie" , "vie" , "low4" );
146
+ vie .setSize (3 );
147
+ try {
148
+ assertThrows (ConstraintViolationException .class , () -> airportRepository .save (vie ));
149
+ } catch (AssertionFailedError e ) {
150
+ airportRepository .delete (vie );
151
+ throw e ;
152
+ }
153
+ }
154
+
136
155
@ Autowired PersonRepository personRepository ;
137
156
138
157
@ Test
@@ -780,5 +799,14 @@ public DateTimeProvider testDateTimeProvider() {
780
799
return new AuditingDateTimeProvider ();
781
800
}
782
801
802
+ @ Bean
803
+ public LocalValidatorFactoryBean validator () {
804
+ return new LocalValidatorFactoryBean ();
805
+ }
806
+
807
+ @ Bean
808
+ public ValidatingCouchbaseEventListener validationEventListener () {
809
+ return new ValidatingCouchbaseEventListener (validator ());
810
+ }
783
811
}
784
812
}
0 commit comments